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