@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,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Emitter = void 0;
|
|
7
|
+
exports.createSubscription = createSubscription;
|
|
8
|
+
/** Returned by `onResult`: call it (or `.cancel()`) to stop listening. */
|
|
9
|
+
|
|
10
|
+
function createSubscription(off) {
|
|
11
|
+
let active = true;
|
|
12
|
+
const stop = () => {
|
|
13
|
+
if (!active) return;
|
|
14
|
+
active = false;
|
|
15
|
+
off();
|
|
16
|
+
};
|
|
17
|
+
return Object.assign(stop, {
|
|
18
|
+
cancel: stop
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Minimal broadcast emitter. Like a Dart broadcast `StreamController`, events are delivered
|
|
24
|
+
* asynchronously (next microtask), and a throwing listener never affects the others.
|
|
25
|
+
*/
|
|
26
|
+
class Emitter {
|
|
27
|
+
listeners = new Set();
|
|
28
|
+
closed = false;
|
|
29
|
+
get listenerCount() {
|
|
30
|
+
return this.listeners.size;
|
|
31
|
+
}
|
|
32
|
+
on(listener) {
|
|
33
|
+
if (typeof listener !== 'function') throw new TypeError('listener must be a function');
|
|
34
|
+
const wrapped = value => listener(value);
|
|
35
|
+
this.listeners.add(wrapped);
|
|
36
|
+
return createSubscription(() => this.listeners.delete(wrapped));
|
|
37
|
+
}
|
|
38
|
+
emit(value) {
|
|
39
|
+
if (this.closed || this.listeners.size === 0) return;
|
|
40
|
+
const snapshot = [...this.listeners];
|
|
41
|
+
queueMicrotask(() => {
|
|
42
|
+
for (const listener of snapshot) {
|
|
43
|
+
if (!this.listeners.has(listener)) continue;
|
|
44
|
+
try {
|
|
45
|
+
listener(value);
|
|
46
|
+
} catch {
|
|
47
|
+
// Isolated: a broken listener must not break payment flows or other listeners.
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
close() {
|
|
53
|
+
this.closed = true;
|
|
54
|
+
this.listeners.clear();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.Emitter = Emitter;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.FlowController = void 0;
|
|
7
|
+
/** Called once when a checkout finishes; `null` means dismissed without a result. */
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Base for the headless checkout controllers. Holds immutable state (a new object per change, so
|
|
11
|
+
* React's `useSyncExternalStore` and Vue's `shallowRef` can both track it), tracked timers, and
|
|
12
|
+
* a disposed flag that is checked after every `await`.
|
|
13
|
+
*/
|
|
14
|
+
class FlowController {
|
|
15
|
+
listeners = new Set();
|
|
16
|
+
timers = new Set();
|
|
17
|
+
intervals = new Set();
|
|
18
|
+
finished = false;
|
|
19
|
+
_disposed = false;
|
|
20
|
+
constructor(initial, onFinish) {
|
|
21
|
+
this.onFinish = onFinish;
|
|
22
|
+
this.state = initial;
|
|
23
|
+
}
|
|
24
|
+
getState = () => this.state;
|
|
25
|
+
subscribe = listener => {
|
|
26
|
+
this.listeners.add(listener);
|
|
27
|
+
return () => {
|
|
28
|
+
this.listeners.delete(listener);
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
get disposed() {
|
|
32
|
+
return this._disposed;
|
|
33
|
+
}
|
|
34
|
+
get isFinished() {
|
|
35
|
+
return this.finished;
|
|
36
|
+
}
|
|
37
|
+
setState(patch) {
|
|
38
|
+
if (this._disposed) return;
|
|
39
|
+
const next = typeof patch === 'function' ? patch(this.state) : patch;
|
|
40
|
+
this.state = this.derive({
|
|
41
|
+
...this.state,
|
|
42
|
+
...next
|
|
43
|
+
});
|
|
44
|
+
for (const listener of [...this.listeners]) listener();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Recomputes derived fields; override to keep labels/flags in sync with the inputs. */
|
|
48
|
+
derive(state) {
|
|
49
|
+
return state;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Resolves the checkout once; later calls are ignored. */
|
|
53
|
+
finish(result) {
|
|
54
|
+
if (this.finished || this._disposed) return;
|
|
55
|
+
this.finished = true;
|
|
56
|
+
this.onFinish?.(result);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Backdrop / Escape. Dismissing this way resolves `null`;
|
|
61
|
+
* the payment sheet and the busy card checkout override it.
|
|
62
|
+
*/
|
|
63
|
+
dismiss() {
|
|
64
|
+
this.finish(null);
|
|
65
|
+
}
|
|
66
|
+
setTimer(fn, ms) {
|
|
67
|
+
const id = setTimeout(() => {
|
|
68
|
+
this.timers.delete(id);
|
|
69
|
+
if (!this._disposed) fn();
|
|
70
|
+
}, ms);
|
|
71
|
+
this.timers.add(id);
|
|
72
|
+
return id;
|
|
73
|
+
}
|
|
74
|
+
clearTimer(id) {
|
|
75
|
+
if (id == null) return;
|
|
76
|
+
clearTimeout(id);
|
|
77
|
+
this.timers.delete(id);
|
|
78
|
+
}
|
|
79
|
+
setIntervalTimer(fn, ms) {
|
|
80
|
+
const id = setInterval(() => {
|
|
81
|
+
if (this._disposed) return;
|
|
82
|
+
fn();
|
|
83
|
+
}, ms);
|
|
84
|
+
this.intervals.add(id);
|
|
85
|
+
return id;
|
|
86
|
+
}
|
|
87
|
+
clearIntervalTimer(id) {
|
|
88
|
+
if (id == null) return;
|
|
89
|
+
clearInterval(id);
|
|
90
|
+
this.intervals.delete(id);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Cancels every timer and stops all further state changes. Safe to call twice. */
|
|
94
|
+
dispose() {
|
|
95
|
+
if (this._disposed) return;
|
|
96
|
+
this._disposed = true;
|
|
97
|
+
for (const id of this.timers) clearTimeout(id);
|
|
98
|
+
for (const id of this.intervals) clearInterval(id);
|
|
99
|
+
this.timers.clear();
|
|
100
|
+
this.intervals.clear();
|
|
101
|
+
this.listeners.clear();
|
|
102
|
+
this.onDispose();
|
|
103
|
+
}
|
|
104
|
+
onDispose() {}
|
|
105
|
+
}
|
|
106
|
+
exports.FlowController = FlowController;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.CountdownController = void 0;
|
|
7
|
+
var _formatters = require("../utils/formatters.js");
|
|
8
|
+
var _controller = require("./controller.js");
|
|
9
|
+
/** A display-only countdown (the PWA page's timer box). Call `start()`; `dispose()` stops it. */
|
|
10
|
+
class CountdownController extends _controller.FlowController {
|
|
11
|
+
timer = null;
|
|
12
|
+
constructor(minutes) {
|
|
13
|
+
const seconds = Math.max(0, Math.round(minutes * 60));
|
|
14
|
+
super({
|
|
15
|
+
secondsLeft: seconds,
|
|
16
|
+
label: (0, _formatters.formatCountdown)(seconds),
|
|
17
|
+
done: seconds === 0
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
derive(state) {
|
|
21
|
+
return {
|
|
22
|
+
...state,
|
|
23
|
+
label: (0, _formatters.formatCountdown)(state.secondsLeft),
|
|
24
|
+
done: state.secondsLeft <= 0
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
start() {
|
|
28
|
+
if (this.timer || this.getState().done) return;
|
|
29
|
+
this.timer = this.setIntervalTimer(() => {
|
|
30
|
+
const left = this.getState().secondsLeft - 1;
|
|
31
|
+
this.setState({
|
|
32
|
+
secondsLeft: Math.max(0, left)
|
|
33
|
+
});
|
|
34
|
+
if (left <= 0) {
|
|
35
|
+
this.clearIntervalTimer(this.timer);
|
|
36
|
+
this.timer = null;
|
|
37
|
+
}
|
|
38
|
+
}, 1000);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.CountdownController = CountdownController;
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.OTP_RESEND_SECONDS = exports.DirectBankTransferController = void 0;
|
|
7
|
+
var _errors = require("../errors.js");
|
|
8
|
+
var _payment = require("../models/payment.js");
|
|
9
|
+
var _formatters = require("../utils/formatters.js");
|
|
10
|
+
var _controller = require("./controller.js");
|
|
11
|
+
var _shared = require("./shared.js");
|
|
12
|
+
const OTP_RESEND_SECONDS = exports.OTP_RESEND_SECONDS = 60;
|
|
13
|
+
/** Direct bank transfer with OTP confirmation. */
|
|
14
|
+
class DirectBankTransferController extends _controller.FlowController {
|
|
15
|
+
resendTimer = null;
|
|
16
|
+
constructor(client, options) {
|
|
17
|
+
const details = (0, _shared.resolveDetails)(options.details);
|
|
18
|
+
super({
|
|
19
|
+
details,
|
|
20
|
+
banks: [],
|
|
21
|
+
loadingBanks: true,
|
|
22
|
+
selectedBankCode: null,
|
|
23
|
+
account: '',
|
|
24
|
+
otp: '',
|
|
25
|
+
submitting: false,
|
|
26
|
+
error: null,
|
|
27
|
+
errors: {},
|
|
28
|
+
meta: {
|
|
29
|
+
touched: {},
|
|
30
|
+
submitted: false
|
|
31
|
+
},
|
|
32
|
+
otpChallenge: null,
|
|
33
|
+
resendSecondsLeft: OTP_RESEND_SECONDS,
|
|
34
|
+
usedUpFailure: null,
|
|
35
|
+
payLabel: (0, _shared.directPayLabel)(details)
|
|
36
|
+
}, options.onFinish);
|
|
37
|
+
this.client = client;
|
|
38
|
+
}
|
|
39
|
+
static validate(s) {
|
|
40
|
+
const errors = {};
|
|
41
|
+
if (s.otpChallenge) {
|
|
42
|
+
if (s.otp.trim().length !== 6) errors.otp = 'Enter the 6-digit OTP.';
|
|
43
|
+
return errors;
|
|
44
|
+
}
|
|
45
|
+
if (s.selectedBankCode == null) errors.bank = 'Choose your bank';
|
|
46
|
+
if (s.account.trim() === '') errors.account = 'Enter your account number.';
|
|
47
|
+
return errors;
|
|
48
|
+
}
|
|
49
|
+
derive(state) {
|
|
50
|
+
return {
|
|
51
|
+
...state,
|
|
52
|
+
errors: (0, _shared.visibleErrors)(DirectBankTransferController.validate(state), state.meta)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
async start() {
|
|
56
|
+
try {
|
|
57
|
+
const banks = await this.client.fetchDirectBankList();
|
|
58
|
+
this.setState({
|
|
59
|
+
banks,
|
|
60
|
+
loadingBanks: false
|
|
61
|
+
});
|
|
62
|
+
} catch (error) {
|
|
63
|
+
this.setState({
|
|
64
|
+
error: (0, _errors.swichpayUserError)(error),
|
|
65
|
+
loadingBanks: false
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
touch(field) {
|
|
70
|
+
const meta = this.getState().meta;
|
|
71
|
+
return {
|
|
72
|
+
...meta,
|
|
73
|
+
touched: {
|
|
74
|
+
...meta.touched,
|
|
75
|
+
[field]: true
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
selectBank(code) {
|
|
80
|
+
if (this.getState().submitting) return;
|
|
81
|
+
this.setState({
|
|
82
|
+
selectedBankCode: code,
|
|
83
|
+
meta: this.touch('bank')
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
setAccount(value) {
|
|
87
|
+
this.setState({
|
|
88
|
+
account: value,
|
|
89
|
+
meta: this.touch('account')
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
setOtp(value) {
|
|
93
|
+
this.setState({
|
|
94
|
+
otp: (0, _formatters.digitsOnly)(value, 6),
|
|
95
|
+
meta: this.touch('otp')
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
startResendCountdown() {
|
|
99
|
+
this.clearIntervalTimer(this.resendTimer);
|
|
100
|
+
this.setState({
|
|
101
|
+
resendSecondsLeft: OTP_RESEND_SECONDS
|
|
102
|
+
});
|
|
103
|
+
this.resendTimer = this.setIntervalTimer(() => {
|
|
104
|
+
const left = this.getState().resendSecondsLeft;
|
|
105
|
+
if (left <= 1) {
|
|
106
|
+
this.clearIntervalTimer(this.resendTimer);
|
|
107
|
+
this.resendTimer = null;
|
|
108
|
+
this.setState({
|
|
109
|
+
resendSecondsLeft: 0
|
|
110
|
+
});
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
this.setState({
|
|
114
|
+
resendSecondsLeft: left - 1
|
|
115
|
+
});
|
|
116
|
+
}, 1000);
|
|
117
|
+
}
|
|
118
|
+
get canResendOtp() {
|
|
119
|
+
const s = this.getState();
|
|
120
|
+
return s.otpChallenge != null && s.resendSecondsLeft === 0 && !s.submitting;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Step 1 (MAKE PAYMENT), also used by Resend OTP with the already-validated input. */
|
|
124
|
+
async requestOtp() {
|
|
125
|
+
const s = this.getState();
|
|
126
|
+
if (s.submitting) return;
|
|
127
|
+
const onForm = s.otpChallenge == null;
|
|
128
|
+
if (onForm) {
|
|
129
|
+
if (s.usedUpFailure) return;
|
|
130
|
+
this.setState({
|
|
131
|
+
meta: {
|
|
132
|
+
...s.meta,
|
|
133
|
+
submitted: true
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
if ((0, _shared.hasErrors)(DirectBankTransferController.validate(s))) return;
|
|
137
|
+
}
|
|
138
|
+
const bank = s.banks.find(b => b.code === s.selectedBankCode);
|
|
139
|
+
if (!bank) return;
|
|
140
|
+
this.setState({
|
|
141
|
+
submitting: true,
|
|
142
|
+
error: null
|
|
143
|
+
});
|
|
144
|
+
try {
|
|
145
|
+
const result = await this.client.directBankTransfer({
|
|
146
|
+
details: s.details,
|
|
147
|
+
bankCode: bank.code,
|
|
148
|
+
accountNo: s.account.trim()
|
|
149
|
+
});
|
|
150
|
+
if (this.disposed) return;
|
|
151
|
+
if (result.otpRequired) {
|
|
152
|
+
this.setState({
|
|
153
|
+
otpChallenge: result,
|
|
154
|
+
meta: {
|
|
155
|
+
touched: {},
|
|
156
|
+
submitted: false
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
this.startResendCountdown();
|
|
160
|
+
} else if (result.isSuccess) {
|
|
161
|
+
this.finish(result);
|
|
162
|
+
} else {
|
|
163
|
+
this.setState({
|
|
164
|
+
error: result.message ?? 'Bank transfer failed.',
|
|
165
|
+
usedUpFailure: this.getState().otpChallenge == null ? result : this.getState().usedUpFailure
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (this.disposed) return;
|
|
170
|
+
const message = (0, _errors.swichpayUserError)(error);
|
|
171
|
+
const used = this.getState().otpChallenge == null && (0, _errors.attemptUsedTransactionId)(error);
|
|
172
|
+
this.setState({
|
|
173
|
+
error: message,
|
|
174
|
+
usedUpFailure: used ? (0, _payment.failedResult)(message) : this.getState().usedUpFailure
|
|
175
|
+
});
|
|
176
|
+
} finally {
|
|
177
|
+
this.setState({
|
|
178
|
+
submitting: false
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
resendOtp() {
|
|
183
|
+
if (!this.canResendOtp) return Promise.resolve();
|
|
184
|
+
return this.requestOtp();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Step 2 (CONFIRM). A wrong OTP can be retried. */
|
|
188
|
+
async confirmOtp() {
|
|
189
|
+
const s = this.getState();
|
|
190
|
+
const challenge = s.otpChallenge;
|
|
191
|
+
if (!challenge || s.submitting) return;
|
|
192
|
+
this.setState({
|
|
193
|
+
meta: {
|
|
194
|
+
...s.meta,
|
|
195
|
+
submitted: true
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
if ((0, _shared.hasErrors)(DirectBankTransferController.validate(s))) return;
|
|
199
|
+
this.setState({
|
|
200
|
+
submitting: true,
|
|
201
|
+
error: null
|
|
202
|
+
});
|
|
203
|
+
try {
|
|
204
|
+
const result = await this.client.confirmDirectBankTransfer({
|
|
205
|
+
customerTransactionId: s.details.customerTransactionId,
|
|
206
|
+
billReferenceNo: s.details.billReferenceNo,
|
|
207
|
+
transactionId: challenge.bankTransactionId ?? '',
|
|
208
|
+
orderId: challenge.orderId ?? '',
|
|
209
|
+
transactionReferenceNo: challenge.transactionReferenceNo ?? '',
|
|
210
|
+
retreivalReferenceNo: challenge.retreivalReferenceNo ?? '',
|
|
211
|
+
otp: s.otp.trim()
|
|
212
|
+
});
|
|
213
|
+
if (this.disposed) return;
|
|
214
|
+
if (result.isSuccess) this.finish(result);else this.setState({
|
|
215
|
+
error: result.message ?? 'Invalid OTP.'
|
|
216
|
+
});
|
|
217
|
+
} catch (error) {
|
|
218
|
+
if (this.disposed) return;
|
|
219
|
+
this.setState({
|
|
220
|
+
error: (0, _errors.swichpayUserError)(error)
|
|
221
|
+
});
|
|
222
|
+
} finally {
|
|
223
|
+
this.setState({
|
|
224
|
+
submitting: false
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
closeWithFailure() {
|
|
229
|
+
const failure = this.getState().usedUpFailure;
|
|
230
|
+
if (failure) this.finish(failure);
|
|
231
|
+
}
|
|
232
|
+
cancel() {
|
|
233
|
+
this.finish((0, _payment.cancelledResult)());
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
exports.DirectBankTransferController = DirectBankTransferController;
|