DianDianPay JS SDK Integration Guide
The DianDianPay JS SDK enables merchants to securely obtain a Stripe Radar Session ID on their checkout pages. Simply include a single script tag and pass your merchant ID to receive a sessionId for subsequent payment risk control verification.
Quick Start
1. Include the SDK
Add the following script tag at the bottom of your HTML page:
<script src="https://js.diandianpay.com/sdk/v1/session.js"></script>2. Initialize and Get sessionId
After DianDianPay({env, mid}) is initialized, it will be automatically mounted into the window object.
<script>
const env = 'test'; // Environment: 'test' or 'prod'
const mid = 'acct_wo0DnjcKAgtTjmmh'; // Your merchant ID (required)
(async () => {
try {
// const sessionId = await window.DianDianPay({env, mid}).getSessionId();
const sessionId = await new DianDianPay({env, mid}).getSessionId();
console.log('Session ID:', sessionId); // Example output: rse_1TfxAmD82ANvaKvDkIEcgNN3
// Pass sessionId to your backend
} catch (err) {
// If retrieval fails, pass null to your backend
console.error('Failed to get sessionId:', err.message);
}
})();
</script>3. Complete Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Radar Session Test</title>
</head>
<body>
<!-- Include DianDianPay SDK -->
<script src="https://js.diandianpay.com/sdk/v1/session.js"></script>
<script>
const env = 'test';
const mid = 'acct_wo0DnjcKAgtTjmmh';
(async () => {
try {
const sessionId = await new DianDianPay({ env, mid }).getSessionId();
console.log('Current environment:', env);
console.log('Session ID:', sessionId);
} catch (err) {
console.error('Failed to get sessionId:', err.message);
}
})();
</script>
</body>
</html>JS API Reference
new DianDianPay(options)
Creates an SDK instance.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
env | string | No | 'test' | Runtime environment. Options: 'test' or 'prod' |
mid | string | Yes | — | Merchant ID. Cannot be empty |
Example:
const pay = new DianDianPay({ env: 'prod', mid: 'acct_xxx' });If mid is not provided, an error is thrown immediately: Merchant ID cannot be empty.
getSessionId()
Asynchronously retrieves the Stripe Radar Session ID.
- Returns:
Promise<string>— resolves tosessionIdon success - On failure:
Promiserejects with an error message string
Example:
const sessionId = await pay.getSessionId();Backend API Reference
When your backend initiates a payment order request to DianDianPay, include the risk control token collected from the frontend in the request body.
If the frontend fails to obtain the value due to timeout, network interception, ad blockers, or other reasons, you must pass null here to avoid blocking the user's payment.
{
"merchant_id": "1",
"env": {
"risk_token": "rse_1TfxAmD82ANvaKvDkIEcgNN3"
}
}
risk_tokenmay benull.
