Skip to main content

Full Integration Example

Two ready-to-run snippets: pick the one that matches your backend.

  • Basic: the order ID (invoiceId) is known at page-load time. Pass it to init() and you are done.
  • CMS pattern: the order does not exist yet at page-load (typical for WooCommerce, Magento, OpenCart, custom platforms). When the customer taps the WeChat Pay button, your backend creates the order and returns the resulting invoiceId (and a fresh access token, if needed) through onBeforeProcessPayment.

Both snippets pass paymentData (with amount + currency) and token to init(). The component opens a QR modal with a built-in countdown (default 15 minutes).

Dynamic carts

If the cart amount can change after the button is rendered (quantity edits, coupons), you have two options:

  • Fetch values on every click. Skip paymentData and token at init() entirely; return fresh values from onBeforeProcessPayment based on the current cart state. This is the simplest path for highly dynamic carts, no re-init needed.
  • Re-initialize on change. If you do pass paymentData and token at init(), call init() again with fresh values whenever the cart changes. The access token is minted for the values you passed and the backend rejects mismatches.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src='https://test-pay.dnapayments.com/components/alipay-wechat-pay/alipay-wechat-pay-component.js'></script>
<script>
async function renderWeChatPay() {
const token = 'YOUR_ACCESS_TOKEN';

const paymentData = {
amount: 24,
currency: 'GBP',
invoiceId: 'YOUR_INVOICE_ID',
description: 'Shoes',
paymentSettings: {
terminalId: 'YOUR_TERMINAL_ID',
callbackUrl: 'https://example.com/callback',
failureCallbackUrl: 'https://example.com/failure-callback'
},
customerDetails: {
accountDetails: { accountId: 'uuid000001' },
billingAddress: {
firstName: 'John', lastName: 'Doe',
addressLine1: 'Fulham Rd',
postalCode: 'SW6 1HS', city: 'London', country: 'GB'
},
email: 'example@email.com',
mobilePhone: '+441234567890'
},
orderLines: [{
name: 'Running shoe',
quantity: 1,
unitPrice: 24,
taxRate: 20,
totalAmount: 24,
totalTaxAmount: 4,
imageUrl: 'https://www.example.com/logo.png',
productUrl: 'https://www.example.com/AD6654412.html'
}]
};

const events = {
onClick: () => {
console.log('WeChat Pay button clicked');
},
onError: (error) => {
console.error('WeChat Pay error:', error);
},
onCancel: () => {
console.log('Payment cancelled');
},
onPaymentSuccess: (result) => {
console.log('Payment processed', result);
window.location.href = '/success';
}
};

window.DNAPayments.WeChatPayComponent.init({
containerElement: document.getElementById('wechat-pay-btn-container'),
paymentData,
events,
token,
paymentTimeoutInSeconds: 900 // optional, default
});
}

window.addEventListener('load', renderWeChatPay);
</script>
</head>
<body>
<div id='wechat-pay-btn-container' style='width: 100%; height: 46px;'></div>
</body>
</html>
Switch to production

Replace the script source with the production CDN before going live:

<script src='https://pay.dnapayments.com/components/alipay-wechat-pay/alipay-wechat-pay-component.js'></script>

See Deployment for the full go-live checklist.