Midasbuy Payment SDK
1. SDK Description
To launch the available payment channels for items on Midasbuy based on the player's "person" and "item" information.
2. Integrating the SDK
Include the script in your HTML file:
<script src="https://cdn.midasbuy.com/no-sw-common-sdk/static/js/commonSdkApi.js"></script>
3. API Documentation
3.1. Initialize
Initialize the pay instance:
const { pay } = window.midasbuyCommonSdk;
pay?.preload({
appid: "1450015065",
region: "hk",
buysdk_mode: 1,
sdkUrl: 'https://sandbox.midasbuy.com/payment-sdk'
});
Parameter | Required | Location | Type | Format | Description |
---|---|---|---|---|---|
appid | Yes | params | String | string.min_len: 1 | Application ID |
region | Yes | params | String | string.len: 2 | region code; refer to the [region select] for available options |
buysdk_mode | Yes | params | Number | Fixed to 1. | |
sdkUrl | Yes | params | Number | Used to differentiate between sandbox and production environments. |
3.2. Trigger channel payment and event callback.
Trigger channel payment popup:
const reqParams = {
params: {
app_id: '1460000764',
game_openid: '123456789',
role_id: '123',
product_id: 'productId',
region: 'HK'
},
extra: {},
} as any;
pay?.emit('checkout', reqParams);
Parameter
Parameter | Required | Location | Type | Format | Description |
---|---|---|---|---|---|
game_openid | Yes | params | String | string.min_len: 1 | Game user ID |
role_id | Yes | params | String | string.min_len: 1 | Role ID |
product_id | Yes | params | String | string.min_len: 1 | Product ID |
region | Yes | params | String | string.min_len: 1 | Region, language affecting item information. |
3.2.1. Payment success callback
Successful example:
pay.on('paymentSuccess', (data) => {
console.log('data', data);
console.log(`payment success: ${JSON.stringify(data)}`);
Data instance:
{
"order_no":"SG250225BHGE8KPUZKXDFE",
"openid":"34131737143805224",
"order_no_hash":"ca171bec7737d557d54f15706404bddc129bd2f09749f76b5dfae055a44b11e9"
}
Parameter | Description |
---|---|
order_no | Order NO |
openid | Game user ID |
order_no_hash | Order NO HASH |
3.2.2. Other event enumerations
参数 | 说明 |
---|---|
hide | Hide payment popup |
queryChannelsFailed | Failed to initialize payment popup, usually due to item query failure or channel query failure. |
paymentFailed | Successful initialization of payment popup, but the user was not successful in subsequent payment. |
4. Sandbox demo
<html>
<head>
<title>payment sdk demo</title>
<script src="https://cdn.midasbuy.com/no-sw-common-sdk/static/js/commonSdkApi.js"></script>
</head>
<body>
<button onclick="clickToPay()">pay</button>
<script>
const { pay } = window.midasbuyCommonSdk;
pay?.preload({
appid: "1450015065",
region: "hk",
buysdk_mode: 1,
sdkUrl: 'https://sandbox.midasbuy.com/payment-sdk'
});
try {
pay?.on('loaded', () => {
console.log('PaySDK has been loaded.');
this.channelWraperLoaded = true;
});
} catch (error) {
console.log('error', error);
throw error;
}
function clickToPay() {
console.log('Loading payment channels...');
pay?.off('hide');
pay?.off('queryChannelsFailed');
pay?.off('paymentSuccess');
pay?.off('paymentFailed');
pay?.off('handlerPlayerId');
pay?.off('drawerFilterPop');
pay?.on('hide', () => {
pay.emit('hide');
}).on('queryChannelsFailed', (msg) => {
console.log(`Failed to query channels: ${JSON.stringify(msg)}`);
})?.on('paymentSuccess', (data) => {
console.log('data', data);
console.log(`Payment successful: ${JSON.stringify(data)}`);
})?.on('paymentFailed', (error) => {
console.log('error', error);
console.log(`Payment failed: ${JSON.stringify(error)}`);
})?.on('show', () => {
pay.emit('show');
});
const reqParams = {
params: {
app_id: "1450015065",
game_openid: "34131737143805224",
role_id: "5557607661",
product_id: "60_coins_vip",
region: "hk",
is_vip_product: true,
},
extra: {},
};
pay?.emit('checkout', reqParams);
pay?.emit('show');
}
</script>
</body>
</html>