@swichpay/react-native 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/LICENSE +21 -0
- package/README.md +82 -2
- package/docs/checkouts.md +98 -0
- package/docs/getting-started.md +140 -0
- package/docs/going-live.md +45 -0
- package/docs/own-screen.md +81 -0
- package/docs/results-and-errors.md +75 -0
- package/docs/server.md +163 -0
- package/docs/troubleshooting.md +57 -0
- package/lib/commonjs/HostedCheckout.js +115 -0
- package/lib/commonjs/checkout-webview.js +85 -0
- package/lib/commonjs/checkouts/PaymentSheet.js +1317 -0
- package/lib/commonjs/checkouts/direct.js +671 -0
- package/lib/commonjs/context.js +64 -0
- package/lib/commonjs/core/api/direct-payment-api.js +425 -0
- package/lib/commonjs/core/api/discount-api.js +19 -0
- package/lib/commonjs/core/api/gateway-api.js +366 -0
- package/lib/commonjs/core/api/payments-api.js +79 -0
- package/lib/commonjs/core/api/repositories.js +5 -0
- package/lib/commonjs/core/assets/images.js +12 -0
- package/lib/commonjs/core/assets/method-logos.js +19 -0
- package/lib/commonjs/core/assets/pwa-images.js +13 -0
- package/lib/commonjs/core/browser/clipboard.js +30 -0
- package/lib/commonjs/core/browser/dom.js +71 -0
- package/lib/commonjs/core/browser/hosted-checkout.js +107 -0
- package/lib/commonjs/core/browser/index.js +119 -0
- package/lib/commonjs/core/browser/qr.js +109 -0
- package/lib/commonjs/core/browser/three-ds.js +151 -0
- package/lib/commonjs/core/client.js +344 -0
- package/lib/commonjs/core/config.js +132 -0
- package/lib/commonjs/core/environment.js +34 -0
- package/lib/commonjs/core/errors.js +138 -0
- package/lib/commonjs/core/events.js +57 -0
- package/lib/commonjs/core/flows/controller.js +106 -0
- package/lib/commonjs/core/flows/countdown.js +41 -0
- package/lib/commonjs/core/flows/direct-bank-transfer.js +236 -0
- package/lib/commonjs/core/flows/direct-card.js +354 -0
- package/lib/commonjs/core/flows/direct-ewallet.js +124 -0
- package/lib/commonjs/core/flows/direct-one-bill.js +172 -0
- package/lib/commonjs/core/flows/direct-qr.js +109 -0
- package/lib/commonjs/core/flows/direct-rtp.js +302 -0
- package/lib/commonjs/core/flows/discount-lookup.js +80 -0
- package/lib/commonjs/core/flows/headless.js +94 -0
- package/lib/commonjs/core/flows/payment-sheet.js +1321 -0
- package/lib/commonjs/core/flows/services.js +31 -0
- package/lib/commonjs/core/flows/shared.js +90 -0
- package/lib/commonjs/core/http/api-client.js +208 -0
- package/lib/commonjs/core/http/endpoints.js +61 -0
- package/lib/commonjs/core/http/token-manager.js +46 -0
- package/lib/commonjs/core/http/transport.js +134 -0
- package/lib/commonjs/core/index.js +688 -0
- package/lib/commonjs/core/models/category.js +113 -0
- package/lib/commonjs/core/models/discount.js +51 -0
- package/lib/commonjs/core/models/payment.js +95 -0
- package/lib/commonjs/core/models/session.js +131 -0
- package/lib/commonjs/core/server/index.js +64 -0
- package/lib/commonjs/core/server/node.js +60 -0
- package/lib/commonjs/core/server/proxy.js +194 -0
- package/lib/commonjs/core/server/verify.js +122 -0
- package/lib/commonjs/core/swichpay.js +72 -0
- package/lib/commonjs/core/utils/crypto.js +78 -0
- package/lib/commonjs/core/utils/formatters.js +68 -0
- package/lib/commonjs/core/utils/json.js +110 -0
- package/lib/commonjs/core/utils/log.js +75 -0
- package/lib/commonjs/core/utils/runtime.js +34 -0
- package/lib/commonjs/core/utils/validators.js +59 -0
- package/lib/commonjs/hooks.js +77 -0
- package/lib/commonjs/index.js +203 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/polyfills.js +3 -0
- package/lib/commonjs/presenter.js +61 -0
- package/lib/commonjs/provider.js +192 -0
- package/lib/commonjs/swichpay.js +192 -0
- package/lib/commonjs/theme.js +54 -0
- package/lib/commonjs/ui.js +1142 -0
- package/lib/commonjs/webviews.js +279 -0
- package/lib/module/HostedCheckout.js +111 -0
- package/lib/module/checkout-webview.js +83 -0
- package/lib/module/checkouts/PaymentSheet.js +1313 -0
- package/lib/module/checkouts/direct.js +661 -0
- package/lib/module/context.js +58 -0
- package/lib/module/core/api/direct-payment-api.js +413 -0
- package/lib/module/core/api/discount-api.js +14 -0
- package/lib/module/core/api/gateway-api.js +358 -0
- package/lib/module/core/api/payments-api.js +73 -0
- package/lib/module/core/api/repositories.js +3 -0
- package/lib/module/core/assets/images.js +8 -0
- package/lib/module/core/assets/method-logos.js +15 -0
- package/lib/module/core/assets/pwa-images.js +9 -0
- package/lib/module/core/browser/clipboard.js +27 -0
- package/lib/module/core/browser/dom.js +63 -0
- package/lib/module/core/browser/hosted-checkout.js +102 -0
- package/lib/module/core/browser/index.js +12 -0
- package/lib/module/core/browser/qr.js +100 -0
- package/lib/module/core/browser/three-ds.js +145 -0
- package/lib/module/core/client.js +338 -0
- package/lib/module/core/config.js +128 -0
- package/lib/module/core/environment.js +29 -0
- package/lib/module/core/errors.js +125 -0
- package/lib/module/core/events.js +51 -0
- package/lib/module/core/flows/controller.js +101 -0
- package/lib/module/core/flows/countdown.js +36 -0
- package/lib/module/core/flows/direct-bank-transfer.js +231 -0
- package/lib/module/core/flows/direct-card.js +349 -0
- package/lib/module/core/flows/direct-ewallet.js +119 -0
- package/lib/module/core/flows/direct-one-bill.js +167 -0
- package/lib/module/core/flows/direct-qr.js +104 -0
- package/lib/module/core/flows/direct-rtp.js +297 -0
- package/lib/module/core/flows/discount-lookup.js +75 -0
- package/lib/module/core/flows/headless.js +90 -0
- package/lib/module/core/flows/payment-sheet.js +1315 -0
- package/lib/module/core/flows/services.js +27 -0
- package/lib/module/core/flows/shared.js +78 -0
- package/lib/module/core/http/api-client.js +203 -0
- package/lib/module/core/http/endpoints.js +57 -0
- package/lib/module/core/http/token-manager.js +41 -0
- package/lib/module/core/http/transport.js +126 -0
- package/lib/module/core/index.js +52 -0
- package/lib/module/core/models/category.js +104 -0
- package/lib/module/core/models/discount.js +46 -0
- package/lib/module/core/models/payment.js +89 -0
- package/lib/module/core/models/session.js +120 -0
- package/lib/module/core/server/index.js +11 -0
- package/lib/module/core/server/node.js +56 -0
- package/lib/module/core/server/proxy.js +191 -0
- package/lib/module/core/server/verify.js +115 -0
- package/lib/module/core/swichpay.js +68 -0
- package/lib/module/core/utils/crypto.js +72 -0
- package/lib/module/core/utils/formatters.js +56 -0
- package/lib/module/core/utils/json.js +94 -0
- package/lib/module/core/utils/log.js +67 -0
- package/lib/module/core/utils/runtime.js +28 -0
- package/lib/module/core/utils/validators.js +54 -0
- package/lib/module/hooks.js +72 -0
- package/lib/module/index.js +36 -0
- package/lib/module/polyfills.js +4 -0
- package/lib/module/presenter.js +58 -0
- package/lib/module/provider.js +188 -0
- package/lib/module/swichpay.js +187 -0
- package/lib/module/theme.js +50 -0
- package/lib/module/ui.js +1116 -0
- package/lib/module/webviews.js +272 -0
- package/lib/typescript/commonjs/HostedCheckout.d.ts +17 -0
- package/lib/typescript/commonjs/checkout-webview.d.ts +14 -0
- package/lib/typescript/commonjs/checkouts/PaymentSheet.d.ts +14 -0
- package/lib/typescript/commonjs/checkouts/direct.d.ts +18 -0
- package/lib/typescript/commonjs/context.d.ts +20 -0
- package/lib/typescript/commonjs/core/api/direct-payment-api.d.ts +103 -0
- package/lib/typescript/commonjs/core/api/discount-api.d.ts +10 -0
- package/lib/typescript/commonjs/core/api/gateway-api.d.ts +150 -0
- package/lib/typescript/commonjs/core/api/payments-api.d.ts +22 -0
- package/lib/typescript/commonjs/core/api/repositories.d.ts +86 -0
- package/lib/typescript/commonjs/core/assets/images.d.ts +5 -0
- package/lib/typescript/commonjs/core/assets/method-logos.d.ts +12 -0
- package/lib/typescript/commonjs/core/assets/pwa-images.d.ts +7 -0
- package/lib/typescript/commonjs/core/browser/clipboard.d.ts +3 -0
- package/lib/typescript/commonjs/core/browser/dom.d.ts +11 -0
- package/lib/typescript/commonjs/core/browser/hosted-checkout.d.ts +27 -0
- package/lib/typescript/commonjs/core/browser/index.d.ts +11 -0
- package/lib/typescript/commonjs/core/browser/qr.d.ts +18 -0
- package/lib/typescript/commonjs/core/browser/three-ds.d.ts +45 -0
- package/lib/typescript/commonjs/core/client.d.ts +181 -0
- package/lib/typescript/commonjs/core/config.d.ts +86 -0
- package/lib/typescript/commonjs/core/environment.d.ts +22 -0
- package/lib/typescript/commonjs/core/errors.d.ts +78 -0
- package/lib/typescript/commonjs/core/events.d.ts +19 -0
- package/lib/typescript/commonjs/core/flows/controller.d.ts +41 -0
- package/lib/typescript/commonjs/core/flows/countdown.d.ts +15 -0
- package/lib/typescript/commonjs/core/flows/direct-bank-transfer.d.ts +49 -0
- package/lib/typescript/commonjs/core/flows/direct-card.d.ts +94 -0
- package/lib/typescript/commonjs/core/flows/direct-ewallet.d.ts +39 -0
- package/lib/typescript/commonjs/core/flows/direct-one-bill.d.ts +38 -0
- package/lib/typescript/commonjs/core/flows/direct-qr.d.ts +25 -0
- package/lib/typescript/commonjs/core/flows/direct-rtp.d.ts +72 -0
- package/lib/typescript/commonjs/core/flows/discount-lookup.d.ts +37 -0
- package/lib/typescript/commonjs/core/flows/headless.d.ts +38 -0
- package/lib/typescript/commonjs/core/flows/payment-sheet.d.ts +263 -0
- package/lib/typescript/commonjs/core/flows/services.d.ts +22 -0
- package/lib/typescript/commonjs/core/flows/shared.d.ts +40 -0
- package/lib/typescript/commonjs/core/http/api-client.d.ts +37 -0
- package/lib/typescript/commonjs/core/http/endpoints.d.ts +39 -0
- package/lib/typescript/commonjs/core/http/token-manager.d.ts +19 -0
- package/lib/typescript/commonjs/core/http/transport.d.ts +37 -0
- package/lib/typescript/commonjs/core/index.d.ts +42 -0
- package/lib/typescript/commonjs/core/models/category.d.ts +35 -0
- package/lib/typescript/commonjs/core/models/discount.d.ts +41 -0
- package/lib/typescript/commonjs/core/models/payment.d.ts +104 -0
- package/lib/typescript/commonjs/core/models/session.d.ts +87 -0
- package/lib/typescript/commonjs/core/server/index.d.ts +10 -0
- package/lib/typescript/commonjs/core/server/node.d.ts +19 -0
- package/lib/typescript/commonjs/core/server/proxy.d.ts +30 -0
- package/lib/typescript/commonjs/core/server/verify.d.ts +61 -0
- package/lib/typescript/commonjs/core/swichpay.d.ts +40 -0
- package/lib/typescript/commonjs/core/utils/crypto.d.ts +19 -0
- package/lib/typescript/commonjs/core/utils/formatters.d.ts +19 -0
- package/lib/typescript/commonjs/core/utils/json.d.ts +27 -0
- package/lib/typescript/commonjs/core/utils/log.d.ts +15 -0
- package/lib/typescript/commonjs/core/utils/runtime.d.ts +12 -0
- package/lib/typescript/commonjs/core/utils/validators.d.ts +23 -0
- package/lib/typescript/commonjs/hooks.d.ts +21 -0
- package/lib/typescript/commonjs/index.d.ts +26 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/polyfills.d.ts +2 -0
- package/lib/typescript/commonjs/presenter.d.ts +25 -0
- package/lib/typescript/commonjs/provider.d.ts +22 -0
- package/lib/typescript/commonjs/swichpay.d.ts +125 -0
- package/lib/typescript/commonjs/theme.d.ts +52 -0
- package/lib/typescript/commonjs/ui.d.ts +583 -0
- package/lib/typescript/commonjs/webviews.d.ts +40 -0
- package/lib/typescript/module/HostedCheckout.d.ts +17 -0
- package/lib/typescript/module/checkout-webview.d.ts +14 -0
- package/lib/typescript/module/checkouts/PaymentSheet.d.ts +14 -0
- package/lib/typescript/module/checkouts/direct.d.ts +18 -0
- package/lib/typescript/module/context.d.ts +20 -0
- package/lib/typescript/module/core/api/direct-payment-api.d.ts +103 -0
- package/lib/typescript/module/core/api/discount-api.d.ts +10 -0
- package/lib/typescript/module/core/api/gateway-api.d.ts +150 -0
- package/lib/typescript/module/core/api/payments-api.d.ts +22 -0
- package/lib/typescript/module/core/api/repositories.d.ts +86 -0
- package/lib/typescript/module/core/assets/images.d.ts +5 -0
- package/lib/typescript/module/core/assets/method-logos.d.ts +12 -0
- package/lib/typescript/module/core/assets/pwa-images.d.ts +7 -0
- package/lib/typescript/module/core/browser/clipboard.d.ts +3 -0
- package/lib/typescript/module/core/browser/dom.d.ts +11 -0
- package/lib/typescript/module/core/browser/hosted-checkout.d.ts +27 -0
- package/lib/typescript/module/core/browser/index.d.ts +11 -0
- package/lib/typescript/module/core/browser/qr.d.ts +18 -0
- package/lib/typescript/module/core/browser/three-ds.d.ts +45 -0
- package/lib/typescript/module/core/client.d.ts +181 -0
- package/lib/typescript/module/core/config.d.ts +86 -0
- package/lib/typescript/module/core/environment.d.ts +22 -0
- package/lib/typescript/module/core/errors.d.ts +78 -0
- package/lib/typescript/module/core/events.d.ts +19 -0
- package/lib/typescript/module/core/flows/controller.d.ts +41 -0
- package/lib/typescript/module/core/flows/countdown.d.ts +15 -0
- package/lib/typescript/module/core/flows/direct-bank-transfer.d.ts +49 -0
- package/lib/typescript/module/core/flows/direct-card.d.ts +94 -0
- package/lib/typescript/module/core/flows/direct-ewallet.d.ts +39 -0
- package/lib/typescript/module/core/flows/direct-one-bill.d.ts +38 -0
- package/lib/typescript/module/core/flows/direct-qr.d.ts +25 -0
- package/lib/typescript/module/core/flows/direct-rtp.d.ts +72 -0
- package/lib/typescript/module/core/flows/discount-lookup.d.ts +37 -0
- package/lib/typescript/module/core/flows/headless.d.ts +38 -0
- package/lib/typescript/module/core/flows/payment-sheet.d.ts +263 -0
- package/lib/typescript/module/core/flows/services.d.ts +22 -0
- package/lib/typescript/module/core/flows/shared.d.ts +40 -0
- package/lib/typescript/module/core/http/api-client.d.ts +37 -0
- package/lib/typescript/module/core/http/endpoints.d.ts +39 -0
- package/lib/typescript/module/core/http/token-manager.d.ts +19 -0
- package/lib/typescript/module/core/http/transport.d.ts +37 -0
- package/lib/typescript/module/core/index.d.ts +42 -0
- package/lib/typescript/module/core/models/category.d.ts +35 -0
- package/lib/typescript/module/core/models/discount.d.ts +41 -0
- package/lib/typescript/module/core/models/payment.d.ts +104 -0
- package/lib/typescript/module/core/models/session.d.ts +87 -0
- package/lib/typescript/module/core/server/index.d.ts +10 -0
- package/lib/typescript/module/core/server/node.d.ts +19 -0
- package/lib/typescript/module/core/server/proxy.d.ts +30 -0
- package/lib/typescript/module/core/server/verify.d.ts +61 -0
- package/lib/typescript/module/core/swichpay.d.ts +40 -0
- package/lib/typescript/module/core/utils/crypto.d.ts +19 -0
- package/lib/typescript/module/core/utils/formatters.d.ts +19 -0
- package/lib/typescript/module/core/utils/json.d.ts +27 -0
- package/lib/typescript/module/core/utils/log.d.ts +15 -0
- package/lib/typescript/module/core/utils/runtime.d.ts +12 -0
- package/lib/typescript/module/core/utils/validators.d.ts +23 -0
- package/lib/typescript/module/hooks.d.ts +21 -0
- package/lib/typescript/module/index.d.ts +26 -0
- package/lib/typescript/module/polyfills.d.ts +2 -0
- package/lib/typescript/module/presenter.d.ts +25 -0
- package/lib/typescript/module/provider.d.ts +22 -0
- package/lib/typescript/module/swichpay.d.ts +125 -0
- package/lib/typescript/module/theme.d.ts +52 -0
- package/lib/typescript/module/ui.d.ts +583 -0
- package/lib/typescript/module/webviews.d.ts +40 -0
- package/package.json +129 -4
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { SwichPayClient } from '../client.js';
|
|
4
|
+
import { SwichPayApiException, SwichPayValidationException } from '../errors.js';
|
|
5
|
+
|
|
6
|
+
/** What to do with the order: fulfil (`success`), cancel (`failed`) or wait and re-check (`pending`). */
|
|
7
|
+
|
|
8
|
+
/** Statuses SwichPay reports for a payment that will not complete. Anything else unpaid is `pending`. */
|
|
9
|
+
const FINAL_FAILURE = new Set(['failed', 'failure', 'fail', 'declined', 'rejected', 'cancelled', 'canceled', 'expired', 'error', 'reversed']);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Maps an inquiry to an order decision. Conservative on purpose: unknown statuses are `pending`,
|
|
13
|
+
* so an order is never fulfilled without a confirmed success and never cancelled without a
|
|
14
|
+
* confirmed failure.
|
|
15
|
+
*/
|
|
16
|
+
export function classifyInquiry(inquiry) {
|
|
17
|
+
if (inquiry.isSuccess) return 'success';
|
|
18
|
+
const status = (inquiry.status ?? '').trim().toLowerCase();
|
|
19
|
+
return FINAL_FAILURE.has(status) ? 'failed' : 'pending';
|
|
20
|
+
}
|
|
21
|
+
export function evaluateInquiry(inquiry, input) {
|
|
22
|
+
const status = classifyInquiry(inquiry);
|
|
23
|
+
if (status === 'failed') return {
|
|
24
|
+
status,
|
|
25
|
+
verified: false,
|
|
26
|
+
reason: 'declined',
|
|
27
|
+
inquiry
|
|
28
|
+
};
|
|
29
|
+
if (status === 'pending') return {
|
|
30
|
+
status,
|
|
31
|
+
verified: false,
|
|
32
|
+
reason: 'not_final',
|
|
33
|
+
inquiry
|
|
34
|
+
};
|
|
35
|
+
if (input.expectedAmount !== undefined && Math.abs(inquiry.amount - input.expectedAmount) > 0.005) {
|
|
36
|
+
return {
|
|
37
|
+
status: 'failed',
|
|
38
|
+
verified: false,
|
|
39
|
+
reason: 'amount_mismatch',
|
|
40
|
+
inquiry
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (input.expectedOrderId !== undefined && inquiry.orderId != null && inquiry.orderId !== input.expectedOrderId) {
|
|
44
|
+
return {
|
|
45
|
+
status: 'failed',
|
|
46
|
+
verified: false,
|
|
47
|
+
reason: 'order_mismatch',
|
|
48
|
+
inquiry
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
status: 'success',
|
|
53
|
+
verified: true,
|
|
54
|
+
reason: null,
|
|
55
|
+
inquiry
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Options for a server-side client, or a function that returns them (read lazily, on first use). */
|
|
60
|
+
|
|
61
|
+
/** A lazily created, server-side `SwichPayClient` authenticated with `clientSecret`. */
|
|
62
|
+
export function lazyServerClient(source, purpose) {
|
|
63
|
+
let client = null;
|
|
64
|
+
return () => {
|
|
65
|
+
if (!client) {
|
|
66
|
+
const options = typeof source === 'function' ? source() : source;
|
|
67
|
+
if (!options.clientSecret) {
|
|
68
|
+
throw new SwichPayValidationException(`${purpose} needs clientSecret (server-side only).`);
|
|
69
|
+
}
|
|
70
|
+
client = new SwichPayClient(options);
|
|
71
|
+
}
|
|
72
|
+
return client;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Server-side transaction verification with SwichPay's existing inquire API. Call it where the
|
|
78
|
+
* customer returns (`successRedirectUrl` / `failedRedirectUrl`) and fulfil the order only when
|
|
79
|
+
* `verified` is `true`. Never trust the redirect alone: anyone can open that URL.
|
|
80
|
+
*
|
|
81
|
+
* Network and authentication errors are thrown (as `SwichPayException`s), not reported as
|
|
82
|
+
* `failed`, so a SwichPay outage never cancels a paid order.
|
|
83
|
+
*/
|
|
84
|
+
export function createPaymentVerifier(source) {
|
|
85
|
+
const client = lazyServerClient(source, 'Payment verification');
|
|
86
|
+
return async input => {
|
|
87
|
+
const guid = input.paymentSessionGuid?.trim();
|
|
88
|
+
if (!guid) throw new SwichPayValidationException('paymentSessionGuid is required.');
|
|
89
|
+
const inquiry = await client().inquirePaymentSession({
|
|
90
|
+
paymentSessionGuid: guid
|
|
91
|
+
});
|
|
92
|
+
return evaluateInquiry(inquiry, input);
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Creates a payment session server-side with SwichPay's existing
|
|
97
|
+
* `POST /gateway/paymentsession/initiate` and returns the hosted checkout URL plus the session
|
|
98
|
+
* GUID. Save the GUID with the order, then redirect the customer to `url`.
|
|
99
|
+
*/
|
|
100
|
+
export function createCheckoutSessionCreator(source) {
|
|
101
|
+
const client = lazyServerClient(source, 'Creating a checkout session');
|
|
102
|
+
return async input => {
|
|
103
|
+
const url = await client().createHostedCheckoutUrl(input);
|
|
104
|
+
const paymentSessionGuid = new URL(url).searchParams.get('Id');
|
|
105
|
+
if (!paymentSessionGuid) {
|
|
106
|
+
throw new SwichPayApiException('paymentsession/initiate url did not include an Id.', {
|
|
107
|
+
statusCode: 200
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
url,
|
|
112
|
+
paymentSessionGuid
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { SwichPayClient } from './client.js';
|
|
4
|
+
import { SwichPayNotInitializedException } from './errors.js';
|
|
5
|
+
let current = null;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Static facade. UI methods (`presentPaymentSheet`, ...)
|
|
9
|
+
* live in `@swichpay/react` and `@swichpay/nuxt`, which need a framework to render.
|
|
10
|
+
*/
|
|
11
|
+
export const SwichPay = {
|
|
12
|
+
/**
|
|
13
|
+
* Call once, as early as possible. Calling again replaces (and disposes) the previous client.
|
|
14
|
+
* Returns the client for direct access to every method.
|
|
15
|
+
*/
|
|
16
|
+
initialize(options, deps) {
|
|
17
|
+
const next = new SwichPayClient(options, deps);
|
|
18
|
+
current?.dispose();
|
|
19
|
+
current = next;
|
|
20
|
+
return next;
|
|
21
|
+
},
|
|
22
|
+
/** Makes an existing client the live one (used by `<SwichPayProvider options>`). */
|
|
23
|
+
attach(client) {
|
|
24
|
+
current = client;
|
|
25
|
+
},
|
|
26
|
+
/** Clears the live client if it is `client` (without disposing it). */
|
|
27
|
+
detach(client) {
|
|
28
|
+
if (current === client) current = null;
|
|
29
|
+
},
|
|
30
|
+
/** The live client; throws `SwichPayNotInitializedException` before `initialize`. */
|
|
31
|
+
get client() {
|
|
32
|
+
if (!current || current.isDisposed) throw new SwichPayNotInitializedException();
|
|
33
|
+
return current;
|
|
34
|
+
},
|
|
35
|
+
get isInitialized() {
|
|
36
|
+
return current != null && !current.isDisposed;
|
|
37
|
+
},
|
|
38
|
+
/** Submits a card payment directly against your own form. */
|
|
39
|
+
pay(input) {
|
|
40
|
+
return run(() => SwichPay.client.pay(input));
|
|
41
|
+
},
|
|
42
|
+
/** Listens for every payment result. Returns an unsubscribe function (also `.cancel()`). */
|
|
43
|
+
onResult(callback) {
|
|
44
|
+
return SwichPay.client.onResult(callback);
|
|
45
|
+
},
|
|
46
|
+
/** Payment method categories enabled for this merchant. */
|
|
47
|
+
availableCategories() {
|
|
48
|
+
return run(() => SwichPay.client.availableCategories());
|
|
49
|
+
},
|
|
50
|
+
/** Queries a session's current status. */
|
|
51
|
+
inquirePaymentSession(input) {
|
|
52
|
+
return run(() => SwichPay.client.inquirePaymentSession(input));
|
|
53
|
+
},
|
|
54
|
+
/** Disposes the live client; the facade is uninitialized afterwards. */
|
|
55
|
+
dispose() {
|
|
56
|
+
current?.dispose();
|
|
57
|
+
current = null;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** Turns synchronous throws (e.g. not initialized) into rejections, like a Dart `Future`. */
|
|
62
|
+
function run(fn) {
|
|
63
|
+
try {
|
|
64
|
+
return fn();
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return Promise.reject(error);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { SwichPayValidationException } from '../errors.js';
|
|
4
|
+
function subtle() {
|
|
5
|
+
const s = globalThis.crypto?.subtle;
|
|
6
|
+
if (!s) {
|
|
7
|
+
throw new SwichPayValidationException('Web Crypto (crypto.subtle) is unavailable - it needs a secure context (https or localhost) or Node 18+.');
|
|
8
|
+
}
|
|
9
|
+
return s;
|
|
10
|
+
}
|
|
11
|
+
function bytesToBase64(bytes) {
|
|
12
|
+
let binary = '';
|
|
13
|
+
for (const b of bytes) binary += String.fromCharCode(b);
|
|
14
|
+
return btoa(binary);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Same as every SwichPay SDK's `generateTransactionCheckSum`: base64 of
|
|
19
|
+
* HMAC-SHA256(secretHashKey, "Currency~Amount~CustomerTransactionId"), amount fixed to 2 decimals.
|
|
20
|
+
*/
|
|
21
|
+
export async function generateTransactionCheckSum(input) {
|
|
22
|
+
const canonical = `${input.currency.trim()}~${input.amount.toFixed(2)}~${input.customerTransactionId.trim()}`;
|
|
23
|
+
const encoder = new TextEncoder();
|
|
24
|
+
const key = await subtle().importKey('raw', encoder.encode(input.secretHashKey), {
|
|
25
|
+
name: 'HMAC',
|
|
26
|
+
hash: 'SHA-256'
|
|
27
|
+
}, false, ['sign']);
|
|
28
|
+
const signature = await subtle().sign('HMAC', key, encoder.encode(canonical));
|
|
29
|
+
return bytesToBase64(new Uint8Array(signature));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Random UUID v4 (RTP Now's `rtpId`). These are unique references, not
|
|
34
|
+
* secrets: without `crypto.getRandomValues` (Expo Go has no native crypto module) it falls back
|
|
35
|
+
* to `Math.random`. Install `react-native-get-random-values` for crypto-quality ids.
|
|
36
|
+
*/
|
|
37
|
+
export function generateUuidV4() {
|
|
38
|
+
const c = globalThis.crypto;
|
|
39
|
+
if (c && typeof c.randomUUID === 'function') return c.randomUUID();
|
|
40
|
+
const bytes = new Uint8Array(16);
|
|
41
|
+
if (c && typeof c.getRandomValues === 'function') c.getRandomValues(bytes);else for (let i = 0; i < 16; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
42
|
+
bytes[6] = bytes[6] & 0x0f | 0x40;
|
|
43
|
+
bytes[8] = bytes[8] & 0x3f | 0x80;
|
|
44
|
+
const hex = Array.from(bytes, b => b.toString(16).padStart(2, '0')).join('');
|
|
45
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** UTF-8 bytes to text; works without `TextDecoder` (older Hermes / React Native). */
|
|
49
|
+
function utf8Decode(bytes) {
|
|
50
|
+
if (typeof TextDecoder !== 'undefined') return new TextDecoder().decode(bytes);
|
|
51
|
+
let encoded = '';
|
|
52
|
+
for (const byte of bytes) encoded += `%${byte.toString(16).padStart(2, '0')}`;
|
|
53
|
+
return decodeURIComponent(encoded);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Decodes a string `claim` from a JWT's payload without verifying its signature. */
|
|
57
|
+
export function decodeJwtClaim(jwt, claim) {
|
|
58
|
+
const parts = jwt.split('.');
|
|
59
|
+
if (parts.length !== 3) return null;
|
|
60
|
+
try {
|
|
61
|
+
let b64 = parts[1].replace(/-/g, '+').replace(/_/g, '/');
|
|
62
|
+
while (b64.length % 4 !== 0) b64 += '=';
|
|
63
|
+
const binary = atob(b64);
|
|
64
|
+
const bytes = Uint8Array.from(binary, ch => ch.charCodeAt(0));
|
|
65
|
+
const data = JSON.parse(utf8Decode(bytes));
|
|
66
|
+
if (typeof data !== 'object' || data === null) return null;
|
|
67
|
+
const value = data[claim];
|
|
68
|
+
return typeof value === 'string' ? value : null;
|
|
69
|
+
} catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/** Card number in 4-digit groups (`4242 4242 4242 4242`), capped at 19 digits. */
|
|
4
|
+
export function formatCardNumber(input) {
|
|
5
|
+
const digits = digitsOnly(input, 19);
|
|
6
|
+
return digits.replace(/(\d{4})(?=\d)/g, '$1 ');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Expiry as `MM/YY` while typing. */
|
|
10
|
+
export function formatExpiry(input) {
|
|
11
|
+
const digits = digitsOnly(input, 4);
|
|
12
|
+
return digits.length > 2 ? `${digits.slice(0, 2)}/${digits.slice(2)}` : digits;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Strips non-digits and optionally caps the length. */
|
|
16
|
+
export function digitsOnly(input, maxLength) {
|
|
17
|
+
const digits = String(input ?? '').replace(/\D/g, '');
|
|
18
|
+
return maxLength == null ? digits : digits.slice(0, maxLength);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Display amount like Dart's `num.toString()` for the payment sheet (`1000`, `10.5`). */
|
|
22
|
+
export function formatAmount(amount) {
|
|
23
|
+
return String(amount);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Dart's `toStringAsFixed(0)`, used by the direct sheets' pay buttons. */
|
|
27
|
+
export function formatAmountWhole(amount) {
|
|
28
|
+
return amount.toFixed(0);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Dart's `num.toDouble().toString()` - `1000` becomes `"1000.0"` (RTP E-wallet wire format). */
|
|
32
|
+
export function dartDoubleString(amount) {
|
|
33
|
+
if (!Number.isFinite(amount)) return String(amount);
|
|
34
|
+
return Number.isInteger(amount) ? `${amount}.0` : String(amount);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Today as `yyyy-MM-dd` in local time. */
|
|
38
|
+
export function todayIsoDate(now = new Date()) {
|
|
39
|
+
const two = n => String(n).padStart(2, '0');
|
|
40
|
+
return `${now.getFullYear()}-${two(now.getMonth() + 1)}-${two(now.getDate())}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** `mm:ss` for countdowns. */
|
|
44
|
+
export function formatCountdown(totalSeconds) {
|
|
45
|
+
const s = Math.max(0, Math.floor(totalSeconds));
|
|
46
|
+
const two = n => String(n).padStart(2, '0');
|
|
47
|
+
return `${two(Math.floor(s / 60))}:${two(s % 60)}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** `"<first 4 digits>***<last 4 digits>"`, regardless of whether already masked. */
|
|
51
|
+
export function maskForDisplay(value) {
|
|
52
|
+
const digits = String(value ?? '').replace(/\D/g, '');
|
|
53
|
+
if (digits.length <= 4) return value;
|
|
54
|
+
const prefix = digits.length >= 8 ? digits.slice(0, 4) : '';
|
|
55
|
+
return `${prefix}***${digits.slice(-4)}`;
|
|
56
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Defensive JSON helpers. The SwichPay backend mixes types for nominally-typed fields (numeric
|
|
5
|
+
* codes, `data: false` where an object is documented), so every read coerces instead of casting -
|
|
6
|
+
* same policy as every SwichPay SDK.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export function isJsonMap(value) {
|
|
10
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** `value` as a JSON object, or `null` when it's anything else. */
|
|
14
|
+
export function mapOrNull(value) {
|
|
15
|
+
return isJsonMap(value) ? value : null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Parses a response body leniently: empty or non-object JSON becomes `{}`. */
|
|
19
|
+
export function parseJsonObject(text) {
|
|
20
|
+
if (text === '') return {};
|
|
21
|
+
try {
|
|
22
|
+
const decoded = JSON.parse(text);
|
|
23
|
+
return isJsonMap(decoded) ? decoded : {};
|
|
24
|
+
} catch {
|
|
25
|
+
return {};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Any non-null value as a string (Dart `value.toString()` for primitives). */
|
|
30
|
+
export function str(value) {
|
|
31
|
+
if (value == null) return null;
|
|
32
|
+
if (typeof value === 'string') return value;
|
|
33
|
+
if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint') return String(value);
|
|
34
|
+
return JSON.stringify(value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A string only when the value is a non-blank string. */
|
|
38
|
+
export function nonBlankString(value) {
|
|
39
|
+
return typeof value === 'string' && value.trim() !== '' ? value : null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** `code` may arrive as a raw JSON number instead of a `"0000"`-style string. */
|
|
43
|
+
export function normalizedCode(code) {
|
|
44
|
+
if (code == null) return null;
|
|
45
|
+
if (typeof code === 'string') return code.trim();
|
|
46
|
+
if (typeof code === 'number') return String(Math.trunc(code));
|
|
47
|
+
return str(code);
|
|
48
|
+
}
|
|
49
|
+
export function intOrNull(value) {
|
|
50
|
+
return typeof value === 'number' && Number.isFinite(value) ? Math.trunc(value) : null;
|
|
51
|
+
}
|
|
52
|
+
export function numOr(value, fallback) {
|
|
53
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
54
|
+
if (typeof value === 'string' && value.trim() !== '') {
|
|
55
|
+
const parsed = Number(value);
|
|
56
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
57
|
+
}
|
|
58
|
+
return fallback;
|
|
59
|
+
}
|
|
60
|
+
export function stringOrNull(value) {
|
|
61
|
+
return typeof value === 'string' ? value : null;
|
|
62
|
+
}
|
|
63
|
+
export function boolOrNull(value) {
|
|
64
|
+
return typeof value === 'boolean' ? value : null;
|
|
65
|
+
}
|
|
66
|
+
export function listOfMaps(value) {
|
|
67
|
+
return Array.isArray(value) ? value.filter(isJsonMap) : [];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** First string value under `key` at any nesting depth. */
|
|
71
|
+
export function deepFindString(json, key) {
|
|
72
|
+
if (isJsonMap(json)) {
|
|
73
|
+
const direct = json[key];
|
|
74
|
+
if (typeof direct === 'string') return direct;
|
|
75
|
+
for (const value of Object.values(json)) {
|
|
76
|
+
const found = deepFindString(value, key);
|
|
77
|
+
if (found != null) return found;
|
|
78
|
+
}
|
|
79
|
+
} else if (Array.isArray(json)) {
|
|
80
|
+
for (const item of json) {
|
|
81
|
+
const found = deepFindString(item, key);
|
|
82
|
+
if (found != null) return found;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Map values coerced to strings (for `formFields`). */
|
|
89
|
+
export function stringMap(map) {
|
|
90
|
+
if (!map) return null;
|
|
91
|
+
const out = {};
|
|
92
|
+
for (const [key, value] of Object.entries(map)) out[key] = str(value) ?? 'null';
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Logging. The SDK prints nothing by default.
|
|
5
|
+
* Pass `logger` to `SwichPay.initialize()` to receive redacted debug lines.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Request/response body keys never logged in full. */
|
|
9
|
+
export const SENSITIVE_DATA_KEYS = new Set(['cardNumber', 'cvc', 'cvv', 'expiryDate', 'cnic', 'msisdn', 'mobileNumber', 'accountNumber', 'accountNo', 'accountNumberAlias', 'iban', 'otp',
|
|
10
|
+
// Discount API's referenceValue can carry a full card PAN for card-payment lookups.
|
|
11
|
+
'refrenceValue',
|
|
12
|
+
// Never present in a logged response today; masked defensively.
|
|
13
|
+
'clientSecret', 'client_secret', 'access_token', 'recievedCheckSum']);
|
|
14
|
+
|
|
15
|
+
/** Copies `data` with sensitive keys masked at any depth (nested objects and arrays), for logging only. */
|
|
16
|
+
export function redactForLog(data) {
|
|
17
|
+
if (data == null) return null;
|
|
18
|
+
return redactObject(data, 0);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Deeper than any SwichPay payload; stops a cyclic or hostile value from recursing forever. */
|
|
22
|
+
const MAX_REDACT_DEPTH = 8;
|
|
23
|
+
function redactValue(value, depth) {
|
|
24
|
+
if (Array.isArray(value)) return depth >= MAX_REDACT_DEPTH ? '<truncated>' : value.map(v => redactValue(v, depth + 1));
|
|
25
|
+
if (value !== null && typeof value === 'object') {
|
|
26
|
+
return depth >= MAX_REDACT_DEPTH ? '<truncated>' : redactObject(value, depth + 1);
|
|
27
|
+
}
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
function redactObject(data, depth) {
|
|
31
|
+
const out = {};
|
|
32
|
+
for (const [key, value] of Object.entries(data)) {
|
|
33
|
+
out[key] = SENSITIVE_DATA_KEYS.has(key) ? '<redacted>' : redactValue(value, depth);
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Headers that carry credentials or session keys (lower-case). */
|
|
39
|
+
const SENSITIVE_HEADERS = new Set(['authorization', 'cookie', 'publickey', 'x-public-key']);
|
|
40
|
+
|
|
41
|
+
/** Copies `headers` with credentials masked, for logging only. */
|
|
42
|
+
export function redactHeaders(headers) {
|
|
43
|
+
const out = {};
|
|
44
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
45
|
+
out[key] = SENSITIVE_HEADERS.has(key.toLowerCase()) ? '<redacted>' : value;
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Wraps an optional logger so callers can log unconditionally. */
|
|
51
|
+
export function createLog(logger) {
|
|
52
|
+
if (!logger) return () => {};
|
|
53
|
+
return message => {
|
|
54
|
+
try {
|
|
55
|
+
logger(message);
|
|
56
|
+
} catch {
|
|
57
|
+
// A broken logger must never break a payment.
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export function safeJson(value) {
|
|
62
|
+
try {
|
|
63
|
+
return JSON.stringify(value);
|
|
64
|
+
} catch {
|
|
65
|
+
return String(value);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { SwichPayValidationException } from '../errors.js';
|
|
4
|
+
|
|
5
|
+
/** `true` in a browser (or jsdom); `false` during SSR, in Node and in workers without a DOM. */
|
|
6
|
+
export function isBrowser() {
|
|
7
|
+
return typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** `true` inside a React Native app (Hermes/JSC), where there is a `window` but no `document`. */
|
|
11
|
+
export function isReactNative() {
|
|
12
|
+
return typeof navigator !== 'undefined' && navigator.product === 'ReactNative';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* `true` when the code runs on an end user's device - a browser or a mobile app - where any
|
|
17
|
+
* value in the bundle can be read by whoever has the app.
|
|
18
|
+
*/
|
|
19
|
+
export function isClientRuntime() {
|
|
20
|
+
return isBrowser() || isReactNative();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Throws a `SwichPayValidationException` when a browser-only feature is used during SSR. */
|
|
24
|
+
export function assertBrowser(feature) {
|
|
25
|
+
if (!isBrowser()) {
|
|
26
|
+
throw new SwichPayValidationException(`${feature} needs a browser and can't run during server-side rendering.`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/** Client-side card validation, run before anything leaves the device. */
|
|
4
|
+
export const CardValidators = {
|
|
5
|
+
/** Digits-only and 12-19 long after removing whitespace. No Luhn: sandbox test cards fail it. */
|
|
6
|
+
isValidNumber(number) {
|
|
7
|
+
const digits = String(number ?? '').replace(/\s+/g, '');
|
|
8
|
+
return digits !== '' && /^\d{12,19}$/.test(digits);
|
|
9
|
+
},
|
|
10
|
+
/** Month 1-12 and not expired; a 2-digit year means 20xx. Valid through the end of the month. */
|
|
11
|
+
isValidExpiry(month, year, now = new Date()) {
|
|
12
|
+
if (!Number.isInteger(month) || !Number.isInteger(year)) return false;
|
|
13
|
+
if (month < 1 || month > 12) return false;
|
|
14
|
+
const normalizedYear = year < 100 ? 2000 + year : year;
|
|
15
|
+
// First instant of the following month, local time - same as Dart's DateTime(year, month + 1).
|
|
16
|
+
const expiry = new Date(normalizedYear, month, 1, 0, 0, 0, 0);
|
|
17
|
+
return expiry.getTime() > now.getTime();
|
|
18
|
+
},
|
|
19
|
+
isValidCvv(cvv) {
|
|
20
|
+
return /^\d{3,4}$/.test(String(cvv ?? ''));
|
|
21
|
+
},
|
|
22
|
+
detectBrand(number) {
|
|
23
|
+
const digits = String(number ?? '').replace(/\s+/g, '');
|
|
24
|
+
if (digits.startsWith('4')) return 'visa';
|
|
25
|
+
if (/^5[1-5]/.test(digits)) return 'mastercard';
|
|
26
|
+
if (/^3[47]/.test(digits)) return 'amex';
|
|
27
|
+
return 'unknown';
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const PK_IBAN = /^PK\d{2}[A-Z]{4}[A-Z0-9]{16}$/;
|
|
31
|
+
|
|
32
|
+
/** Client-side validation for bank/RTP account identifiers. */
|
|
33
|
+
export const AccountValidators = {
|
|
34
|
+
/** Spaces removed and upper-cased - the form IBANs are sent in. */
|
|
35
|
+
normalizeIban(input) {
|
|
36
|
+
return String(input ?? '').replace(/\s+/g, '').toUpperCase();
|
|
37
|
+
},
|
|
38
|
+
/** `PK` + 2 check digits + 4-letter bank code + 16 characters. Format only, no mod-97. */
|
|
39
|
+
isValidPkIban(input) {
|
|
40
|
+
return PK_IBAN.test(AccountValidators.normalizeIban(input));
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Parses `MM/YY` into numbers, or `null` when it isn't that shape. */
|
|
45
|
+
export function parseExpiry(value) {
|
|
46
|
+
const parts = String(value ?? '').split('/');
|
|
47
|
+
if (parts.length !== 2) return null;
|
|
48
|
+
const [m, y] = parts;
|
|
49
|
+
if (!/^\d+$/.test(m) || !/^\d+$/.test(y)) return null;
|
|
50
|
+
return {
|
|
51
|
+
month: Number(m),
|
|
52
|
+
year: Number(y)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useRef, useState, useSyncExternalStore } from 'react';
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Owns a headless controller for the component's lifetime and subscribes to its state. `start`
|
|
9
|
+
* runs one tick after mount, so StrictMode's mount → unmount → mount starts it exactly once
|
|
10
|
+
* (a second start would create a second payment session, which SwichPay rejects).
|
|
11
|
+
*/
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
|
+
export function useController(create, start) {
|
|
14
|
+
const startRef = useLatest(start);
|
|
15
|
+
const createRef = useLatest(create);
|
|
16
|
+
const [controller, setController] = useState(() => create());
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (controller.disposed) {
|
|
19
|
+
setController(createRef.current());
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const timer = setTimeout(() => startRef.current?.(controller), 0);
|
|
23
|
+
return () => {
|
|
24
|
+
clearTimeout(timer);
|
|
25
|
+
controller.dispose();
|
|
26
|
+
};
|
|
27
|
+
}, [controller, createRef, startRef]);
|
|
28
|
+
const state = useSyncExternalStore(controller.subscribe, controller.getState, controller.getState);
|
|
29
|
+
return [controller, state];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The latest value, readable from callbacks without re-subscribing. */
|
|
33
|
+
export function useLatest(value) {
|
|
34
|
+
const ref = useRef(value);
|
|
35
|
+
ref.current = value;
|
|
36
|
+
return ref;
|
|
37
|
+
}
|
|
38
|
+
/** Runs `load` on mount (and on `reload()`), ignoring results that arrive after unmount. */
|
|
39
|
+
export function useAsync(load, deps) {
|
|
40
|
+
const [state, setState] = useState({
|
|
41
|
+
data: undefined,
|
|
42
|
+
error: undefined,
|
|
43
|
+
loading: true
|
|
44
|
+
});
|
|
45
|
+
const [nonce, setNonce] = useState(0);
|
|
46
|
+
const loadRef = useLatest(load);
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
let active = true;
|
|
49
|
+
setState(s => ({
|
|
50
|
+
...s,
|
|
51
|
+
loading: true,
|
|
52
|
+
error: undefined
|
|
53
|
+
}));
|
|
54
|
+
loadRef.current().then(data => active && setState({
|
|
55
|
+
data,
|
|
56
|
+
error: undefined,
|
|
57
|
+
loading: false
|
|
58
|
+
}), error => active && setState({
|
|
59
|
+
data: undefined,
|
|
60
|
+
error,
|
|
61
|
+
loading: false
|
|
62
|
+
}));
|
|
63
|
+
return () => {
|
|
64
|
+
active = false;
|
|
65
|
+
};
|
|
66
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
67
|
+
}, [...deps, nonce]);
|
|
68
|
+
return {
|
|
69
|
+
...state,
|
|
70
|
+
reload: () => setNonce(n => n + 1)
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `@swichpay/react-native` - the SwichPay SDK for React Native.
|
|
5
|
+
*
|
|
6
|
+
* ```tsx
|
|
7
|
+
* <SwichPayProvider options={{ clientId, referrer, merchantId, appId, environment: 'sandbox', proxyUrl }}>
|
|
8
|
+
* <App />
|
|
9
|
+
* </SwichPayProvider>
|
|
10
|
+
*
|
|
11
|
+
* const result = await SwichPay.presentPaymentSheet(details);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
import './polyfills.js';
|
|
15
|
+
|
|
16
|
+
// Facade, provider and hooks
|
|
17
|
+
export { SwichPay, useSwichPay, usePaymentResult, useAvailableCategories } from './swichpay.js';
|
|
18
|
+
export { SwichPayProvider } from './provider.js';
|
|
19
|
+
export { useSwichPayClient, useFlowServices, nativeFlowServices } from './context.js';
|
|
20
|
+
export { useController, useAsync } from './hooks.js';
|
|
21
|
+
|
|
22
|
+
// Ready-made checkouts (render them yourself, or open them with SwichPay.present*)
|
|
23
|
+
export { PaymentSheet } from './checkouts/PaymentSheet.js';
|
|
24
|
+
export { BankTransferCheckout, CardCheckout, EwalletCheckout, OneBillCheckout, QrCheckout, RtpEwalletCheckout, RtpNowCheckout } from './checkouts/direct.js';
|
|
25
|
+
export { HostedCheckout } from './HostedCheckout.js';
|
|
26
|
+
|
|
27
|
+
// Building blocks
|
|
28
|
+
export { StepUpView, DeviceDataCollectionHost } from './webviews.js';
|
|
29
|
+
export { QrCode } from './ui.js';
|
|
30
|
+
export { colors as swichPayColors } from './theme.js';
|
|
31
|
+
/** Payment method logos (Visa, Mastercard, PayPak, EasyPaisa, JazzCash, 1Bill, ...) as image data URIs. */
|
|
32
|
+
export * from './core/assets/method-logos.js';
|
|
33
|
+
|
|
34
|
+
// Everything framework-agnostic: client, models, errors, validators, formatters, controllers.
|
|
35
|
+
// (`SwichPay` above replaces the core facade of the same name.)
|
|
36
|
+
export * from './core/index.js';
|