@stackonward/onex-checkout 0.0.0-stage → 0.2.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 +98 -2
- package/dist/index.d.mts +939 -0
- package/dist/index.d.ts +939 -0
- package/dist/index.mjs +462 -0
- package/package.json +40 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 StackOnward
|
|
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,99 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @stackonward/onex-checkout
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Browser-facing OneX buyer checkout client for a product's same-origin backend. Install with `pnpm add @stackonward/onex-checkout`.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { createCheckoutClient } from "@stackonward/onex-checkout";
|
|
7
|
+
|
|
8
|
+
const checkout = createCheckoutClient({
|
|
9
|
+
transport: api,
|
|
10
|
+
basePath: "/api/billing/checkout",
|
|
11
|
+
mutationHeaders: () => ({ "X-CSRF-Token": readCsrfToken() }),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const { groups, session_revision } = await checkout.methods(sessionNo, signal);
|
|
15
|
+
const choice = groups
|
|
16
|
+
.flatMap((group) => group.methods)
|
|
17
|
+
.find((method) => method.method_binding === selectedBinding);
|
|
18
|
+
if (choice) {
|
|
19
|
+
const execution = await checkout.start(
|
|
20
|
+
sessionNo,
|
|
21
|
+
{
|
|
22
|
+
session_revision,
|
|
23
|
+
method_reference: choice.method_reference,
|
|
24
|
+
},
|
|
25
|
+
operationKey,
|
|
26
|
+
signal,
|
|
27
|
+
);
|
|
28
|
+
checkout.assertMethod(choice, execution);
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Use the application's authenticated `ApiClient` transport. Each explicit start, cancel, and payment-free completion takes an idempotency key. Reuse that key when retrying the same operation with the same input. Pass a request-scoped `AbortSignal` to stop local requests when a view unmounts. Only an explicit `cancel()` requests cancellation of an execution.
|
|
33
|
+
|
|
34
|
+
| Method | Buyer endpoint relative to the supplied base path |
|
|
35
|
+
| ---------------------------------------------- | ------------------------------------------------- |
|
|
36
|
+
| `session(no, signal?)` | GET `/:no/state` |
|
|
37
|
+
| `methods(no, signal?)` | GET `/:no/payment-methods` |
|
|
38
|
+
| `start(no, input, key, signal?)` | POST `/:no/executions` |
|
|
39
|
+
| `execution(no, executionNo, signal?)` | GET `/:no/executions/:executionNo` |
|
|
40
|
+
| `reconcile(no, executionNo, signal?)` | POST `/:no/executions/:executionNo/reconcile` |
|
|
41
|
+
| `cancel(no, executionNo, key, signal?)` | POST `/:no/executions/:executionNo/cancel` |
|
|
42
|
+
| `complete(no, expectedRevision, key, signal?)` | POST `/:no/complete` |
|
|
43
|
+
|
|
44
|
+
Session state exposes authoritative purchase options, available actions, and the active execution number. Match the selected method and purchase option by ID. Offer payment-free completion when the session allows that action and the specific option has `complete_without_payment: true`. Amounts are decimal strings. Renewal terms, when available, give the amount and interval for subsequent charges. Session creation and the commercial result are product operations.
|
|
45
|
+
|
|
46
|
+
Restore an active attempt with `execution()`. The optional presentation carries an execution-scoped client secret. Keep it in memory, outside logs, analytics, or persisted state. A null presentation is a valid execution response. Consult the execution status and session expiration before mounting an official provider component. Server-side reconciliation and the product's business result determine fulfillment.
|
|
47
|
+
|
|
48
|
+
The built-in decoder supports `embedded/airwallex_dropin@2`. All three methods require an execution-bound payment intent and its short-lived secret. A standalone authorization also requires a zero amount, customer, and scheduled consent. Wallets require the merchant country. The product backend negotiates supported flows with OneX. An additional `PresentationDecoder` can be supplied in `presentations` when another embedded flow is negotiated. The decoder owns its payload validation. Unknown flow/version pairs fail with `CheckoutProtocolError`. Transport failures retain the application's original error.
|
|
49
|
+
|
|
50
|
+
A payment integration registers its own `PresentationDecoder` for one exact flow and version in `createCheckoutClient({ presentations })`. Calling `client.assertMethod(method, execution)` applies the shared binding check and the selected adapter’s checks using that registration. The built-in `airwallex_dropin@2` decoder, its three supported `airwallexMethods`, and `airwallexSession(execution)` live together. The product selects a renderer by flow, version and method, and passes localized labels and renewal terms. Each client captures its own list of adapters when created. A client can register several flows or versions and TypeScript derives the result from that list. The original seven-operation `CheckoutClient` interface remains available to consumers that implement their own transport facade.
|
|
51
|
+
|
|
52
|
+
## Integrating an embedded flow
|
|
53
|
+
|
|
54
|
+
The decoder is a plain TypeScript object. A storefront can keep its decoder in its own package and use the SDK through the published entry point. This example consumes OneX’s WeChat Native presentation after its backend has negotiated that flow.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import {
|
|
58
|
+
CheckoutProtocolError,
|
|
59
|
+
createCheckoutClient,
|
|
60
|
+
type CheckoutClientOptions,
|
|
61
|
+
type PresentationDecoder,
|
|
62
|
+
} from "@stackonward/onex-checkout";
|
|
63
|
+
|
|
64
|
+
type NativeQr = {
|
|
65
|
+
mode: "embedded";
|
|
66
|
+
flow: "wechatpay_native";
|
|
67
|
+
version: 1;
|
|
68
|
+
payload: { code_url: string };
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const nativeQr: PresentationDecoder<NativeQr> = {
|
|
72
|
+
flow: "wechatpay_native",
|
|
73
|
+
version: 1,
|
|
74
|
+
decode(payload) {
|
|
75
|
+
if (
|
|
76
|
+
!payload ||
|
|
77
|
+
typeof payload !== "object" ||
|
|
78
|
+
!("code_url" in payload) ||
|
|
79
|
+
typeof payload.code_url !== "string" ||
|
|
80
|
+
!URL.canParse(payload.code_url) ||
|
|
81
|
+
new URL(payload.code_url).protocol !== "weixin:"
|
|
82
|
+
) {
|
|
83
|
+
throw new CheckoutProtocolError("invalid_response", "WeChat Native code URL");
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
mode: "embedded",
|
|
87
|
+
flow: "wechatpay_native",
|
|
88
|
+
version: 1,
|
|
89
|
+
payload: { code_url: payload.code_url },
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export function createStorefrontCheckout(options: CheckoutClientOptions) {
|
|
95
|
+
return createCheckoutClient({ ...options, presentations: [nativeQr] });
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Render the decoded URL as a QR code. After the buyer scans it, read or reconcile the original execution. Another storefront can register its own decoders using the same public API. Applications with their own payment backend can import the card or wallet controls directly from `@stackonward/airwallex-payments`. Those controls accept a server-issued Airwallex session and DOM containers.
|