Wechat Pay V3

const Koa = require("koa");
const app = new Koa();
const axios = require("axios");
const fs = require("fs");
const crypto = require("crypto");

/** * generates a random string *@param {number} Len The character string contains */
function createRandomString(len) {
  let data = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
  let str = "";
  for (let i = 0; i < len; i++) {
    str += data.charAt(Math.floor(Math.random() * data.length));
  }
  return str;
}

/** * wechat Pay V3 signature generation *@param {string} Method Request method *@param {string} url
 * @param {number} Timestamp Indicates the timestamp in seconds *@param {string} Nonce_str Random character string *@param {Object} Order Principal information */
function createSign(method, url, timestamp, nonce_str, order) {
  let signStr = `${method}\n${url}\n${timestamp}\n${nonce_str}\nThe ${JSON.stringify(
    order
  )}\n`;
  let cert = fs.readFileSync("./apiclient_key.pem"."utf-8");
  let sign = crypto.createSign("RSA-SHA256");
  sign.update(signStr);
  return sign.sign(cert, "base64");
}

/** * wechat Pay v3 *@param {Object} Order Order information */
function v3Pay(order, serial_no) {
  let timestamp = Math.floor(new Date().getTime() / 1000);
  let nonce_str = createRandomString(32);
  let signature = createSign(
    "POST"."/v3/pay/transactions/native",
    timestamp,
    nonce_str,
    order
  );
  let Authorization = `WECHATPAY2-SHA256-RSA2048 mchid="xxxx",nonce_str="${nonce_str}",timestamp="${timestamp}",signature="${signature}",serial_no="${serial_no}"`;
  axios
    .post("https://api.mch.weixin.qq.com/v3/pay/transactions/native", order, {
      headers: { Authorization: Authorization },
    })
    .then((res) = > {
      console.log(res);
    })
    .catch((err) = > {
      console.log(err);
    });
}
let order = {
  appid: "xxxx".mchid: "xxxx".description: "Test scan payment".out_trade_no: "wzm20210209".amount: {
    total: 1,},notify_url: "https://xxx.cn/"}; v3Pay(order,"Certificate Serial Number");

app.listen(3000);
Copy the code

The certificate (apiclient_cert.pem) can be uploaded to myssl.com/cert_decode… Check the serial number Pay.weixin.qq.com/wiki/doc/ap WeChat document…

Wechat Pay V2

const Koa = require("koa");
const app = new Koa();
const xml2js = require("xml2js");
const axios = require("axios");
const crypto = require("crypto");
const hash = crypto.createHash("md5");

let info = {
  appid: "xxxx".mch_id: "xxxx".nonce_str: "xxxx".spbill_create_ip: "123.12.12.123".//
  body: "Test scan payment".out_trade_no: "wzm20210215".total_fee: 1.notify_url: "https://xxxx.cn/".trade_type: "NATIVE"};/ * * * *@param {object} Info Order information *@param {string} Key Merchant key */
function createSignV2(info, key) {
  let stringA = "";
  let keys = Object.keys(info);
  keys.sort();
  console.log(keys);
  for (let item of keys) {
    stringA += `${item}=${info[item]}& `;
  }
  let stringSignTemp = `${stringA}key=${key}`;
  return hash.update(stringSignTemp).digest("hex");
}
/** * wechat pay v2 */
function v2Pay(info) {
  let sign = createSignV2(info, "xxxx");
  info.sign = sign;
  let builder = new xml2js.Builder();
  let xmlData = builder.buildObject(info);
  axios
    .post("https://api.mch.weixin.qq.com/pay/unifiedorder", {
      xmlData,
      headers: { "Content-Type": "application/xml" },
    })
    .then((res) = > {
      var parser = new xml2js.Parser({ explicitArray: false });
      parser
        .parseStringPromise(res.data)
        .then(function (result) {
          console.dir(result);
        })
        .catch(function (err) {
          // Failed
        });
    });
}
v2Pay(info);
Copy the code

Pay.weixin.qq.com/wiki/doc/ap WeChat document…