pa-sdk-react-native 0.0.0-stage → 3.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/LICENSE +21 -0
- package/README.md +268 -2
- package/lib/api.d.ts +88 -0
- package/lib/api.js +163 -0
- package/lib/confirm.d.ts +66 -0
- package/lib/confirm.js +359 -0
- package/lib/errors.d.ts +38 -0
- package/lib/errors.generated.d.ts +13 -0
- package/lib/errors.generated.js +63 -0
- package/lib/errors.js +80 -0
- package/lib/index.d.ts +43 -0
- package/lib/index.js +94 -0
- package/lib/init.d.ts +47 -0
- package/lib/init.js +96 -0
- package/lib/locales.d.ts +36 -0
- package/lib/locales.generated.d.ts +22 -0
- package/lib/locales.generated.js +134 -0
- package/lib/locales.js +100 -0
- package/lib/poll.d.ts +61 -0
- package/lib/poll.js +172 -0
- package/lib/threeDs.d.ts +59 -0
- package/lib/threeDs.js +64 -0
- package/lib/threeDsHost.d.ts +17 -0
- package/lib/threeDsHost.js +159 -0
- package/lib/tokenize.d.ts +22 -0
- package/lib/tokenize.js +50 -0
- package/lib/types.d.ts +128 -0
- package/lib/types.js +17 -0
- package/lib/validator.d.ts +71 -0
- package/lib/validator.js +191 -0
- package/locales/en-US.json +30 -0
- package/locales/ms-MY.json +30 -0
- package/locales/zh-CN.json +30 -0
- package/locales/zh-TW.json +30 -0
- package/native/android/build.gradle +49 -0
- package/native/android/src/main/AndroidManifest.xml +41 -0
- package/native/android/src/main/java/com/pasdk/reactnative/PaSdk3dsActivity.kt +250 -0
- package/native/android/src/main/java/com/pasdk/reactnative/PaSdk3dsModule.kt +110 -0
- package/native/android/src/main/java/com/pasdk/reactnative/PaSdkReactNativePackage.kt +27 -0
- package/native/ios/PaSdk3ds.swift +311 -0
- package/native/ios/PaSdk3dsModule.m +47 -0
- package/pa-sdk-react-native.podspec +23 -0
- package/package.json +46 -3
- package/scripts/generate-errors.js +86 -0
- package/scripts/generate-locales.js +95 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Payment Asia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,269 @@
|
|
|
1
|
-
#
|
|
1
|
+
# PA SDK React Native
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Payment Asia card payment SDK for React Native** — headless card validation,
|
|
4
|
+
direct tokenization, confirm with built-in 3D Secure, save-card and
|
|
5
|
+
multi-market merchant accounts. The native-app counterpart of the PASDK web
|
|
6
|
+
SDK, built to the Flow Travel unified requirements (deliverable D4).
|
|
7
|
+
|
|
8
|
+
- Scope: **Visa and Mastercard** (other schemes are detected early so the app
|
|
9
|
+
can block them).
|
|
10
|
+
- Result states (exactly five, A3): `succeeded` · `authorized` · `processing`
|
|
11
|
+
· `cancelled` · `failed`. `requires_action` is never resolved (COM-05);
|
|
12
|
+
unknown outcomes resolve `processing`, never `failed` (COM-06).
|
|
13
|
+
|
|
14
|
+
## Requirements
|
|
15
|
+
|
|
16
|
+
| | |
|
|
17
|
+
|---|---|
|
|
18
|
+
| React Native | >= 0.79 (developed against 0.79; New Architecture + Hermes, RN-01/RN-02) |
|
|
19
|
+
| iOS | 13.0+ (Flow floor 15 — RN-03) |
|
|
20
|
+
| Android | API 24+ (RN-03) |
|
|
21
|
+
| Native dependency | `react-native-webview` ^13 (used by the JS fallback presenter; the primary 3DS presenter is the SDK's own native WebView) |
|
|
22
|
+
|
|
23
|
+
No other native dependencies — no ads, analytics or billing SDKs (RN-05).
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install pa-sdk-react-native
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### iOS
|
|
32
|
+
|
|
33
|
+
The podspec ships with the package (autolinked by RN 0.79+):
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
cd ios && pod install && cd ..
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The native 3DS module is a classic NativeModules bridge
|
|
40
|
+
(`native/ios/PaSdk3dsModule.m` + `PaSdk3ds.swift`). It runs unchanged on the
|
|
41
|
+
New Architecture through React Native's interop layer — no codegen setup
|
|
42
|
+
needed. (A TurboModule codegen spec can be added later without changing the
|
|
43
|
+
JS API.)
|
|
44
|
+
|
|
45
|
+
### Android
|
|
46
|
+
|
|
47
|
+
Autolinking picks up `native/android` — no manual gradle edits. The manifest
|
|
48
|
+
of the library registers `PaSdk3dsActivity` with intent-filters for the 3DS
|
|
49
|
+
return URLs:
|
|
50
|
+
|
|
51
|
+
- custom scheme `flow://`
|
|
52
|
+
- universal-link host `https://app.flowtheroom.com`
|
|
53
|
+
|
|
54
|
+
While a challenge is pending, the SDK **consumes** its own 3DS returns there;
|
|
55
|
+
the app's own deep-link handler never sees them (RN-20). When no challenge is
|
|
56
|
+
pending, the activity re-dispatches the URI to the app's launch intent, so
|
|
57
|
+
Flow's normal link handling proceeds. Do not declare overlapping
|
|
58
|
+
intent-filters for these exact paths if you also want to handle them yourself
|
|
59
|
+
in production beyond 3DS.
|
|
60
|
+
|
|
61
|
+
## Quickstart
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { PaSdk3dsHost } from 'pa-sdk-react-native'; // optional JS 3DS fallback
|
|
65
|
+
|
|
66
|
+
// 1. Init — once, from build config (COM-03). One init, many merchant
|
|
67
|
+
// accounts (COM-40). Publishable merchant tokens only — never a secret
|
|
68
|
+
// key (COM-01).
|
|
69
|
+
import { init } from 'pa-sdk-react-native';
|
|
70
|
+
|
|
71
|
+
init({
|
|
72
|
+
environment: 'sandbox', // or 'production'
|
|
73
|
+
accounts: {
|
|
74
|
+
HK: 'mt_hk_...', // publishable merchantToken
|
|
75
|
+
MY: 'mt_my_...',
|
|
76
|
+
SG: 'mt_sg_...',
|
|
77
|
+
TW: 'mt_tw_...',
|
|
78
|
+
},
|
|
79
|
+
defaultAccount: 'HK',
|
|
80
|
+
locale: 'zh_HK', // en_US / zh_HK / zh_CN / my_MY (A10)
|
|
81
|
+
origin: 'https://app.flowtheroom.com',
|
|
82
|
+
threeDsReturnUrls: { // consumed by the SDK (RN-20)
|
|
83
|
+
scheme: 'flow://',
|
|
84
|
+
universalLinkHost: 'https://app.flowtheroom.com',
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Mount the fallback presenter once if the native module may be absent:
|
|
89
|
+
// <PaSdk3dsHost />
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Flow 1 — one-off payment (combined, RN-14)
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
import { confirmWithCard } from 'pa-sdk-react-native';
|
|
96
|
+
|
|
97
|
+
const result = await confirmWithCard({
|
|
98
|
+
card: { cardNumber, expiryMonth, expiryYear, cvc, cardHolderName }, // from YOUR UI
|
|
99
|
+
intentToken, clientToken, // from Flow's server only (COM-01)
|
|
100
|
+
idempotencyKey, // yours, unique per attempt (COM-12 / RN-30)
|
|
101
|
+
allowedBrands: ['visa', 'mastercard'], // from your server (RN-12)
|
|
102
|
+
onThreeDsWillPresent: () => hideLoadingOverlay(), // RN-19
|
|
103
|
+
market: 'HK', // merchant account selection (COM-40)
|
|
104
|
+
});
|
|
105
|
+
// result.status: 'succeeded' | 'authorized' | 'processing' | 'cancelled' | 'failed'
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Flow 2 — two-step (mandatory form, RN-14)
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
import { tokenizeCard, confirmWithToken } from 'pa-sdk-react-native';
|
|
112
|
+
|
|
113
|
+
// Step 1: card data goes straight to PA (COM-02)
|
|
114
|
+
const { result: token } = await tokenizeCard(getSdk().api, {
|
|
115
|
+
card,
|
|
116
|
+
merchantToken: 'mt_hk_...', // per-market publishable token (COM-41)
|
|
117
|
+
});
|
|
118
|
+
// token = { paToken, last4, brand }
|
|
119
|
+
|
|
120
|
+
// Step 2: confirm the intent with the token
|
|
121
|
+
const result = await confirmWithToken({
|
|
122
|
+
paToken: token.paToken,
|
|
123
|
+
intentToken, clientToken, idempotencyKey,
|
|
124
|
+
onThreeDsWillPresent: () => hideLoadingOverlay(),
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Flow 3 — save during payment (COM-14 / RN-23)
|
|
129
|
+
|
|
130
|
+
```tsx
|
|
131
|
+
const result = await confirmWithCard({ card, intentToken, clientToken,
|
|
132
|
+
saveCard: true, idempotencyKey });
|
|
133
|
+
// result.paToken / result.brand / result.last4 arrive synchronously (COM-16).
|
|
134
|
+
// The token is MIT-ready: Flow's server charges it later via the Backend
|
|
135
|
+
// Recurring Payment API (/recurring/pay, COM-18) or an intent-based
|
|
136
|
+
// server-side confirm — PASDK's /charge endpoint was disabled on 2026-11-13.
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Flow 4 — manual save / add card (COM-15 / RN-24)
|
|
140
|
+
|
|
141
|
+
Same call against a **verification intent** your server creates (zero amount,
|
|
142
|
+
or a minimal amount settable per request). `saveCard: true`, 3DS runs if the
|
|
143
|
+
issuer requires it; nothing is charged; `pa_token` returns synchronously.
|
|
144
|
+
|
|
145
|
+
### Flow 5 — 3DS (RN-18..RN-20)
|
|
146
|
+
|
|
147
|
+
Everything is inside confirm:
|
|
148
|
+
|
|
149
|
+
- The SDK presents the challenge (full-screen native WebView; Cardinal StepUp
|
|
150
|
+
JWT POST, or ACS `creq` POST for the 3DS1 / non-frameable fallback).
|
|
151
|
+
- `onThreeDsWillPresent` fires immediately before it appears (dismiss your
|
|
152
|
+
overlay there), `onThreeDsEnded` when it is gone (informational).
|
|
153
|
+
- The user can cancel the challenge → `cancelled` (the SDK also cancels the
|
|
154
|
+
intent server-side, best effort).
|
|
155
|
+
- While the challenge is open the SDK polls `intent-status`; the gateway
|
|
156
|
+
posts the result server-side, and the first observed terminal state wins.
|
|
157
|
+
- App-switch (banking app): polling is **AppState-aware** — the schedule is
|
|
158
|
+
2s x 15 then 5s x 12, suspended time while backgrounded does not count
|
|
159
|
+
against the budget, and polling resumes on return. Budget exhausted with
|
|
160
|
+
no answer → `processing` (COM-06), never `failed`.
|
|
161
|
+
- The challenge's return URLs (`flow://`, universal link) are consumed by the
|
|
162
|
+
SDK without surfacing to the app's link handler.
|
|
163
|
+
|
|
164
|
+
### Flow 6 — multi-market (COM-40)
|
|
165
|
+
|
|
166
|
+
One init; pick the account per call with `market`. Currency and country are
|
|
167
|
+
per intent on your server (COM-39).
|
|
168
|
+
|
|
169
|
+
### Cancel an intent
|
|
170
|
+
|
|
171
|
+
```tsx
|
|
172
|
+
import { cancelIntent } from 'pa-sdk-react-native';
|
|
173
|
+
await cancelIntent({ intentToken, clientToken }); // -> { status: 'cancelled' }
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Validation helpers (RN-12)
|
|
177
|
+
|
|
178
|
+
```tsx
|
|
179
|
+
import { validateCard, validateCardNumber, validateExpiry, validateCvc, detectBrand } from 'pa-sdk-react-native';
|
|
180
|
+
|
|
181
|
+
detectBrand('222100...').brand // 'mastercard' — 2-series 2221–2720 recognised
|
|
182
|
+
validateCardNumber('4242...', ['visa']) // Luhn, length, brand filter
|
|
183
|
+
validateCard(card, ['visa', 'mastercard'])
|
|
184
|
+
// -> { brand, complete, fields: { cardNumber?, expiry?, cvc? } }
|
|
185
|
+
// field codes (COM-26): incomplete_number, invalid_number, incomplete_expiry,
|
|
186
|
+
// invalid_expiry, incomplete_cvc, invalid_cvc, unsupported_brand
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Rules: Visa/Mastercard lengths, Luhn, current BIN ranges (Mastercard 2-series
|
|
190
|
+
2221–2720 included), 3-digit CVC, expiry not in the past — no valid Visa /
|
|
191
|
+
Mastercard is ever blocked (COM-31).
|
|
192
|
+
|
|
193
|
+
## Results and errors (RN-28 / RN-29)
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
interface PaymentResult {
|
|
197
|
+
status: 'succeeded' | 'authorized' | 'processing' | 'cancelled' | 'failed';
|
|
198
|
+
intentToken?: string;
|
|
199
|
+
requestReference?: string | null; // PA's reference for this attempt
|
|
200
|
+
merchantReference?: string | null;
|
|
201
|
+
paToken?: string | null; // set when a card was saved
|
|
202
|
+
brand?: string | null;
|
|
203
|
+
last4?: string | null;
|
|
204
|
+
errorCode?: string | null; // shared table (A8) for 'failed'
|
|
205
|
+
errorMessage?: string | null; // English developer message
|
|
206
|
+
userMessage?: string | null; // localized — show this, log the code
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
Every rejection is a `PaError` with `code`, `developerMessage` (English, from
|
|
211
|
+
the shared `errors.json` table — one table for API, webhooks, web and this
|
|
212
|
+
SDK, COM-24) and `userMessage` (localized). Technical failures reject with
|
|
213
|
+
typed errors and are never payment results (COM-08). Client-side, a second
|
|
214
|
+
confirm on an in-flight intent rejects with `duplicate_confirm`; confirming
|
|
215
|
+
an already-final intent returns its stored result (COM-13).
|
|
216
|
+
|
|
217
|
+
## Locales (A10)
|
|
218
|
+
|
|
219
|
+
`en-US`, `zh-TW` (serves `zh_HK`/`zh-HK`), `zh-CN`, `ms-MY` (Flow's `my_MY`).
|
|
220
|
+
Accepts underscore/hyphen, case-insensitive (`zh_HK`, `zh-hk`, `ms`). Unknown
|
|
221
|
+
tags fall back along exact tag → same language → `en-US`, never error
|
|
222
|
+
(COM-36). `ms-MY` currently ships empty with `_meta.fallback = 'en-US'` and
|
|
223
|
+
renders English until translations land. Change locale at runtime with
|
|
224
|
+
`updateLocale('zh_CN')` (COM-35).
|
|
225
|
+
|
|
226
|
+
## Gateway endpoints used
|
|
227
|
+
|
|
228
|
+
All POST JSON under `https://gateway.pa-sys.com/pasdk/v1` (prod) /
|
|
229
|
+
`https://gateway-sandbox.pa-sys.com/pasdk/v1` (sandbox), envelope
|
|
230
|
+
`{request, response:{code,message}, payload}`; business errors ride HTTP 200
|
|
231
|
+
with `response.code != 200`:
|
|
232
|
+
|
|
233
|
+
| Endpoint | Used by |
|
|
234
|
+
|---|---|
|
|
235
|
+
| `/{merchantToken}/element-sessions` | `createElementSession` |
|
|
236
|
+
| `/tokenize` | `tokenizeCard` |
|
|
237
|
+
| `/confirm` | `confirmWithToken` / `confirmWithCard` |
|
|
238
|
+
| `/intent-cancel` | `cancelIntent`, 3DS user-cancel |
|
|
239
|
+
| `/intent-status` | polling (2s x 15, then 5s x 12) |
|
|
240
|
+
|
|
241
|
+
## Error-code sharing
|
|
242
|
+
|
|
243
|
+
`src/errors.generated.ts` is generated from the shared table:
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
node scripts/generate-errors.js [path/to/errors.json]
|
|
247
|
+
# default input: ../system-v3/public_html/pasdk/v3/errors.json
|
|
248
|
+
node scripts/generate-locales.js # mirrors locales/*.json into src/
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## Example app
|
|
252
|
+
|
|
253
|
+
See `example/` — a runnable RN 0.79 (New Architecture) app covering one-off
|
|
254
|
+
payment, two-step payment, save during payment, manual save, 3DS
|
|
255
|
+
challenge/cancel and multi-market switching.
|
|
256
|
+
|
|
257
|
+
## Known deviations / notes
|
|
258
|
+
|
|
259
|
+
- The native 3DS modules use the classic `NativeModules` bridge via
|
|
260
|
+
`RCT_EXTERN_METHOD` (iOS) and `ReactPackage` (Android). On the New
|
|
261
|
+
Architecture this runs through RN's interop layer — supported, but not a
|
|
262
|
+
codegen TurboModule spec yet (documented per deliverable note).
|
|
263
|
+
- The JS fallback presenter (`<PaSdk3dsHost />`, backed by
|
|
264
|
+
`react-native-webview`) is only used when the native module is missing;
|
|
265
|
+
its return-URL consumption happens via WebView navigation interception.
|
|
266
|
+
- `confirmWithCard` validates with `allowedBrands` before any network call;
|
|
267
|
+
brand detection and `allowedBrands` filtering both live in `validator.ts`.
|
|
268
|
+
- Android: when no 3DS challenge is pending, `PaSdk3dsActivity` re-dispatches
|
|
269
|
+
a matched URI to the app's launch intent so non-3DS deep links are not lost.
|
package/lib/api.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway API client (RN-13/14 back end).
|
|
3
|
+
*
|
|
4
|
+
* All endpoints are POST JSON on the pasdk/v1 base URL. Responses use the
|
|
5
|
+
* PA envelope:
|
|
6
|
+
* { request, response: { code, message }, payload }
|
|
7
|
+
* Business errors ride HTTP 200 with response.code != 200 and are converted
|
|
8
|
+
* to PaError with a shared-table code (COM-24). Transport failures reject
|
|
9
|
+
* with `network_error` / `rate_limited` / `provider_error` (COM-08).
|
|
10
|
+
*/
|
|
11
|
+
import type { ConfirmGatewayPayload, Environment } from './types';
|
|
12
|
+
export declare const GATEWAY_BASE_URLS: {
|
|
13
|
+
[env in Environment]: string;
|
|
14
|
+
};
|
|
15
|
+
export interface ApiCallOptions {
|
|
16
|
+
/** Publishable merchantToken — used for per-account paths (COM-41). */
|
|
17
|
+
merchantToken?: string;
|
|
18
|
+
/** Flow-supplied idempotency key (COM-12 / RN-30), sent as a body field. */
|
|
19
|
+
idempotencyKey?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface ElementSessionPayload {
|
|
22
|
+
session_token: string;
|
|
23
|
+
expiry_time?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface TokenizePayload {
|
|
26
|
+
pa_token: string;
|
|
27
|
+
last4: string;
|
|
28
|
+
brand: string;
|
|
29
|
+
}
|
|
30
|
+
export interface IntentStatusPayload {
|
|
31
|
+
status: string;
|
|
32
|
+
request_reference?: string | null;
|
|
33
|
+
merchant_reference?: string | null;
|
|
34
|
+
pa_token?: string | null;
|
|
35
|
+
brand?: string | null;
|
|
36
|
+
last4?: string | null;
|
|
37
|
+
error_code?: string | null;
|
|
38
|
+
error_message?: string | null;
|
|
39
|
+
}
|
|
40
|
+
export type ConfirmPayload = ConfirmGatewayPayload;
|
|
41
|
+
export declare class ApiClient {
|
|
42
|
+
readonly environment: Environment;
|
|
43
|
+
readonly baseUrl: string;
|
|
44
|
+
/** App origin, sent as the Origin header where the gateway recommends it. */
|
|
45
|
+
origin?: string;
|
|
46
|
+
/** Hook for extra headers (tests / tracing). */
|
|
47
|
+
extraHeaders?: {
|
|
48
|
+
[name: string]: string;
|
|
49
|
+
};
|
|
50
|
+
constructor(environment: Environment, origin?: string);
|
|
51
|
+
/** Map an HTTP-level failure to a shared-table code. */
|
|
52
|
+
private static codeForHttpStatus;
|
|
53
|
+
private post;
|
|
54
|
+
/** POST /{merchantToken}/element-sessions — public app session (publishable
|
|
55
|
+
* credential is the merchantToken, never a secret key; COM-01). */
|
|
56
|
+
createElementSession(merchantToken: string): Promise<ElementSessionPayload>;
|
|
57
|
+
/** POST /tokenize — card data goes straight from the app to PA (COM-02). */
|
|
58
|
+
tokenize(params: {
|
|
59
|
+
sessionToken: string;
|
|
60
|
+
cardNumber: string;
|
|
61
|
+
expiryMonth: number | string;
|
|
62
|
+
expiryYear: number | string;
|
|
63
|
+
cvc: string;
|
|
64
|
+
cardHolderName?: string;
|
|
65
|
+
}): Promise<TokenizePayload>;
|
|
66
|
+
/** POST /confirm — one confirm per attempt; while status is
|
|
67
|
+
* requires_action / processing the app keeps polling (see poll.ts). */
|
|
68
|
+
confirm(params: {
|
|
69
|
+
clientToken: string;
|
|
70
|
+
intentToken: string;
|
|
71
|
+
paToken?: string;
|
|
72
|
+
idempotencyKey?: string;
|
|
73
|
+
saveCard?: boolean;
|
|
74
|
+
billing?: Record<string, unknown>;
|
|
75
|
+
}): Promise<ConfirmPayload>;
|
|
76
|
+
/** POST /intent-cancel. */
|
|
77
|
+
intentCancel(params: {
|
|
78
|
+
clientToken: string;
|
|
79
|
+
intentToken: string;
|
|
80
|
+
}): Promise<{
|
|
81
|
+
status: string;
|
|
82
|
+
}>;
|
|
83
|
+
/** POST /intent-status — polled until a terminal status or budget end. */
|
|
84
|
+
intentStatus(params: {
|
|
85
|
+
clientToken: string;
|
|
86
|
+
intentToken: string;
|
|
87
|
+
}): Promise<IntentStatusPayload>;
|
|
88
|
+
}
|
package/lib/api.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Gateway API client (RN-13/14 back end).
|
|
4
|
+
*
|
|
5
|
+
* All endpoints are POST JSON on the pasdk/v1 base URL. Responses use the
|
|
6
|
+
* PA envelope:
|
|
7
|
+
* { request, response: { code, message }, payload }
|
|
8
|
+
* Business errors ride HTTP 200 with response.code != 200 and are converted
|
|
9
|
+
* to PaError with a shared-table code (COM-24). Transport failures reject
|
|
10
|
+
* with `network_error` / `rate_limited` / `provider_error` (COM-08).
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.ApiClient = exports.GATEWAY_BASE_URLS = void 0;
|
|
14
|
+
const errors_1 = require("./errors");
|
|
15
|
+
const errors_generated_1 = require("./errors.generated");
|
|
16
|
+
/** Whether a code exists in the shared table (COM-24). */
|
|
17
|
+
function ERROR_TABLE_HAS(code) {
|
|
18
|
+
return Object.prototype.hasOwnProperty.call(errors_generated_1.ERROR_DEVELOPER_MESSAGES, code);
|
|
19
|
+
}
|
|
20
|
+
exports.GATEWAY_BASE_URLS = {
|
|
21
|
+
production: 'https://gateway.pa-sys.com/pasdk/v1',
|
|
22
|
+
sandbox: 'https://gateway-sandbox.pa-sys.com/pasdk/v1',
|
|
23
|
+
};
|
|
24
|
+
const HTTP_OK = 200;
|
|
25
|
+
class ApiClient {
|
|
26
|
+
constructor(environment, origin) {
|
|
27
|
+
this.environment = environment;
|
|
28
|
+
this.baseUrl = exports.GATEWAY_BASE_URLS[environment];
|
|
29
|
+
this.origin = origin;
|
|
30
|
+
}
|
|
31
|
+
/** Map an HTTP-level failure to a shared-table code. */
|
|
32
|
+
static codeForHttpStatus(status) {
|
|
33
|
+
if (status === 429) {
|
|
34
|
+
return 'rate_limited';
|
|
35
|
+
}
|
|
36
|
+
if (status === 401 || status === 403) {
|
|
37
|
+
return 'invalid_client_token';
|
|
38
|
+
}
|
|
39
|
+
if (status >= 500) {
|
|
40
|
+
return 'provider_error';
|
|
41
|
+
}
|
|
42
|
+
if (status >= 400) {
|
|
43
|
+
return 'request_invalid';
|
|
44
|
+
}
|
|
45
|
+
return 'network_error';
|
|
46
|
+
}
|
|
47
|
+
async post(path, body, options = {}) {
|
|
48
|
+
const url = options.merchantToken
|
|
49
|
+
? `${this.baseUrl}/${encodeURIComponent(options.merchantToken)}${path}`
|
|
50
|
+
: `${this.baseUrl}${path}`;
|
|
51
|
+
const headers = {
|
|
52
|
+
'Content-Type': 'application/json',
|
|
53
|
+
...(this.extraHeaders || {}),
|
|
54
|
+
};
|
|
55
|
+
if (this.origin) {
|
|
56
|
+
// Recommended by the gateway; note that some fetch runtimes treat
|
|
57
|
+
// Origin as a forbidden header and ignore it — harmless.
|
|
58
|
+
headers['Origin'] = this.origin;
|
|
59
|
+
}
|
|
60
|
+
let response;
|
|
61
|
+
try {
|
|
62
|
+
response = await fetch(url, {
|
|
63
|
+
method: 'POST',
|
|
64
|
+
headers,
|
|
65
|
+
body: JSON.stringify(body),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
catch (cause) {
|
|
69
|
+
// COM-08: technical failure -> typed error, never a payment result.
|
|
70
|
+
throw new errors_1.PaError('network_error', {
|
|
71
|
+
developerMessage: `Network error calling ${path}.`,
|
|
72
|
+
cause,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
if (response.status !== HTTP_OK) {
|
|
76
|
+
throw new errors_1.PaError(ApiClient.codeForHttpStatus(response.status), {
|
|
77
|
+
developerMessage: `Gateway returned HTTP ${response.status} for ${path}.`,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
let envelope;
|
|
81
|
+
try {
|
|
82
|
+
envelope = (await response.json());
|
|
83
|
+
}
|
|
84
|
+
catch (cause) {
|
|
85
|
+
throw new errors_1.PaError('provider_error', {
|
|
86
|
+
developerMessage: `Gateway returned a non-JSON body for ${path}.`,
|
|
87
|
+
cause,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const responseCode = envelope.response && typeof envelope.response.code === 'number' ? envelope.response.code : HTTP_OK;
|
|
91
|
+
if (responseCode !== HTTP_OK) {
|
|
92
|
+
// Business error riding HTTP 200 — map to the shared table (COM-24).
|
|
93
|
+
const payload = (envelope.payload || {});
|
|
94
|
+
const code = payload.error_code && ERROR_TABLE_HAS(payload.error_code)
|
|
95
|
+
? payload.error_code
|
|
96
|
+
: ApiClient.codeForHttpStatus(responseCode);
|
|
97
|
+
throw new errors_1.PaError(code, {
|
|
98
|
+
developerMessage: payload.error_message || (envelope.response && envelope.response.message) || undefined,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return envelope.payload;
|
|
102
|
+
}
|
|
103
|
+
/** POST /{merchantToken}/element-sessions — public app session (publishable
|
|
104
|
+
* credential is the merchantToken, never a secret key; COM-01). */
|
|
105
|
+
createElementSession(merchantToken) {
|
|
106
|
+
return this.post('/element-sessions', {
|
|
107
|
+
origin: this.origin,
|
|
108
|
+
element_type: 'card',
|
|
109
|
+
}, { merchantToken });
|
|
110
|
+
}
|
|
111
|
+
/** POST /tokenize — card data goes straight from the app to PA (COM-02). */
|
|
112
|
+
tokenize(params) {
|
|
113
|
+
return this.post('/tokenize', {
|
|
114
|
+
session_token: params.sessionToken,
|
|
115
|
+
card_number: params.cardNumber,
|
|
116
|
+
card_expiry_month: String(params.expiryMonth).padStart(2, '0'),
|
|
117
|
+
card_expiry_year: String(params.expiryYear),
|
|
118
|
+
card_cvv: params.cvc,
|
|
119
|
+
card_holder_name: params.cardHolderName,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/** POST /confirm — one confirm per attempt; while status is
|
|
123
|
+
* requires_action / processing the app keeps polling (see poll.ts). */
|
|
124
|
+
confirm(params) {
|
|
125
|
+
const body = {
|
|
126
|
+
client_token: params.clientToken,
|
|
127
|
+
intent_token: params.intentToken,
|
|
128
|
+
payment_method: 'card',
|
|
129
|
+
browser_info: {
|
|
130
|
+
channel: 'app',
|
|
131
|
+
framework: 'react-native',
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
if (params.paToken) {
|
|
135
|
+
body.pa_token = params.paToken;
|
|
136
|
+
}
|
|
137
|
+
if (params.idempotencyKey) {
|
|
138
|
+
body.idempotency_key = params.idempotencyKey; // COM-12 / RN-30
|
|
139
|
+
}
|
|
140
|
+
if (params.saveCard != null) {
|
|
141
|
+
body.save_card = params.saveCard; // COM-14 / RN-23
|
|
142
|
+
}
|
|
143
|
+
if (params.billing) {
|
|
144
|
+
body.billing = params.billing; // COM-23 pass-through
|
|
145
|
+
}
|
|
146
|
+
return this.post('/confirm', body);
|
|
147
|
+
}
|
|
148
|
+
/** POST /intent-cancel. */
|
|
149
|
+
intentCancel(params) {
|
|
150
|
+
return this.post('/intent-cancel', {
|
|
151
|
+
client_token: params.clientToken,
|
|
152
|
+
intent_token: params.intentToken,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/** POST /intent-status — polled until a terminal status or budget end. */
|
|
156
|
+
intentStatus(params) {
|
|
157
|
+
return this.post('/intent-status', {
|
|
158
|
+
client_token: params.clientToken,
|
|
159
|
+
intent_token: params.intentToken,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
exports.ApiClient = ApiClient;
|
package/lib/confirm.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Confirm orchestration (RN-14 / RN-16 / COM-05..COM-08 / COM-12).
|
|
3
|
+
*
|
|
4
|
+
* Both mandatory forms are exported:
|
|
5
|
+
* - confirmWithToken() — the two-step flow: tokenizeCard() first, then
|
|
6
|
+
* confirm with the pa_token (RN-14 "the two-step form must exist").
|
|
7
|
+
* - confirmWithCard() — combined convenience call: validates, tokenizes
|
|
8
|
+
* and confirms in one await.
|
|
9
|
+
*
|
|
10
|
+
* Exactly one final result per confirm (COM-05): any 3DS runs inside the
|
|
11
|
+
* call (SDK-presented WebView, RN-19); requires_action / processing are
|
|
12
|
+
* polled until terminal; when the poll budget is exhausted the result is
|
|
13
|
+
* `processing`, never `failed` (COM-06).
|
|
14
|
+
*/
|
|
15
|
+
import { PaError } from './errors';
|
|
16
|
+
import type { BillingDetails, CardInput, PaymentResult } from './types';
|
|
17
|
+
export interface ConfirmBaseOptions {
|
|
18
|
+
intentToken: string;
|
|
19
|
+
clientToken: string;
|
|
20
|
+
/** Market key selecting the merchant account when it is not the default (COM-40). */
|
|
21
|
+
market?: string;
|
|
22
|
+
/** Flow-supplied idempotency key — repeated taps never double charge (COM-12 / RN-30). */
|
|
23
|
+
idempotencyKey?: string;
|
|
24
|
+
/** Save the card during payment (COM-14 / RN-23). */
|
|
25
|
+
saveCard?: boolean;
|
|
26
|
+
/** Optional pass-through billing details (COM-22 / COM-23 / RN-15). */
|
|
27
|
+
billing?: BillingDetails;
|
|
28
|
+
/** Fires immediately before the 3DS challenge appears (RN-19) — dismiss
|
|
29
|
+
* your loading overlay here. */
|
|
30
|
+
onThreeDsWillPresent?: () => void;
|
|
31
|
+
/** Fires when the 3DS challenge UI is gone (informational, WEB-48 analog). */
|
|
32
|
+
onThreeDsEnded?: () => void;
|
|
33
|
+
}
|
|
34
|
+
export interface ConfirmWithTokenOptions extends ConfirmBaseOptions {
|
|
35
|
+
/** Token from tokenizeCard() (RN-14). */
|
|
36
|
+
paToken: string;
|
|
37
|
+
}
|
|
38
|
+
export interface ConfirmWithCardOptions extends ConfirmBaseOptions {
|
|
39
|
+
/** Card from Flow's own UI (RN-13). */
|
|
40
|
+
card: CardInput;
|
|
41
|
+
/** Optional allowed-brands filter (RN-12); rejects other brands with
|
|
42
|
+
* `unsupported_brand` before any network call. */
|
|
43
|
+
allowedBrands?: readonly string[];
|
|
44
|
+
}
|
|
45
|
+
export declare class AlreadyFinalError extends PaError {
|
|
46
|
+
readonly result: PaymentResult;
|
|
47
|
+
constructor(result: PaymentResult);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Confirm a payment intent with a tokenized card (RN-14, mandatory two-step:
|
|
51
|
+
* tokenizeCard() -> confirmWithToken()).
|
|
52
|
+
*/
|
|
53
|
+
export declare function confirmWithToken(options: ConfirmWithTokenOptions): Promise<PaymentResult>;
|
|
54
|
+
/**
|
|
55
|
+
* Combined confirm: validate + tokenize + confirm in one call (RN-14,
|
|
56
|
+
* "a single combined confirm with card details is welcome in addition").
|
|
57
|
+
* Throws the COM-26 field codes before any network call when validation fails.
|
|
58
|
+
*/
|
|
59
|
+
export declare function confirmWithCard(options: ConfirmWithCardOptions): Promise<PaymentResult>;
|
|
60
|
+
/** Cancel an intent explicitly (A3 `cancelled` via intent-cancel). */
|
|
61
|
+
export declare function cancelIntent(options: {
|
|
62
|
+
intentToken: string;
|
|
63
|
+
clientToken: string;
|
|
64
|
+
}): Promise<PaymentResult>;
|
|
65
|
+
/** Test/diagnostic helper: clear the in-process final-result cache. */
|
|
66
|
+
export declare function resetConfirmState(): void;
|