zg-payment-sdk 0.0.0-stage → 0.1.1
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/README.md +65 -2
- package/package.json +26 -3
- package/src/index.ts +53 -0
- package/src/providers/adyen-method.ts +8 -0
- package/src/providers/adyen.ts +20 -0
- package/src/providers/airwallex.ts +20 -0
- package/src/types.ts +48 -0
package/README.md
CHANGED
|
@@ -1,3 +1,66 @@
|
|
|
1
|
-
#
|
|
1
|
+
# zg-payment-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
按支付方式选择渠道的公共入口。支持 `apple-pay`、`google-pay`、`card`,以及 Airwallex 的 `drop-in`。每种方式的渠道独立决定,不需要按渠道渲染整套收银台。
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { createPayment, type PaymentMethodProviders } from 'zg-payment-sdk';
|
|
7
|
+
|
|
8
|
+
const providers = {
|
|
9
|
+
'apple-pay': 'adyen',
|
|
10
|
+
'google-pay': 'airwallex',
|
|
11
|
+
card: 'adyen',
|
|
12
|
+
} satisfies PaymentMethodProviders;
|
|
13
|
+
|
|
14
|
+
const apple = await createPayment({
|
|
15
|
+
provider: providers['apple-pay'],
|
|
16
|
+
method: 'apple-pay',
|
|
17
|
+
core: adyenCore, // 复用业务已有的 AdyenCore
|
|
18
|
+
});
|
|
19
|
+
await apple.mount(appleContainer, appleOptions);
|
|
20
|
+
|
|
21
|
+
const google = await createPayment({
|
|
22
|
+
provider: 'airwallex',
|
|
23
|
+
method: 'google-pay',
|
|
24
|
+
configuration: { env: 'demo' },
|
|
25
|
+
});
|
|
26
|
+
await google.mount(googleContainer, {
|
|
27
|
+
intent_id: order.intent_id,
|
|
28
|
+
client_secret: order.client_secret,
|
|
29
|
+
amount: { value: 9.99, currency: order.currency },
|
|
30
|
+
countryCode: 'US',
|
|
31
|
+
}, {
|
|
32
|
+
onSuccess: () => { /* 查询后端订单结果 */ },
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// 切换商品:查询新订单的真实金额后更新,保留同一个按钮实例。
|
|
36
|
+
google.native.update({
|
|
37
|
+
intent_id: nextOrder.intent_id,
|
|
38
|
+
client_secret: nextOrder.client_secret,
|
|
39
|
+
amount: { value: 12.99, currency: nextOrder.currency },
|
|
40
|
+
customer_id: undefined,
|
|
41
|
+
payment_consent: undefined,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// 关闭收银台时销毁;不要在每次 SKU 变化时重新 create / mount。
|
|
45
|
+
apple.destroy();
|
|
46
|
+
google.destroy();
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
实际代码按渠道分支调用 `createPayment`,TypeScript 会推断对应的配置、挂载参数和 `native` 类型。渠道参数和回调保留原有类型;公共包不转换金额单位,不创建业务订单、不决定订阅授权,也不替代服务端确认支付结果。
|
|
50
|
+
|
|
51
|
+
Adyen 可传已有 `core`,或传 `configuration` 和 Advanced Flow 的 `submitOrder`。同一业务收银台建议共享 core,沿用现有下单、3DS 和金额更新流程。Airwallex 同样可传 `core` 或 `configuration`,其 core 仍按页面共享环境及语言配置。`native` 保留渠道扩展能力:Adyen 卡使用 `native.submit()`,Airwallex 单行卡使用 `native.confirm()`,完整卡表单使用 `drop-in` 并传 `methods: ['card']`。
|
|
52
|
+
|
|
53
|
+
此包在 `createPayment` 被调用后动态加载选中的渠道。Adyen 继续按 `apple`、`google`、`cards` 子入口拆分,提供已有 `core` 时不会额外加载 Core 入口;Airwallex 官方支付组件共用一个 Payments 包。渠道包通过 peer dependency 复用业务已安装的版本,需安装 `zg-adyen-sdk@^1.0.1` 和 `zg-airwallex-sdk@^0.1.1`;未选中的渠道不会因导入公共入口而执行或初始化。TS 源码使用方需把这三个包加入构建工具的转译配置。
|
|
54
|
+
|
|
55
|
+
Airwallex 更新订单时,应同时更新 intent、凭证和服务端确认的金额;从订阅切换至单次购买时清除 `customer_id`、`payment_consent`。新订单准备期间保持按钮禁用;忽略旧请求的迟到结果。`update` 沿用官方 [Apple Pay](https://www.airwallex.com/docs/js/payments/applepaybutton) / [Google Pay](https://www.airwallex.com/docs/js/payments/googlepaybutton) Element API。
|
|
56
|
+
|
|
57
|
+
验证:仓库根目录运行 `pnpm --filter zg-payment-sdk test`、`pnpm -r typecheck`。测试使用 mock 渠道组件,不发起真实付款。
|
|
58
|
+
|
|
59
|
+
用户分层确定路由后,可提前预热会展示的渠道和支付方式:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { preloadPayment } from 'zg-payment-sdk';
|
|
63
|
+
await preloadPayment({ provider: 'adyen', method: 'apple-pay' });
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`preloadPayment` 只下载模块,不初始化 Core、挂载组件或创建订单。预热失败不应阻止业务策略就绪,实际挂载时仍可重试。宿主应过滤当前设备不可用的钱包;Adyen 样式可在选中 Adyen 后通过 `import('zg-adyen-sdk/styles')` 加载。不要在页面公共模块中从渠道总入口导入运行时类或错误处理函数:文案使用 `zg-adyen-sdk/i18n`,拒付分类使用 `zg-adyen-sdk/refusal-reason`(1.0.3+),类型使用 `import type`。
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zg-payment-sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
|
+
"types": "./src/index.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.ts"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"zg-adyen-sdk": "^1.0.1",
|
|
16
|
+
"zg-airwallex-sdk": "^0.1.1"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"typescript": "~5.6.3",
|
|
20
|
+
"vitest": "^2.1.9",
|
|
21
|
+
"zg-adyen-sdk": "1.0.3",
|
|
22
|
+
"zg-airwallex-sdk": "0.1.7"
|
|
23
|
+
},
|
|
24
|
+
"license": "ISC",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"test": "vitest run"
|
|
28
|
+
}
|
|
6
29
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AdyenPayment, AdyenSetup, AirwallexMethod, AirwallexPayment, AirwallexSetup, PaymentMethod,
|
|
3
|
+
} from "./types";
|
|
4
|
+
|
|
5
|
+
export type * from "./types";
|
|
6
|
+
|
|
7
|
+
/** 仅预热代码,不创建 Core、支付组件或订单。 */
|
|
8
|
+
export async function preloadPayment(options: { provider: "adyen" | "airwallex"; method: PaymentMethod }): Promise<void> {
|
|
9
|
+
if (!["apple-pay", "google-pay", "card"].includes(options.method)) {
|
|
10
|
+
throw new Error(`[PaymentSDK] 不支持支付方式:${options.method}`);
|
|
11
|
+
}
|
|
12
|
+
if (options.provider === "adyen") {
|
|
13
|
+
const { loadAdyenMethod } = await import("./providers/adyen-method");
|
|
14
|
+
await Promise.all([import("zg-adyen-sdk/core"), loadAdyenMethod(options.method)]);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (options.provider === "airwallex") {
|
|
18
|
+
// 官方 Payments Elements 共用一个包,没有按钱包 / 卡拆分的加载入口。
|
|
19
|
+
await import("zg-airwallex-sdk");
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
throw new Error("[PaymentSDK] 不支持的支付渠道。");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function createPayment<M extends PaymentMethod>(
|
|
26
|
+
options: { provider: "adyen"; method: M } & AdyenSetup,
|
|
27
|
+
): Promise<AdyenPayment<M>>;
|
|
28
|
+
export function createPayment<M extends AirwallexMethod>(
|
|
29
|
+
options: { provider: "airwallex"; method: M } & AirwallexSetup,
|
|
30
|
+
): Promise<AirwallexPayment<M>>;
|
|
31
|
+
export async function createPayment(options:
|
|
32
|
+
| ({ provider: "adyen"; method: PaymentMethod } & AdyenSetup)
|
|
33
|
+
| ({ provider: "airwallex"; method: AirwallexMethod } & AirwallexSetup),
|
|
34
|
+
): Promise<AdyenPayment | AirwallexPayment> {
|
|
35
|
+
// 此入口没有任何渠道的运行时依赖;未选择的渠道不会执行或初始化。
|
|
36
|
+
if (options.provider === "adyen") {
|
|
37
|
+
if (!["apple-pay", "google-pay", "card"].includes(options.method)) {
|
|
38
|
+
throw new Error(`[PaymentSDK] Adyen 不支持支付方式:${options.method}`);
|
|
39
|
+
}
|
|
40
|
+
if (!options.core && !options.configuration) throw new Error("[PaymentSDK] 请提供 Adyen core 或 configuration。");
|
|
41
|
+
const { createAdyenPayment } = await import("./providers/adyen");
|
|
42
|
+
return createAdyenPayment(options);
|
|
43
|
+
}
|
|
44
|
+
if (options.provider === "airwallex") {
|
|
45
|
+
if (!["apple-pay", "google-pay", "card", "drop-in"].includes(options.method)) {
|
|
46
|
+
throw new Error(`[PaymentSDK] Airwallex 不支持支付方式:${options.method}`);
|
|
47
|
+
}
|
|
48
|
+
if (!options.core && !options.configuration) throw new Error("[PaymentSDK] 请提供 Airwallex core 或 configuration。");
|
|
49
|
+
const { createAirwallexPayment } = await import("./providers/airwallex");
|
|
50
|
+
return createAirwallexPayment(options);
|
|
51
|
+
}
|
|
52
|
+
throw new Error("[PaymentSDK] 不支持的支付渠道。");
|
|
53
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { PaymentMethod } from "../types";
|
|
2
|
+
|
|
3
|
+
/** 独立异步边界,避免某个钱包把同渠道的其他支付组件一起加载。 */
|
|
4
|
+
export async function loadAdyenMethod(method: PaymentMethod) {
|
|
5
|
+
if (method === "apple-pay") return (await import("zg-adyen-sdk/apple")).AdyenApple;
|
|
6
|
+
if (method === "google-pay") return (await import("zg-adyen-sdk/google")).AdyenGoogle;
|
|
7
|
+
return (await import("zg-adyen-sdk/cards")).AdyenCard;
|
|
8
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { loadAdyenMethod } from "./adyen-method";
|
|
2
|
+
import type { AdyenPayment, AdyenSetup, PaymentMethod } from "../types";
|
|
3
|
+
|
|
4
|
+
export async function createAdyenPayment(
|
|
5
|
+
options: { method: PaymentMethod } & AdyenSetup,
|
|
6
|
+
): Promise<AdyenPayment> {
|
|
7
|
+
const [core, Component] = await Promise.all([
|
|
8
|
+
options.core ?? import("zg-adyen-sdk/core").then(({ AdyenCore }) =>
|
|
9
|
+
AdyenCore.create(options.configuration!, options.submitOrder)),
|
|
10
|
+
loadAdyenMethod(options.method),
|
|
11
|
+
]);
|
|
12
|
+
const native = new Component(core);
|
|
13
|
+
return {
|
|
14
|
+
provider: "adyen",
|
|
15
|
+
method: options.method,
|
|
16
|
+
native,
|
|
17
|
+
mount: native.render.bind(native),
|
|
18
|
+
destroy: () => native.destroy(),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AirwallexApplePay, AirwallexGooglePay, AirwallexCard, AirwallexDropIn, AirwallexCore,
|
|
3
|
+
} from "zg-airwallex-sdk";
|
|
4
|
+
import type { AirwallexMethod, AirwallexPayment, AirwallexSetup } from "../types";
|
|
5
|
+
|
|
6
|
+
export async function createAirwallexPayment(
|
|
7
|
+
options: { method: AirwallexMethod } & AirwallexSetup,
|
|
8
|
+
): Promise<AirwallexPayment> {
|
|
9
|
+
const core = options.core ?? await AirwallexCore.create(options.configuration!);
|
|
10
|
+
const native = options.method === "apple-pay" ? new AirwallexApplePay(core)
|
|
11
|
+
: options.method === "google-pay" ? new AirwallexGooglePay(core)
|
|
12
|
+
: options.method === "card" ? new AirwallexCard(core) : new AirwallexDropIn(core);
|
|
13
|
+
return {
|
|
14
|
+
provider: "airwallex",
|
|
15
|
+
method: options.method,
|
|
16
|
+
native,
|
|
17
|
+
mount: native.mount.bind(native),
|
|
18
|
+
destroy: () => native.destroy(),
|
|
19
|
+
};
|
|
20
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AdyenApple, AdyenGoogle, AdyenCard, AdyenCore, AdyenCoreConfiguration, AdyenSubmitOrder,
|
|
3
|
+
} from "zg-adyen-sdk";
|
|
4
|
+
import type {
|
|
5
|
+
AirwallexApplePay, AirwallexGooglePay, AirwallexCard, AirwallexDropIn,
|
|
6
|
+
AirwallexCore, AirwallexCoreConfiguration,
|
|
7
|
+
} from "zg-airwallex-sdk";
|
|
8
|
+
|
|
9
|
+
export type PaymentProvider = "adyen" | "airwallex";
|
|
10
|
+
export type PaymentMethod = "apple-pay" | "google-pay" | "card";
|
|
11
|
+
/** 每种支付方式只有一个渠道;渠道可任意组合。 */
|
|
12
|
+
export type PaymentMethodProviders = Record<PaymentMethod, PaymentProvider>;
|
|
13
|
+
|
|
14
|
+
export type AdyenSetup =
|
|
15
|
+
| { core: AdyenCore; configuration?: never; submitOrder?: never }
|
|
16
|
+
| { core?: never; configuration: AdyenCoreConfiguration; submitOrder?: AdyenSubmitOrder };
|
|
17
|
+
export type AirwallexSetup =
|
|
18
|
+
| { core: AirwallexCore; configuration?: never }
|
|
19
|
+
| { core?: never; configuration: AirwallexCoreConfiguration };
|
|
20
|
+
|
|
21
|
+
export type AdyenNativeMethods = {
|
|
22
|
+
"apple-pay": AdyenApple;
|
|
23
|
+
"google-pay": AdyenGoogle;
|
|
24
|
+
card: AdyenCard;
|
|
25
|
+
};
|
|
26
|
+
export type AirwallexNativeMethods = {
|
|
27
|
+
"apple-pay": AirwallexApplePay;
|
|
28
|
+
"google-pay": AirwallexGooglePay;
|
|
29
|
+
card: AirwallexCard;
|
|
30
|
+
"drop-in": AirwallexDropIn;
|
|
31
|
+
};
|
|
32
|
+
export type AirwallexMethod = keyof AirwallexNativeMethods;
|
|
33
|
+
|
|
34
|
+
/** 统一创建、挂载、销毁;渠道特有的配置和事件保持原生类型。 */
|
|
35
|
+
export type AdyenPayment<M extends PaymentMethod = PaymentMethod> = {
|
|
36
|
+
provider: "adyen";
|
|
37
|
+
method: M;
|
|
38
|
+
native: AdyenNativeMethods[M];
|
|
39
|
+
mount: AdyenNativeMethods[M]["render"];
|
|
40
|
+
destroy: () => void;
|
|
41
|
+
};
|
|
42
|
+
export type AirwallexPayment<M extends AirwallexMethod = AirwallexMethod> = {
|
|
43
|
+
provider: "airwallex";
|
|
44
|
+
method: M;
|
|
45
|
+
native: AirwallexNativeMethods[M];
|
|
46
|
+
mount: AirwallexNativeMethods[M]["mount"];
|
|
47
|
+
destroy: () => void;
|
|
48
|
+
};
|