网络 SDK
安装
选项 1:NPM
npm install @yuno-payments/sdk-webimport { Yuno } from '@yuno-payments/sdk-web';选项 2:CDN
<script src="https://sdk-web.y.uno/v1.5/main.js"></script>选项3:动态负载
const script = document.createElement('script');
script.src = 'https://sdk-web.y.uno/v1.5/main.js';
document.head.appendChild(script);
支持 TypeScript安装类型:
npm install @yuno-payments/sdk-web-types
基本支付流程
1.Initialize
const yuno = await Yuno.initialize(public-api-key);2. 创建结算会话(后端)
// Your backend endpoint
const session = await fetch('/api/checkout/session', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
customer_id: 'cus_123',
amount: { currency: 'USD', value: 2500 },
country: 'US'
})
}).then(r => r.json());3. 配置结账流程
yuno.startCheckout({
checkoutSession: session.checkout_session,
elementSelector: '#payment-container',
countryCode: 'US',
language: 'en-US',
async yunoCreatePayment(oneTimeToken) {
const result = await fetch('/api/payment/create', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
one_time_token: oneTimeToken,
checkout_session: session.checkout_session
})
}).then(r => r.json());
yuno.continuePayment({ showPaymentStatus: true });
},
yunoPaymentResult: (data) => {
console.log('Payment completed:', data.status);
}
});4. 付款单据
yunomountCheckout();5. 启动支付流程
添加支付按钮并触发支付流程:
const payButton = document.querySelector('#pay-button');
payButton.addEventListener('click', () => {
yuno.startPayment();
});6. 添加HTML
<div id="payment-container"></div>
<button id="pay-button">Pay Now</button>完整工作示例
<!DOCTYPE html>
<html>
<head>
<title>Yuno Payment</title>
<script src="https://sdk-web.y.uno/v1.5/main.js"></script>
</head>
<body>
<div id="payment-container"></div>
<button id="pay-button">Pay Now</button>
<script>
async function initPayment() {
// Initialize
const yuno = await Yuno.initialize('pk_test_123');
// Create session
const session = await fetch('/api/checkout/session', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
customer_id: 'cus_123',
amount: { currency: 'USD', value: 2500 },
country: 'US'
})
}).then(r => r.json());
// Configure checkout
yuno.startCheckout({
checkoutSession: session.checkout_session,
elementSelector: '#payment-container',
countryCode: 'US',
async yunoCreatePayment(token) {
await fetch('/api/payment/create', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
one_time_token: token,
checkout_session: session.checkout_session
})
});
yuno.continuePayment();
},
yunoPaymentResult: (data) => {
if (data.status === 'SUCCEEDED') {
window.location.href = '/success';
}
}
});
// Mount form
yuno.mountCheckout();
// Start payment on button click
document.querySelector('#pay-button').addEventListener('click', () => {
yuno.startPayment();
});
}
initPayment();
</script>
</body>
</html>处理支付结果
yuno.startCheckout({
// ... other config
yunoPaymentResult: (data) => {
switch(data.status) {
case 'SUCCEEDED':
window.location.href = '/success';
break;
case 'FAILED':
alert('Payment failed: ' + data.error?.message);
break;
case 'PENDING':
console.log('Payment is being processed');
break;
case 'REJECTED':
alert('Payment was rejected');
break;
}
},
yunoError: (error) => {
console.error('Error:', error);
}
});3DS认证
3DS验证由SDK自动处理。对于异步支付方式:
async yunoCreatePayment(token) {
await createPaymentOnBackend(token);
// Handle redirect if needed
const result = await yuno.continuePayment();
if (result?.action === 'REDIRECT_URL') {
window.location.href = result.redirect.init_url;
}
}配置选项
基本参数
| 参数 | 类型 | 说明 |
|---|---|---|
checkoutSession | 字符串 | 后端会话ID |
elementSelector | 字符串 | 用于容器的CSS选择器 |
countryCode | 字符串 | ISO国家代码(例如'US') |
language | 字符串 | 语言代码(例如 'en-US') |
yunoCreatePayment | 函数 | 支付创建回调 |
卡片配置
yuno.startCheckout({
// ... other config
card: {
type: 'extends', // or 'only'
cardSaveEnable: true, // Show save checkbox
isCreditCardProcessingOnly: false, // Allow debit
onChange: ({ error, data }) => {
if (error) {
console.log('Card validation error:', error);
}
}
}
});加载器与状态
yuno.startCheckout({
// ... other config
showLoading: true, // Show Yuno's loader
showPaymentStatus: true, // Show payment result page
onLoading: (isLoading) => {
console.log('Loading state:', isLoading);
}
});下一步行动
准备探索更多高级功能了吗?查看《高级功能指南》了解:
- 替代安装方案 -
mountCheckoutLite()和mountSeamlessCheckout()用于自定义支付方式选择 - 注册(保存卡片)——保存支付方式以备将来使用
- 拱顶Token ——一键支付,保存卡片
- 自定义用户界面(无头集成)——构建完全定制的支付表单
- 安全字段- 符合PCI标准的自定义卡片表单
- 样式与定制- 使SDK契合您的品牌
- 高级配置- 动态视图、渲染模式及其他
另见:
- 代码示例- 常见场景的可复制粘贴示例
1 天前已更新