@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,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/** A payment method category. Values are the Dart enum names. */
|
|
4
|
+
export const PaymentCategory = {
|
|
5
|
+
/** Local-only - opens the hosted checkout page full-screen. */
|
|
6
|
+
paymentSession: 'paymentSession',
|
|
7
|
+
/** Local-only - opens the hosted checkout page in a sheet. */
|
|
8
|
+
paymentSessionSheet: 'paymentSessionSheet',
|
|
9
|
+
/** Local-only - opens the SDK's direct card checkout. */
|
|
10
|
+
sdkCheckout: 'sdkCheckout',
|
|
11
|
+
ewallet: 'ewallet',
|
|
12
|
+
biller: 'biller',
|
|
13
|
+
bankAccount: 'bankAccount',
|
|
14
|
+
visaMasterCardPayment: 'visaMasterCardPayment',
|
|
15
|
+
unionPayCardPayment: 'unionPayCardPayment',
|
|
16
|
+
dqrcPayment: 'dqrcPayment',
|
|
17
|
+
sqrcPayment: 'sqrcPayment',
|
|
18
|
+
rtpNowPayment: 'rtpNowPayment',
|
|
19
|
+
rtpLaterPayment: 'rtpLaterPayment',
|
|
20
|
+
tokenization: 'tokenization',
|
|
21
|
+
rtpEwalletPayment: 'rtpEwalletPayment',
|
|
22
|
+
bnpl: 'bnpl',
|
|
23
|
+
/** An unrecognized category id, kept instead of dropped. */
|
|
24
|
+
unknown: 'unknown'
|
|
25
|
+
};
|
|
26
|
+
export const ALL_PAYMENT_CATEGORIES = Object.values(PaymentCategory);
|
|
27
|
+
const API_VALUES = {
|
|
28
|
+
paymentSession: '',
|
|
29
|
+
paymentSessionSheet: '',
|
|
30
|
+
sdkCheckout: '',
|
|
31
|
+
ewallet: 'ewallet',
|
|
32
|
+
biller: 'biller',
|
|
33
|
+
bankAccount: 'bankaccount',
|
|
34
|
+
visaMasterCardPayment: 'visamastercardpayment',
|
|
35
|
+
unionPayCardPayment: 'unionpaycardpayment',
|
|
36
|
+
dqrcPayment: 'dqrcpayment',
|
|
37
|
+
sqrcPayment: 'sqrcpayment',
|
|
38
|
+
rtpNowPayment: 'rtpnowpayment',
|
|
39
|
+
rtpLaterPayment: 'rtplaterpayment',
|
|
40
|
+
tokenization: 'tokenization',
|
|
41
|
+
rtpEwalletPayment: 'rtpewalletpayment',
|
|
42
|
+
bnpl: 'bnpl',
|
|
43
|
+
unknown: ''
|
|
44
|
+
};
|
|
45
|
+
const LABELS = {
|
|
46
|
+
paymentSession: 'Payment Session (WebSDK)',
|
|
47
|
+
paymentSessionSheet: 'Payment Session (Sheet)',
|
|
48
|
+
sdkCheckout: 'My SDK Checkout',
|
|
49
|
+
ewallet: 'Ewallet',
|
|
50
|
+
biller: '1Bill',
|
|
51
|
+
bankAccount: 'Bank Transfer',
|
|
52
|
+
visaMasterCardPayment: 'Credit/Debit',
|
|
53
|
+
unionPayCardPayment: 'UnionPay Card',
|
|
54
|
+
dqrcPayment: 'QR',
|
|
55
|
+
sqrcPayment: 'QR',
|
|
56
|
+
rtpNowPayment: 'RTP',
|
|
57
|
+
rtpLaterPayment: 'RTP Later',
|
|
58
|
+
tokenization: 'Tokenization',
|
|
59
|
+
rtpEwalletPayment: 'RTP Ewallet',
|
|
60
|
+
bnpl: 'Buy Now, Pay Later',
|
|
61
|
+
unknown: 'Other'
|
|
62
|
+
};
|
|
63
|
+
const LOGO_NAMES = {
|
|
64
|
+
paymentSession: [],
|
|
65
|
+
paymentSessionSheet: [],
|
|
66
|
+
sdkCheckout: [],
|
|
67
|
+
ewallet: ['EasyPaisa', 'JazzCash'],
|
|
68
|
+
biller: ['1Bill'],
|
|
69
|
+
bankAccount: ['BankTransfer'],
|
|
70
|
+
visaMasterCardPayment: ['Visa', 'Mastercard', 'PayPak'],
|
|
71
|
+
unionPayCardPayment: ['UnionPay'],
|
|
72
|
+
dqrcPayment: ['QR'],
|
|
73
|
+
sqrcPayment: ['QR'],
|
|
74
|
+
rtpNowPayment: [],
|
|
75
|
+
rtpLaterPayment: [],
|
|
76
|
+
tokenization: [],
|
|
77
|
+
rtpEwalletPayment: ['EasyPaisa', 'JazzCash'],
|
|
78
|
+
bnpl: [],
|
|
79
|
+
unknown: []
|
|
80
|
+
};
|
|
81
|
+
const FROM_API = new Map([['paymentsession', 'paymentSession'], ['ewallet', 'ewallet'], ['biller', 'biller'], ['bankaccount', 'bankAccount'], ['visamastercardpayment', 'visaMasterCardPayment'], ['unionpaycardpayment', 'unionPayCardPayment'], ['dqrcpayment', 'dqrcPayment'], ['sqrcpayment', 'sqrcPayment'], ['rtpnowpayment', 'rtpNowPayment'], ['rtplaterpayment', 'rtpLaterPayment'], ['tokenization', 'tokenization'], ['rtpewalletpayment', 'rtpEwalletPayment'], ['bnpl', 'bnpl']]);
|
|
82
|
+
|
|
83
|
+
/** Parses a raw category id from the API, falling back to `unknown`. Exact match. */
|
|
84
|
+
export function paymentCategoryFromApiValue(value) {
|
|
85
|
+
return typeof value === 'string' && FROM_API.get(value) || 'unknown';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The raw category id the API uses; empty for local-only categories. */
|
|
89
|
+
export function paymentCategoryCode(category) {
|
|
90
|
+
return API_VALUES[category] ?? '';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Human-readable label for a button. */
|
|
94
|
+
export function paymentCategoryLabel(category) {
|
|
95
|
+
return LABELS[category] ?? LABELS.unknown;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** `logos[].name` values that belong to this category. */
|
|
99
|
+
export function paymentCategoryLogoNames(category) {
|
|
100
|
+
return LOGO_NAMES[category] ?? [];
|
|
101
|
+
}
|
|
102
|
+
export function isPaymentCategory(value) {
|
|
103
|
+
return typeof value === 'string' && Object.prototype.hasOwnProperty.call(LABELS, value);
|
|
104
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { intOrNull, numOr, str, stringOrNull } from '../utils/json.js';
|
|
4
|
+
|
|
5
|
+
/** Request body for `POST /api/Discount/GetDiscount`. */
|
|
6
|
+
|
|
7
|
+
/** Wire body - note the API's own spelling `refrenceValue`. */
|
|
8
|
+
export function discountRequestToJson(request) {
|
|
9
|
+
return {
|
|
10
|
+
amount: request.amount,
|
|
11
|
+
refrenceValue: request.referenceValue,
|
|
12
|
+
categoryId: request.categoryId ?? 'Ewallet',
|
|
13
|
+
merchantId: request.merchantId,
|
|
14
|
+
customerTransactionId: request.customerTransactionId,
|
|
15
|
+
ipAddress: request.ipAddress ?? '',
|
|
16
|
+
txnDate: (request.txnDate ?? new Date()).toISOString(),
|
|
17
|
+
discountableAmount: request.discountableAmount ?? request.amount,
|
|
18
|
+
msisdn: request.msisdn
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Response from `POST /api/Discount/GetDiscount`. */
|
|
23
|
+
|
|
24
|
+
export function parseDiscountResult(json) {
|
|
25
|
+
return {
|
|
26
|
+
applied: json.applied === true,
|
|
27
|
+
transactionAmount: numOr(json.transactionAmount, 0),
|
|
28
|
+
discountAmount: numOr(json.discountAmount, 0),
|
|
29
|
+
finalAmount: numOr(json.finalAmount, 0),
|
|
30
|
+
campaignMasterId: intOrNull(json.campaignMasterId),
|
|
31
|
+
campaignDetailId: intOrNull(json.campaignDetailId),
|
|
32
|
+
merchantId: intOrNull(json.merchantId),
|
|
33
|
+
customerTransactionId: stringOrNull(json.customerTransactionId),
|
|
34
|
+
ruleType: stringOrNull(json.ruleType),
|
|
35
|
+
categoryId: str(json.categoryId),
|
|
36
|
+
appliedOn: stringOrNull(json.appliedOn)
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Whether `result` is safe to apply against `requestedAmount` - guards against a stale or
|
|
42
|
+
* malformed response producing a zero, negative or amount-increasing total.
|
|
43
|
+
*/
|
|
44
|
+
export function isDiscountUsableFor(result, requestedAmount) {
|
|
45
|
+
return result.applied && result.finalAmount > 0 && result.finalAmount <= requestedAmount && result.discountAmount >= 0;
|
|
46
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { SwichPayValidationException } from '../errors.js';
|
|
4
|
+
|
|
5
|
+
/** Order, customer and redirect details for a single payment attempt. */
|
|
6
|
+
|
|
7
|
+
/** `PaymentDetails` with every default applied. */
|
|
8
|
+
|
|
9
|
+
const REQUIRED_STRING_FIELDS = ['orderNumber', 'customerTransactionId', 'billReferenceNo', 'merchantName', 'consumerName', 'currency', 'email', 'msisdn', 'cnic', 'item', 'successRedirectUrl', 'failedRedirectUrl'];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Applies the defaults and checks the required fields at runtime
|
|
13
|
+
* (`required` named parameters), so plain-JavaScript callers get the same contract.
|
|
14
|
+
*/
|
|
15
|
+
export function resolvePaymentDetails(details) {
|
|
16
|
+
if (details == null || typeof details !== 'object') {
|
|
17
|
+
throw new SwichPayValidationException('Payment details are required.');
|
|
18
|
+
}
|
|
19
|
+
for (const field of REQUIRED_STRING_FIELDS) {
|
|
20
|
+
if (typeof details[field] !== 'string') {
|
|
21
|
+
throw new SwichPayValidationException(`PaymentDetails.${field} is required and must be a string.`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (typeof details.amount !== 'number' || !Number.isFinite(details.amount)) {
|
|
25
|
+
throw new SwichPayValidationException('PaymentDetails.amount must be a finite number.');
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
...details,
|
|
29
|
+
description: details.description ?? null,
|
|
30
|
+
date: details.date ?? null,
|
|
31
|
+
successRedirectParam: details.successRedirectParam ?? null,
|
|
32
|
+
discountAmount: details.discountAmount ?? 0,
|
|
33
|
+
isRecurringPayment: details.isRecurringPayment ?? false,
|
|
34
|
+
attempts: details.attempts ?? 10,
|
|
35
|
+
expiryMinutes: details.expiryMinutes ?? 10,
|
|
36
|
+
merchantCustomerToken: details.merchantCustomerToken ?? null,
|
|
37
|
+
doCapture: details.doCapture ?? false
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Raw card details - never logged or persisted. */
|
|
42
|
+
|
|
43
|
+
/** Outcome of a payment attempt. */
|
|
44
|
+
|
|
45
|
+
/** Builds a `PaymentResult` with every optional field defaulted. */
|
|
46
|
+
export function createPaymentResult(init) {
|
|
47
|
+
return {
|
|
48
|
+
code: null,
|
|
49
|
+
message: null,
|
|
50
|
+
timestamp: null,
|
|
51
|
+
orderId: null,
|
|
52
|
+
redirectUrl: null,
|
|
53
|
+
isRedirectionRequired: false,
|
|
54
|
+
merchantRedirectionUrl: null,
|
|
55
|
+
remainingAttempts: null,
|
|
56
|
+
consumerNumber: null,
|
|
57
|
+
redirectFormFields: null,
|
|
58
|
+
postUrl: null,
|
|
59
|
+
continuation: null,
|
|
60
|
+
ddcCollectionUrl: null,
|
|
61
|
+
vendorTransactionId: null,
|
|
62
|
+
stepUpUrl: null,
|
|
63
|
+
qrString: null,
|
|
64
|
+
otpRequired: false,
|
|
65
|
+
bankTransactionId: null,
|
|
66
|
+
transactionReferenceNo: null,
|
|
67
|
+
retreivalReferenceNo: null,
|
|
68
|
+
retryable: false,
|
|
69
|
+
...init
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The result a checkout resolves with when the user closes it. */
|
|
74
|
+
export function cancelledResult() {
|
|
75
|
+
return createPaymentResult({
|
|
76
|
+
isSuccess: false,
|
|
77
|
+
status: 'cancelled',
|
|
78
|
+
message: 'User closed the payment sheet'
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The failure a direct checkout hands back once its transaction id is used up. */
|
|
83
|
+
export function failedResult(message) {
|
|
84
|
+
return createPaymentResult({
|
|
85
|
+
isSuccess: false,
|
|
86
|
+
status: 'failed',
|
|
87
|
+
message: message ?? 'Payment failed.'
|
|
88
|
+
});
|
|
89
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { maskForDisplay } from '../utils/formatters.js';
|
|
4
|
+
import { intOrNull, listOfMaps, normalizedCode, stringOrNull } from '../utils/json.js';
|
|
5
|
+
import { paymentCategoryFromApiValue } from './category.js';
|
|
6
|
+
|
|
7
|
+
/** A wallet channel (e.g. EasyPaisa, JazzCash) enabled for a payment session. */
|
|
8
|
+
|
|
9
|
+
/** A bank enabled for a payment session, scoped per `channelCode` - the same `bankId` can repeat. */
|
|
10
|
+
|
|
11
|
+
/** A wallet account the customer previously saved. `channel` is a `channelId` as a string. */
|
|
12
|
+
|
|
13
|
+
/** A card previously saved via `doCardOnFile: true`. `id` is sent as `savedCardId`. */
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Details of a payment session (`paymentsession/get`'s `data`). Never carries the response's
|
|
17
|
+
* `clientSecret`.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Result of `GET /gateway/paymentsession/inquire`. */
|
|
21
|
+
|
|
22
|
+
/** A bank from a direct (session-less) flow's bank-list endpoint. */
|
|
23
|
+
|
|
24
|
+
/** `mobileappinit`: enabled categories plus logo URLs keyed by logo name. */
|
|
25
|
+
|
|
26
|
+
export function maskedSavedWallet(wallet) {
|
|
27
|
+
return maskForDisplay(wallet.msisdn);
|
|
28
|
+
}
|
|
29
|
+
export function maskedSavedCard(card) {
|
|
30
|
+
return maskForDisplay(card.cardNumber);
|
|
31
|
+
}
|
|
32
|
+
function requiredString(json, key, fallback = '') {
|
|
33
|
+
const value = json[key];
|
|
34
|
+
return typeof value === 'string' ? value : fallback;
|
|
35
|
+
}
|
|
36
|
+
export function parsePaymentChannel(json) {
|
|
37
|
+
return {
|
|
38
|
+
channelId: intOrNull(json.channelId) ?? 0,
|
|
39
|
+
channelName: requiredString(json, 'channelName'),
|
|
40
|
+
channelCode: requiredString(json, 'channelCode')
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export function parsePaymentBank(json) {
|
|
44
|
+
const bankCode = requiredString(json, 'bankCode');
|
|
45
|
+
return {
|
|
46
|
+
bankId: intOrNull(json.bankId) ?? 0,
|
|
47
|
+
bankName: requiredString(json, 'bankName'),
|
|
48
|
+
bankCode,
|
|
49
|
+
swichBankCode: stringOrNull(json.swich_BankCode) ?? bankCode,
|
|
50
|
+
channelId: intOrNull(json.channelId) ?? 0,
|
|
51
|
+
channelCode: requiredString(json, 'channelCode')
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export function parseSavedWallet(json) {
|
|
55
|
+
return {
|
|
56
|
+
id: intOrNull(json.id) ?? 0,
|
|
57
|
+
msisdn: stringOrNull(json.msisdn) ?? '',
|
|
58
|
+
channel: stringOrNull(json.channel) ?? '',
|
|
59
|
+
customerToken: stringOrNull(json.customerToken) ?? ''
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The id arrives under `savedCardId`, the PAN under `pan`; `id`/`cardNumber` are fallbacks. */
|
|
64
|
+
export function parseSavedCard(json) {
|
|
65
|
+
return {
|
|
66
|
+
id: intOrNull(json.savedCardId) ?? intOrNull(json.id) ?? 0,
|
|
67
|
+
cardNumber: stringOrNull(json.pan) ?? stringOrNull(json.cardNumber) ?? '',
|
|
68
|
+
cardHolderName: stringOrNull(json.cardHolderName) ?? '',
|
|
69
|
+
expiryDate: stringOrNull(json.expiryDate) ?? '',
|
|
70
|
+
customerToken: stringOrNull(json.customerToken) ?? '',
|
|
71
|
+
channel: stringOrNull(json.channel) ?? ''
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Parses the `data` object of a `paymentsession/get` response. */
|
|
76
|
+
export function parsePaymentSessionDetails(json) {
|
|
77
|
+
// Only entries explicitly marked `status: false` are dropped.
|
|
78
|
+
const categoryMaps = listOfMaps(json.categories).filter(c => c.status !== false);
|
|
79
|
+
const categoryIds = {};
|
|
80
|
+
for (const c of categoryMaps) {
|
|
81
|
+
const id = c.categoryId ?? c.id;
|
|
82
|
+
if (typeof id === 'number' && Number.isInteger(id)) {
|
|
83
|
+
categoryIds[paymentCategoryFromApiValue(c.categoryCode)] = id;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
paymentSessionGuid: requiredString(json, 'paymentSessionGuid'),
|
|
88
|
+
categories: categoryMaps.map(c => paymentCategoryFromApiValue(c.categoryCode)),
|
|
89
|
+
categoryIds,
|
|
90
|
+
expiryMinutes: intOrNull(json.expiry) ?? 15,
|
|
91
|
+
remainingAttempts: intOrNull(json.remainingAttempts) ?? 3,
|
|
92
|
+
channels: listOfMaps(json.channels).map(parsePaymentChannel),
|
|
93
|
+
banks: listOfMaps(json.banks).map(parsePaymentBank),
|
|
94
|
+
savedWallets: listOfMaps(json.savedWallets).map(parseSavedWallet),
|
|
95
|
+
savedCards: listOfMaps(json.savedCards).map(parseSavedCard),
|
|
96
|
+
clientId: stringOrNull(json.clientId)
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
export function parsePaymentSessionInquiryResult(json) {
|
|
100
|
+
const code = normalizedCode(json.code);
|
|
101
|
+
const status = stringOrNull(json.status);
|
|
102
|
+
return {
|
|
103
|
+
transactionId: intOrNull(json.transactionId) ?? 0,
|
|
104
|
+
orderId: stringOrNull(json.orderId),
|
|
105
|
+
paymentSessionGuid: stringOrNull(json.paymentSessionGuid),
|
|
106
|
+
status,
|
|
107
|
+
code,
|
|
108
|
+
message: stringOrNull(json.message),
|
|
109
|
+
timestamp: stringOrNull(json.timestamp),
|
|
110
|
+
amount: typeof json.amount === 'number' ? json.amount : 0,
|
|
111
|
+
isSuccess: code === '0000' || status?.toLowerCase() === 'success'
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
export function parseDirectBank(json) {
|
|
115
|
+
return {
|
|
116
|
+
name: stringOrNull(json.name) ?? '',
|
|
117
|
+
code: stringOrNull(json.code) ?? '',
|
|
118
|
+
bankProviderCode: stringOrNull(json.bankProviderCode)
|
|
119
|
+
};
|
|
120
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `@swichpay/core/server` - runs on your server (Node 18+, Nitro, edge runtimes with fetch).
|
|
5
|
+
* Never import it into browser code: it handles `clientSecret` and `secretHashKey`.
|
|
6
|
+
*/
|
|
7
|
+
export { createSwichPayProxy } from './proxy.js';
|
|
8
|
+
export { createNodeProxyMiddleware } from './node.js';
|
|
9
|
+
export { fetchClientCredentialsToken } from '../http/api-client.js';
|
|
10
|
+
export { generateTransactionCheckSum } from '../utils/crypto.js';
|
|
11
|
+
export { classifyInquiry, createCheckoutSessionCreator, createPaymentVerifier, evaluateInquiry, lazyServerClient } from './verify.js';
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/** The parts of Node's `IncomingMessage` the adapter uses (no `node:http` import needed). */
|
|
4
|
+
|
|
5
|
+
/** The parts of Node's `ServerResponse` the adapter uses. */
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Adapts a proxy to Node/Express/Connect (`(req, res, next)`), mounted at `basePath`.
|
|
9
|
+
* Requests outside `basePath` go to `next()` when given, else 404.
|
|
10
|
+
*/
|
|
11
|
+
export function createNodeProxyMiddleware(proxy, basePath) {
|
|
12
|
+
const base = `/${basePath.replace(/^\/+|\/+$/g, '')}`;
|
|
13
|
+
return async (req, res, next) => {
|
|
14
|
+
const rawUrl = req.url ?? '/';
|
|
15
|
+
const pathname = rawUrl.split('?')[0] ?? '/';
|
|
16
|
+
if (pathname !== base && !pathname.startsWith(`${base}/`)) {
|
|
17
|
+
if (next) return next();
|
|
18
|
+
res.statusCode = 404;
|
|
19
|
+
res.end();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
const headers = new Headers();
|
|
24
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
25
|
+
if (typeof value === 'string') headers.set(key, value);else if (Array.isArray(value)) headers.set(key, value.join(', '));
|
|
26
|
+
}
|
|
27
|
+
const method = (req.method ?? 'GET').toUpperCase();
|
|
28
|
+
let body;
|
|
29
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
30
|
+
let text = '';
|
|
31
|
+
const decoder = new TextDecoder();
|
|
32
|
+
for await (const chunk of req) text += typeof chunk === 'string' ? chunk : decoder.decode(chunk, {
|
|
33
|
+
stream: true
|
|
34
|
+
});
|
|
35
|
+
body = text + decoder.decode();
|
|
36
|
+
}
|
|
37
|
+
const host = headers.get('host') ?? 'localhost';
|
|
38
|
+
const request = new Request(`http://${host}${rawUrl}`, {
|
|
39
|
+
method,
|
|
40
|
+
headers,
|
|
41
|
+
body
|
|
42
|
+
});
|
|
43
|
+
const response = await proxy(request, pathname.slice(base.length));
|
|
44
|
+
res.statusCode = response.status;
|
|
45
|
+
res.setHeader('content-type', response.headers.get('content-type') ?? 'application/json');
|
|
46
|
+
res.setHeader('cache-control', 'no-store');
|
|
47
|
+
res.end(await response.text());
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (next) return next(error);
|
|
50
|
+
res.statusCode = 500;
|
|
51
|
+
res.end(JSON.stringify({
|
|
52
|
+
message: 'SwichPay proxy error.'
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { resolveConfig } from '../config.js';
|
|
4
|
+
import { SwichPayValidationException, isSwichPayException } from '../errors.js';
|
|
5
|
+
import { fetchClientCredentialsToken } from '../http/api-client.js';
|
|
6
|
+
import { Endpoints, PROXY_SEGMENTS, SERVICE_ENDPOINTS } from '../http/endpoints.js';
|
|
7
|
+
import { TokenManager } from '../http/token-manager.js';
|
|
8
|
+
import { generateTransactionCheckSum } from '../utils/crypto.js';
|
|
9
|
+
import { isJsonMap } from '../utils/json.js';
|
|
10
|
+
|
|
11
|
+
/** `(request, path)` → response. `path` is what follows the proxy's mount point, e.g. `/gateway/gateway/paymentsession/get`. */
|
|
12
|
+
|
|
13
|
+
/** Request headers forwarded from the browser; everything else (cookies, auth) is dropped. */
|
|
14
|
+
const FORWARDED_HEADERS = ['content-type', 'accept', 'x-public-key', 'publickey'];
|
|
15
|
+
const SEGMENT_TO_SERVICE = new Map(Object.entries(PROXY_SEGMENTS).map(([service, segment]) => [segment, service]));
|
|
16
|
+
function json(status, body) {
|
|
17
|
+
return new Response(JSON.stringify(body), {
|
|
18
|
+
status,
|
|
19
|
+
headers: {
|
|
20
|
+
'content-type': 'application/json'
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A server-side proxy for the web SDK's `proxyUrl` mode. It forwards only SwichPay's own
|
|
27
|
+
* endpoint paths to the configured environment and adds what a browser can't safely send: the
|
|
28
|
+
* OAuth bearer token (from `clientSecret`), the `Referer`/`referrer` headers and, for direct card
|
|
29
|
+
* payments, the `recievedCheckSum` signed with `secretHashKey`.
|
|
30
|
+
*/
|
|
31
|
+
export function createSwichPayProxy(options) {
|
|
32
|
+
const config = resolveConfig({
|
|
33
|
+
...options,
|
|
34
|
+
proxyUrl: undefined,
|
|
35
|
+
getAccessToken: undefined,
|
|
36
|
+
getChecksum: undefined
|
|
37
|
+
});
|
|
38
|
+
if (!options.authorize && !options.dangerouslyAllowUnauthenticated) {
|
|
39
|
+
throw new SwichPayValidationException('createSwichPayProxy needs `authorize` (check your user session) - or dangerouslyAllowUnauthenticated: true for local testing only.');
|
|
40
|
+
}
|
|
41
|
+
if (config.secretHashKey && !options.verifyAmount && !options.dangerouslySignAnyAmount) {
|
|
42
|
+
throw new SwichPayValidationException('createSwichPayProxy with secretHashKey needs `verifyAmount` (check the amount against your order) - ' + 'or dangerouslySignAnyAmount: true for local testing only.');
|
|
43
|
+
}
|
|
44
|
+
if (config.environment === 'production' && (options.dangerouslyAllowUnauthenticated || options.dangerouslySignAnyAmount)) {
|
|
45
|
+
config.log('proxy: WARNING - a dangerously* option is set in production.');
|
|
46
|
+
}
|
|
47
|
+
const tokens = config.clientSecret ? new TokenManager(() => fetchClientCredentialsToken({
|
|
48
|
+
fetch: config.fetch,
|
|
49
|
+
authHost: config.hosts.auth,
|
|
50
|
+
clientId: config.clientId,
|
|
51
|
+
clientSecret: config.clientSecret,
|
|
52
|
+
timeoutMs: config.timeouts.request,
|
|
53
|
+
log: config.log
|
|
54
|
+
})) : null;
|
|
55
|
+
const hostFor = service => config.hosts[service];
|
|
56
|
+
return async function swichPayProxy(request, path) {
|
|
57
|
+
const method = request.method.toUpperCase();
|
|
58
|
+
if (method !== 'GET' && method !== 'POST') return json(405, {
|
|
59
|
+
message: 'Method not allowed.'
|
|
60
|
+
});
|
|
61
|
+
const clean = `/${path.replace(/^\/+/, '')}`;
|
|
62
|
+
const slash = clean.indexOf('/', 1);
|
|
63
|
+
const segment = slash === -1 ? clean.slice(1) : clean.slice(1, slash);
|
|
64
|
+
const endpoint = slash === -1 ? '' : clean.slice(slash);
|
|
65
|
+
const service = SEGMENT_TO_SERVICE.get(segment);
|
|
66
|
+
if (!service || service === 'auth' || !SERVICE_ENDPOINTS[service].includes(endpoint)) {
|
|
67
|
+
return json(404, {
|
|
68
|
+
message: 'Not a SwichPay endpoint.'
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const host = hostFor(service);
|
|
72
|
+
if (!host) return json(404, {
|
|
73
|
+
message: `This endpoint is not available in ${config.environment} yet.`
|
|
74
|
+
});
|
|
75
|
+
if (options.authorize) {
|
|
76
|
+
const rejected = await options.authorize(request);
|
|
77
|
+
if (rejected instanceof Response) return rejected;
|
|
78
|
+
}
|
|
79
|
+
const headers = {};
|
|
80
|
+
for (const name of FORWARDED_HEADERS) {
|
|
81
|
+
const value = request.headers.get(name);
|
|
82
|
+
if (value != null) headers[name] = value;
|
|
83
|
+
}
|
|
84
|
+
if (service === 'checkout') headers.Referer = config.referrer;
|
|
85
|
+
if (service === 'gateway' || service === 'paymentApi') headers.referrer = config.appId;
|
|
86
|
+
let body;
|
|
87
|
+
if (method === 'POST') {
|
|
88
|
+
body = await request.text();
|
|
89
|
+
if (service === 'paymentApi' && endpoint === Endpoints.directCardPayment && config.secretHashKey) {
|
|
90
|
+
const signed = await signDirectCardBody(body, config.secretHashKey, payment => options.verifyAmount ? options.verifyAmount(payment, request) : true);
|
|
91
|
+
if (signed == null) {
|
|
92
|
+
config.log('proxy: verifyAmount rejected a direct card payment');
|
|
93
|
+
return json(400, {
|
|
94
|
+
message: 'Payment amount does not match the order.',
|
|
95
|
+
status: 'failed'
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
body = signed;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const search = new URL(request.url).search;
|
|
102
|
+
const upstreamUrl = `${host}${endpoint}${search}`;
|
|
103
|
+
const needsAuth = service !== 'checkout';
|
|
104
|
+
const send = async fresh => {
|
|
105
|
+
const h = {
|
|
106
|
+
...headers
|
|
107
|
+
};
|
|
108
|
+
if (needsAuth) {
|
|
109
|
+
if (tokens) {
|
|
110
|
+
if (fresh) tokens.invalidate();
|
|
111
|
+
h.Authorization = `Bearer ${await tokens.getToken()}`;
|
|
112
|
+
} else {
|
|
113
|
+
h['x-public-key'] = config.clientId;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const controller = new AbortController();
|
|
117
|
+
const timer = setTimeout(() => controller.abort(), service === 'discount' ? config.timeouts.discount : config.timeouts.request);
|
|
118
|
+
try {
|
|
119
|
+
return await config.fetch(upstreamUrl, {
|
|
120
|
+
method,
|
|
121
|
+
headers: h,
|
|
122
|
+
body,
|
|
123
|
+
signal: controller.signal
|
|
124
|
+
});
|
|
125
|
+
} finally {
|
|
126
|
+
clearTimeout(timer);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
let upstream;
|
|
130
|
+
try {
|
|
131
|
+
upstream = await send(false);
|
|
132
|
+
if (upstream.status === 401 && needsAuth && tokens) {
|
|
133
|
+
config.log('proxy: 401 with a cached token - refreshing it and retrying once');
|
|
134
|
+
upstream = await send(true);
|
|
135
|
+
}
|
|
136
|
+
} catch (error) {
|
|
137
|
+
config.log(`proxy: ${upstreamUrl} failed: ${String(error)}`);
|
|
138
|
+
if (isSwichPayException(error)) {
|
|
139
|
+
// The token exchange failed - an authorization problem (401), never leak the secret.
|
|
140
|
+
return json(401, {
|
|
141
|
+
message: 'SwichPay authentication failed.',
|
|
142
|
+
status: 'failed'
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return json(502, {
|
|
146
|
+
message: 'Failed to reach SwichPay API.',
|
|
147
|
+
status: 'failed'
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
const text = await upstream.text();
|
|
151
|
+
return new Response(text, {
|
|
152
|
+
status: upstream.status,
|
|
153
|
+
headers: {
|
|
154
|
+
'content-type': upstream.headers.get('content-type') ?? 'application/json'
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Sets `recievedCheckSum` from the body's own currency/amount/customerTransactionId once `verify`
|
|
162
|
+
* approves them. `null` = rejected. A body without those fields is forwarded unsigned (SwichPay
|
|
163
|
+
* refuses it).
|
|
164
|
+
*/
|
|
165
|
+
async function signDirectCardBody(body, secretHashKey, verify) {
|
|
166
|
+
let parsed;
|
|
167
|
+
try {
|
|
168
|
+
parsed = JSON.parse(body);
|
|
169
|
+
} catch {
|
|
170
|
+
return body;
|
|
171
|
+
}
|
|
172
|
+
if (!isJsonMap(parsed)) return body;
|
|
173
|
+
const {
|
|
174
|
+
currency,
|
|
175
|
+
amount,
|
|
176
|
+
customerTransactionId
|
|
177
|
+
} = parsed;
|
|
178
|
+
if (typeof currency !== 'string' || typeof amount !== 'number' || typeof customerTransactionId !== 'string') return body;
|
|
179
|
+
if ((await verify({
|
|
180
|
+
currency,
|
|
181
|
+
amount,
|
|
182
|
+
customerTransactionId
|
|
183
|
+
})) !== true) return null;
|
|
184
|
+
parsed.recievedCheckSum = await generateTransactionCheckSum({
|
|
185
|
+
currency,
|
|
186
|
+
amount,
|
|
187
|
+
customerTransactionId,
|
|
188
|
+
secretHashKey
|
|
189
|
+
});
|
|
190
|
+
return JSON.stringify(parsed);
|
|
191
|
+
}
|