@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.
- package/CHANGELOG.md +32 -0
- package/LICENSE +21 -0
- package/README.md +82 -2
- package/docs/checkouts.md +98 -0
- package/docs/getting-started.md +140 -0
- package/docs/going-live.md +45 -0
- package/docs/own-screen.md +81 -0
- package/docs/results-and-errors.md +75 -0
- package/docs/server.md +163 -0
- package/docs/troubleshooting.md +57 -0
- package/lib/commonjs/HostedCheckout.js +115 -0
- package/lib/commonjs/checkout-webview.js +85 -0
- package/lib/commonjs/checkouts/PaymentSheet.js +1317 -0
- package/lib/commonjs/checkouts/direct.js +671 -0
- package/lib/commonjs/context.js +64 -0
- package/lib/commonjs/core/api/direct-payment-api.js +425 -0
- package/lib/commonjs/core/api/discount-api.js +19 -0
- package/lib/commonjs/core/api/gateway-api.js +366 -0
- package/lib/commonjs/core/api/payments-api.js +79 -0
- package/lib/commonjs/core/api/repositories.js +5 -0
- package/lib/commonjs/core/assets/images.js +12 -0
- package/lib/commonjs/core/assets/method-logos.js +19 -0
- package/lib/commonjs/core/assets/pwa-images.js +13 -0
- package/lib/commonjs/core/browser/clipboard.js +30 -0
- package/lib/commonjs/core/browser/dom.js +71 -0
- package/lib/commonjs/core/browser/hosted-checkout.js +107 -0
- package/lib/commonjs/core/browser/index.js +119 -0
- package/lib/commonjs/core/browser/qr.js +109 -0
- package/lib/commonjs/core/browser/three-ds.js +151 -0
- package/lib/commonjs/core/client.js +344 -0
- package/lib/commonjs/core/config.js +132 -0
- package/lib/commonjs/core/environment.js +34 -0
- package/lib/commonjs/core/errors.js +138 -0
- package/lib/commonjs/core/events.js +57 -0
- package/lib/commonjs/core/flows/controller.js +106 -0
- package/lib/commonjs/core/flows/countdown.js +41 -0
- package/lib/commonjs/core/flows/direct-bank-transfer.js +236 -0
- package/lib/commonjs/core/flows/direct-card.js +354 -0
- package/lib/commonjs/core/flows/direct-ewallet.js +124 -0
- package/lib/commonjs/core/flows/direct-one-bill.js +172 -0
- package/lib/commonjs/core/flows/direct-qr.js +109 -0
- package/lib/commonjs/core/flows/direct-rtp.js +302 -0
- package/lib/commonjs/core/flows/discount-lookup.js +80 -0
- package/lib/commonjs/core/flows/headless.js +94 -0
- package/lib/commonjs/core/flows/payment-sheet.js +1321 -0
- package/lib/commonjs/core/flows/services.js +31 -0
- package/lib/commonjs/core/flows/shared.js +90 -0
- package/lib/commonjs/core/http/api-client.js +208 -0
- package/lib/commonjs/core/http/endpoints.js +61 -0
- package/lib/commonjs/core/http/token-manager.js +46 -0
- package/lib/commonjs/core/http/transport.js +134 -0
- package/lib/commonjs/core/index.js +688 -0
- package/lib/commonjs/core/models/category.js +113 -0
- package/lib/commonjs/core/models/discount.js +51 -0
- package/lib/commonjs/core/models/payment.js +95 -0
- package/lib/commonjs/core/models/session.js +131 -0
- package/lib/commonjs/core/server/index.js +64 -0
- package/lib/commonjs/core/server/node.js +60 -0
- package/lib/commonjs/core/server/proxy.js +194 -0
- package/lib/commonjs/core/server/verify.js +122 -0
- package/lib/commonjs/core/swichpay.js +72 -0
- package/lib/commonjs/core/utils/crypto.js +78 -0
- package/lib/commonjs/core/utils/formatters.js +68 -0
- package/lib/commonjs/core/utils/json.js +110 -0
- package/lib/commonjs/core/utils/log.js +75 -0
- package/lib/commonjs/core/utils/runtime.js +34 -0
- package/lib/commonjs/core/utils/validators.js +59 -0
- package/lib/commonjs/hooks.js +77 -0
- package/lib/commonjs/index.js +203 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/polyfills.js +3 -0
- package/lib/commonjs/presenter.js +61 -0
- package/lib/commonjs/provider.js +192 -0
- package/lib/commonjs/swichpay.js +192 -0
- package/lib/commonjs/theme.js +54 -0
- package/lib/commonjs/ui.js +1142 -0
- package/lib/commonjs/webviews.js +279 -0
- package/lib/module/HostedCheckout.js +111 -0
- package/lib/module/checkout-webview.js +83 -0
- package/lib/module/checkouts/PaymentSheet.js +1313 -0
- package/lib/module/checkouts/direct.js +661 -0
- package/lib/module/context.js +58 -0
- package/lib/module/core/api/direct-payment-api.js +413 -0
- package/lib/module/core/api/discount-api.js +14 -0
- package/lib/module/core/api/gateway-api.js +358 -0
- package/lib/module/core/api/payments-api.js +73 -0
- package/lib/module/core/api/repositories.js +3 -0
- package/lib/module/core/assets/images.js +8 -0
- package/lib/module/core/assets/method-logos.js +15 -0
- package/lib/module/core/assets/pwa-images.js +9 -0
- package/lib/module/core/browser/clipboard.js +27 -0
- package/lib/module/core/browser/dom.js +63 -0
- package/lib/module/core/browser/hosted-checkout.js +102 -0
- package/lib/module/core/browser/index.js +12 -0
- package/lib/module/core/browser/qr.js +100 -0
- package/lib/module/core/browser/three-ds.js +145 -0
- package/lib/module/core/client.js +338 -0
- package/lib/module/core/config.js +128 -0
- package/lib/module/core/environment.js +29 -0
- package/lib/module/core/errors.js +125 -0
- package/lib/module/core/events.js +51 -0
- package/lib/module/core/flows/controller.js +101 -0
- package/lib/module/core/flows/countdown.js +36 -0
- package/lib/module/core/flows/direct-bank-transfer.js +231 -0
- package/lib/module/core/flows/direct-card.js +349 -0
- package/lib/module/core/flows/direct-ewallet.js +119 -0
- package/lib/module/core/flows/direct-one-bill.js +167 -0
- package/lib/module/core/flows/direct-qr.js +104 -0
- package/lib/module/core/flows/direct-rtp.js +297 -0
- package/lib/module/core/flows/discount-lookup.js +75 -0
- package/lib/module/core/flows/headless.js +90 -0
- package/lib/module/core/flows/payment-sheet.js +1315 -0
- package/lib/module/core/flows/services.js +27 -0
- package/lib/module/core/flows/shared.js +78 -0
- package/lib/module/core/http/api-client.js +203 -0
- package/lib/module/core/http/endpoints.js +57 -0
- package/lib/module/core/http/token-manager.js +41 -0
- package/lib/module/core/http/transport.js +126 -0
- package/lib/module/core/index.js +52 -0
- package/lib/module/core/models/category.js +104 -0
- package/lib/module/core/models/discount.js +46 -0
- package/lib/module/core/models/payment.js +89 -0
- package/lib/module/core/models/session.js +120 -0
- package/lib/module/core/server/index.js +11 -0
- package/lib/module/core/server/node.js +56 -0
- package/lib/module/core/server/proxy.js +191 -0
- package/lib/module/core/server/verify.js +115 -0
- package/lib/module/core/swichpay.js +68 -0
- package/lib/module/core/utils/crypto.js +72 -0
- package/lib/module/core/utils/formatters.js +56 -0
- package/lib/module/core/utils/json.js +94 -0
- package/lib/module/core/utils/log.js +67 -0
- package/lib/module/core/utils/runtime.js +28 -0
- package/lib/module/core/utils/validators.js +54 -0
- package/lib/module/hooks.js +72 -0
- package/lib/module/index.js +36 -0
- package/lib/module/polyfills.js +4 -0
- package/lib/module/presenter.js +58 -0
- package/lib/module/provider.js +188 -0
- package/lib/module/swichpay.js +187 -0
- package/lib/module/theme.js +50 -0
- package/lib/module/ui.js +1116 -0
- package/lib/module/webviews.js +272 -0
- package/lib/typescript/commonjs/HostedCheckout.d.ts +17 -0
- package/lib/typescript/commonjs/checkout-webview.d.ts +14 -0
- package/lib/typescript/commonjs/checkouts/PaymentSheet.d.ts +14 -0
- package/lib/typescript/commonjs/checkouts/direct.d.ts +18 -0
- package/lib/typescript/commonjs/context.d.ts +20 -0
- package/lib/typescript/commonjs/core/api/direct-payment-api.d.ts +103 -0
- package/lib/typescript/commonjs/core/api/discount-api.d.ts +10 -0
- package/lib/typescript/commonjs/core/api/gateway-api.d.ts +150 -0
- package/lib/typescript/commonjs/core/api/payments-api.d.ts +22 -0
- package/lib/typescript/commonjs/core/api/repositories.d.ts +86 -0
- package/lib/typescript/commonjs/core/assets/images.d.ts +5 -0
- package/lib/typescript/commonjs/core/assets/method-logos.d.ts +12 -0
- package/lib/typescript/commonjs/core/assets/pwa-images.d.ts +7 -0
- package/lib/typescript/commonjs/core/browser/clipboard.d.ts +3 -0
- package/lib/typescript/commonjs/core/browser/dom.d.ts +11 -0
- package/lib/typescript/commonjs/core/browser/hosted-checkout.d.ts +27 -0
- package/lib/typescript/commonjs/core/browser/index.d.ts +11 -0
- package/lib/typescript/commonjs/core/browser/qr.d.ts +18 -0
- package/lib/typescript/commonjs/core/browser/three-ds.d.ts +45 -0
- package/lib/typescript/commonjs/core/client.d.ts +181 -0
- package/lib/typescript/commonjs/core/config.d.ts +86 -0
- package/lib/typescript/commonjs/core/environment.d.ts +22 -0
- package/lib/typescript/commonjs/core/errors.d.ts +78 -0
- package/lib/typescript/commonjs/core/events.d.ts +19 -0
- package/lib/typescript/commonjs/core/flows/controller.d.ts +41 -0
- package/lib/typescript/commonjs/core/flows/countdown.d.ts +15 -0
- package/lib/typescript/commonjs/core/flows/direct-bank-transfer.d.ts +49 -0
- package/lib/typescript/commonjs/core/flows/direct-card.d.ts +94 -0
- package/lib/typescript/commonjs/core/flows/direct-ewallet.d.ts +39 -0
- package/lib/typescript/commonjs/core/flows/direct-one-bill.d.ts +38 -0
- package/lib/typescript/commonjs/core/flows/direct-qr.d.ts +25 -0
- package/lib/typescript/commonjs/core/flows/direct-rtp.d.ts +72 -0
- package/lib/typescript/commonjs/core/flows/discount-lookup.d.ts +37 -0
- package/lib/typescript/commonjs/core/flows/headless.d.ts +38 -0
- package/lib/typescript/commonjs/core/flows/payment-sheet.d.ts +263 -0
- package/lib/typescript/commonjs/core/flows/services.d.ts +22 -0
- package/lib/typescript/commonjs/core/flows/shared.d.ts +40 -0
- package/lib/typescript/commonjs/core/http/api-client.d.ts +37 -0
- package/lib/typescript/commonjs/core/http/endpoints.d.ts +39 -0
- package/lib/typescript/commonjs/core/http/token-manager.d.ts +19 -0
- package/lib/typescript/commonjs/core/http/transport.d.ts +37 -0
- package/lib/typescript/commonjs/core/index.d.ts +42 -0
- package/lib/typescript/commonjs/core/models/category.d.ts +35 -0
- package/lib/typescript/commonjs/core/models/discount.d.ts +41 -0
- package/lib/typescript/commonjs/core/models/payment.d.ts +104 -0
- package/lib/typescript/commonjs/core/models/session.d.ts +87 -0
- package/lib/typescript/commonjs/core/server/index.d.ts +10 -0
- package/lib/typescript/commonjs/core/server/node.d.ts +19 -0
- package/lib/typescript/commonjs/core/server/proxy.d.ts +30 -0
- package/lib/typescript/commonjs/core/server/verify.d.ts +61 -0
- package/lib/typescript/commonjs/core/swichpay.d.ts +40 -0
- package/lib/typescript/commonjs/core/utils/crypto.d.ts +19 -0
- package/lib/typescript/commonjs/core/utils/formatters.d.ts +19 -0
- package/lib/typescript/commonjs/core/utils/json.d.ts +27 -0
- package/lib/typescript/commonjs/core/utils/log.d.ts +15 -0
- package/lib/typescript/commonjs/core/utils/runtime.d.ts +12 -0
- package/lib/typescript/commonjs/core/utils/validators.d.ts +23 -0
- package/lib/typescript/commonjs/hooks.d.ts +21 -0
- package/lib/typescript/commonjs/index.d.ts +26 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/polyfills.d.ts +2 -0
- package/lib/typescript/commonjs/presenter.d.ts +25 -0
- package/lib/typescript/commonjs/provider.d.ts +22 -0
- package/lib/typescript/commonjs/swichpay.d.ts +125 -0
- package/lib/typescript/commonjs/theme.d.ts +52 -0
- package/lib/typescript/commonjs/ui.d.ts +583 -0
- package/lib/typescript/commonjs/webviews.d.ts +40 -0
- package/lib/typescript/module/HostedCheckout.d.ts +17 -0
- package/lib/typescript/module/checkout-webview.d.ts +14 -0
- package/lib/typescript/module/checkouts/PaymentSheet.d.ts +14 -0
- package/lib/typescript/module/checkouts/direct.d.ts +18 -0
- package/lib/typescript/module/context.d.ts +20 -0
- package/lib/typescript/module/core/api/direct-payment-api.d.ts +103 -0
- package/lib/typescript/module/core/api/discount-api.d.ts +10 -0
- package/lib/typescript/module/core/api/gateway-api.d.ts +150 -0
- package/lib/typescript/module/core/api/payments-api.d.ts +22 -0
- package/lib/typescript/module/core/api/repositories.d.ts +86 -0
- package/lib/typescript/module/core/assets/images.d.ts +5 -0
- package/lib/typescript/module/core/assets/method-logos.d.ts +12 -0
- package/lib/typescript/module/core/assets/pwa-images.d.ts +7 -0
- package/lib/typescript/module/core/browser/clipboard.d.ts +3 -0
- package/lib/typescript/module/core/browser/dom.d.ts +11 -0
- package/lib/typescript/module/core/browser/hosted-checkout.d.ts +27 -0
- package/lib/typescript/module/core/browser/index.d.ts +11 -0
- package/lib/typescript/module/core/browser/qr.d.ts +18 -0
- package/lib/typescript/module/core/browser/three-ds.d.ts +45 -0
- package/lib/typescript/module/core/client.d.ts +181 -0
- package/lib/typescript/module/core/config.d.ts +86 -0
- package/lib/typescript/module/core/environment.d.ts +22 -0
- package/lib/typescript/module/core/errors.d.ts +78 -0
- package/lib/typescript/module/core/events.d.ts +19 -0
- package/lib/typescript/module/core/flows/controller.d.ts +41 -0
- package/lib/typescript/module/core/flows/countdown.d.ts +15 -0
- package/lib/typescript/module/core/flows/direct-bank-transfer.d.ts +49 -0
- package/lib/typescript/module/core/flows/direct-card.d.ts +94 -0
- package/lib/typescript/module/core/flows/direct-ewallet.d.ts +39 -0
- package/lib/typescript/module/core/flows/direct-one-bill.d.ts +38 -0
- package/lib/typescript/module/core/flows/direct-qr.d.ts +25 -0
- package/lib/typescript/module/core/flows/direct-rtp.d.ts +72 -0
- package/lib/typescript/module/core/flows/discount-lookup.d.ts +37 -0
- package/lib/typescript/module/core/flows/headless.d.ts +38 -0
- package/lib/typescript/module/core/flows/payment-sheet.d.ts +263 -0
- package/lib/typescript/module/core/flows/services.d.ts +22 -0
- package/lib/typescript/module/core/flows/shared.d.ts +40 -0
- package/lib/typescript/module/core/http/api-client.d.ts +37 -0
- package/lib/typescript/module/core/http/endpoints.d.ts +39 -0
- package/lib/typescript/module/core/http/token-manager.d.ts +19 -0
- package/lib/typescript/module/core/http/transport.d.ts +37 -0
- package/lib/typescript/module/core/index.d.ts +42 -0
- package/lib/typescript/module/core/models/category.d.ts +35 -0
- package/lib/typescript/module/core/models/discount.d.ts +41 -0
- package/lib/typescript/module/core/models/payment.d.ts +104 -0
- package/lib/typescript/module/core/models/session.d.ts +87 -0
- package/lib/typescript/module/core/server/index.d.ts +10 -0
- package/lib/typescript/module/core/server/node.d.ts +19 -0
- package/lib/typescript/module/core/server/proxy.d.ts +30 -0
- package/lib/typescript/module/core/server/verify.d.ts +61 -0
- package/lib/typescript/module/core/swichpay.d.ts +40 -0
- package/lib/typescript/module/core/utils/crypto.d.ts +19 -0
- package/lib/typescript/module/core/utils/formatters.d.ts +19 -0
- package/lib/typescript/module/core/utils/json.d.ts +27 -0
- package/lib/typescript/module/core/utils/log.d.ts +15 -0
- package/lib/typescript/module/core/utils/runtime.d.ts +12 -0
- package/lib/typescript/module/core/utils/validators.d.ts +23 -0
- package/lib/typescript/module/hooks.d.ts +21 -0
- package/lib/typescript/module/index.d.ts +26 -0
- package/lib/typescript/module/polyfills.d.ts +2 -0
- package/lib/typescript/module/presenter.d.ts +25 -0
- package/lib/typescript/module/provider.d.ts +22 -0
- package/lib/typescript/module/swichpay.d.ts +125 -0
- package/lib/typescript/module/theme.d.ts +52 -0
- package/lib/typescript/module/ui.d.ts +583 -0
- package/lib/typescript/module/webviews.d.ts +40 -0
- package/package.json +129 -4
package/docs/server.md
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# Server setup
|
|
2
|
+
|
|
3
|
+
Your backend has three jobs in a SwichPay integration:
|
|
4
|
+
|
|
5
|
+
1. **Hold the secrets.** The client secret and the secret hash key never go into the app.
|
|
6
|
+
2. **Proxy the SDK's requests** to SwichPay, adding the secrets.
|
|
7
|
+
3. **Verify payments** before you ship an order.
|
|
8
|
+
|
|
9
|
+
Everything here runs on Node 18+ (Express, Fastify, Next.js route handlers, serverless functions, or anything else that speaks the web `Request`/`Response` API) and uses only SwichPay's existing APIs.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import {
|
|
13
|
+
createSwichPayProxy,
|
|
14
|
+
createNodeProxyMiddleware,
|
|
15
|
+
createPaymentVerifier,
|
|
16
|
+
createCheckoutSessionCreator,
|
|
17
|
+
} from '@swichpay/react-native/server';
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Never import `@swichpay/react-native/server` in app code.
|
|
21
|
+
|
|
22
|
+
## The proxy
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
const merchant = {
|
|
26
|
+
clientId: process.env.SWICHPAY_CLIENT_ID!,
|
|
27
|
+
merchantId: Number(process.env.SWICHPAY_MERCHANT_ID),
|
|
28
|
+
appId: process.env.SWICHPAY_APP_ID!,
|
|
29
|
+
referrer: process.env.SWICHPAY_REFERRER!,
|
|
30
|
+
environment: process.env.SWICHPAY_ENVIRONMENT as 'sandbox' | 'production',
|
|
31
|
+
clientSecret: process.env.SWICHPAY_CLIENT_SECRET,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const swichpay = createSwichPayProxy({
|
|
35
|
+
...merchant,
|
|
36
|
+
secretHashKey: process.env.SWICHPAY_SECRET_HASH_KEY,
|
|
37
|
+
authorize: async (request) => { /* see below */ },
|
|
38
|
+
verifyAmount: async (payment, request) => { /* see below */ },
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Express / Connect
|
|
42
|
+
app.use(createNodeProxyMiddleware(swichpay, '/api/swichpay'));
|
|
43
|
+
|
|
44
|
+
// Web-standard runtimes (Next.js, Hono, Cloudflare, Bun, ...)
|
|
45
|
+
export const POST = (request: Request) => swichpay(request, new URL(request.url).pathname.replace('/api/swichpay', ''));
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Point the app's `proxyUrl` at the mount path, for example `https://api.yourshop.com/api/swichpay`.
|
|
49
|
+
|
|
50
|
+
What the proxy does:
|
|
51
|
+
|
|
52
|
+
- It forwards **only** SwichPay's own payment endpoints (an allowlist) and answers 404 for anything else, including the token endpoint.
|
|
53
|
+
- It adds the OAuth token (fetched with your client secret, cached, refreshed automatically) and the referrer headers.
|
|
54
|
+
- For card payments, it signs the request with your secret hash key, after `verifyAmount` approves it.
|
|
55
|
+
- It forwards only content headers from the app. Cookies and authorization headers are dropped.
|
|
56
|
+
|
|
57
|
+
### `authorize` (required)
|
|
58
|
+
|
|
59
|
+
Runs before every forwarded request. Return a `Response` to reject it. Check that the caller is one of your signed-in users:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
authorize: async (request) => {
|
|
63
|
+
const user = await sessions.fromRequest(request);
|
|
64
|
+
if (!user) return new Response('Unauthorized', { status: 401 });
|
|
65
|
+
},
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Without it, anyone who finds your proxy URL can create payment sessions in your name. `createSwichPayProxy` refuses to start without `authorize`. `dangerouslyAllowUnauthenticated: true` skips the check, for local testing only.
|
|
69
|
+
|
|
70
|
+
Add your usual rate limiting in front of the route.
|
|
71
|
+
|
|
72
|
+
### `verifyAmount` (required with `secretHashKey`)
|
|
73
|
+
|
|
74
|
+
The card checksum tells SwichPay that **you** approved this amount. Check it against your order before the proxy signs it:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
verifyAmount: async ({ customerTransactionId, amount, currency }) => {
|
|
78
|
+
const order = await db.orders.findByPaymentRef(customerTransactionId);
|
|
79
|
+
return order != null && order.total === amount && order.currency === currency;
|
|
80
|
+
},
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Return `false` and the payment is refused. Without this check, a tampered app could pay 1 rupee for a 10,000-rupee order. `dangerouslySignAnyAmount: true` skips it, for local testing only.
|
|
84
|
+
|
|
85
|
+
## Verify payments before you ship
|
|
86
|
+
|
|
87
|
+
The app's `result.isSuccess` is for your UI. Someone can tamper with an app, so before you mark an order paid or ship anything, ask SwichPay directly from your server:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
const verify = createPaymentVerifier(merchant); // needs clientSecret
|
|
91
|
+
|
|
92
|
+
const result = await verify({
|
|
93
|
+
paymentSessionGuid: order.paymentSessionGuid,
|
|
94
|
+
expectedAmount: order.total,
|
|
95
|
+
expectedOrderId: order.number,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
switch (result.status) {
|
|
99
|
+
case 'success': // verified: true. Paid, and the amount and order id match
|
|
100
|
+
await db.orders.markPaid(order.id);
|
|
101
|
+
break;
|
|
102
|
+
case 'failed': // declined, cancelled, expired, or the amount / order id did not match
|
|
103
|
+
await db.orders.markFailed(order.id, result.reason);
|
|
104
|
+
break;
|
|
105
|
+
case 'pending': // no final answer yet: check again later
|
|
106
|
+
await jobs.schedule('recheck-payment', order.id, { inMinutes: 2 });
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
| Field | Meaning |
|
|
112
|
+
|---|---|
|
|
113
|
+
| `status` | `success`, `failed` or `pending` |
|
|
114
|
+
| `verified` | `true` only for `success` with every expectation matched |
|
|
115
|
+
| `reason` | `null`, `declined`, `not_final`, `amount_mismatch` or `order_mismatch` |
|
|
116
|
+
| `inquiry` | SwichPay's raw answer (`status`, `code`, `amount`, `orderId`, …) |
|
|
117
|
+
|
|
118
|
+
The verifier is deliberately cautious. An unknown status counts as `pending`, so an order is never fulfilled without a confirmed success and never cancelled without a confirmed failure. Network and authentication errors are **thrown**, never reported as `failed`, so a SwichPay outage can't cancel a paid order.
|
|
119
|
+
|
|
120
|
+
### Getting the session GUID
|
|
121
|
+
|
|
122
|
+
The cleanest way is to create the payment session on your server, store its GUID on the order, and hand the app the hosted checkout URL:
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
const createSession = createCheckoutSessionCreator(merchant);
|
|
126
|
+
|
|
127
|
+
app.post('/orders/:id/checkout', async (req, res) => {
|
|
128
|
+
const order = await db.orders.find(req.params.id);
|
|
129
|
+
const session = await createSession({ details: toPaymentDetails(order) });
|
|
130
|
+
await db.orders.update(order.id, { paymentSessionGuid: session.paymentSessionGuid });
|
|
131
|
+
res.json({ url: session.url });
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The app then shows the page with `<HostedCheckout details={order} url={url} onFinish={...} />`.
|
|
136
|
+
|
|
137
|
+
## Without the proxy
|
|
138
|
+
|
|
139
|
+
If your backend already issues tokens, pass `getAccessToken` (and `getChecksum` for card payments) to the provider instead of `proxyUrl`. The SDK calls them whenever it needs a token or a card checksum.
|
|
140
|
+
|
|
141
|
+
```tsx
|
|
142
|
+
<SwichPayProvider
|
|
143
|
+
options={{
|
|
144
|
+
...publicOptions,
|
|
145
|
+
getAccessToken: () => api.get('/swichpay/token'),
|
|
146
|
+
getChecksum: (payment) => api.post('/swichpay/checksum', payment),
|
|
147
|
+
}}
|
|
148
|
+
/>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Your `/swichpay/checksum` endpoint must do the same amount check as `verifyAmount`.
|
|
152
|
+
|
|
153
|
+
## Environment variables
|
|
154
|
+
|
|
155
|
+
| Variable | Where | Secret |
|
|
156
|
+
|---|---|---|
|
|
157
|
+
| `SWICHPAY_CLIENT_ID` | server and app | no |
|
|
158
|
+
| `SWICHPAY_MERCHANT_ID`, `SWICHPAY_APP_ID`, `SWICHPAY_REFERRER` | server and app | no |
|
|
159
|
+
| `SWICHPAY_ENVIRONMENT` | server and app | no |
|
|
160
|
+
| `SWICHPAY_CLIENT_SECRET` | **server only** | yes |
|
|
161
|
+
| `SWICHPAY_SECRET_HASH_KEY` | **server only** | yes |
|
|
162
|
+
|
|
163
|
+
In Expo, only variables prefixed with `EXPO_PUBLIC_` reach the app. Never give a secret that prefix.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Troubleshooting
|
|
2
|
+
|
|
3
|
+
## "This payment could not be authorized" / 401 from the proxy
|
|
4
|
+
|
|
5
|
+
The proxy could not get a token from SwichPay.
|
|
6
|
+
|
|
7
|
+
- Check `SWICHPAY_CLIENT_ID` and `SWICHPAY_CLIENT_SECRET` on the server. Sandbox and production credentials are different.
|
|
8
|
+
- Make sure the server environment matches the credentials (`sandbox` credentials with `environment: 'sandbox'`).
|
|
9
|
+
- Restart the server after you change its `.env`.
|
|
10
|
+
|
|
11
|
+
## `SwichPayNotInitializedException`
|
|
12
|
+
|
|
13
|
+
A call ran before the SDK was set up. Mount `<SwichPayProvider>` at the root of your app, above every screen that pays.
|
|
14
|
+
|
|
15
|
+
## "SwichPay can only present a checkout inside `<SwichPayProvider>`"
|
|
16
|
+
|
|
17
|
+
Same cause: `SwichPay.present*` needs the provider mounted.
|
|
18
|
+
|
|
19
|
+
## The app can't reach the proxy (network error)
|
|
20
|
+
|
|
21
|
+
| Where the app runs | Use |
|
|
22
|
+
|---|---|
|
|
23
|
+
| Android emulator | `http://10.0.2.2:3230`, not `localhost` |
|
|
24
|
+
| Phone on USB | `adb reverse tcp:3230 tcp:3230`, then `http://localhost:3230` |
|
|
25
|
+
| Phone on Wi-Fi | Your PC's LAN IP, and allow port 3230 in the PC firewall |
|
|
26
|
+
|
|
27
|
+
Android release builds block `http://` unless cleartext traffic is allowed. Use `https://` in real apps.
|
|
28
|
+
|
|
29
|
+
## The SDK refuses `clientSecret`
|
|
30
|
+
|
|
31
|
+
On purpose: a secret in an app can be extracted. Move it to your server and use `proxyUrl`. See [Server setup](server.md).
|
|
32
|
+
|
|
33
|
+
## `createSwichPayProxy needs authorize` / `needs verifyAmount`
|
|
34
|
+
|
|
35
|
+
The proxy refuses to start without these checks. Add them (see [Server setup](server.md)), or for local testing only set `dangerouslyAllowUnauthenticated` / `dangerouslySignAnyAmount`.
|
|
36
|
+
|
|
37
|
+
## Card payment fails with a checksum error
|
|
38
|
+
|
|
39
|
+
- The server needs `SWICHPAY_SECRET_HASH_KEY`.
|
|
40
|
+
- `verifyAmount` returned `false`. Check that the order lookup by `customerTransactionId` finds the order and that the amounts match exactly.
|
|
41
|
+
|
|
42
|
+
## The 3-D Secure page doesn't open, or stays blank
|
|
43
|
+
|
|
44
|
+
- `react-native-webview` must be installed. In bare React Native, run `pod install` too.
|
|
45
|
+
- `successRedirectUrl` and `failedRedirectUrl` must be valid HTTPS URLs.
|
|
46
|
+
|
|
47
|
+
## A payment shows `pending`
|
|
48
|
+
|
|
49
|
+
The customer hasn't approved it yet (wallet, RTP), or the bank hasn't answered. Don't charge again. Verify it on your server a little later.
|
|
50
|
+
|
|
51
|
+
## Android emulator: no on-screen keyboard
|
|
52
|
+
|
|
53
|
+
When the emulator's keyboard is in "physical keyboard" mode, it shows only a small toolbar in every app, not just yours. Type with your computer's keyboard, or turn on **Settings → System → Keyboard → Physical keyboard → Use on-screen keyboard**. Real phones are not affected.
|
|
54
|
+
|
|
55
|
+
## Metro: "Unable to resolve module @swichpay/react-native"
|
|
56
|
+
|
|
57
|
+
Only when you develop the SDK itself: rebuilding it (`npm run build`) briefly deletes `lib/`, and Metro can cache that. Restart Metro with `npx expo start --clear`.
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.HostedCheckout = HostedCheckout;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
var _reactNative = require("react-native");
|
|
9
|
+
var _hostedCheckout = require("./core/browser/hosted-checkout.js");
|
|
10
|
+
var _dom = require("./core/browser/dom.js");
|
|
11
|
+
var _index = require("./core/index.js");
|
|
12
|
+
var _checkoutWebview = require("./checkout-webview.js");
|
|
13
|
+
var _context = require("./context.js");
|
|
14
|
+
var _hooks = require("./hooks.js");
|
|
15
|
+
var _ui = require("./ui.js");
|
|
16
|
+
var _theme = require("./theme.js");
|
|
17
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
18
|
+
/**
|
|
19
|
+
* SwichPay's hosted checkout page: resolves from the redirect
|
|
20
|
+
* to `successRedirectUrl` / `failedRedirectUrl` or from the page's
|
|
21
|
+
* `redirecttocustomer` / `closewindow` messages. Close resolves `null`.
|
|
22
|
+
*/
|
|
23
|
+
function HostedCheckout(props) {
|
|
24
|
+
const client = (0, _context.useSwichPayClient)();
|
|
25
|
+
const [url, setUrl] = (0, _react.useState)(props.url ?? null);
|
|
26
|
+
const [error, setError] = (0, _react.useState)(null);
|
|
27
|
+
const onFinish = (0, _hooks.useLatest)(props.onFinish);
|
|
28
|
+
const onError = (0, _hooks.useLatest)(props.onError);
|
|
29
|
+
const detailsRef = (0, _hooks.useLatest)(props.details);
|
|
30
|
+
const categoriesRef = (0, _hooks.useLatest)(props.categories);
|
|
31
|
+
const settled = (0, _react.useRef)(false);
|
|
32
|
+
const lastStatus = (0, _react.useRef)(null);
|
|
33
|
+
const finish = result => {
|
|
34
|
+
if (settled.current) return;
|
|
35
|
+
settled.current = true;
|
|
36
|
+
onFinish.current(result);
|
|
37
|
+
};
|
|
38
|
+
(0, _context.useDismissHandler)(() => finish(null));
|
|
39
|
+
(0, _react.useEffect)(() => {
|
|
40
|
+
if (url) return;
|
|
41
|
+
let active = true;
|
|
42
|
+
const timer = setTimeout(() => {
|
|
43
|
+
client.createHostedCheckoutUrl({
|
|
44
|
+
details: detailsRef.current,
|
|
45
|
+
categories: categoriesRef.current
|
|
46
|
+
}).then(u => active && setUrl(u), e => {
|
|
47
|
+
if (!active) return;
|
|
48
|
+
if (onError.current) onError.current(e);else setError((0, _index.swichpayUserError)(e));
|
|
49
|
+
});
|
|
50
|
+
}, 0);
|
|
51
|
+
return () => {
|
|
52
|
+
active = false;
|
|
53
|
+
clearTimeout(timer);
|
|
54
|
+
};
|
|
55
|
+
}, [client, url, detailsRef, categoriesRef, onError]);
|
|
56
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
|
|
57
|
+
style: styles.fill,
|
|
58
|
+
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_ui.SheetHeader, {
|
|
59
|
+
title: "Payment Session",
|
|
60
|
+
onClose: () => finish(null)
|
|
61
|
+
}), error ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
|
|
62
|
+
style: styles.center,
|
|
63
|
+
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_ui.ErrorBanner, {
|
|
64
|
+
message: error
|
|
65
|
+
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_ui.Button, {
|
|
66
|
+
onPress: () => finish(null),
|
|
67
|
+
children: "Close"
|
|
68
|
+
})]
|
|
69
|
+
}) : url ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_checkoutWebview.CheckoutWebView, {
|
|
70
|
+
url: url,
|
|
71
|
+
testID: "swichpay-hosted-webview",
|
|
72
|
+
allowInsecure: client.config.environment === 'sandbox',
|
|
73
|
+
onNavigate: next => {
|
|
74
|
+
const result = (0, _hostedCheckout.hostedCheckoutResultForUrl)(next, detailsRef.current);
|
|
75
|
+
if (!result) return false;
|
|
76
|
+
finish(result);
|
|
77
|
+
return true;
|
|
78
|
+
},
|
|
79
|
+
onPageMessage: (_origin, raw) => {
|
|
80
|
+
const data = (0, _dom.messageDataAsObject)(raw);
|
|
81
|
+
if (!data) return;
|
|
82
|
+
if (data.type === 'TRANSACTION_COMPLETE' && typeof data.status === 'string') {
|
|
83
|
+
lastStatus.current = data.status.toLowerCase();
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (data.command === 'closewindow') finish(null);
|
|
87
|
+
if (data.command === 'redirecttocustomer' && typeof data.url === 'string') {
|
|
88
|
+
finish((0, _hostedCheckout.hostedCheckoutResultForUrl)(data.url, detailsRef.current) ?? (0, _index.createPaymentResult)({
|
|
89
|
+
isSuccess: lastStatus.current === 'success',
|
|
90
|
+
status: lastStatus.current ?? 'unknown',
|
|
91
|
+
redirectUrl: data.url
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
|
|
96
|
+
style: styles.center,
|
|
97
|
+
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ui.Spinner, {
|
|
98
|
+
dark: true,
|
|
99
|
+
large: true,
|
|
100
|
+
label: "Opening checkout"
|
|
101
|
+
})
|
|
102
|
+
})]
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
const styles = _reactNative.StyleSheet.create({
|
|
106
|
+
fill: {
|
|
107
|
+
flex: 1
|
|
108
|
+
},
|
|
109
|
+
center: {
|
|
110
|
+
flex: 1,
|
|
111
|
+
justifyContent: 'center',
|
|
112
|
+
padding: _theme.space.lg,
|
|
113
|
+
gap: _theme.space.md
|
|
114
|
+
}
|
|
115
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.CheckoutWebView = CheckoutWebView;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
var _reactNative = require("react-native");
|
|
9
|
+
var _reactNativeWebview = require("react-native-webview");
|
|
10
|
+
var _webviews = require("./webviews.js");
|
|
11
|
+
var _ui = require("./ui.js");
|
|
12
|
+
var _hooks = require("./hooks.js");
|
|
13
|
+
var _theme = require("./theme.js");
|
|
14
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
15
|
+
/** Forwards the page's own `window` messages (it posts to `window.parent`, which is itself here). */const FORWARD_MESSAGES = `(function () {
|
|
16
|
+
window.addEventListener('message', function (e) {
|
|
17
|
+
var data = e.data;
|
|
18
|
+
try { JSON.stringify(data); } catch (err) { data = String(data); }
|
|
19
|
+
if (window.ReactNativeWebView) window.ReactNativeWebView.postMessage(JSON.stringify({ origin: e.origin, fromFrame: false, data: data }));
|
|
20
|
+
});
|
|
21
|
+
})(); true;`;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A full-screen SwichPay page. `onNavigate` sees every top-level navigation first and returns
|
|
25
|
+
* `true` to stop it (a redirect URL that ends the checkout - it is never loaded, so it can be any
|
|
26
|
+
* URL, even one that doesn't exist). `onPageMessage` receives the page's `postMessage`s.
|
|
27
|
+
*/
|
|
28
|
+
function CheckoutWebView(props) {
|
|
29
|
+
const [loading, setLoading] = (0, _react.useState)(true);
|
|
30
|
+
const onNavigate = (0, _hooks.useLatest)(props.onNavigate);
|
|
31
|
+
const onPageMessage = (0, _hooks.useLatest)(props.onPageMessage);
|
|
32
|
+
const stopped = (0, _react.useRef)(false);
|
|
33
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
|
|
34
|
+
style: styles.fill,
|
|
35
|
+
testID: props.testID,
|
|
36
|
+
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeWebview.WebView, {
|
|
37
|
+
source: {
|
|
38
|
+
uri: props.url
|
|
39
|
+
}
|
|
40
|
+
// http:// stays whitelisted so an http redirect URL still reaches `onNavigate` (otherwise the
|
|
41
|
+
// WebView hands it to the OS browser) - plain-HTTP *loads* are blocked below.
|
|
42
|
+
,
|
|
43
|
+
originWhitelist: ['https://*', 'http://*'],
|
|
44
|
+
javaScriptEnabled: true,
|
|
45
|
+
domStorageEnabled: true,
|
|
46
|
+
sharedCookiesEnabled: true,
|
|
47
|
+
setSupportMultipleWindows: false,
|
|
48
|
+
injectedJavaScriptBeforeContentLoaded: FORWARD_MESSAGES,
|
|
49
|
+
injectedJavaScriptForMainFrameOnly: true,
|
|
50
|
+
onLoadEnd: () => setLoading(false),
|
|
51
|
+
onShouldStartLoadWithRequest: request => {
|
|
52
|
+
if (stopped.current) return false;
|
|
53
|
+
if (request.isTopFrame !== false && onNavigate.current?.(request.url)) {
|
|
54
|
+
stopped.current = true;
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
return props.allowInsecure === true || !(0, _webviews.isPlainHttp)(request.url);
|
|
58
|
+
},
|
|
59
|
+
onMessage: event => {
|
|
60
|
+
const message = (0, _webviews.parseForwarded)(event);
|
|
61
|
+
if (message) onPageMessage.current?.(message.origin, message.data);
|
|
62
|
+
},
|
|
63
|
+
style: styles.fill
|
|
64
|
+
}), loading ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
|
|
65
|
+
style: styles.loading,
|
|
66
|
+
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ui.Spinner, {
|
|
67
|
+
dark: true,
|
|
68
|
+
large: true,
|
|
69
|
+
label: "Opening checkout"
|
|
70
|
+
})
|
|
71
|
+
}) : null]
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const styles = _reactNative.StyleSheet.create({
|
|
75
|
+
fill: {
|
|
76
|
+
flex: 1,
|
|
77
|
+
backgroundColor: '#fff'
|
|
78
|
+
},
|
|
79
|
+
loading: {
|
|
80
|
+
..._theme.FILL,
|
|
81
|
+
alignItems: 'center',
|
|
82
|
+
justifyContent: 'center',
|
|
83
|
+
backgroundColor: '#fff'
|
|
84
|
+
}
|
|
85
|
+
});
|