@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.
Files changed (277) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/LICENSE +21 -0
  3. package/README.md +82 -2
  4. package/docs/checkouts.md +98 -0
  5. package/docs/getting-started.md +140 -0
  6. package/docs/going-live.md +45 -0
  7. package/docs/own-screen.md +81 -0
  8. package/docs/results-and-errors.md +75 -0
  9. package/docs/server.md +163 -0
  10. package/docs/troubleshooting.md +57 -0
  11. package/lib/commonjs/HostedCheckout.js +115 -0
  12. package/lib/commonjs/checkout-webview.js +85 -0
  13. package/lib/commonjs/checkouts/PaymentSheet.js +1317 -0
  14. package/lib/commonjs/checkouts/direct.js +671 -0
  15. package/lib/commonjs/context.js +64 -0
  16. package/lib/commonjs/core/api/direct-payment-api.js +425 -0
  17. package/lib/commonjs/core/api/discount-api.js +19 -0
  18. package/lib/commonjs/core/api/gateway-api.js +366 -0
  19. package/lib/commonjs/core/api/payments-api.js +79 -0
  20. package/lib/commonjs/core/api/repositories.js +5 -0
  21. package/lib/commonjs/core/assets/images.js +12 -0
  22. package/lib/commonjs/core/assets/method-logos.js +19 -0
  23. package/lib/commonjs/core/assets/pwa-images.js +13 -0
  24. package/lib/commonjs/core/browser/clipboard.js +30 -0
  25. package/lib/commonjs/core/browser/dom.js +71 -0
  26. package/lib/commonjs/core/browser/hosted-checkout.js +107 -0
  27. package/lib/commonjs/core/browser/index.js +119 -0
  28. package/lib/commonjs/core/browser/qr.js +109 -0
  29. package/lib/commonjs/core/browser/three-ds.js +151 -0
  30. package/lib/commonjs/core/client.js +344 -0
  31. package/lib/commonjs/core/config.js +132 -0
  32. package/lib/commonjs/core/environment.js +34 -0
  33. package/lib/commonjs/core/errors.js +138 -0
  34. package/lib/commonjs/core/events.js +57 -0
  35. package/lib/commonjs/core/flows/controller.js +106 -0
  36. package/lib/commonjs/core/flows/countdown.js +41 -0
  37. package/lib/commonjs/core/flows/direct-bank-transfer.js +236 -0
  38. package/lib/commonjs/core/flows/direct-card.js +354 -0
  39. package/lib/commonjs/core/flows/direct-ewallet.js +124 -0
  40. package/lib/commonjs/core/flows/direct-one-bill.js +172 -0
  41. package/lib/commonjs/core/flows/direct-qr.js +109 -0
  42. package/lib/commonjs/core/flows/direct-rtp.js +302 -0
  43. package/lib/commonjs/core/flows/discount-lookup.js +80 -0
  44. package/lib/commonjs/core/flows/headless.js +94 -0
  45. package/lib/commonjs/core/flows/payment-sheet.js +1321 -0
  46. package/lib/commonjs/core/flows/services.js +31 -0
  47. package/lib/commonjs/core/flows/shared.js +90 -0
  48. package/lib/commonjs/core/http/api-client.js +208 -0
  49. package/lib/commonjs/core/http/endpoints.js +61 -0
  50. package/lib/commonjs/core/http/token-manager.js +46 -0
  51. package/lib/commonjs/core/http/transport.js +134 -0
  52. package/lib/commonjs/core/index.js +688 -0
  53. package/lib/commonjs/core/models/category.js +113 -0
  54. package/lib/commonjs/core/models/discount.js +51 -0
  55. package/lib/commonjs/core/models/payment.js +95 -0
  56. package/lib/commonjs/core/models/session.js +131 -0
  57. package/lib/commonjs/core/server/index.js +64 -0
  58. package/lib/commonjs/core/server/node.js +60 -0
  59. package/lib/commonjs/core/server/proxy.js +194 -0
  60. package/lib/commonjs/core/server/verify.js +122 -0
  61. package/lib/commonjs/core/swichpay.js +72 -0
  62. package/lib/commonjs/core/utils/crypto.js +78 -0
  63. package/lib/commonjs/core/utils/formatters.js +68 -0
  64. package/lib/commonjs/core/utils/json.js +110 -0
  65. package/lib/commonjs/core/utils/log.js +75 -0
  66. package/lib/commonjs/core/utils/runtime.js +34 -0
  67. package/lib/commonjs/core/utils/validators.js +59 -0
  68. package/lib/commonjs/hooks.js +77 -0
  69. package/lib/commonjs/index.js +203 -0
  70. package/lib/commonjs/package.json +1 -0
  71. package/lib/commonjs/polyfills.js +3 -0
  72. package/lib/commonjs/presenter.js +61 -0
  73. package/lib/commonjs/provider.js +192 -0
  74. package/lib/commonjs/swichpay.js +192 -0
  75. package/lib/commonjs/theme.js +54 -0
  76. package/lib/commonjs/ui.js +1142 -0
  77. package/lib/commonjs/webviews.js +279 -0
  78. package/lib/module/HostedCheckout.js +111 -0
  79. package/lib/module/checkout-webview.js +83 -0
  80. package/lib/module/checkouts/PaymentSheet.js +1313 -0
  81. package/lib/module/checkouts/direct.js +661 -0
  82. package/lib/module/context.js +58 -0
  83. package/lib/module/core/api/direct-payment-api.js +413 -0
  84. package/lib/module/core/api/discount-api.js +14 -0
  85. package/lib/module/core/api/gateway-api.js +358 -0
  86. package/lib/module/core/api/payments-api.js +73 -0
  87. package/lib/module/core/api/repositories.js +3 -0
  88. package/lib/module/core/assets/images.js +8 -0
  89. package/lib/module/core/assets/method-logos.js +15 -0
  90. package/lib/module/core/assets/pwa-images.js +9 -0
  91. package/lib/module/core/browser/clipboard.js +27 -0
  92. package/lib/module/core/browser/dom.js +63 -0
  93. package/lib/module/core/browser/hosted-checkout.js +102 -0
  94. package/lib/module/core/browser/index.js +12 -0
  95. package/lib/module/core/browser/qr.js +100 -0
  96. package/lib/module/core/browser/three-ds.js +145 -0
  97. package/lib/module/core/client.js +338 -0
  98. package/lib/module/core/config.js +128 -0
  99. package/lib/module/core/environment.js +29 -0
  100. package/lib/module/core/errors.js +125 -0
  101. package/lib/module/core/events.js +51 -0
  102. package/lib/module/core/flows/controller.js +101 -0
  103. package/lib/module/core/flows/countdown.js +36 -0
  104. package/lib/module/core/flows/direct-bank-transfer.js +231 -0
  105. package/lib/module/core/flows/direct-card.js +349 -0
  106. package/lib/module/core/flows/direct-ewallet.js +119 -0
  107. package/lib/module/core/flows/direct-one-bill.js +167 -0
  108. package/lib/module/core/flows/direct-qr.js +104 -0
  109. package/lib/module/core/flows/direct-rtp.js +297 -0
  110. package/lib/module/core/flows/discount-lookup.js +75 -0
  111. package/lib/module/core/flows/headless.js +90 -0
  112. package/lib/module/core/flows/payment-sheet.js +1315 -0
  113. package/lib/module/core/flows/services.js +27 -0
  114. package/lib/module/core/flows/shared.js +78 -0
  115. package/lib/module/core/http/api-client.js +203 -0
  116. package/lib/module/core/http/endpoints.js +57 -0
  117. package/lib/module/core/http/token-manager.js +41 -0
  118. package/lib/module/core/http/transport.js +126 -0
  119. package/lib/module/core/index.js +52 -0
  120. package/lib/module/core/models/category.js +104 -0
  121. package/lib/module/core/models/discount.js +46 -0
  122. package/lib/module/core/models/payment.js +89 -0
  123. package/lib/module/core/models/session.js +120 -0
  124. package/lib/module/core/server/index.js +11 -0
  125. package/lib/module/core/server/node.js +56 -0
  126. package/lib/module/core/server/proxy.js +191 -0
  127. package/lib/module/core/server/verify.js +115 -0
  128. package/lib/module/core/swichpay.js +68 -0
  129. package/lib/module/core/utils/crypto.js +72 -0
  130. package/lib/module/core/utils/formatters.js +56 -0
  131. package/lib/module/core/utils/json.js +94 -0
  132. package/lib/module/core/utils/log.js +67 -0
  133. package/lib/module/core/utils/runtime.js +28 -0
  134. package/lib/module/core/utils/validators.js +54 -0
  135. package/lib/module/hooks.js +72 -0
  136. package/lib/module/index.js +36 -0
  137. package/lib/module/polyfills.js +4 -0
  138. package/lib/module/presenter.js +58 -0
  139. package/lib/module/provider.js +188 -0
  140. package/lib/module/swichpay.js +187 -0
  141. package/lib/module/theme.js +50 -0
  142. package/lib/module/ui.js +1116 -0
  143. package/lib/module/webviews.js +272 -0
  144. package/lib/typescript/commonjs/HostedCheckout.d.ts +17 -0
  145. package/lib/typescript/commonjs/checkout-webview.d.ts +14 -0
  146. package/lib/typescript/commonjs/checkouts/PaymentSheet.d.ts +14 -0
  147. package/lib/typescript/commonjs/checkouts/direct.d.ts +18 -0
  148. package/lib/typescript/commonjs/context.d.ts +20 -0
  149. package/lib/typescript/commonjs/core/api/direct-payment-api.d.ts +103 -0
  150. package/lib/typescript/commonjs/core/api/discount-api.d.ts +10 -0
  151. package/lib/typescript/commonjs/core/api/gateway-api.d.ts +150 -0
  152. package/lib/typescript/commonjs/core/api/payments-api.d.ts +22 -0
  153. package/lib/typescript/commonjs/core/api/repositories.d.ts +86 -0
  154. package/lib/typescript/commonjs/core/assets/images.d.ts +5 -0
  155. package/lib/typescript/commonjs/core/assets/method-logos.d.ts +12 -0
  156. package/lib/typescript/commonjs/core/assets/pwa-images.d.ts +7 -0
  157. package/lib/typescript/commonjs/core/browser/clipboard.d.ts +3 -0
  158. package/lib/typescript/commonjs/core/browser/dom.d.ts +11 -0
  159. package/lib/typescript/commonjs/core/browser/hosted-checkout.d.ts +27 -0
  160. package/lib/typescript/commonjs/core/browser/index.d.ts +11 -0
  161. package/lib/typescript/commonjs/core/browser/qr.d.ts +18 -0
  162. package/lib/typescript/commonjs/core/browser/three-ds.d.ts +45 -0
  163. package/lib/typescript/commonjs/core/client.d.ts +181 -0
  164. package/lib/typescript/commonjs/core/config.d.ts +86 -0
  165. package/lib/typescript/commonjs/core/environment.d.ts +22 -0
  166. package/lib/typescript/commonjs/core/errors.d.ts +78 -0
  167. package/lib/typescript/commonjs/core/events.d.ts +19 -0
  168. package/lib/typescript/commonjs/core/flows/controller.d.ts +41 -0
  169. package/lib/typescript/commonjs/core/flows/countdown.d.ts +15 -0
  170. package/lib/typescript/commonjs/core/flows/direct-bank-transfer.d.ts +49 -0
  171. package/lib/typescript/commonjs/core/flows/direct-card.d.ts +94 -0
  172. package/lib/typescript/commonjs/core/flows/direct-ewallet.d.ts +39 -0
  173. package/lib/typescript/commonjs/core/flows/direct-one-bill.d.ts +38 -0
  174. package/lib/typescript/commonjs/core/flows/direct-qr.d.ts +25 -0
  175. package/lib/typescript/commonjs/core/flows/direct-rtp.d.ts +72 -0
  176. package/lib/typescript/commonjs/core/flows/discount-lookup.d.ts +37 -0
  177. package/lib/typescript/commonjs/core/flows/headless.d.ts +38 -0
  178. package/lib/typescript/commonjs/core/flows/payment-sheet.d.ts +263 -0
  179. package/lib/typescript/commonjs/core/flows/services.d.ts +22 -0
  180. package/lib/typescript/commonjs/core/flows/shared.d.ts +40 -0
  181. package/lib/typescript/commonjs/core/http/api-client.d.ts +37 -0
  182. package/lib/typescript/commonjs/core/http/endpoints.d.ts +39 -0
  183. package/lib/typescript/commonjs/core/http/token-manager.d.ts +19 -0
  184. package/lib/typescript/commonjs/core/http/transport.d.ts +37 -0
  185. package/lib/typescript/commonjs/core/index.d.ts +42 -0
  186. package/lib/typescript/commonjs/core/models/category.d.ts +35 -0
  187. package/lib/typescript/commonjs/core/models/discount.d.ts +41 -0
  188. package/lib/typescript/commonjs/core/models/payment.d.ts +104 -0
  189. package/lib/typescript/commonjs/core/models/session.d.ts +87 -0
  190. package/lib/typescript/commonjs/core/server/index.d.ts +10 -0
  191. package/lib/typescript/commonjs/core/server/node.d.ts +19 -0
  192. package/lib/typescript/commonjs/core/server/proxy.d.ts +30 -0
  193. package/lib/typescript/commonjs/core/server/verify.d.ts +61 -0
  194. package/lib/typescript/commonjs/core/swichpay.d.ts +40 -0
  195. package/lib/typescript/commonjs/core/utils/crypto.d.ts +19 -0
  196. package/lib/typescript/commonjs/core/utils/formatters.d.ts +19 -0
  197. package/lib/typescript/commonjs/core/utils/json.d.ts +27 -0
  198. package/lib/typescript/commonjs/core/utils/log.d.ts +15 -0
  199. package/lib/typescript/commonjs/core/utils/runtime.d.ts +12 -0
  200. package/lib/typescript/commonjs/core/utils/validators.d.ts +23 -0
  201. package/lib/typescript/commonjs/hooks.d.ts +21 -0
  202. package/lib/typescript/commonjs/index.d.ts +26 -0
  203. package/lib/typescript/commonjs/package.json +1 -0
  204. package/lib/typescript/commonjs/polyfills.d.ts +2 -0
  205. package/lib/typescript/commonjs/presenter.d.ts +25 -0
  206. package/lib/typescript/commonjs/provider.d.ts +22 -0
  207. package/lib/typescript/commonjs/swichpay.d.ts +125 -0
  208. package/lib/typescript/commonjs/theme.d.ts +52 -0
  209. package/lib/typescript/commonjs/ui.d.ts +583 -0
  210. package/lib/typescript/commonjs/webviews.d.ts +40 -0
  211. package/lib/typescript/module/HostedCheckout.d.ts +17 -0
  212. package/lib/typescript/module/checkout-webview.d.ts +14 -0
  213. package/lib/typescript/module/checkouts/PaymentSheet.d.ts +14 -0
  214. package/lib/typescript/module/checkouts/direct.d.ts +18 -0
  215. package/lib/typescript/module/context.d.ts +20 -0
  216. package/lib/typescript/module/core/api/direct-payment-api.d.ts +103 -0
  217. package/lib/typescript/module/core/api/discount-api.d.ts +10 -0
  218. package/lib/typescript/module/core/api/gateway-api.d.ts +150 -0
  219. package/lib/typescript/module/core/api/payments-api.d.ts +22 -0
  220. package/lib/typescript/module/core/api/repositories.d.ts +86 -0
  221. package/lib/typescript/module/core/assets/images.d.ts +5 -0
  222. package/lib/typescript/module/core/assets/method-logos.d.ts +12 -0
  223. package/lib/typescript/module/core/assets/pwa-images.d.ts +7 -0
  224. package/lib/typescript/module/core/browser/clipboard.d.ts +3 -0
  225. package/lib/typescript/module/core/browser/dom.d.ts +11 -0
  226. package/lib/typescript/module/core/browser/hosted-checkout.d.ts +27 -0
  227. package/lib/typescript/module/core/browser/index.d.ts +11 -0
  228. package/lib/typescript/module/core/browser/qr.d.ts +18 -0
  229. package/lib/typescript/module/core/browser/three-ds.d.ts +45 -0
  230. package/lib/typescript/module/core/client.d.ts +181 -0
  231. package/lib/typescript/module/core/config.d.ts +86 -0
  232. package/lib/typescript/module/core/environment.d.ts +22 -0
  233. package/lib/typescript/module/core/errors.d.ts +78 -0
  234. package/lib/typescript/module/core/events.d.ts +19 -0
  235. package/lib/typescript/module/core/flows/controller.d.ts +41 -0
  236. package/lib/typescript/module/core/flows/countdown.d.ts +15 -0
  237. package/lib/typescript/module/core/flows/direct-bank-transfer.d.ts +49 -0
  238. package/lib/typescript/module/core/flows/direct-card.d.ts +94 -0
  239. package/lib/typescript/module/core/flows/direct-ewallet.d.ts +39 -0
  240. package/lib/typescript/module/core/flows/direct-one-bill.d.ts +38 -0
  241. package/lib/typescript/module/core/flows/direct-qr.d.ts +25 -0
  242. package/lib/typescript/module/core/flows/direct-rtp.d.ts +72 -0
  243. package/lib/typescript/module/core/flows/discount-lookup.d.ts +37 -0
  244. package/lib/typescript/module/core/flows/headless.d.ts +38 -0
  245. package/lib/typescript/module/core/flows/payment-sheet.d.ts +263 -0
  246. package/lib/typescript/module/core/flows/services.d.ts +22 -0
  247. package/lib/typescript/module/core/flows/shared.d.ts +40 -0
  248. package/lib/typescript/module/core/http/api-client.d.ts +37 -0
  249. package/lib/typescript/module/core/http/endpoints.d.ts +39 -0
  250. package/lib/typescript/module/core/http/token-manager.d.ts +19 -0
  251. package/lib/typescript/module/core/http/transport.d.ts +37 -0
  252. package/lib/typescript/module/core/index.d.ts +42 -0
  253. package/lib/typescript/module/core/models/category.d.ts +35 -0
  254. package/lib/typescript/module/core/models/discount.d.ts +41 -0
  255. package/lib/typescript/module/core/models/payment.d.ts +104 -0
  256. package/lib/typescript/module/core/models/session.d.ts +87 -0
  257. package/lib/typescript/module/core/server/index.d.ts +10 -0
  258. package/lib/typescript/module/core/server/node.d.ts +19 -0
  259. package/lib/typescript/module/core/server/proxy.d.ts +30 -0
  260. package/lib/typescript/module/core/server/verify.d.ts +61 -0
  261. package/lib/typescript/module/core/swichpay.d.ts +40 -0
  262. package/lib/typescript/module/core/utils/crypto.d.ts +19 -0
  263. package/lib/typescript/module/core/utils/formatters.d.ts +19 -0
  264. package/lib/typescript/module/core/utils/json.d.ts +27 -0
  265. package/lib/typescript/module/core/utils/log.d.ts +15 -0
  266. package/lib/typescript/module/core/utils/runtime.d.ts +12 -0
  267. package/lib/typescript/module/core/utils/validators.d.ts +23 -0
  268. package/lib/typescript/module/hooks.d.ts +21 -0
  269. package/lib/typescript/module/index.d.ts +26 -0
  270. package/lib/typescript/module/polyfills.d.ts +2 -0
  271. package/lib/typescript/module/presenter.d.ts +25 -0
  272. package/lib/typescript/module/provider.d.ts +22 -0
  273. package/lib/typescript/module/swichpay.d.ts +125 -0
  274. package/lib/typescript/module/theme.d.ts +52 -0
  275. package/lib/typescript/module/ui.d.ts +583 -0
  276. package/lib/typescript/module/webviews.d.ts +40 -0
  277. package/package.json +129 -4
