alphapay
v0.2.4
Published
Node.js implementation for alphapay
Downloads
14
Readme
Overview
Node.js implementation for alphapay
See official doc
Install
npm i -S alphapay
Example
Generate payment url
Create an Alphapay instance
const alphapay = new Alphapay(PARTNER_CODE, CREDENTIAL_CODE);
Create a QR Code payment
const resp = await alphapay.createQRCodePayment("your-order-id", {
description: 'Developer Test',
price: Number((total * 100)).toFixed(0), // price should be an integer of the base unit of the currency
currency: CurrencyType.CAD, // CAD or CNY
notify_url: "https://your.domain.backend/api/alphapay-success",
channel: ChannelType.WECHAT // ALIPAY or UNION_PAY, some channel might not be available for different payment gateways
});
Handle alphapay response
if (resp.return_code != 'SUCCESS') {
// handle exception
return;
}
const redirectUrl = alphapay.getQRCodePaymentPageUrl("your-order-id", `https://your.domain.frontend/payment-success?orderId=your-order-id`);
Pass redirectUrl
to the frontend
Redirect browser to the payment page
Redirect the page to the url generated in the backend:
window.location.href = redirectUrl;
Handle success notification in backend
Alphapay will post SuccessNotification up to 3 times to notify_url
when payment succceeds.
const notification: SuccessNotification = req.body;
if (!alphapay.isNotificationValid(notification)) {
// you can ignore validation for some alphapay apis
// https://www.alphapay.com/api/CAD_en.html#PaymentSuccessPage
// handle exception
return;
}
const orderId = notification.partner_order_id; // your-order-id
const alphaOrder = await alphapay.getOrderStatus(orderId);
// IMPORTANT: your backend should be able to handle the repeated success notifications
doSomethingElseInYourBackend(orderId, alphaOrder);
Check payment status in redirectUrl
In redirectUrl
you may want to check if SuccessNotification
is properly handled in your backend.