Alipay Signature Generation using NodeJS

Learn how to generate the signature required for Alipay Payment APIs using NodeJS.

The first step to integrate Alipay to your application is to figure out how to generate the signature needed for authorisation when calling any Alipay Payment API.

Photo by Markus Winkler on Unsplash

Why is this a difficult task? Isn't it just a signature?
Well trust me, there is no proper documentation out there that explains how to generate this signature accurately.

Note:
Please double-check with the latest documentation if you are viewing this article much later than a year of writing this blog —
April 2021.

Alipay Cashier Payment Request — Sample Payload (request.json)

Import crypto and request.json

Required Functions

  1. signContent: Function to generate the signature by signing the content using a private key.
  2. verifySignature: Function to verify the generated signature using a public key.

Helper Functions

3. base64KeyToPEM: Function to convert the private key to PEM format.

4. splitStringIntoChunks: Function to split a string into chunks used while creating the PEM format key.

Signature Generation

Verify the generated signature

Checkout the entire code here
https://github.com/aswinkumar4018/alipay-signature-calc

That’s it, you’ve got a perfectly working Signature to integrate with Alipay Payment API.

Thank you so much for reading! Please clap 👏 and share if you found this article useful.

Learn something new every day — Cheers!

Lead full-stack engineer — AWS, GCP Certified Solutions Architect working at Presidio with love & experience in building cloud-native applications.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store