kasapay 0.0.5

One payment API in Rust over any payment provider
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! One contract, checked the same way against every adapter in the workspace.
//!
//! `Capabilities` is a promise about behaviour: `separate_capture: false` says
//! this provider has no capture step, and a checkout reads it to decide what
//! to offer before it has a payment. Nothing made the promise and the
//! behaviour agree. Each adapter had its own tests, each asserted its own
//! answers, and a flag flipped in one file while the method below it kept
//! doing what it always did would have passed every one of them.
//!
//! So this asks the same questions of all six clients, through nothing but
//! `dyn Provider`:
//!
//! 1. Where a capability is `false`, the paired call answers `Unsupported`.
//! 2. Where it is `true`, that call does **not** — it goes to the provider,
//!    and what comes back is the provider's answer rather than a refusal.
//! 3. A refusal costs nothing: not one byte reaches the network.
//! 4. Every error names the provider it came from.
//! 5. A charge in a currency the provider settles in is never `Unsupported`.
//!
//! The mock server answers 500 to everything, which is the point: a call that
//! is genuinely implemented fails *there*, and one that is refused never
//! arrives. The two are told apart by counting requests.
//!
//! Six clients, five `ProviderId`s: `classic` and `in_store` are two APIs of
//! iyzico's and both answer `iyzico`, so each subject carries its own label
//! for the assertion messages.

#![cfg(all(
    feature = "iyzico",
    feature = "mollie",
    feature = "paypal",
    feature = "paytr",
    feature = "stripe"
))]
#![allow(
    clippy::expect_used,
    reason = "a fixture that cannot be built is a failed test"
)]

use kasapay::{
    ChargeRequest, Currency, ErrorKind, IdempotencyKey, Money, OrderRef, PaymentId, Provider,
    Secret,
};
use serde_json::json;
use wiremock::matchers::{any, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

/// One adapter, and the server it is pointed at.
struct Subject {
    /// Which client this is — `classic` and `in_store` share a `ProviderId`.
    label: &'static str,
    provider: Box<dyn Provider>,
    server: MockServer,
    /// A currency this provider settles in. Asking one of the Turkish two for
    /// euro, or Mollie for lira, is `Unsupported` on purpose and would say
    /// nothing about whether a charge can be started at all.
    currency: Currency,
    /// Requests the server had already taken before the test began — PayPal
    /// fetches a token when its client is built.
    baseline: usize,
}

impl Subject {
    /// How many requests have reached the server since the client was built.
    async fn reached(&self) -> usize {
        self.server
            .received_requests()
            .await
            .expect("the mock server records requests")
            .len()
            - self.baseline
    }
}

/// A server that answers every call with a 500 it has no body for.
///
/// PayPal is the exception: its client asks for a token before anything else,
/// and a client that will not build cannot be asked about its capabilities.
async fn dead_server() -> MockServer {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v1/oauth2/token"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "access_token": "A21AA",
            "token_type": "Bearer",
            "expires_in": 31_668,
        })))
        .mount(&server)
        .await;
    Mock::given(any())
        .respond_with(ResponseTemplate::new(500))
        .mount(&server)
        .await;
    server
}

async fn subject(
    label: &'static str,
    provider: Box<dyn Provider>,
    server: MockServer,
    currency: Currency,
) -> Subject {
    let baseline = server
        .received_requests()
        .await
        .expect("the mock server records requests")
        .len();
    Subject {
        label,
        provider,
        server,
        currency,
        baseline,
    }
}

