@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,104 @@
1
+ "use strict";
2
+
3
+ import { swichpayUserError } from '../errors.js';
4
+ import { cancelledResult } from '../models/payment.js';
5
+ import { FlowController } from './controller.js';
6
+ import { resolveServices } from './services.js';
7
+ import { resolveDetails } from './shared.js';
8
+ /** Direct RAAST QR checkout - generates on start, verifies on a manual tap. */
9
+ export class DirectQrController extends FlowController {
10
+ constructor(client, options) {
11
+ super({
12
+ details: resolveDetails(options.details),
13
+ generating: true,
14
+ checkingStatus: false,
15
+ error: null,
16
+ statusMessage: null,
17
+ qrPayload: null
18
+ }, options.onFinish);
19
+ this.client = client;
20
+ this.services = resolveServices(options.services);
21
+ }
22
+
23
+ /** Generates the QR - call once after creating the controller. */
24
+ async start() {
25
+ const s = this.getState();
26
+ this.setState({
27
+ generating: true,
28
+ error: null
29
+ });
30
+ try {
31
+ const result = await this.client.generateDirectQrPayment({
32
+ details: s.details
33
+ });
34
+ if (this.disposed) return;
35
+ if (result.isSuccess && result.qrString) this.setState({
36
+ qrPayload: result.qrString
37
+ });else this.setState({
38
+ error: result.message ?? 'Failed to generate QR.'
39
+ });
40
+ } catch (error) {
41
+ if (this.disposed) return;
42
+ this.setState({
43
+ error: swichpayUserError(error)
44
+ });
45
+ } finally {
46
+ this.setState({
47
+ generating: false
48
+ });
49
+ }
50
+ }
51
+ async checkStatus() {
52
+ const s = this.getState();
53
+ if (s.checkingStatus || !s.qrPayload) return;
54
+ this.setState({
55
+ checkingStatus: true,
56
+ statusMessage: null
57
+ });
58
+ try {
59
+ const result = await this.client.checkDirectQrStatus({
60
+ customerTransactionId: s.details.customerTransactionId
61
+ });
62
+ if (this.disposed) return;
63
+ if (result.isSuccess) this.finish(result);else this.setState({
64
+ statusMessage: result.message ?? 'Payment status: pending'
65
+ });
66
+ } catch (error) {
67
+ if (this.disposed) return;
68
+ this.setState({
69
+ error: swichpayUserError(error)
70
+ });
71
+ } finally {
72
+ this.setState({
73
+ checkingStatus: false
74
+ });
75
+ }
76
+ }
77
+ async download() {
78
+ const payload = this.getState().qrPayload;
79
+ if (!payload) return;
80
+ try {
81
+ await this.services.downloadQr(payload);
82
+ } catch (error) {
83
+ this.setState({
84
+ error: swichpayUserError(error, 'Could not save QR code. Please try again.')
85
+ });
86
+ }
87
+ }
88
+ async share() {
89
+ const payload = this.getState().qrPayload;
90
+ if (!payload) return;
91
+ try {
92
+ await this.services.shareQr(payload);
93
+ } catch (error) {
94
+ // The user dismissing the share sheet isn't an error.
95
+ if (error instanceof Error && error.name === 'AbortError') return;
96
+ this.setState({
97
+ error: swichpayUserError(error, 'Could not share QR code. Please try again.')
98
+ });
99
+ }
100
+ }
101
+ cancel() {
102
+ this.finish(cancelledResult());
103
+ }
104
+ }
@@ -0,0 +1,297 @@
1
+ "use strict";
2
+
3
+ import { attemptUsedTransactionId, swichpayUserError } from '../errors.js';
4
+ import { cancelledResult, failedResult } from '../models/payment.js';
5
+ import { AccountValidators } from '../utils/validators.js';
6
+ import { FlowController } from './controller.js';
7
+ import { resolveServices } from './services.js';
8
+ import { directPayLabel, hasErrors, resolveDetails, visibleErrors } from './shared.js';
9
+ const IBAN_ERROR = 'Enter a valid IBAN (PK + 22 characters).';
10
+
11
+ // ── RTP Now ──────────────────────────────────────────────────────────────
12
+
13
+ /** Direct RTP Now checkout - Raast mobile, Raast IBAN, or bank + account. */
14
+ export class DirectRtpNowController extends FlowController {
15
+ constructor(client, options) {
16
+ const details = resolveDetails(options.details);
17
+ super({
18
+ details,
19
+ mode: 'raastMobile',
20
+ mobile: '',
21
+ iban: '',
22
+ bankAccount: '',
23
+ banks: [],
24
+ loadingBanks: true,
25
+ selectedBankCode: null,
26
+ submitting: false,
27
+ error: null,
28
+ errors: {},
29
+ meta: {
30
+ touched: {},
31
+ submitted: false
32
+ },
33
+ usedUpFailure: null,
34
+ payLabel: directPayLabel(details)
35
+ }, options.onFinish);
36
+ this.client = client;
37
+ this.services = resolveServices(options.services);
38
+ }
39
+
40
+ /** Only the active mode's field is validated. */
41
+ static validate(s) {
42
+ if (s.mode === 'raastMobile') return s.mobile.trim().length < 10 ? {
43
+ mobile: 'Enter a valid mobile number.'
44
+ } : {};
45
+ if (s.mode === 'raastIban') return AccountValidators.isValidPkIban(s.iban) ? {} : {
46
+ iban: IBAN_ERROR
47
+ };
48
+ return s.bankAccount.trim().length < 8 ? {
49
+ bankAccount: 'Enter a valid account number.'
50
+ } : {};
51
+ }
52
+ derive(state) {
53
+ return {
54
+ ...state,
55
+ errors: visibleErrors(DirectRtpNowController.validate(state), state.meta)
56
+ };
57
+ }
58
+
59
+ /** Loads the bank list; call once after creating the controller. */
60
+ async start() {
61
+ try {
62
+ const banks = await this.client.fetchDirectRtpNowBanks();
63
+ this.setState({
64
+ banks,
65
+ loadingBanks: false
66
+ });
67
+ } catch (error) {
68
+ this.setState({
69
+ error: swichpayUserError(error),
70
+ loadingBanks: false
71
+ });
72
+ }
73
+ }
74
+ setMode(mode) {
75
+ if (this.getState().submitting) return;
76
+ this.setState({
77
+ mode
78
+ });
79
+ }
80
+ setField(field, value) {
81
+ const s = this.getState();
82
+ this.setState({
83
+ [field]: value,
84
+ meta: {
85
+ ...s.meta,
86
+ touched: {
87
+ ...s.meta.touched,
88
+ [field]: true
89
+ }
90
+ }
91
+ });
92
+ }
93
+ selectBank(code) {
94
+ if (this.getState().submitting) return;
95
+ this.setState({
96
+ selectedBankCode: code
97
+ });
98
+ }
99
+ async submit() {
100
+ const s = this.getState();
101
+ if (s.submitting || s.usedUpFailure) return;
102
+ this.setState({
103
+ meta: {
104
+ ...s.meta,
105
+ submitted: true
106
+ }
107
+ });
108
+ if (hasErrors(DirectRtpNowController.validate(s))) return;
109
+ const bank = s.banks.find(b => b.code === s.selectedBankCode) ?? null;
110
+ if (s.mode === 'bank' && !bank) {
111
+ this.setState({
112
+ error: 'Choose your bank.'
113
+ });
114
+ return;
115
+ }
116
+ this.setState({
117
+ submitting: true,
118
+ error: null
119
+ });
120
+ const iban = AccountValidators.normalizeIban(s.iban);
121
+ try {
122
+ const result = await this.client.directRtpNowPayment({
123
+ details: s.details,
124
+ msisdn: s.details.msisdn,
125
+ rtpId: this.services.uuid(),
126
+ bankCode: s.mode === 'bank' ? bank.code : null,
127
+ // Sent on every submission, not only in bank mode.
128
+ accountTitle: s.details.consumerName,
129
+ accountNumber: s.mode === 'bank' ? s.bankAccount.trim() : s.mode === 'raastIban' ? iban : null,
130
+ accountNumberAlias: s.mode === 'raastMobile' ? s.mobile.trim() : s.mode === 'raastIban' ? iban : null
131
+ });
132
+ if (this.disposed) return;
133
+ if (result.isSuccess) this.finish(result);else this.setState({
134
+ error: result.message ?? 'Failed to process RTP payment.',
135
+ usedUpFailure: result
136
+ });
137
+ } catch (error) {
138
+ if (this.disposed) return;
139
+ const message = swichpayUserError(error);
140
+ this.setState({
141
+ error: message,
142
+ usedUpFailure: attemptUsedTransactionId(error) ? failedResult(message) : this.getState().usedUpFailure
143
+ });
144
+ } finally {
145
+ this.setState({
146
+ submitting: false
147
+ });
148
+ }
149
+ }
150
+ closeWithFailure() {
151
+ const failure = this.getState().usedUpFailure;
152
+ if (failure) this.finish(failure);
153
+ }
154
+ cancel() {
155
+ this.finish(cancelledResult());
156
+ }
157
+ }
158
+
159
+ // ── RTP E-wallet ─────────────────────────────────────────────────────────
160
+
161
+ /** Direct RTP E-wallet checkout - phone or IBAN plus a bank. */
162
+ export class DirectRtpEwalletController extends FlowController {
163
+ constructor(client, options) {
164
+ const details = resolveDetails(options.details);
165
+ super({
166
+ details,
167
+ mode: 'phone',
168
+ input: '',
169
+ banks: [],
170
+ loadingBanks: true,
171
+ selectedBankCode: null,
172
+ submitting: false,
173
+ error: null,
174
+ errors: {},
175
+ meta: {
176
+ touched: {},
177
+ submitted: false
178
+ },
179
+ usedUpFailure: null,
180
+ payLabel: directPayLabel(details)
181
+ }, options.onFinish);
182
+ this.client = client;
183
+ }
184
+ static validate(s) {
185
+ if (s.mode === 'iban') return AccountValidators.isValidPkIban(s.input) ? {} : {
186
+ input: IBAN_ERROR
187
+ };
188
+ return s.input.trim().length < 10 ? {
189
+ input: 'Enter a valid mobile number.'
190
+ } : {};
191
+ }
192
+ derive(state) {
193
+ return {
194
+ ...state,
195
+ errors: visibleErrors(DirectRtpEwalletController.validate(state), state.meta)
196
+ };
197
+ }
198
+ async start() {
199
+ try {
200
+ const banks = await this.client.fetchDirectRtpEwalletBanks();
201
+ this.setState({
202
+ banks,
203
+ loadingBanks: false
204
+ });
205
+ } catch (error) {
206
+ this.setState({
207
+ error: swichpayUserError(error),
208
+ loadingBanks: false
209
+ });
210
+ }
211
+ }
212
+
213
+ /** Switching mode clears the input. */
214
+ setMode(mode) {
215
+ const s = this.getState();
216
+ if (s.submitting) return;
217
+ this.setState({
218
+ mode,
219
+ input: '',
220
+ meta: {
221
+ ...s.meta,
222
+ touched: {}
223
+ }
224
+ });
225
+ }
226
+ setInput(value) {
227
+ const s = this.getState();
228
+ this.setState({
229
+ input: value,
230
+ meta: {
231
+ ...s.meta,
232
+ touched: {
233
+ input: true
234
+ }
235
+ }
236
+ });
237
+ }
238
+ selectBank(code) {
239
+ if (this.getState().submitting) return;
240
+ this.setState({
241
+ selectedBankCode: code
242
+ });
243
+ }
244
+ async submit() {
245
+ const s = this.getState();
246
+ if (s.submitting || s.usedUpFailure) return;
247
+ this.setState({
248
+ meta: {
249
+ ...s.meta,
250
+ submitted: true
251
+ }
252
+ });
253
+ const bank = s.banks.find(b => b.code === s.selectedBankCode) ?? null;
254
+ if (hasErrors(DirectRtpEwalletController.validate(s)) || !bank) {
255
+ if (!bank) this.setState({
256
+ error: 'Choose your bank.'
257
+ });
258
+ return;
259
+ }
260
+ this.setState({
261
+ submitting: true,
262
+ error: null
263
+ });
264
+ const value = s.input.trim();
265
+ try {
266
+ const result = await this.client.directRtpEwalletPayment({
267
+ details: s.details,
268
+ bankCode: bank.code,
269
+ msisdn: s.mode === 'phone' ? value : '',
270
+ iban: s.mode === 'iban' ? AccountValidators.normalizeIban(value) : ''
271
+ });
272
+ if (this.disposed) return;
273
+ if (result.isSuccess) this.finish(result);else this.setState({
274
+ error: result.message ?? 'Failed to process RTP E-Wallet payment.',
275
+ usedUpFailure: result
276
+ });
277
+ } catch (error) {
278
+ if (this.disposed) return;
279
+ const message = swichpayUserError(error);
280
+ this.setState({
281
+ error: message,
282
+ usedUpFailure: attemptUsedTransactionId(error) ? failedResult(message) : this.getState().usedUpFailure
283
+ });
284
+ } finally {
285
+ this.setState({
286
+ submitting: false
287
+ });
288
+ }
289
+ }
290
+ closeWithFailure() {
291
+ const failure = this.getState().usedUpFailure;
292
+ if (failure) this.finish(failure);
293
+ }
294
+ cancel() {
295
+ this.finish(cancelledResult());
296
+ }
297
+ }
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+
3
+ import { isDiscountUsableFor } from '../models/discount.js';
4
+ export const DISCOUNT_DEBOUNCE_MS = 600;
5
+
6
+ /**
7
+ * Debounced Discount API lookup with staleness and duplicate guards.
8
+ * One instance per form; `onChange` should push the new state to the UI.
9
+ */
10
+ export class DiscountLookup {
11
+ loading = false;
12
+ result = null;
13
+ debounce = null;
14
+ /** Bumped on every new lookup and every reset; a response under an older id is stale. */
15
+ requestId = 0;
16
+ requestKey = null;
17
+ disposed = false;
18
+ constructor(onChange, log = () => {}, debounceMs = DISCOUNT_DEBOUNCE_MS) {
19
+ this.onChange = onChange;
20
+ this.log = log;
21
+ this.debounceMs = debounceMs;
22
+ }
23
+ get state() {
24
+ return {
25
+ loading: this.loading,
26
+ result: this.result
27
+ };
28
+ }
29
+
30
+ /** Clears any applied/loading discount and invalidates in-flight lookups. */
31
+ reset() {
32
+ if (this.debounce) clearTimeout(this.debounce);
33
+ this.debounce = null;
34
+ this.requestId++;
35
+ this.requestKey = null;
36
+ if (this.result == null && !this.loading) return;
37
+ this.result = null;
38
+ this.loading = false;
39
+ this.onChange();
40
+ }
41
+ scheduleFetch(input) {
42
+ if (this.disposed) return;
43
+ if (this.debounce) clearTimeout(this.debounce);
44
+ this.debounce = setTimeout(() => {
45
+ this.debounce = null;
46
+ void this.run(input);
47
+ }, this.debounceMs);
48
+ }
49
+ async run(input) {
50
+ if (this.disposed) return;
51
+ // A second call for the same in-flight key is a no-op.
52
+ if (this.loading && input.requestKey === this.requestKey) return;
53
+ this.requestKey = input.requestKey;
54
+ const id = ++this.requestId;
55
+ this.loading = true;
56
+ this.onChange();
57
+ let fetched = null;
58
+ try {
59
+ fetched = await input.fetch();
60
+ } catch (error) {
61
+ // Best-effort only - a failed lookup must never block checkout.
62
+ this.log(`${input.debugLabel} error: ${String(error)}`);
63
+ }
64
+ if (this.disposed || id !== this.requestId) return; // stale
65
+
66
+ this.loading = false;
67
+ this.result = fetched != null && isDiscountUsableFor(fetched, input.requestedAmount) ? fetched : null;
68
+ this.onChange();
69
+ }
70
+ dispose() {
71
+ this.disposed = true;
72
+ if (this.debounce) clearTimeout(this.debounce);
73
+ this.debounce = null;
74
+ }
75
+ }
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+
3
+ import { SwichPayValidationException } from '../errors.js';
4
+ import { createPaymentResult } from '../models/payment.js';
5
+ import { DirectCardController } from './direct-card.js';
6
+ import { WALLET_TYPES } from './direct-ewallet.js';
7
+
8
+ /**
9
+ * Shows the bank's 3-D Secure page and resolves with its completion data (any string) once the
10
+ * customer is done. The SDKs implement it with a WebView / iframe; it is the only UI a headless
11
+ * card payment ever needs, and only when the bank asks for an OTP.
12
+ */
13
+
14
+ /**
15
+ * Pays with a card from the merchant's own screen - the SDK's card checkout without its screen
16
+ * (same requests, device check, 3-D Secure and status polling as `presentCardCheckout`).
17
+ * Resolves with the final `PaymentResult` (success, decline, pending or error); rejects with a
18
+ * `SwichPayValidationException` before any network call when the card fields are invalid.
19
+ */
20
+ export function runHeadlessCardPayment(client, input, deps) {
21
+ return new Promise((resolve, reject) => {
22
+ let settled = false;
23
+ const settle = result => {
24
+ if (settled) return;
25
+ settled = true;
26
+ off();
27
+ controller.dispose();
28
+ resolve(result);
29
+ };
30
+ const controller = new DirectCardController(client, {
31
+ details: input.details,
32
+ services: deps.services,
33
+ allowSaveCard: true,
34
+ onFinish: result => settle(result ?? createPaymentResult({
35
+ isSuccess: false,
36
+ status: 'cancelled',
37
+ message: 'Payment was cancelled.'
38
+ }))
39
+ });
40
+ let challengeShown = null;
41
+ const off = controller.subscribe(() => {
42
+ const s = controller.getState();
43
+ if (s.stepUp && s.stepUp !== challengeShown) {
44
+ challengeShown = s.stepUp;
45
+ deps.presentStepUp(s.stepUp, controller.isStepUpReturnUrl).then(data => void controller.completeStepUp(data), () => void controller.completeStepUp());
46
+ return;
47
+ }
48
+ if (s.busy || s.submitting || s.stepUp) return;
49
+ if (s.declinedResult) settle(s.declinedResult);else if (s.errorMessage != null) settle(createPaymentResult({
50
+ isSuccess: false,
51
+ status: 'error',
52
+ message: s.errorMessage
53
+ }));
54
+ });
55
+ const {
56
+ card
57
+ } = input;
58
+ controller.setNumber(card.number);
59
+ controller.setExpiry(`${String(card.expMonth).padStart(2, '0')}/${String(card.expYear % 100).padStart(2, '0')}`);
60
+ controller.setCvv(card.cvv);
61
+ controller.setSaveCard(input.saveCard ?? false);
62
+ const errors = DirectCardController.validate(controller.getState());
63
+ const messages = Object.values(errors).filter(m => typeof m === 'string');
64
+ if (messages.length > 0) {
65
+ settled = true;
66
+ off();
67
+ controller.dispose();
68
+ reject(new SwichPayValidationException(messages.join(' ')));
69
+ return;
70
+ }
71
+ void controller.submit();
72
+ });
73
+ }
74
+ /**
75
+ * Pays with JazzCash / EasyPaisa from the merchant's own screen. The customer approves the
76
+ * request in their wallet app; resolves when the wallet answers (same request as
77
+ * `presentEwalletCheckout`).
78
+ */
79
+ export function runHeadlessWalletPayment(client, input) {
80
+ const msisdn = input.msisdn.replace(/\D/g, '');
81
+ if (msisdn.length < 10) return Promise.reject(new SwichPayValidationException('Enter a valid account number.'));
82
+ const type = input.wallet === 'jazzcash' ? 'jazzCash' : input.wallet === 'easypaisa' ? 'easyPaisa' : null;
83
+ if (!type) return Promise.reject(new SwichPayValidationException('wallet must be "jazzcash" or "easypaisa".'));
84
+ const channelId = WALLET_TYPES.find(w => w.type === type).channelId;
85
+ return client.directEwalletPayment({
86
+ details: input.details,
87
+ channelId,
88
+ msisdn
89
+ });
90
+ }