@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,58 @@
1
+ "use strict";
2
+
3
+ import { createContext, useContext, useEffect } from 'react';
4
+ import { Share } from 'react-native';
5
+ import { SwichPay as CoreSwichPay } from './core/swichpay.js';
6
+ import { resolveServices } from './core/flows/services.js';
7
+ import { runDeviceDataCollectionNative } from './webviews.js';
8
+ import { useLatest } from './hooks.js';
9
+
10
+ /**
11
+ * `FlowServices` for React Native: 3-D Secure device-data collection in a hidden WebView;
12
+ * "copy" / QR save and share open the system share sheet (React Native has no built-in
13
+ * clipboard or file APIs, and the SDK adds no native dependencies for them).
14
+ */
15
+ export const nativeFlowServices = resolveServices({
16
+ runDeviceDataCollection: runDeviceDataCollectionNative,
17
+ copyText: async text => {
18
+ await Share.share({
19
+ message: text
20
+ });
21
+ },
22
+ downloadQr: async payload => {
23
+ await Share.share({
24
+ message: payload
25
+ });
26
+ },
27
+ shareQr: async payload => {
28
+ await Share.share({
29
+ message: payload
30
+ });
31
+ return 'shared';
32
+ }
33
+ });
34
+ export const SwichPayContext = /*#__PURE__*/createContext(null);
35
+
36
+ /** The provider's client, or the one from `SwichPay.initialize()` when used outside a provider. */
37
+ export function useSwichPayClient() {
38
+ return useContext(SwichPayContext)?.client ?? CoreSwichPay.client;
39
+ }
40
+ export function useFlowServices(overrides) {
41
+ const base = useContext(SwichPayContext)?.services ?? nativeFlowServices;
42
+ return overrides ? {
43
+ ...base,
44
+ ...overrides
45
+ } : base;
46
+ }
47
+
48
+ /** A presented checkout registers what Android's back button / swipe-down should do. */
49
+ export const DismissContext = /*#__PURE__*/createContext(null);
50
+ export function useDismissHandler(handler) {
51
+ const register = useContext(DismissContext);
52
+ const ref = useLatest(handler);
53
+ useEffect(() => {
54
+ if (!register) return;
55
+ register(() => ref.current());
56
+ return () => register(null);
57
+ }, [register, ref]);
58
+ }
@@ -0,0 +1,413 @@
1
+ "use strict";
2
+
3
+ import { Endpoints } from '../http/endpoints.js';
4
+ import { createPaymentResult } from '../models/payment.js';
5
+ import { parseDirectBank } from '../models/session.js';
6
+ import { dartDoubleString } from '../utils/formatters.js';
7
+ import { listOfMaps, mapOrNull, normalizedCode, str, stringMap } from '../utils/json.js';
8
+ /** Strips vendor messages to whatever's between `[` and `]` when present. */
9
+ export function extractVendorMessage(message) {
10
+ if (message == null) return null;
11
+ const start = message.indexOf('[');
12
+ const end = message.indexOf(']');
13
+ if (start !== -1 && end !== -1 && end > start) return message.slice(start + 1, end);
14
+ return message;
15
+ }
16
+
17
+ /** `code` in {0000, 0, 00} (string or number) or `status == success`. */
18
+ export function isSuccessCode(json) {
19
+ const code = json.code;
20
+ const normalized = typeof code === 'string' ? code.trim() : typeof code === 'number' ? String(Math.trunc(code)) : null;
21
+ const status = str(json.status)?.toLowerCase();
22
+ return normalized === '0000' || normalized === '0' || normalized === '00' || status === 'success';
23
+ }
24
+
25
+ /** Shared shape for e-wallet / bank transfer; OTP required = success code && !isTransactionCompleted. */
26
+ export function parseDirectPaymentResponse(json) {
27
+ const success = isSuccessCode(json);
28
+ const otpRequired = success && json.isTransactionCompleted === false;
29
+ return createPaymentResult({
30
+ isSuccess: success && !otpRequired,
31
+ status: otpRequired ? 'otp_required' : success ? 'success' : 'failed',
32
+ code: str(json.code),
33
+ message: extractVendorMessage(str(json.message)),
34
+ timestamp: str(json.timestamp),
35
+ orderId: str(json.orderId),
36
+ otpRequired,
37
+ bankTransactionId: str(json.transactionId),
38
+ transactionReferenceNo: str(json.transactionReferenceNo),
39
+ retreivalReferenceNo: str(json.retreivalReferenceNo)
40
+ });
41
+ }
42
+
43
+ /** Shared shape for RTP Now / RTP E-wallet; prefers `success`, falls back to code/status. */
44
+ export function parseRtpResponse(json) {
45
+ const success = typeof json.success === 'boolean' ? json.success : isSuccessCode(json);
46
+ return createPaymentResult({
47
+ isSuccess: success,
48
+ status: success ? 'success' : 'failed',
49
+ code: str(json.code),
50
+ message: extractVendorMessage(str(json.message)),
51
+ timestamp: str(json.timestamp),
52
+ orderId: str(json.orderId),
53
+ vendorTransactionId: str(json.transactionId)
54
+ });
55
+ }
56
+
57
+ /**
58
+ * `PaymentV3`: `nextAction.type == "iframe_collect"` is a DDC step; any other non-`complete`
59
+ * `nextAction` with a `url` is a step-up; `complete` (or no `nextAction` with code 0000) is final.
60
+ */
61
+ export function parseDirectCardResult(json, fallbackVendorTransactionId) {
62
+ const code = normalizedCode(json.code);
63
+ const message = str(json.message);
64
+ const nextAction = mapOrNull(json.nextAction);
65
+ const nextActionType = typeof nextAction?.type === 'string' ? nextAction.type : null;
66
+ const isDdcStep = nextActionType === 'iframe_collect';
67
+ // `complete` is a final verdict, not a step - its `url` is just the merchant redirect.
68
+ const isComplete = nextActionType === 'complete';
69
+ const completeStatus = isComplete ? str(nextAction?.status)?.toLowerCase() ?? null : null;
70
+ const continuation = mapOrNull(nextAction?.continuation);
71
+ const proceeds = code === '0000' || nextAction != null && completeStatus !== 'failed';
72
+ if (!proceeds) {
73
+ return createPaymentResult({
74
+ isSuccess: false,
75
+ status: 'failed',
76
+ code,
77
+ message: extractVendorMessage(message) ?? 'Payment failed',
78
+ orderId: str(json.orderId),
79
+ retryable: json.retryable === true
80
+ });
81
+ }
82
+ const url = typeof nextAction?.url === 'string' ? nextAction.url : null;
83
+ const ddcCollectionUrl = isDdcStep ? url : null;
84
+ const stepUpUrl = !isDdcStep && !isComplete ? url : null;
85
+ const hasNextStep = ddcCollectionUrl != null && ddcCollectionUrl !== '' || stepUpUrl != null && stepUpUrl !== '';
86
+ // With nothing left to do only code 0000 is confirmed; anything else is verified by polling.
87
+ const isSuccess = !hasNextStep && code === '0000';
88
+ return createPaymentResult({
89
+ isSuccess,
90
+ status: isSuccess ? 'success' : 'pending',
91
+ code,
92
+ message,
93
+ orderId: str(json.orderId),
94
+ timestamp: str(json.timestamp),
95
+ vendorTransactionId: str(continuation?.vendorTransactionId) ?? str(json.orderId) ?? fallbackVendorTransactionId,
96
+ ddcCollectionUrl,
97
+ stepUpUrl,
98
+ redirectFormFields: stringMap(mapOrNull(nextAction?.formFields))
99
+ });
100
+ }
101
+
102
+ /**
103
+ * `CompleteTokenPayment`: the step-up is under `next_action` (snake_case), whose `nextAction` is a
104
+ * string step type and whose `token` is the JWT to POST.
105
+ */
106
+ export function parseTokenizedPaymentResult(json, fallbackVendorTransactionId) {
107
+ const code = normalizedCode(json.code);
108
+ const message = str(json.message);
109
+ const nextAction = mapOrNull(json.next_action);
110
+ const isStepUpStep = nextAction?.nextAction === 'redirect';
111
+ const stepUpUrl = isStepUpStep ? str(nextAction?.url) : null;
112
+ const stepUpToken = str(nextAction?.token);
113
+ const hasNextStep = stepUpUrl != null && stepUpUrl !== '';
114
+ const success = code === '0000' || str(json.status)?.toLowerCase() === 'success';
115
+ if (!hasNextStep && !success) {
116
+ return createPaymentResult({
117
+ isSuccess: false,
118
+ status: 'failed',
119
+ code,
120
+ message: extractVendorMessage(message) ?? 'Payment failed',
121
+ orderId: str(json.orderId),
122
+ retryable: json.retryable === true
123
+ });
124
+ }
125
+ return createPaymentResult({
126
+ isSuccess: !hasNextStep,
127
+ status: hasNextStep ? 'pending' : 'success',
128
+ code,
129
+ message,
130
+ orderId: str(json.orderId),
131
+ timestamp: str(json.timestamp),
132
+ vendorTransactionId: str(json.vendorTransactionId) ?? fallbackVendorTransactionId,
133
+ stepUpUrl,
134
+ redirectFormFields: stepUpToken == null ? null : {
135
+ JWT: stepUpToken
136
+ }
137
+ });
138
+ }
139
+
140
+ /** `InquireV3`: `transactionStatus` is authoritative; `Initiated` means still pending. */
141
+ export function parseDirectCardStatus(json, tracker) {
142
+ const status = str(json.transactionStatus) ?? '';
143
+ const code = normalizedCode(json.code);
144
+ const lower = status.toLowerCase();
145
+ const normalized = lower === 'initiated' ? 'pending' : lower;
146
+ const isSuccess = normalized === '' ? code === '0000' : normalized === 'success';
147
+ return createPaymentResult({
148
+ isSuccess,
149
+ status: normalized === '' ? isSuccess ? 'success' : 'failed' : normalized,
150
+ code,
151
+ message: str(json.message) ?? status,
152
+ orderId: str(json.orderId),
153
+ redirectUrl: str(json.redirectUrl),
154
+ vendorTransactionId: tracker,
155
+ retryable: !isSuccess && json.retryable === true
156
+ });
157
+ }
158
+ const DIRECT_QR_KEYS = ['qrString', 'qrPayload', 'qrCode', 'qrData', 'qr'];
159
+ /** Direct (session-less) payments on the gateway's `payin` endpoints and the payment-api host. */
160
+ export class DirectPaymentApi {
161
+ constructor(client, now = Date.now) {
162
+ this.client = client;
163
+ this.now = now;
164
+ }
165
+ async directEwalletPayment(input) {
166
+ const d = input.details;
167
+ const json = await this.client.postToGateway(Endpoints.directEwallet, {
168
+ customerTransactionId: d.customerTransactionId,
169
+ categoryId: input.categoryId ?? 2,
170
+ channelId: input.channelId,
171
+ item: d.item,
172
+ UCID: '',
173
+ amount: d.amount,
174
+ msisdn: input.msisdn,
175
+ cnic: d.cnic,
176
+ email: d.email
177
+ });
178
+ return parseDirectPaymentResponse(json);
179
+ }
180
+ async directBankTransfer(input) {
181
+ const d = input.details;
182
+ const json = await this.client.postToGateway(Endpoints.directBankOtp, {
183
+ customerTransactionId: d.customerTransactionId,
184
+ bankCode: input.bankCode,
185
+ item: d.item,
186
+ billReferenceNo: d.billReferenceNo,
187
+ description: d.description ?? d.item,
188
+ amount: d.amount,
189
+ accountNo: input.accountNo,
190
+ cnic: d.cnic,
191
+ email: d.email,
192
+ msisdn: d.msisdn,
193
+ consumerName: d.consumerName
194
+ });
195
+ return parseDirectPaymentResponse(json);
196
+ }
197
+ async confirmDirectBankTransfer(input) {
198
+ const asInt = /^[-+]?\d+$/.test(input.transactionId.trim()) ? Number.parseInt(input.transactionId, 10) : null;
199
+ const json = await this.client.postToGateway(Endpoints.directBankConfirm, {
200
+ customerTransactionId: input.customerTransactionId,
201
+ billReferenceNo: input.billReferenceNo ?? '',
202
+ transactionId: asInt != null && Number.isSafeInteger(asInt) ? asInt : input.transactionId,
203
+ orderId: input.orderId,
204
+ transactionReferenceNo: input.transactionReferenceNo,
205
+ retreivalReferenceNo: input.retreivalReferenceNo,
206
+ otp: input.otp
207
+ });
208
+ return parseDirectPaymentResponse(json);
209
+ }
210
+ fetchDirectBankList() {
211
+ return this.fetchBanks(Endpoints.directBankList);
212
+ }
213
+ async generateDirectOneBill(input) {
214
+ const d = input.details;
215
+ const json = await this.client.postToGateway(Endpoints.directBiller, {
216
+ customerTransactionId: d.customerTransactionId,
217
+ categoryId: input.categoryId ?? 3,
218
+ channelId: input.channelId ?? 11,
219
+ ucid: input.ucid ?? '',
220
+ item: d.item,
221
+ amount: d.amount,
222
+ msisdn: d.msisdn,
223
+ email: d.email,
224
+ cnic: d.cnic,
225
+ bankMnemonic: input.bankMnemonic ?? '',
226
+ prefix: input.prefix ?? '',
227
+ reserved: input.reserved ?? ''
228
+ });
229
+ const success = isSuccessCode(json);
230
+ return createPaymentResult({
231
+ isSuccess: success,
232
+ status: success ? 'success' : 'failed',
233
+ code: str(json.code),
234
+ message: str(json.message),
235
+ timestamp: str(json.timestamp),
236
+ orderId: str(json.orderId),
237
+ consumerNumber: str(json.consumerNumber)
238
+ });
239
+ }
240
+ async checkDirectOneBillStatus(customerTransactionId) {
241
+ const json = await this.client.get(Endpoints.directBillerInquire, {
242
+ CustomerTransactionId: customerTransactionId
243
+ });
244
+ const transaction = mapOrNull(json.transaction);
245
+ const status = str(transaction?.transactionStatus) ?? 'failed';
246
+ const success = status.toLowerCase() === 'success';
247
+ return createPaymentResult({
248
+ isSuccess: success,
249
+ status: status.toLowerCase(),
250
+ message: success ? null : `Payment status: ${status}`,
251
+ timestamp: str(json.timestamp),
252
+ orderId: str(transaction?.orderId)
253
+ });
254
+ }
255
+ async generateDirectQrPayment(details) {
256
+ const json = await this.client.postToGateway(Endpoints.directQrGenerate, {
257
+ customerTransactionId: details.customerTransactionId,
258
+ billReferenceNo: details.orderNumber,
259
+ transactionType: '00003',
260
+ name: details.item,
261
+ amount: details.amount,
262
+ msisdn: details.msisdn,
263
+ email: details.email,
264
+ cnic: details.cnic,
265
+ currency: details.currency
266
+ });
267
+ const success = isSuccessCode(json);
268
+ let qrString = null;
269
+ for (const key of DIRECT_QR_KEYS) {
270
+ const value = json[key];
271
+ if (typeof value === 'string' && value !== '') {
272
+ qrString = value;
273
+ break;
274
+ }
275
+ }
276
+ return createPaymentResult({
277
+ isSuccess: success,
278
+ status: success ? 'success' : 'failed',
279
+ code: str(json.code),
280
+ message: str(json.message),
281
+ timestamp: str(json.timestamp),
282
+ orderId: str(json.orderId),
283
+ qrString
284
+ });
285
+ }
286
+ async checkDirectQrStatus(customerTransactionId) {
287
+ const json = await this.client.postToGateway(Endpoints.directQrInquire, {
288
+ customerTransactionId
289
+ });
290
+ const status = str(json.paymentStatus) ?? 'failed';
291
+ const success = status.toLowerCase() === 'success';
292
+ return createPaymentResult({
293
+ isSuccess: success,
294
+ status: status.toLowerCase(),
295
+ message: success ? null : `Payment status: ${status}`,
296
+ timestamp: str(json.timestamp)
297
+ });
298
+ }
299
+ fetchDirectRtpNowBanks() {
300
+ return this.fetchBanks(Endpoints.directRtpNowBanks);
301
+ }
302
+ async directRtpNowPayment(input) {
303
+ const d = input.details;
304
+ const body = {
305
+ customerTransactionId: d.customerTransactionId,
306
+ billReferenceNo: d.billReferenceNo
307
+ };
308
+ if (input.bankCode != null) body.bankCode = input.bankCode;
309
+ if (input.accountTitle != null) body.accountTitle = input.accountTitle;
310
+ if (input.accountNumber != null) body.accountNumber = input.accountNumber;
311
+ if (input.accountNumberAlias != null) body.accountNumberAlias = input.accountNumberAlias;
312
+ Object.assign(body, {
313
+ transactionType: input.transactionType ?? '00002',
314
+ // `name` is always sent alongside `accountTitle`, regardless of mode.
315
+ name: d.consumerName,
316
+ amount: d.amount,
317
+ msisdn: input.msisdn,
318
+ rtpId: input.rtpId,
319
+ email: d.email,
320
+ cnic: d.cnic,
321
+ currency: d.currency
322
+ });
323
+ return parseRtpResponse(await this.client.postToGateway(Endpoints.directRtpPurchase, body));
324
+ }
325
+ fetchDirectRtpEwalletBanks() {
326
+ return this.fetchBanks(Endpoints.directRtpEwalletBanks);
327
+ }
328
+ async directRtpEwalletPayment(input) {
329
+ const d = input.details;
330
+ const json = await this.client.postToGateway(Endpoints.directRtpPurchase, {
331
+ orderId: d.orderNumber,
332
+ customerTransactionId: d.customerTransactionId,
333
+ billReferenceNo: d.billReferenceNo,
334
+ amount: dartDoubleString(d.amount),
335
+ currency: d.currency,
336
+ customerName: d.consumerName,
337
+ customerEmail: d.email,
338
+ customerPhone: d.msisdn,
339
+ cnic: d.cnic,
340
+ bankCode: input.bankCode,
341
+ msisdn: input.msisdn ?? '',
342
+ iban: input.iban ?? ''
343
+ });
344
+ return parseRtpResponse(json);
345
+ }
346
+ async directCardPayment(input) {
347
+ const {
348
+ details: d,
349
+ card
350
+ } = input;
351
+ const expYear = card.expYear < 100 ? 2000 + card.expYear : card.expYear;
352
+ const json = await this.client.postToPaymentApi(Endpoints.directCardPayment, {
353
+ customerTransactionId: d.customerTransactionId,
354
+ billReferenceNo: d.billReferenceNo,
355
+ // Always true, matching native's `chargeWithNewCard`.
356
+ doCapture: true,
357
+ doCardOnFile: input.doCardOnFile ?? false,
358
+ orderNumber: String(this.now()),
359
+ consumerName: d.consumerName,
360
+ amount: d.amount,
361
+ description: d.description ?? d.item,
362
+ cardNumber: card.number,
363
+ month: String(card.expMonth).padStart(2, '0'),
364
+ year: String(expYear),
365
+ cvc: card.cvv,
366
+ currency: d.currency,
367
+ email: d.email,
368
+ msisdn: d.msisdn,
369
+ cnic: d.cnic,
370
+ clientId: input.clientId,
371
+ item: d.item,
372
+ isRecurringPayment: false,
373
+ recurringPaymentType: 0,
374
+ recurringFrequency: 0,
375
+ recievedCheckSum: input.checksum,
376
+ merchantId: input.merchantId,
377
+ discountAmount: 0,
378
+ IsDiscountApplicable: true,
379
+ convertedAmount: 0,
380
+ discountableAmount: 0,
381
+ // Boolean - PaymentV3 400s on the string form.
382
+ isApi: true
383
+ });
384
+ return parseDirectCardResult(json, d.customerTransactionId);
385
+ }
386
+ async executeDirectCardTokenizedPayment(input) {
387
+ const json = await this.client.postToPaymentApi(Endpoints.directCardTokenizedPayment, {
388
+ customerTransactionId: input.customerTransactionId,
389
+ billReferenceNo: input.billReferenceNo,
390
+ doCapture: true,
391
+ // The "save card" choice was already sent on PaymentV3.
392
+ doCardOnFile: false,
393
+ vendorTransactionId: input.vendorTransactionId,
394
+ sessionId: input.sessionId,
395
+ failedRedirectUrl: '',
396
+ sucessRedirectUrl: ''
397
+ });
398
+ return parseTokenizedPaymentResult(json, input.vendorTransactionId);
399
+ }
400
+ async checkDirectCardPaymentStatus(tracker) {
401
+ const json = await this.client.postToPaymentApi(Endpoints.directCardInquire, {
402
+ tracker,
403
+ paymentMethod: 'card',
404
+ // Matches native - always true, even mid-poll.
405
+ forceTimeout: true
406
+ });
407
+ return parseDirectCardStatus(json, tracker);
408
+ }
409
+ async fetchBanks(path) {
410
+ const json = await this.client.get(path);
411
+ return listOfMaps(json.data).map(parseDirectBank);
412
+ }
413
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ import { Endpoints } from '../http/endpoints.js';
4
+ import { discountRequestToJson, parseDiscountResult } from '../models/discount.js';
5
+ /** Calls to the standalone Discount API host. */
6
+ export class DiscountApi {
7
+ constructor(client) {
8
+ this.client = client;
9
+ }
10
+ async getDiscount(request) {
11
+ const json = await this.client.postToDiscount(Endpoints.getDiscount, discountRequestToJson(request));
12
+ return parseDiscountResult(json);
13
+ }
14
+ }