0.2.4 • Published 4 years ago

alphapay v0.2.4

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

build npm downloads

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.

For more info, see test file and live demo

0.2.4

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.1.2

4 years ago

0.1.3

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.1

4 years ago