Payment Functions
Show Payment Page minigameApi.showPayment(options)
Event-based Style (Legacy):
// Show payment page with event listeners
minigameApi.on('payment:success', (data) => {
console.log('Payment successful:', data);
});
minigameApi.on('payment:failed', (error) => {
console.log('Payment failed:', error);
});
minigameApi.showPayment({
params: {
game_openid: '1018553529511732',
role_id: '1555629938',
product_id: 'coins_01-midasbuy'
},
extra: {
hideResultPage: true
}
});
Promise Style (Recommended):
// Show payment page with 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('Payment successful:', data);
// data contains: { order_no, openid, order_no_hash }
})
.catch(error => {
console.log('Payment error:', error);
// Handle different error types
switch(error.type) {
case 'failed':
console.log('Payment failed:', error.data);
break;
case 'hide':
console.log('Payment dialog was closed');
break;
case 'queryChannelsFailed':
console.log('Failed to load payment channels:', error.data);
break;
}
});
// Using 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('Payment successful:', result);
} catch (error) {
console.log('Payment failed:', error.type, error.message);
}
}
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| params | PaymentParams | Yes | Payment parameters |
| extra | PaymentExtra | No | Additional payment parameters |
PaymentParams Type
interface PaymentParams {
game_openid: string; // Game user ID
role_id: string; // Role ID
product_id: string; // Product ID
server_id?: string; // Server ID for game partitioning
is_vip_product?: boolean; // Whether it's a VIP product
}
PaymentExtra Type
interface PaymentExtra {
hideResultPage?: boolean; // Whether to hide result page
}
PaymentError Type
interface PaymentError extends Error {
code: string; // Error code
type: 'failed' | 'hide' | 'queryChannelsFailed'; // Error type
data?: any; // Additional error data
timestamp: number; // Error timestamp
}
Error Types
| Type | Description | When it occurs |
|---|---|---|
failed | Payment transaction failed | User payment was unsuccessful |
hide | Payment dialog closed | User closed the payment dialog |
queryChannelsFailed | Failed to load payment channels | Network error or invalid product |
Hide Payment Page minigameApi.hidePayment()
// Hide payment page
minigameApi.hidePayment();
Best Practices: Promise-based Payment Flow
// Modern async/await payment flow
class PaymentManager {
constructor(minigameApi) {
this.minigameApi = minigameApi;
}
async processPayment(productId, userId, roleId) {
try {
console.log('Starting payment process...');
const result = await this.minigameApi.showPayment({
params: {
game_openid: userId,
role_id: roleId,
product_id: productId,
server_id: 'server_001'
},
extra: {
hideResultPage: true
}
});
// Payment successful
console.log('Payment completed successfully:', result);
this.handlePaymentSuccess(result);
return result;
} catch (error) {
console.log('Payment error occurred:', error);
this.handlePaymentError(error);
throw error;
}
}
handlePaymentSuccess(result) {
// Update game state, unlock content, etc.
console.log(`Order ${result.order_no} completed for user ${result.openid}`);
// Show success message
this.showNotification('Payment successful! Your purchase is now available.');
}
handlePaymentError(error) {
switch (error.type) {
case 'failed':
this.showNotification('Payment failed. Please try again.');
this.trackEvent('payment_failed', error.data);
break;
case 'hide':
this.showNotification('Payment was cancelled.');
this.trackEvent('payment_cancelled');
break;
case 'queryChannelsFailed':
this.showNotification('Unable to load payment options. Please check your connection.');
this.trackEvent('payment_channels_failed', error.data);
break;
}
}
showNotification(message) {
// Show user-friendly notification
console.log(`[Notification] ${message}`);
}
trackEvent(eventName, data = null) {
// Track analytics event
console.log(`[Analytics] ${eventName}`, data);
}
}