@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,279 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.DeviceDataCollectionHost = DeviceDataCollectionHost;
7
+ exports.StepUpView = StepUpView;
8
+ exports.isPlainHttp = isPlainHttp;
9
+ exports.parseForwarded = parseForwarded;
10
+ exports.postFormPage = postFormPage;
11
+ exports.runDeviceDataCollectionNative = runDeviceDataCollectionNative;
12
+ var _react = require("react");
13
+ var _reactNative = require("react-native");
14
+ var _reactNativeWebview = require("react-native-webview");
15
+ var _threeDs = require("./core/browser/three-ds.js");
16
+ var _dom = require("./core/browser/dom.js");
17
+ var _hooks = require("./hooks.js");
18
+ var _ui = require("./ui.js");
19
+ var _theme = require("./theme.js");
20
+ var _jsxRuntime = require("react/jsx-runtime");
21
+ /** Hosts Cardinal Commerce serves its 3-D Secure pages from (same rule as the web SDK). */const CARDINAL_HOST = /(^|\.)(cardinalcommerce\.com|cardinaltrusted\.com)$/i;
22
+ /** SwichPay / JazzCash hosts - a TRANSACTION_COMPLETE from them ends the challenge. */
23
+ const SWICHPAY_HOST = /(^|\.)(swichnow\.com|jazzcash\.com\.pk)$/i;
24
+ function hostMatches(origin, pattern) {
25
+ const match = /^[a-z][a-z0-9+.-]*:\/\/([^/:?#]+)/i.exec(origin);
26
+ return !!match && pattern.test(match[1]);
27
+ }
28
+ function escapeHtml(value) {
29
+ return value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
30
+ }
31
+
32
+ /** `http://` - a page that would load without TLS. */
33
+ function isPlainHttp(url) {
34
+ return /^http:\/\//i.test(url);
35
+ }
36
+
37
+ /**
38
+ * A page that POSTs `fields` to `action` inside an iframe and forwards every `message` event to
39
+ * React Native as `{ origin, fromFrame, data }` - the same signals the web SDK reads from
40
+ * `window` (the 3-D Secure pages talk to `window.parent`, which is this wrapper).
41
+ */
42
+ function postFormPage(action, fields, visible) {
43
+ const inputs = Object.entries(fields).map(([name, value]) => `<input type="hidden" name="${escapeHtml(name)}" value="${escapeHtml(value)}">`).join('');
44
+ const frameStyle = visible ? 'position:fixed;inset:0;width:100%;height:100%;border:0;' : 'width:1px;height:1px;border:0;opacity:0;';
45
+ return `<!doctype html><html><head><meta name="viewport" content="width=device-width,initial-scale=1"></head>
46
+ <body style="margin:0;background:#fff">
47
+ <iframe name="sp-frame" id="sp-frame" style="${frameStyle}" allow="payment"></iframe>
48
+ <form id="sp-form" method="POST" action="${escapeHtml(action)}" target="sp-frame" enctype="application/x-www-form-urlencoded" style="display:none">${inputs}</form>
49
+ <script>
50
+ (function () {
51
+ var frame = document.getElementById('sp-frame');
52
+ window.addEventListener('message', function (e) {
53
+ var data = e.data;
54
+ try { JSON.stringify(data); } catch (err) { data = String(data); }
55
+ var payload = { origin: e.origin, fromFrame: !!frame && e.source === frame.contentWindow, data: data };
56
+ if (window.ReactNativeWebView) window.ReactNativeWebView.postMessage(JSON.stringify(payload));
57
+ });
58
+ document.getElementById('sp-form').submit();
59
+ })();
60
+ </script></body></html>`;
61
+ }
62
+ function parseForwarded(event) {
63
+ try {
64
+ const parsed = JSON.parse(event.nativeEvent.data);
65
+ if (typeof parsed !== 'object' || parsed === null) return null;
66
+ return {
67
+ origin: String(parsed.origin ?? ''),
68
+ fromFrame: parsed.fromFrame === true,
69
+ data: parsed.data
70
+ };
71
+ } catch {
72
+ return null;
73
+ }
74
+ }
75
+
76
+ /** `event.data` serialized as a string. */
77
+ function serialize(data) {
78
+ try {
79
+ return JSON.stringify(data) ?? 'null';
80
+ } catch {
81
+ return String(data);
82
+ }
83
+ }
84
+
85
+ // ── Device data collection (hidden) ───────────────────────────────────────────
86
+
87
+ let ddcQueue = [];
88
+ let ddcSeq = 0;
89
+ let ddcHosts = 0;
90
+ const ddcListeners = new Set();
91
+ const notifyDdc = () => ddcListeners.forEach(l => l());
92
+
93
+ /**
94
+ * `FlowServices.runDeviceDataCollection` for React Native: renders Cardinal's collection page in
95
+ * a hidden WebView (inside `<SwichPayProvider>`) and resolves `true` on `profile.complete`,
96
+ * `false` after `timeoutMs` (hidden WebView).
97
+ */
98
+ function runDeviceDataCollectionNative(input) {
99
+ return new Promise(resolve => {
100
+ if (ddcHosts === 0) {
101
+ // No host mounted: nothing can run the page, so verification can't complete.
102
+ resolve(false);
103
+ return;
104
+ }
105
+ const request = {
106
+ id: ++ddcSeq,
107
+ ...input,
108
+ resolve
109
+ };
110
+ ddcQueue = [...ddcQueue, request];
111
+ notifyDdc();
112
+ });
113
+ }
114
+ function finishDdc(id, completed) {
115
+ const request = ddcQueue.find(r => r.id === id);
116
+ if (!request) return;
117
+ ddcQueue = ddcQueue.filter(r => r.id !== id);
118
+ notifyDdc();
119
+ request.resolve(completed);
120
+ }
121
+ const subscribeDdc = listener => {
122
+ ddcListeners.add(listener);
123
+ return () => ddcListeners.delete(listener);
124
+ };
125
+ function DdcFrame({
126
+ request
127
+ }) {
128
+ const html = (0, _react.useMemo)(() => postFormPage(request.url, request.formFields, false), [request]);
129
+ (0, _react.useEffect)(() => {
130
+ const timer = setTimeout(() => finishDdc(request.id, false), request.timeoutMs);
131
+ return () => clearTimeout(timer);
132
+ }, [request]);
133
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeWebview.WebView, {
134
+ source: {
135
+ html
136
+ },
137
+ originWhitelist: ['*'],
138
+ javaScriptEnabled: true,
139
+ style: styles.hidden
140
+ // Card-scheme device checks are always TLS; never post device data over plain HTTP.
141
+ ,
142
+ onShouldStartLoadWithRequest: request => !isPlainHttp(request.url),
143
+ onMessage: event => {
144
+ const message = parseForwarded(event);
145
+ if (!message) return;
146
+ if (!message.fromFrame && !hostMatches(message.origin, CARDINAL_HOST)) return;
147
+ if ((0, _dom.messageDataAsObject)(message.data)?.MessageType === 'profile.complete') finishDdc(request.id, true);
148
+ }
149
+ });
150
+ }
151
+
152
+ /** Mounted once by `<SwichPayProvider>`; runs queued device-data collections off-screen. */
153
+ function DeviceDataCollectionHost() {
154
+ (0, _react.useEffect)(() => {
155
+ ddcHosts += 1;
156
+ return () => {
157
+ ddcHosts -= 1;
158
+ };
159
+ }, []);
160
+ const queue = (0, _react.useSyncExternalStore)(subscribeDdc, () => ddcQueue, () => ddcQueue);
161
+ if (queue.length === 0) return null;
162
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
163
+ style: styles.hiddenHost,
164
+ pointerEvents: "none",
165
+ accessibilityElementsHidden: true,
166
+ importantForAccessibility: "no-hide-descendants",
167
+ children: queue.map(request => /*#__PURE__*/(0, _jsxRuntime.jsx)(DdcFrame, {
168
+ request: request
169
+ }, request.id))
170
+ });
171
+ }
172
+
173
+ // ── Step-up challenge (visible) ───────────────────────────────────────────────
174
+
175
+ /** Same event data the web SDK sends for the manual fallback. */
176
+ const MANUAL_COMPLETION = JSON.stringify({
177
+ type: 'manual'
178
+ });
179
+
180
+ /**
181
+ * The issuer's 3-D Secure challenge. Completes on the challenge frame's first message, on a
182
+ * SwichPay `TRANSACTION_COMPLETE`, on a navigation to a completion URL, or on the manual
183
+ * "I've completed verification" button.
184
+ */
185
+ function StepUpView(props) {
186
+ const [loading, setLoading] = (0, _react.useState)(true);
187
+ const done = (0, _react.useRef)(false);
188
+ const onComplete = (0, _hooks.useLatest)(props.onComplete);
189
+ const html = (0, _react.useMemo)(() => {
190
+ const {
191
+ action,
192
+ fields
193
+ } = (0, _threeDs.stepUpRequest)(props.challenge.url, props.challenge.jwt);
194
+ return postFormPage(action, fields, true);
195
+ }, [props.challenge.url, props.challenge.jwt]);
196
+ const complete = data => {
197
+ if (done.current) return;
198
+ done.current = true;
199
+ onComplete.current(data);
200
+ };
201
+ return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
202
+ style: {
203
+ gap: 8
204
+ },
205
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
206
+ style: [styles.frame, {
207
+ height: props.height ?? 520
208
+ }],
209
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeWebview.WebView, {
210
+ source: {
211
+ html
212
+ },
213
+ originWhitelist: ['*'],
214
+ javaScriptEnabled: true,
215
+ domStorageEnabled: true,
216
+ onLoadEnd: () => setLoading(false),
217
+ onMessage: event => {
218
+ const message = parseForwarded(event);
219
+ if (!message) return;
220
+ const isSwichPayCompletion = hostMatches(message.origin, SWICHPAY_HOST) && (0, _dom.messageDataAsObject)(message.data)?.type === 'TRANSACTION_COMPLETE';
221
+ if (!message.fromFrame && !isSwichPayCompletion) return;
222
+ complete(serialize(message.data));
223
+ },
224
+ onShouldStartLoadWithRequest: request => {
225
+ try {
226
+ const url = new URL(request.url);
227
+ if (props.isCompletionUrl(url)) complete(JSON.stringify({
228
+ type: 'redirect',
229
+ url: url.href
230
+ }));
231
+ } catch {
232
+ // not a URL we can read (about:blank, data:)
233
+ }
234
+ // 3-D Secure pages are always TLS (EMV 3DS); a completion URL was caught above.
235
+ return !isPlainHttp(request.url);
236
+ }
237
+ }), loading ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
238
+ style: styles.loading,
239
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ui.Spinner, {
240
+ dark: true,
241
+ large: true,
242
+ label: "Loading verification"
243
+ })
244
+ }) : null]
245
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_ui.Button, {
246
+ variant: "ghost",
247
+ onPress: () => complete(MANUAL_COMPLETION),
248
+ children: "I've completed verification"
249
+ })]
250
+ });
251
+ }
252
+ const styles = _reactNative.StyleSheet.create({
253
+ hidden: {
254
+ width: 1,
255
+ height: 1,
256
+ opacity: 0
257
+ },
258
+ hiddenHost: {
259
+ position: 'absolute',
260
+ width: 1,
261
+ height: 1,
262
+ left: -10,
263
+ top: -10,
264
+ opacity: 0
265
+ },
266
+ frame: {
267
+ borderWidth: 1,
268
+ borderColor: _theme.colors.border,
269
+ borderRadius: 10,
270
+ overflow: 'hidden',
271
+ backgroundColor: '#fff'
272
+ },
273
+ loading: {
274
+ ..._theme.FILL,
275
+ alignItems: 'center',
276
+ justifyContent: 'center',
277
+ backgroundColor: 'rgba(255,255,255,0.8)'
278
+ }
279
+ });
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+
3
+ import { useEffect, useRef, useState } from 'react';
4
+ import { StyleSheet, View } from 'react-native';
5
+ import { hostedCheckoutResultForUrl } from './core/browser/hosted-checkout.js';
6
+ import { messageDataAsObject } from './core/browser/dom.js';
7
+ import { createPaymentResult, swichpayUserError } from './core/index.js';
8
+ import { CheckoutWebView } from './checkout-webview.js';
9
+ import { useDismissHandler, useSwichPayClient } from './context.js';
10
+ import { useLatest } from './hooks.js';
11
+ import { Button, ErrorBanner, SheetHeader, Spinner } from './ui.js';
12
+ import { space } from './theme.js';
13
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
14
+ /**
15
+ * SwichPay's hosted checkout page: resolves from the redirect
16
+ * to `successRedirectUrl` / `failedRedirectUrl` or from the page's
17
+ * `redirecttocustomer` / `closewindow` messages. Close resolves `null`.
18
+ */
19
+ export function HostedCheckout(props) {
20
+ const client = useSwichPayClient();
21
+ const [url, setUrl] = useState(props.url ?? null);
22
+ const [error, setError] = useState(null);
23
+ const onFinish = useLatest(props.onFinish);
24
+ const onError = useLatest(props.onError);
25
+ const detailsRef = useLatest(props.details);
26
+ const categoriesRef = useLatest(props.categories);
27
+ const settled = useRef(false);
28
+ const lastStatus = useRef(null);
29
+ const finish = result => {
30
+ if (settled.current) return;
31
+ settled.current = true;
32
+ onFinish.current(result);
33
+ };
34
+ useDismissHandler(() => finish(null));
35
+ useEffect(() => {
36
+ if (url) return;
37
+ let active = true;
38
+ const timer = setTimeout(() => {
39
+ client.createHostedCheckoutUrl({
40
+ details: detailsRef.current,
41
+ categories: categoriesRef.current
42
+ }).then(u => active && setUrl(u), e => {
43
+ if (!active) return;
44
+ if (onError.current) onError.current(e);else setError(swichpayUserError(e));
45
+ });
46
+ }, 0);
47
+ return () => {
48
+ active = false;
49
+ clearTimeout(timer);
50
+ };
51
+ }, [client, url, detailsRef, categoriesRef, onError]);
52
+ return /*#__PURE__*/_jsxs(View, {
53
+ style: styles.fill,
54
+ children: [/*#__PURE__*/_jsx(SheetHeader, {
55
+ title: "Payment Session",
56
+ onClose: () => finish(null)
57
+ }), error ? /*#__PURE__*/_jsxs(View, {
58
+ style: styles.center,
59
+ children: [/*#__PURE__*/_jsx(ErrorBanner, {
60
+ message: error
61
+ }), /*#__PURE__*/_jsx(Button, {
62
+ onPress: () => finish(null),
63
+ children: "Close"
64
+ })]
65
+ }) : url ? /*#__PURE__*/_jsx(CheckoutWebView, {
66
+ url: url,
67
+ testID: "swichpay-hosted-webview",
68
+ allowInsecure: client.config.environment === 'sandbox',
69
+ onNavigate: next => {
70
+ const result = hostedCheckoutResultForUrl(next, detailsRef.current);
71
+ if (!result) return false;
72
+ finish(result);
73
+ return true;
74
+ },
75
+ onPageMessage: (_origin, raw) => {
76
+ const data = messageDataAsObject(raw);
77
+ if (!data) return;
78
+ if (data.type === 'TRANSACTION_COMPLETE' && typeof data.status === 'string') {
79
+ lastStatus.current = data.status.toLowerCase();
80
+ return;
81
+ }
82
+ if (data.command === 'closewindow') finish(null);
83
+ if (data.command === 'redirecttocustomer' && typeof data.url === 'string') {
84
+ finish(hostedCheckoutResultForUrl(data.url, detailsRef.current) ?? createPaymentResult({
85
+ isSuccess: lastStatus.current === 'success',
86
+ status: lastStatus.current ?? 'unknown',
87
+ redirectUrl: data.url
88
+ }));
89
+ }
90
+ }
91
+ }) : /*#__PURE__*/_jsx(View, {
92
+ style: styles.center,
93
+ children: /*#__PURE__*/_jsx(Spinner, {
94
+ dark: true,
95
+ large: true,
96
+ label: "Opening checkout"
97
+ })
98
+ })]
99
+ });
100
+ }
101
+ const styles = StyleSheet.create({
102
+ fill: {
103
+ flex: 1
104
+ },
105
+ center: {
106
+ flex: 1,
107
+ justifyContent: 'center',
108
+ padding: space.lg,
109
+ gap: space.md
110
+ }
111
+ });
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+
3
+ import { useRef, useState } from 'react';
4
+ import { StyleSheet, View } from 'react-native';
5
+ import { WebView } from 'react-native-webview';
6
+ import { isPlainHttp, parseForwarded } from './webviews.js';
7
+ import { Spinner } from './ui.js';
8
+ import { useLatest } from './hooks.js';
9
+ import { FILL } from './theme.js';
10
+
11
+ /** Forwards the page's own `window` messages (it posts to `window.parent`, which is itself here). */
12
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
+ const FORWARD_MESSAGES = `(function () {
14
+ window.addEventListener('message', function (e) {
15
+ var data = e.data;
16
+ try { JSON.stringify(data); } catch (err) { data = String(data); }
17
+ if (window.ReactNativeWebView) window.ReactNativeWebView.postMessage(JSON.stringify({ origin: e.origin, fromFrame: false, data: data }));
18
+ });
19
+ })(); true;`;
20
+
21
+ /**
22
+ * A full-screen SwichPay page. `onNavigate` sees every top-level navigation first and returns
23
+ * `true` to stop it (a redirect URL that ends the checkout - it is never loaded, so it can be any
24
+ * URL, even one that doesn't exist). `onPageMessage` receives the page's `postMessage`s.
25
+ */
26
+ export function CheckoutWebView(props) {
27
+ const [loading, setLoading] = useState(true);
28
+ const onNavigate = useLatest(props.onNavigate);
29
+ const onPageMessage = useLatest(props.onPageMessage);
30
+ const stopped = useRef(false);
31
+ return /*#__PURE__*/_jsxs(View, {
32
+ style: styles.fill,
33
+ testID: props.testID,
34
+ children: [/*#__PURE__*/_jsx(WebView, {
35
+ source: {
36
+ uri: props.url
37
+ }
38
+ // http:// stays whitelisted so an http redirect URL still reaches `onNavigate` (otherwise the
39
+ // WebView hands it to the OS browser) - plain-HTTP *loads* are blocked below.
40
+ ,
41
+ originWhitelist: ['https://*', 'http://*'],
42
+ javaScriptEnabled: true,
43
+ domStorageEnabled: true,
44
+ sharedCookiesEnabled: true,
45
+ setSupportMultipleWindows: false,
46
+ injectedJavaScriptBeforeContentLoaded: FORWARD_MESSAGES,
47
+ injectedJavaScriptForMainFrameOnly: true,
48
+ onLoadEnd: () => setLoading(false),
49
+ onShouldStartLoadWithRequest: request => {
50
+ if (stopped.current) return false;
51
+ if (request.isTopFrame !== false && onNavigate.current?.(request.url)) {
52
+ stopped.current = true;
53
+ return false;
54
+ }
55
+ return props.allowInsecure === true || !isPlainHttp(request.url);
56
+ },
57
+ onMessage: event => {
58
+ const message = parseForwarded(event);
59
+ if (message) onPageMessage.current?.(message.origin, message.data);
60
+ },
61
+ style: styles.fill
62
+ }), loading ? /*#__PURE__*/_jsx(View, {
63
+ style: styles.loading,
64
+ children: /*#__PURE__*/_jsx(Spinner, {
65
+ dark: true,
66
+ large: true,
67
+ label: "Opening checkout"
68
+ })
69
+ }) : null]
70
+ });
71
+ }
72
+ const styles = StyleSheet.create({
73
+ fill: {
74
+ flex: 1,
75
+ backgroundColor: '#fff'
76
+ },
77
+ loading: {
78
+ ...FILL,
79
+ alignItems: 'center',
80
+ justifyContent: 'center',
81
+ backgroundColor: '#fff'
82
+ }
83
+ });