async fn every_adapter() -> Vec<Subject> {
    let mut subjects = Vec::new();

    let server = dead_server().await;
    let config = kasapay::iyzico::classic::Config::new(
        &server.uri(),
        kasapay::iyzico::Credentials::new("api-key", "secret-key"),
    )
    .expect("valid base");
    subjects.push(
        subject(
            "iyzico classic",
            Box::new(kasapay::iyzico::classic::Client::new(config).expect("client builds")),
            server,
            Currency::Try,
        )
        .await,
    );

    let server = dead_server().await;
    let config = kasapay::iyzico::in_store::Config::new(
        &format!("{}/v3/in-store/", server.uri()),
        "api-key",
        "secret-key",
        "merchant-id",
    )
    .expect("valid base");
    subjects.push(
        subject(
            "iyzico in_store",
            Box::new(kasapay::iyzico::in_store::Client::new(config).expect("client builds")),
            server,
            Currency::Try,
        )
        .await,
    );

    let server = dead_server().await;
    let config = kasapay::mollie::Config::at(&server.uri(), Secret::new("test_kasapay"))
        .expect("valid base");
    subjects.push(
        subject(
            "mollie",
            Box::new(kasapay::mollie::Mollie::new(config).expect("client builds")),
            server,
            Currency::Eur,
        )
        .await,
    );

    let server = dead_server().await;
    let config = kasapay::paypal::Config::at(
        &server.uri(),
        Secret::new("client-id"),
        Secret::new("client-secret"),
    )
    .expect("valid base");
    subjects.push(
        subject(
            "paypal",
            Box::new(kasapay::paypal::PayPal::new(config).expect("client builds")),
            server,
            Currency::Eur,
        )
        .await,
    );

    let server = dead_server().await;
    let config = kasapay::paytr::Config::at(
        &server.uri(),
        kasapay::paytr::Credentials::new("merchant-1", "merchant-key", "merchant-salt"),
    )
    .expect("valid base")
    .test_mode();
    subjects.push(
        subject(
            "paytr",
            Box::new(kasapay::paytr::PayTr::new(config).expect("client builds")),
            server,
            Currency::Try,
        )
        .await,
    );

    let server = dead_server().await;
    let stripe = kasapay::stripe::Stripe::at(&server.uri(), &Secret::new("sk_test_kasapay"))
        .expect("client builds");
    subjects.push(subject("stripe", Box::new(stripe), server, Currency::Eur).await);

    subjects
}

fn a_payment() -> PaymentId {
    PaymentId::issued("pay-1")
}

/// `separate_capture: false` says there is no capture step at all — the money
/// was taken at authorisation. A caller reading that flag and skipping the
/// call has to be right, and a caller who calls anyway has to get a refusal
/// rather than a second charge.
#[tokio::test]
async fn capture_answers_the_flag_that_describes_it() {
    for subject in every_adapter().await {
        let who = subject.label;
        let capable = subject.provider.capabilities().separate_capture;
        let error = subject
            .provider
            .capture(&a_payment(), None, None)
            .await
            .expect_err("the server answers 500 to everything it is asked");

        assert_eq!(
            error.provider(),
            subject.provider.id(),
            "{who} answered for somebody else"
        );
        if capable {
            assert_ne!(
                error.kind(),
                ErrorKind::Unsupported,
                "{who} says it captures separately and then refuses to"
            );
            assert!(
                subject.reached().await > 0,
                "{who} says it captures separately and never asked"
            );
        } else {
            assert_eq!(
                error.kind(),
                ErrorKind::Unsupported,
                "{who} has no capture step and did not say so"
            );
            assert_eq!(
                subject.reached().await,
                0,
                "{who} refused a capture only after sending it"
            );
        }
    }
}

/// A key accepted and dropped reads as a guarantee against taking the money
/// twice where there is none, so an adapter that cannot honour one refuses the
/// call. What this pins is *when*: before the request rather than after it. A
/// capture refused once it has already been sent has already taken the money,
/// and the refusal would be a lie about what happened.
#[tokio::test]
async fn a_capture_refuses_an_idempotency_key_before_it_sends_anything() {
    let key = IdempotencyKey::new("idem-1");
    for subject in every_adapter().await {
        let who = subject.label;
        let error = subject
            .provider
            .capture(&a_payment(), None, Some(&key))
            .await
            .expect_err("the server answers 500 to everything it is asked");

        assert_eq!(
            error.provider(),
            subject.provider.id(),
            "{who} answered for somebody else"
        );
        if error.kind() == ErrorKind::Unsupported {
            assert_eq!(
                subject.reached().await,
                0,
                "{who} refused the key only after sending the capture"
            );
        }
    }
}