@@ -0,0 +1,354 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.DirectCardController = exports.DIRECT_CARD_POLL_INTERVAL_MS = exports.DIRECT_CARD_MAX_POLLS = exports.DDC_TIMEOUT_MS = void 0;
7
+ var _errors = require("../errors.js");
8
+ var _payment = require("../models/payment.js");
9
+ var _crypto = require("../utils/crypto.js");
10
+ var _formatters = require("../utils/formatters.js");
11
+ var _validators = require("../utils/validators.js");
12
+ var _controller = require("./controller.js");
13
+ var _services = require("./services.js");
14
+ var _shared = require("./shared.js");
15
+ const DDC_TIMEOUT_MS = exports.DDC_TIMEOUT_MS = 15_000;
16
+ const DIRECT_CARD_MAX_POLLS = exports.DIRECT_CARD_MAX_POLLS = 12;
17
+ const DIRECT_CARD_POLL_INTERVAL_MS = exports.DIRECT_CARD_POLL_INTERVAL_MS = 5_000;
18
+ /**
19
+ * Direct (session-less) card checkout - mirrors the native
20
+ * `CardBottomSheet`: submit, invisible device-data-collection, optional OTP step-up, then
21
+ * status polling.
22
+ */
23
+ class DirectCardController extends _controller.FlowController {
24
+ /** The `tracker` for status polling. */
25
+ vendorTransactionId = null;
26
+ constructor(client, options) {
27
+ const details = (0, _shared.resolveDetails)(options.details);
28
+ super({
29
+ details,
30
+ allowSaveCard: options.allowSaveCard ?? true,
31
+ number: '',
32
+ expiry: '',
33
+ cvv: '',
34
+ saveCard: false,
35
+ brand: 'unknown',
36
+ submitting: false,
37
+ verifyingCard: false,
38
+ stepUp: null,
39
+ checkingStatus: false,
40
+ statusMessage: null,
41
+ declinedResult: null,
42
+ errorMessage: null,
43
+ errors: {},
44
+ meta: {
45
+ touched: {},
46
+ submitted: false
47
+ },
48
+ busy: false,
49
+ payLabel: (0, _shared.directPayLabel)(details)
50
+ }, options.onFinish);
51
+ this.client = client;
52
+ this.services = (0, _services.resolveServices)(options.services);
53
+ this.timing = {
54
+ ddcTimeoutMs: options.timing?.ddcTimeoutMs ?? DDC_TIMEOUT_MS,
55
+ maxPolls: options.timing?.maxPolls ?? DIRECT_CARD_MAX_POLLS,
56
+ pollIntervalMs: options.timing?.pollIntervalMs ?? DIRECT_CARD_POLL_INTERVAL_MS
57
+ };
58
+ }
59
+ static validate(s) {
60
+ const errors = {};
61
+ if (!_validators.CardValidators.isValidNumber(s.number)) errors.number = 'Enter a valid card number';
62
+ const expiry = (0, _validators.parseExpiry)(s.expiry);
63
+ if (!expiry) errors.expiry = 'MM/YY';else if (!_validators.CardValidators.isValidExpiry(expiry.month, expiry.year)) errors.expiry = 'Invalid expiry';
64
+ if (!_validators.CardValidators.isValidCvv(s.cvv)) errors.cvv = 'Invalid CVC';
65
+ return errors;
66
+ }
67
+ derive(state) {
68
+ return {
69
+ ...state,
70
+ brand: _validators.CardValidators.detectBrand(state.number),
71
+ busy: state.submitting || state.verifyingCard || state.stepUp != null || state.checkingStatus,
72
+ errors: (0, _shared.visibleErrors)(DirectCardController.validate(state), state.meta)
73
+ };
74
+ }
75
+ touch(field) {
76
+ const meta = this.getState().meta;
77
+ return {
78
+ ...meta,
79
+ touched: {
80
+ ...meta.touched,
81
+ [field]: true
82
+ }
83
+ };
84
+ }
85
+ setNumber(value) {
86
+ if (this.getState().busy) return;
87
+ this.setState({
88
+ number: (0, _formatters.formatCardNumber)(value),
89
+ meta: this.touch('number')
90
+ });
91
+ }
92
+ setExpiry(value) {
93
+ if (this.getState().busy) return;
94
+ this.setState({
95
+ expiry: (0, _formatters.formatExpiry)(value),
96
+ meta: this.touch('expiry')
97
+ });
98
+ }
99
+ setCvv(value) {
100
+ if (this.getState().busy) return;
101
+ this.setState({
102
+ cvv: (0, _formatters.digitsOnly)(value, 4),
103
+ meta: this.touch('cvv')
104
+ });
105
+ }
106
+ setSaveCard(saveCard) {
107
+ if (this.getState().busy) return;
108
+ this.setState({
109
+ saveCard
110
+ });
111
+ }
112
+
113
+ /** The challenge's return URLs (only readable when same-origin). */
114
+ isStepUpReturnUrl = url => (0, _shared.isThreeDsReturnUrl)(url, this.getState().details);
115
+ async submit() {
116
+ const s = this.getState();
117
+ if (s.busy) return;
118
+ this.setState({
119
+ meta: {
120
+ ...s.meta,
121
+ submitted: true
122
+ }
123
+ });
124
+ if ((0, _shared.hasErrors)(DirectCardController.validate(s))) return;
125
+ this.setState({
126
+ submitting: true,
127
+ errorMessage: null
128
+ });
129
+ try {
130
+ await this.attempt();
131
+ } catch (error) {
132
+ if (this.disposed) return;
133
+ if (error instanceof _errors.SwichPayApiException && error.isPending) {
134
+ // A vendor-side pending arrives as HTTP 400 - not a decline; a retry could charge twice.
135
+ this.vendorTransactionId = error.orderId ?? s.details.customerTransactionId;
136
+ await this.complete((0, _payment.createPaymentResult)({
137
+ isSuccess: false,
138
+ status: 'pending',
139
+ code: error.code,
140
+ message: error.serverMessage,
141
+ orderId: error.orderId
142
+ }));
143
+ } else if (error instanceof _errors.SwichPayApiException && error.serverMessage != null && error.statusCode >= 400 && error.statusCode < 500) {
144
+ // Card declines arrive as HTTP 4xx with the gateway's own message.
145
+ this.setState({
146
+ declinedResult: (0, _payment.createPaymentResult)({
147
+ isSuccess: false,
148
+ status: 'failed',
149
+ code: error.code,
150
+ message: error.serverMessage
151
+ })
152
+ });
153
+ } else {
154
+ this.setState({
155
+ errorMessage: (0, _errors.swichpayUserError)(error)
156
+ });
157
+ }
158
+ } finally {
159
+ this.setState({
160
+ submitting: false
161
+ });
162
+ }
163
+ }
164
+ async attempt() {
165
+ const s = this.getState();
166
+ const expiry = (0, _validators.parseExpiry)(s.expiry);
167
+ const card = {
168
+ number: s.number.replace(/ /g, ''),
169
+ expMonth: expiry.month,
170
+ expYear: expiry.year,
171
+ cvv: s.cvv
172
+ };
173
+ const result = await this.client.directCardPayment({
174
+ details: s.details,
175
+ card,
176
+ doCardOnFile: s.saveCard
177
+ });
178
+ if (this.disposed) return;
179
+ this.vendorTransactionId = result.vendorTransactionId;
180
+ const ddcUrl = result.ddcCollectionUrl;
181
+ if (!ddcUrl) {
182
+ // Some acquirers (e.g. Meezan 3DS2) skip DDC and return the OTP page straight away.
183
+ if (result.stepUpUrl) {
184
+ this.vendorTransactionId ??= s.details.customerTransactionId;
185
+ this.setState({
186
+ stepUp: {
187
+ url: result.stepUpUrl,
188
+ jwt: result.redirectFormFields?.JWT ?? null
189
+ }
190
+ });
191
+ return;
192
+ }
193
+ await this.complete(result);
194
+ return;
195
+ }
196
+ this.setState({
197
+ verifyingCard: true
198
+ });
199
+ let completed;
200
+ try {
201
+ completed = await this.services.runDeviceDataCollection({
202
+ url: ddcUrl,
203
+ formFields: result.redirectFormFields ?? {},
204
+ timeoutMs: this.timing.ddcTimeoutMs
205
+ });
206
+ } finally {
207
+ this.setState({
208
+ verifyingCard: false
209
+ });
210
+ }
211
+ if (this.disposed) return;
212
+ if (!completed) {
213
+ this.setState({
214
+ errorMessage: 'Device verification did not complete in time.'
215
+ });
216
+ return;
217
+ }
218
+
219
+ // The completion message carries no session id; the JWT's `ReferenceId` claim is it.
220
+ const jwt = result.redirectFormFields?.JWT;
221
+ const sessionId = jwt == null ? null : (0, _crypto.decodeJwtClaim)(jwt, 'ReferenceId');
222
+ if (sessionId == null) {
223
+ this.setState({
224
+ errorMessage: 'Could not determine a session ID for verification.'
225
+ });
226
+ return;
227
+ }
228
+ const tracker = result.vendorTransactionId ?? s.details.customerTransactionId;
229
+ const tokenized = await this.client.executeDirectCardTokenizedPayment({
230
+ customerTransactionId: result.orderId ?? s.details.customerTransactionId,
231
+ billReferenceNo: result.orderId ?? s.details.billReferenceNo,
232
+ tracker,
233
+ sessionId
234
+ });
235
+ if (this.disposed) return;
236
+ this.vendorTransactionId = tracker;
237
+ const stepUpJwt = tokenized.redirectFormFields?.JWT;
238
+ if (tokenized.stepUpUrl && stepUpJwt != null) {
239
+ this.setState({
240
+ stepUp: {
241
+ url: tokenized.stepUpUrl,
242
+ jwt: stepUpJwt
243
+ }
244
+ });
245
+ return;
246
+ }
247
+ await this.complete(tokenized);
248
+ }
249
+
250
+ /**
251
+ * The challenge finished (its message, a return URL, or the manual "I've completed
252
+ * verification" button) - poll for the final status.
253
+ */
254
+ async completeStepUp(_eventData = '{"type":"manual"}') {
255
+ if (!this.getState().stepUp) return;
256
+ const tracker = this.vendorTransactionId;
257
+ if (!tracker) {
258
+ this.setState({
259
+ stepUp: null,
260
+ errorMessage: 'Could not confirm payment status.'
261
+ });
262
+ return;
263
+ }
264
+ this.setState({
265
+ stepUp: null
266
+ });
267
+ await this.pollStatus(tracker);
268
+ }
269
+
270
+ /** Polls InquireV3 while `pending`, up to the attempt cap (native `executePost3DSVerification`). */
271
+ async pollStatus(tracker) {
272
+ this.setState({
273
+ checkingStatus: true,
274
+ statusMessage: 'Checking payment status...'
275
+ });
276
+ try {
277
+ let result = null;
278
+ for (let attempt = 1; attempt <= this.timing.maxPolls; attempt++) {
279
+ result = await this.client.checkDirectCardPaymentStatus({
280
+ tracker
281
+ });
282
+ if (this.disposed) return;
283
+ if (result.status.toLowerCase() !== 'pending' || attempt === this.timing.maxPolls) break;
284
+ this.setState({
285
+ statusMessage: result.message ?? 'Payment is pending...'
286
+ });
287
+ await this.services.sleep(this.timing.pollIntervalMs);
288
+ if (this.disposed) return;
289
+ }
290
+ this.setState({
291
+ checkingStatus: false
292
+ });
293
+ await this.complete(result, true);
294
+ } catch (error) {
295
+ if (this.disposed) return;
296
+ this.setState({
297
+ checkingStatus: false,
298
+ errorMessage: (0, _errors.swichpayUserError)(error)
299
+ });
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Success closes; a decline shows the declined screen; `pending` is verified by polling first
305
+ * and, if still pending afterwards, returned as-is rather than shown as declined.
306
+ */
307
+ async complete(result, verified = false) {
308
+ if (result.isSuccess) {
309
+ this.finish(result);
310
+ return;
311
+ }
312
+ if (result.status === 'pending') {
313
+ const tracker = this.vendorTransactionId;
314
+ if (!verified && tracker) {
315
+ await this.pollStatus(tracker);
316
+ return;
317
+ }
318
+ this.finish(result);
319
+ return;
320
+ }
321
+ this.setState({
322
+ declinedResult: result
323
+ });
324
+ }
325
+
326
+ /** The declined screen's Close - resolves the decline. */
327
+ closeDeclined() {
328
+ const declined = this.getState().declinedResult;
329
+ if (declined) this.finish(declined);
330
+ }
331
+
332
+ /** The error screen's Close. */
333
+ closeError() {
334
+ const message = this.getState().errorMessage;
335
+ if (message != null) this.finish((0, _payment.createPaymentResult)({
336
+ isSuccess: false,
337
+ status: 'error',
338
+ message
339
+ }));
340
+ }
341
+
342
+ /** Backdrop / Escape - ignored while a step is in flight, so a payment is never orphaned. */
343
+ dismiss() {
344
+ if (this.getState().busy) return;
345
+ super.dismiss();
346
+ }
347
+
348
+ /** Header close - ignored while a step is in flight */
349
+ cancel() {
350
+ if (this.getState().busy) return;
351
+ this.finish((0, _payment.cancelledResult)());
352
+ }
353
+ }
354
+ exports.DirectCardController = DirectCardController;
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.WALLET_TYPES = exports.DirectEwalletController = 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 WALLET_TYPES = exports.WALLET_TYPES = [{
13
+ type: 'easyPaisa',
14
+ label: 'EasyPaisa',
15
+ channelId: 8
16
+ }, {
17
+ type: 'jazzCash',
18
+ label: 'JazzCash',
19
+ channelId: 10
20
+ }];
21
+ /** Direct EasyPaisa/JazzCash checkout. */
22
+ class DirectEwalletController extends _controller.FlowController {
23
+ constructor(client, options) {
24
+ const details = (0, _shared.resolveDetails)(options.details);
25
+ super({
26
+ details,
27
+ walletType: 'easyPaisa',
28
+ account: '',
29
+ submitting: false,
30
+ error: null,
31
+ errors: {},
32
+ meta: {
33
+ touched: {},
34
+ submitted: false
35
+ },
36
+ usedUpFailure: null,
37
+ payLabel: (0, _shared.directPayLabel)(details)
38
+ }, options.onFinish);
39
+ this.client = client;
40
+ }
41
+ static validate(state) {
42
+ return state.account.trim().length < 10 ? {
43
+ account: 'Enter a valid account number.'
44
+ } : {};
45
+ }
46
+ derive(state) {
47
+ return {
48
+ ...state,
49
+ errors: (0, _shared.visibleErrors)(DirectEwalletController.validate(state), state.meta)
50
+ };
51
+ }
52
+ selectWallet(walletType) {
53
+ if (this.getState().submitting) return;
54
+ this.setState({
55
+ walletType
56
+ });
57
+ }
58
+ setAccount(value) {
59
+ const s = this.getState();
60
+ this.setState({
61
+ account: (0, _formatters.digitsOnly)(value),
62
+ meta: {
63
+ ...s.meta,
64
+ touched: {
65
+ ...s.meta.touched,
66
+ account: true
67
+ }
68
+ }
69
+ });
70
+ }
71
+ async submit() {
72
+ const s = this.getState();
73
+ if (s.submitting || s.usedUpFailure) return;
74
+ this.setState({
75
+ meta: {
76
+ ...s.meta,
77
+ submitted: true
78
+ }
79
+ });
80
+ if ((0, _shared.hasErrors)(DirectEwalletController.validate(s))) return;
81
+ const channelId = WALLET_TYPES.find(w => w.type === s.walletType).channelId;
82
+ this.setState({
83
+ submitting: true,
84
+ error: null
85
+ });
86
+ try {
87
+ const result = await this.client.directEwalletPayment({
88
+ details: s.details,
89
+ channelId,
90
+ msisdn: s.account.trim()
91
+ });
92
+ if (this.disposed) return;
93
+ if (result.isSuccess) {
94
+ this.finish(result);
95
+ } else {
96
+ this.setState({
97
+ error: result.message ?? 'Payment failed.',
98
+ usedUpFailure: result
99
+ });
100
+ }
101
+ } catch (error) {
102
+ if (this.disposed) return;
103
+ const message = (0, _errors.swichpayUserError)(error);
104
+ this.setState({
105
+ error: message,
106
+ usedUpFailure: (0, _errors.attemptUsedTransactionId)(error) ? (0, _payment.failedResult)(message) : this.getState().usedUpFailure
107
+ });
108
+ } finally {
109
+ this.setState({
110
+ submitting: false
111
+ });
112
+ }
113
+ }
114
+
115
+ /** CLOSE after a used-up attempt - resolves the failed result. */
116
+ closeWithFailure() {
117
+ const failure = this.getState().usedUpFailure;
118
+ if (failure) this.finish(failure);
119
+ }
120
+ cancel() {
121
+ this.finish((0, _payment.cancelledResult)());
122
+ }
123
+ }
124
+ exports.DirectEwalletController = DirectEwalletController;
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.DirectOneBillController = 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 _services = require("./services.js");
12
+ var _shared = require("./shared.js");
13
+ /** Direct 1Bill checkout - consumer number, copy, countdown, manual status check. */
14
+ class DirectOneBillController extends _controller.FlowController {
15
+ countdown = null;
16
+ constructor(client, options) {
17
+ const details = (0, _shared.resolveDetails)(options.details);
18
+ const seconds = details.expiryMinutes * 60;
19
+ super({
20
+ details,
21
+ mobile: '',
22
+ submitting: false,
23
+ checkingStatus: false,
24
+ error: null,
25
+ statusMessage: null,
26
+ errors: {},
27
+ meta: {
28
+ touched: {},
29
+ submitted: false
30
+ },
31
+ consumerNumber: null,
32
+ secondsLeft: seconds,
33
+ timeLeftLabel: (0, _formatters.formatCountdown)(seconds),
34
+ expired: false,
35
+ usedUpFailure: null
36
+ }, options.onFinish);
37
+ this.client = client;
38
+ this.services = (0, _services.resolveServices)(options.services);
39
+ }
40
+ static validate(s) {
41
+ return s.mobile.trim().length < 10 ? {
42
+ mobile: 'Enter a valid mobile number.'
43
+ } : {};
44
+ }
45
+ derive(state) {
46
+ return {
47
+ ...state,
48
+ timeLeftLabel: (0, _formatters.formatCountdown)(state.secondsLeft),
49
+ errors: (0, _shared.visibleErrors)(DirectOneBillController.validate(state), state.meta)
50
+ };
51
+ }
52
+ setMobile(value) {
53
+ const s = this.getState();
54
+ this.setState({
55
+ mobile: (0, _formatters.digitsOnly)(value),
56
+ meta: {
57
+ ...s.meta,
58
+ touched: {
59
+ mobile: true
60
+ }
61
+ }
62
+ });
63
+ }
64
+ startCountdown() {
65
+ this.countdown = this.setIntervalTimer(() => {
66
+ const left = this.getState().secondsLeft;
67
+ if (left <= 1) {
68
+ this.clearIntervalTimer(this.countdown);
69
+ this.countdown = null;
70
+ this.setState({
71
+ expired: true
72
+ });
73
+ return;
74
+ }
75
+ this.setState({
76
+ secondsLeft: left - 1
77
+ });
78
+ }, 1000);
79
+ }
80
+ async generate() {
81
+ const s = this.getState();
82
+ if (s.submitting || s.usedUpFailure) return;
83
+ this.setState({
84
+ meta: {
85
+ ...s.meta,
86
+ submitted: true
87
+ }
88
+ });
89
+ if ((0, _shared.hasErrors)(DirectOneBillController.validate(s))) return;
90
+ this.setState({
91
+ submitting: true,
92
+ error: null
93
+ });
94
+ try {
95
+ // The customer's own number replaces the order's msisdn for this method.
96
+ const result = await this.client.generateDirectOneBill({
97
+ details: {
98
+ ...s.details,
99
+ msisdn: s.mobile.trim()
100
+ }
101
+ });
102
+ if (this.disposed) return;
103
+ if (result.isSuccess && result.consumerNumber != null) {
104
+ this.setState({
105
+ consumerNumber: result.consumerNumber
106
+ });
107
+ this.startCountdown();
108
+ } else {
109
+ this.setState({
110
+ error: result.message ?? 'Failed to generate consumer number.',
111
+ usedUpFailure: result
112
+ });
113
+ }
114
+ } catch (error) {
115
+ if (this.disposed) return;
116
+ const message = (0, _errors.swichpayUserError)(error);
117
+ this.setState({
118
+ error: message,
119
+ usedUpFailure: (0, _errors.attemptUsedTransactionId)(error) ? (0, _payment.failedResult)(message) : this.getState().usedUpFailure
120
+ });
121
+ } finally {
122
+ this.setState({
123
+ submitting: false
124
+ });
125
+ }
126
+ }
127
+ async checkStatus() {
128
+ const s = this.getState();
129
+ if (s.checkingStatus || s.consumerNumber == null || s.expired) return;
130
+ this.setState({
131
+ checkingStatus: true,
132
+ statusMessage: null
133
+ });
134
+ try {
135
+ const result = await this.client.checkDirectOneBillStatus({
136
+ customerTransactionId: s.details.customerTransactionId
137
+ });
138
+ if (this.disposed) return;
139
+ if (result.isSuccess) this.finish(result);else this.setState({
140
+ statusMessage: result.message ?? 'Payment status: pending'
141
+ });
142
+ } catch (error) {
143
+ // A genuine exception is more severe than "still pending" - use the error banner.
144
+ if (this.disposed) return;
145
+ this.setState({
146
+ error: (0, _errors.swichpayUserError)(error)
147
+ });
148
+ } finally {
149
+ this.setState({
150
+ checkingStatus: false
151
+ });
152
+ }
153
+ }
154
+ async copyConsumerNumber() {
155
+ const number = this.getState().consumerNumber;
156
+ if (!number) return false;
157
+ try {
158
+ await this.services.copyText(number);
159
+ return true;
160
+ } catch {
161
+ return false;
162
+ }
163
+ }
164
+ closeWithFailure() {
165
+ const failure = this.getState().usedUpFailure;
166
+ if (failure) this.finish(failure);
167
+ }
168
+ cancel() {
169
+ this.finish((0, _payment.cancelledResult)());
170
+ }
171
+ }
172
+ exports.DirectOneBillController = DirectOneBillController;