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