1. Open the alipay open platform
Openhome.alipay.com/platform/ho…
Fast generation of public keys
Miniu.alipay.com/keytool/cre…
Koa API or Node API configuration
Use the Node API to view the source article juejin.cn/post/687336…
First install Alipay SDK:
Alipaysdk. exec(method, params, options); Copy the codeCopy the code
- Method: string type, calling Api, such as Alipay.trade.page.pay;
- Params: Optional parameters, object structure, Api request parameters;
- Options: contains
- ValidateSign: Boolean value. Whether to check the returned value depends on alipay’s public key.
- FormData: object structure, request parameters of the file upload class interface;
- Log: object structure. When it exists, the info and error methods are called to write logs.
Since the AlipaySdk API is the same object each time it is called, the object only needs to be instantiated once:
Const AlipaySdk = require(' Alipay - SDK ').default; // alipay.js = require(' Alipay - SDK ').default; Const alipaySdk = new alipaySdk ({appId: 'appId', // appId signType generated when creating an application on open platform: 'RSA2', / / signature algorithms, the default RSA2 gateway: 'https://openapi.alipaydev.com/gateway.do', / / alipay gateway address, sandbox environment need to be modified when using alipayPublicKey: 'public_key', // alipay public key, privateKey: 'private_key', // apply privateKey string}); module.exports = alipaySdk;Copy the code
To complete the payment, there are several steps,
- The server side needs to call the payment API Alipay. Trade.page.pay to obtain the address of the payment page;
- The server side needs to call the payment API Alipay. Trade.page.pay to obtain the address of the payment page;
Let’s take a look at the implementation of the server-side interface:
const alipaySdk = require('.. /utils/alipay'); const AlipayFormData = require('alipay-sdk/lib/form').default; Router.post ('/pcpay', async (CTX) =>{const formData = new AlipayFormData(); formData.setMethod('get'); // Add parameters through addField // After the user completes the payment, alipay server will notify the merchant system of the payment result as parameter in the form of POST request according to the incoming notifY_URL. formData.addField('notifyUrl', 'http://www.com/notify'); Formdata.addfield ('bizContent', {outTradeNo: {formData.addField('bizContent', {outTradeNo: CTX. Request. Body. OutTradeNo, / / merchant order number, 64 characters, can contain letters, Numbers, underscores, and cannot be repeated productCode: 'FAST_INSTANT_TRADE_PAY', // Sales product code, product code name signed with Alipay, only FAST_INSTANT_TRADE_PAY totalAmount supported: '0.01', // Total order amount, in yuan, to two decimal places subject: 'commodity ', // Order title body:' commodity details ', // Order description}); // If you need to jump to the merchant interface after payment, Const result = await alipaysdK. exec(' Alipay. Trade.page.pay ', // // API request parameters (including "public request parameters" and "business parameters") {formData: formData},); Ctx. body={url: result}});Copy the code
Then there is the front page, which is relatively simple, that is, click the payment button, initiate a request to the server, get the returned payment page address and jump:
$.ajax({ method: 'POST', url: '/alipay/pcpay', data: }}). Done (function(res) {window.open(res.url, '_blank'); }). fail(function (err) { console.log(err); });Copy the code