How to generate signatures and authorization headers
masterWhen making manual HTTP requests to WeChat Pay V3 APIs, you must generate a signature and an Authorization header.
- Generate Signature: Use
pay.getSignature(method, nonce_str, timestamp, url, params).- For
GETrequests, theparamsargument is not appended to the URL in the signature calculation, but the URL should include query parameters if applicable.
- For
- Generate Authorization Header: Use
pay.getAuthorization(nonce_str, timestamp, signature)to create the string for theAuthorizationheader.
Important: You must also include a User-Agent header in your request.
const nonce_str = Math.random().toString(36).substr(2, 15);
const timestamp = parseInt(+new Date() / 1000 + '').toString();
const url = '/v3/pay/transactions/h5';
const params = { /* ... your request body ... */ };
// 1. Get signature
const signature = pay.getSignature('POST', nonce_str, timestamp, url, params);
// 2. Get authorization header
const authorization = pay.getAuthorization(nonce_str, timestamp, signature);
// 3. Use in request
// await request.post(BASE_URL + url).send(params).set({ Authorization: authorization, 'User-Agent': '...' });