/// `lookup_by_order` is read by a crash-recovery path, which is the one place
/// a wrong answer costs a second payment: `Unsupported` there means "ask the
/// provider some other way", and anything else means "you may retry once this
/// says there is no record".
#[tokio::test]
async fn lookup_answers_the_flag_that_describes_it() {
    for subject in every_adapter().await {
        let who = subject.label;
        let capable = subject.provider.capabilities().lookup_by_order;
        let error = subject
            .provider
            .lookup(&OrderRef::new("ord-1"))
            .await
            .expect_err("the server answers 500 to everything it is asked");

        assert_eq!(
            error.provider(),
            subject.provider.id(),
            "{who} answered for somebody else"
        );
        if capable {
            assert_ne!(
                error.kind(),
                ErrorKind::Unsupported,
                "{who} says it looks up by order and then refuses to"
            );
            assert!(
                subject.reached().await > 0,
                "{who} says it looks up by order and never asked"
            );
        } else {
            assert_eq!(
                error.kind(),
                ErrorKind::Unsupported,
                "{who} cannot look up by order and did not say so"
            );
            assert_eq!(
                subject.reached().await,
                0,
                "{who} refused a lookup only after sending it"
            );
        }
    }
}

/// `resume_by_continuation` is what a caller reads when the payer comes back
/// from a hosted form, to decide between the token it kept and the payment id
/// it may not have. Getting it wrong is a shop that cannot tell whether it was
/// paid.
#[tokio::test]
async fn resume_answers_the_flag_that_describes_it() {
    for subject in every_adapter().await {
        let who = subject.label;
        let capable = subject.provider.capabilities().resume_by_continuation;
        let error = subject
            .provider
            .resume("continuation-1")
            .await
            .expect_err("the server answers 500 to everything it is asked");

        assert_eq!(
            error.provider(),
            subject.provider.id(),
            "{who} answered for somebody else"
        );
        if capable {
            assert_ne!(
                error.kind(),
                ErrorKind::Unsupported,
                "{who} says it resumes by continuation and then refuses to"
            );
            assert!(
                subject.reached().await > 0,
                "{who} says it resumes by continuation and never asked"
            );
        } else {
            assert_eq!(
                error.kind(),
                ErrorKind::Unsupported,
                "{who} cannot resume by continuation and did not say so"
            );
            assert_eq!(
                subject.reached().await,
                0,
                "{who} refused a resume only after sending it"
            );
        }
    }
}

/// A refusal that costs a request costs a timeout too, in the retry path where
/// that matters most.
#[tokio::test]
async fn a_refused_cancel_never_reaches_the_network() {
    for subject in every_adapter().await {
        let who = subject.label;
        let error = subject
            .provider
            .cancel(&a_payment())
            .await
            .expect_err("the server answers 500 to everything it is asked");
        assert_eq!(
            error.provider(),
            subject.provider.id(),
            "{who} answered for somebody else"
        );
        if error.kind() == ErrorKind::Unsupported {
            assert_eq!(
                subject.reached().await,
                0,
                "{who} refused a cancel only after sending it"
            );
        }
    }
}

/// The same, for the one call a checkout makes before it has a payment at all.
#[tokio::test]
async fn a_refused_instrument_list_never_reaches_the_network() {
    for subject in every_adapter().await {
        let who = subject.label;
        let error = subject
            .provider
            .instruments("cus-1")
            .await
            .expect_err("the server answers 500 to everything it is asked");
        assert_eq!(
            error.provider(),
            subject.provider.id(),
            "{who} answered for somebody else"
        );
        if error.kind() == ErrorKind::Unsupported {
            assert_eq!(
                subject.reached().await,
                0,
                "{who} refused an instrument list only after sending it"
            );
        }
    }
}

