wechatpay-node-v3

repository·master·Indexed 20 days ago

https://github.com/klover2/wechatpay-node-v3-ts

A TypeScript/JavaScript wrapper for the WeChat Pay V3 API (version 2.2.1) that simplifies access to payment, refund, and billing services. It provides high-level API methods for H5, Native, App, and JSAPI payments, combined transactions, batch transfers to wallets, profit sharing, and bill management. The library includes utilities for generating signatures, authorization headers, and encrypting user names using platform certificates.

Tokens
16.2K
Snippets
52
Records
65
Agent score
70%

What's inside wechatpay-node-v3

  1. How to generate signatures and authorization headers

    master

    When making manual HTTP requests to WeChat Pay V3 APIs, you must generate a signature and an Authorization header.

    1. Generate Signature: Use pay.getSignature(method, nonce_str, timestamp, url, params).
      • For GET requests, the params argument is not appended to the URL in the signature calculation, but the URL should include query parameters if applicable.
    2. Generate Authorization Header: Use pay.getAuthorization(nonce_str, timestamp, signature) to create the string for the Authorization header.

    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': '...' });
  2. Initialize WxPay for App Payment

    master

    To use WeChat Pay App payment, initialize the WxPay class with your merchant credentials. You must provide the appid associated with your direct-connect merchant application, your merchant ID (mchid), and the paths to your public and private keys (typically read from .pem files).

    import WxPay from 'wechatpay-node-v3';
    import fs from 'fs';
    
    const pay = new WxPay({
      appid: '直连商户申请的公众号或移动应用appid',
      mchid: '商户号',
      publicKey: fs.readFileSync('./apiclient_cert.pem'), // 公钥
      privateKey: fs.readFileSync('./apiclient_key.pem'), // 秘钥
    });
  3. Initialize WxPay for WeChat Pay V3

    master

    To use the library, instantiate the WxPay class with your merchant credentials. You will need your AppID, Merchant ID (mchid), and the public/private key files provided by WeChat Pay.

    Required configuration keys:

    • appid: The AppID of the official account or mobile application associated with the direct connection merchant.
    • mchid: Your Merchant ID.
    • publicKey: The content of your apiclient_cert.pem file.
    • privateKey: The content of your apiclient_key.pem file.
    import WxPay from 'wechatpay-node-v3';
    import fs from 'fs';
    
    const pay = new WxPay({
      appid: 'YOUR_APPID',
      mchid: 'YOUR_MCHID',
      publicKey: fs.readFileSync('./apiclient_cert.pem'),
      privateKey: fs.readFileSync('./apiclient_key.pem'),
    });
  4. Initialize the WxPay instance

    master

    To use the library, instantiate the WxPay class with your merchant credentials. You will need your AppID, Merchant ID (mchid), and your API certificates.

    Required Configuration Parameters:

    • appid: The AppID associated with the merchant's official account or mobile application.
    • mchid: Your Merchant ID.
    • publicKey: The public key from your certificate file (e.g., apiclient_cert.pem).
    • privateKey: The private key from your certificate file (e.g., apiclient_key.pem).

    Optional Configuration Parameters:

    • serial_no: Certificate serial number. You can obtain this using: openssl x509 -in apiclient_cert.pem -noout -serial.
    • authType: Authentication type (defaults to WECHATPAY2-SHA256-RSA2048).
    • userAgent: Custom User-Agent string.
    • key: The APIv3 key (required if you need to verify callbacks).
    import WxPay from 'wechatpay-node-v3';
    import fs from 'fs';
    
    const pay = new WxPay({
      appid: 'YOUR_APPID',
      mchid: 'YOUR_MCHID',
      publicKey: fs.readFileSync('./apiclient_cert.pem'),
      privateKey: fs.readFileSync('./apiclient_key.pem'),
    });
  5. Initialize WxPay for JSAPI Payment

    master

    To use JSAPI payment, initialize the WxPay class with your merchant credentials. You must provide the appid (the official account or mobile app ID associated with the merchant), the mchid (merchant ID), and the publicKey and privateKey files obtained from your WeChat Pay merchant platform.

    Note: Use fs.readFileSync to pass the file contents for the keys.

    import WxPay from 'wechatpay-node-v3';
    import fs from 'fs';
    
    const pay = new WxPay({
      appid: '直连商户申请的公众号或移动应用appid',
      mchid: '商户号',
      publicKey: fs.readFileSync('./apiclient_cert.pem'), // 公钥
      privateKey: fs.readFileSync('./apiclient_key.pem'), // 秘钥
    });
  6. Initialize WxPay for downloading bills

    master

    To use the library, instantiate the WxPay class with your WeChat Pay credentials. You must provide your appid, mchid (Merchant ID), and the contents of your publicKey and privateKey files (typically obtained from your WeChat Pay merchant platform).

    Note: Ensure you use fs.readFileSync or a similar method to pass the actual buffer/content of the certificate files to the constructor.

    import WxPay from 'wechatpay-node-v3';
    import fs from 'fs';
    
    const pay = new WxPay({
      appid: '直连商户申请的公众号或移动应用appid',
      mchid: '商户号',
      publicKey: fs.readFileSync('./apiclient_cert.pem'), // Public key
      privateKey: fs.readFileSync('./apiclient_key.pem'), // Private key
    });
  7. Initialize WxPay for transaction bill requests

    master

    To use the transaction bill functionality, you must first initialize the WxPay class. You need to provide the appid (the official account or mobile app ID associated with the direct-connect merchant), the mchid (merchant ID), and the paths to your publicKey and privateKey files (typically apiclient_cert.pem and apiclient_key.pem).

    import WxPay from 'wechatpay-node-v3';
    import fs from 'fs';
    
    const pay = new WxPay({
      appid: 'YOUR_APPID',
      mchid: 'YOUR_MCHID',
      publicKey: fs.readFileSync('./apiclient_cert.pem'),
      privateKey: fs.readFileSync('./apiclient_key.pem'),
    });
  8. Initialize WxPay for Fund Flow Bill requests

    master

    To use the fund flow bill functionality, you must first initialize the WxPay instance with your merchant credentials. You need the appid (the public account or mobile app ID associated with the direct connection merchant), your mchid (merchant ID), and the paths to your publicKey and privateKey files.

    import WxPay from 'wechatpay-node-v3';
    import fs from 'fs';
    
    const pay = new WxPay({
      appid: '直连商户申请的公众号或移动应用appid',
      mchid: '商户号',
      publicKey: fs.readFileSync('./apiclient_cert.pem'), // 公钥
      privateKey: fs.readFileSync('./apiclient_key.pem'), // 秘钥
    });
  9. Initialize WxPay for Native Payment

    master

    To use the WeChat Pay Node.js SDK, instantiate the WxPay class with your merchant credentials. You must provide your appid, mchid (Merchant ID), and the contents of your API certificate files (publicKey and privateKey).

    import WxPay from 'wechatpay-node-v3';
    import fs from 'fs';
    
    const pay = new WxPay({
      appid: '直连商户申请的公众号或移动应用appid',
      mchid: '商户号',
      publicKey: fs.readFileSync('./apiclient_cert.pem'), // Public key
      privateKey: fs.readFileSync('./apiclient_key.pem'), // Private key
    });
  10. Initialize the WxPay client

    master

    To use the library, instantiate the WxPay class with your WeChat Pay credentials. You must provide your appid, mchid, and the contents of your certificate files (publicKey and privateKey).

    import WxPay from 'wechatpay-node-v3';
    import fs from 'fs';
    
    const pay = new WxPay({
      appid: '直连商户申请的公众号或移动应用appid',
      mchid: '商户号',
      publicKey: fs.readFileSync('./apiclient_cert.pem'), // 公钥
      privateKey: fs.readFileSync('./apiclient_key.pem'), // 秘钥
    });
  11. Initialize WxPay for H5 Payment

    master

    To use H5 payment, instantiate the WxPay class with your merchant credentials. You must provide the appid (the official account or mobile app ID associated with the merchant), the mchid (merchant ID), and the file contents for your publicKey and privateKey (typically .pem files).

    import WxPay from 'wechatpay-node-v3';
    import fs from 'fs';
    
    const pay = new WxPay({
      appid: 'YOUR_APPID',
      mchid: 'YOUR_MCHID',
      publicKey: fs.readFileSync('./apiclient_cert.pem'), // Public key
      privateKey: fs.readFileSync('./apiclient_key.pem'), // Private key
    });