JavaScript/TypeScript 浏览器 SDK(Hosted Checkout)
浏览器 Public Key Token 模式已停用。商户服务端使用 API Key 创建 Hosted Checkout,浏览器只打开不透明 checkout_url。
为什么需要迁移
Public Key, Origin and Referer are visible or spoofable and cannot authenticate order creation. The server now rejects /api/v1/sdk/token and public_key checkout mode with PUBLIC_KEY_BROWSER_FLOW_DISABLED.
Install
pnpm add @polypay/sdk1. 商户服务端创建 Checkout
// Merchant server only. Never expose the API Key to browser code.
const response = await fetch('https://api.polypay.ai/api/v1/pay/order/checkout', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': process.env.POLYPAY_API_KEY!
},
body: JSON.stringify({
amount: 100,
mch_order_id: 'ORDER_123456',
notify_url: 'https://your-site.com/webhook',
redirect_url: 'https://your-site.com/success'
})
});
const { data } = await response.json();
return { checkoutUrl: data.checkout_url };2. 浏览器跳转
import { PolyPayCheckout } from '@polypay/sdk/browser';
const { checkoutUrl } = await fetch('/api/create-polypay-checkout', {
method: 'POST'
}).then((response) => response.json());
new PolyPayCheckout().redirect(checkoutUrl);迁移检查
- Remove browser-side public_key, timestamp and signature URL generation.
- Remove PolyPayClient Session Token and order calls.
- Keep the API Key only in private merchant-server configuration.
- Set amount, order ID and callback URLs on the merchant server.