/// The rule that lets `Currency` be more than a handful: every currency it
/// names is either one this adapter settles in — and the request goes to the
/// provider — or one it refuses **before a socket opens**. What is never
/// allowed is the third answer, mapping an unknown currency onto something and
/// sending it, which is what a wildcard arm used to be forbidden to prevent.
///
/// This is the proof that replaced that prohibition. It costs one pass over a
/// hundred-odd currencies per adapter and it is the only reason widening the
/// enum is safe.
#[tokio::test]
async fn every_currency_is_either_settled_or_refused_before_the_wire() {
    for subject in every_adapter().await {
        let who = subject.label;
        let mut settled = 0_usize;
        let mut refused = 0_usize;
        let mut before = subject.reached().await;

        for currency in Currency::KNOWN.iter().copied() {
            // Everything any provider asks for, so the currency is the only
            // thing that can decide the answer. A request missing a field
            // would be refused for the field instead, and this would be
            // measuring nothing.
            let request = ChargeRequest::builder(
                OrderRef::new("ord-1"),
                Money::from_minor_units(1000, currency),
            )
            .description("Order #1")
            .customer("cus-1")
            .return_url("https://merchant.test/ok".parse().expect("valid url"))
            .failure_url("https://merchant.test/no".parse().expect("valid url"))
            .buyer(
                kasapay::Buyer::new("Ayse", "ayse@example.test")
                    .surname("Yilmaz")
                    .identity_number("11111111111")
                    .phone("+905350000000")
                    .ip("203.0.113.7")
                    .address(kasapay::Address::new("Bagdat Cad. 1", "Istanbul", "Turkey")),
            )
            .item(
                kasapay::BasketItem::new("sku-1", "Kahve", Money::from_minor_units(1000, currency))
                    .category("Icecek"),
            )
            .build()
            .expect("valid request");

            let error = subject
                .provider
                .charge(&request)
                .await
                .expect_err("the server answers 500 to everything it is asked");
            assert_eq!(
                error.provider(),
                subject.provider.id(),
                "{who} answered for somebody else"
            );

            let after = subject.reached().await;
            if error.kind() == ErrorKind::Unsupported {
                assert_eq!(
                    after, before,
                    "{who} refused {currency} only after sending it"
                );
                refused += 1;
            } else {
                settled += 1;
            }
            before = after;
        }

        // Not a threshold anybody tuned: every adapter here settles in at
        // least one currency and refuses at least one, so a zero on either
        // side is a mapping that stopped discriminating rather than a
        // provider that changed.
        assert!(settled > 0, "{who} refuses every currency kasapay names");
        assert!(refused > 0, "{who} settles in every currency kasapay names");
    }
}

/// `partial_capture` is only meaningful where there is a capture step, and a
/// pair that says otherwise is a caller offering a partial shipment against a
/// provider that takes the money up front.
#[tokio::test]
async fn no_capability_contradicts_another() {
    for subject in every_adapter().await {
        let who = subject.label;
        let capabilities = subject.provider.capabilities();
        assert!(
            capabilities.separate_capture || !capabilities.partial_capture,
            "{who} captures part of an amount it never authorises separately"
        );
        assert!(
            capabilities.partial_refund || !capabilities.repeated_refund,
            "{who} refunds repeatedly but never for part of an amount"
        );
    }
}

/// A charge with nothing but an order and an amount is what a caller writes
/// first, and no provider here takes it — each wants something more. What the
/// refusal may not be is `Unsupported`, which means "never, for anyone": this
/// is "not with these fields", and the difference is whether the caller can
/// fix it. That distinction is the whole of what `Provider::charge` promises.
#[tokio::test]
async fn a_charge_missing_fields_is_an_invalid_request_rather_than_an_unsupported_one() {
    for subject in every_adapter().await {
        let who = subject.label;
        let request = ChargeRequest::builder(
            OrderRef::new("ord-1"),
            Money::parse("10.00", subject.currency).expect("valid amount"),
        )
        .build()
        .expect("valid request");

        let error = subject
            .provider
            .charge(&request)
            .await
            .expect_err("the server answers 500 to everything it is asked");
        assert_eq!(
            error.provider(),
            subject.provider.id(),
            "{who} answered for somebody else"
        );
        assert_ne!(
            error.kind(),
            ErrorKind::Unsupported,
            "{who} cannot start a payment at all through the trait"
        );
    }
}