支付功能
显示支付页面 minigameApi.showPayment(options)
事件监听方式(传统):
// 使用事件监听器显示支付页面
minigameApi.on('payment:success', (data) => {
console.log('支付成功:', data);
});
minigameApi.on('payment:failed', (error) => {
console.log('支付失败:', error);
});
minigameApi.showPayment({
params: {
game_openid: '1018553529511732',
role_id: '1555629938',
product_id: 'coins_01-midasbuy'
},
extra: {
hideResultPage: true
}
});
Promise 方式(推荐):
// 使用 Promise 显示支付页面
minigameApi.showPayment({
params: {
game_openid: '1018553529511732',
role_id: '1555629938',
product_id: 'coins_01-midasbuy',
server_id: 'server_001'
},
extra: {
hideResultPage: true
}
})
.then(data => {
console.log('支付成功:', data);
// data 包含: { order_no, openid, order_no_hash }
})
.catch(error => {
console.log('支付错误:', error);
// 处理不同类型的错误
switch(error.type) {
case 'failed':
console.log('支付失败:', error.data);
break;
case 'hide':
console.log('支付弹窗被关闭');
break;
case 'queryChannelsFailed':
console.log('加载支付渠道失败:', error.data);
break;
}
});
// 使用 async/await
async function handlePayment() {
try {
const result = await minigameApi.showPayment({
params: {
game_openid: '1018553529511732',
role_id: '1555629938',
product_id: 'coins_01-midasbuy',
server_id: 'server_001'
}
});
console.log('支付成功:', result);
} catch (error) {
console.log('支付失败:', error.type, error.message);
}
}
参数说明
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| params | PaymentParams | 是 | 支付参数 |
| extra | PaymentExtra | 否 | 支付额外参数 |
PaymentParams 类型
interface PaymentParams {
game_openid: string; // 游戏用户 ID
role_id: string; // 角色 ID
product_id: string; // 商品 ID
server_id?: string; // 游戏分区ID
is_vip_product?: boolean; // 是否VIP商品
}
PaymentExtra 类型
interface PaymentExtra {
hideResultPage?: boolean; // 是否隐藏结果页面
}
PaymentError 类型
interface PaymentError extends Error {
code: string; // 错误代码
type: 'failed' | 'hide' | 'queryChannelsFailed'; // 错误类型
data?: any; // 额外错误数据
timestamp: number; // 错误时间戳
}
错误类型
| 类型 | 描述 | 发生时机 |
|---|---|---|
failed | 支付交易失败 | 用户支付未成功 |
hide | 支付弹窗关闭 | 用户关闭了支付弹窗 |
queryChannelsFailed | 加载支付渠道失败 | 网络错误或商品无效 |
隐藏支付页面 minigameApi.hidePayment()
// 隐藏支付页面
minigameApi.hidePayment();
最佳实践:基于 Promise 的支付流程
// 现代化的 async/await 支付流程
class PaymentManager {
constructor(minigameApi) {
this.minigameApi = minigameApi;
}
async processPayment(productId, userId, roleId) {
try {
console.log('开始支付流程...');
const result = await this.minigameApi.showPayment({
params: {
game_openid: userId,
role_id: roleId,
product_id: productId,
server_id: 'server_001'
},
extra: {
hideResultPage: true
}
});
// 支付成功
console.log('支付完成:', result);
this.handlePaymentSuccess(result);
return result;
} catch (error) {
console.log('支付出现错误:', error);
this.handlePaymentError(error);
throw error;
}
}
handlePaymentSuccess(result) {
// 更新游戏状态,解锁内容等
console.log(`订单 ${result.order_no} 已完成,用户 ${result.openid}`);
// 显示成功消息
this.showNotification('支付成功!您的购买内容现已可用。');
}
handlePaymentError(error) {
switch (error.type) {
case 'failed':
this.showNotification('支付失败,请重试。');
this.trackEvent('payment_failed', error.data);
break;
case 'hide':
this.showNotification('支付已取消。');
this.trackEvent('payment_cancelled');
break;
case 'queryChannelsFailed':
this.showNotification('无法加载支付选项,请检查网络连接。');
this.trackEvent('payment_channels_failed', error.data);
break;
}
}
showNotification(message) {
// 显示用户友好的通知
console.log(`[通知] ${message}`);
}
trackEvent(eventName, data = null) {
// 记录分析事件
console.log(`[分析] ${eventName}`, data);
}
}