评论

微信支付商户免充值代金券接口升级验收脚本(NodeJS版) 用例组合1001+1002+1003+1004+1005

开通免充值代金券后,几个支付相关接口参数会发生变动,需要改相应代码适配变动。

前言:

本文是在社区网友Memory的原贴基础上,转译成的NodeJS版本用例。

附原文地址:微信支付商户免充值代金券接口升级验收脚本 用例组合1001+1002+1003+1004+1005(强迫症专用)? - 微信开放社区 https://developers.weixin.qq.com/community/develop/article/doc/0002e82b060c3028230c915f150813

1,为什么要升级?

开通免充值代金券后,几个支付相关接口参数会发生变动,需要改相应代码适配变动。

详见:开通免充值业务API接口升级详情 https://pay.weixin.qq.com/wiki/doc/api/download/ptsh_mczjksj.pdf

2,官方的测试验收示例

详见:https://pay.weixin.qq.com/wiki/doc/api/download/mczyscsyl.pdf

3,接口升级验收进度查询

详见:https://pay.weixin.qq.com/wiki/doc/api/tools/sp_coupon.php?chapter=15_6&index=4

4,使用自动升级验收脚本前的重要提醒!

请务必去改自己的应用层代码,这里只是方便统一验收用。

5,自动升级验收脚本代码

const request = require('request');
const md5 = require('md5');
const xml2js = require('xml2js');
const xmlparser = new xml2js.Parser({ explicitArray: false });

var requestPost = function (url, data) {
  return new Promise(function (resolve, reject) {
    request.post({ url: url, body: data }, function (error, response, body) {
      if (error) return reject(error);
      resolve(body);
    })
  })
}

let SandBox_Url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey'
let MicroPay_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/micropay"
let UnifiedOrder_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder"
let OrderQuery_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/orderquery"
let ReFund_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/refund"
let RefundQuery_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/refundquery"
let DownloadBill_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/downloadbill"
let nonce_str = "5K8264ILTKCH16CQ2502SI8ZNMTM67VS"
let appid = "wxc813982bbb9ad5d4"

async function get_sign_key(mch_id, key) {
  let sign = md5(`mch_id=${mch_id}&nonce_str=${nonce_str}&key=${key}`)
  let template = `<xml><mch_id><![CDATA[${mch_id}]]></mch_id><nonce_str><![CDATA[${nonce_str}]]></nonce_str><sign><![CDATA[${sign}]]></sign></xml>`
  let result = await requestPost(SandBox_Url, template)
  let xmlJSON = await xmlparser.parseStringPromise(result);
  return xmlJSON.xml
}

async function json2xml(params, wxpay_key) {
  const _params = {};
  Object.keys(params).sort().forEach(function (key) { _params[key] = params[key]; });

  let encrypted_str = ``
  let xml = `<xml>`
  for (let key in _params) {
    xml += `<${key}><![CDATA[${_params[key]}]]></${key}>`
    encrypted_str += `${key}=${_params[key]}&`
  }
  encrypted_str += `key=${wxpay_key}`
  encrypted_str = md5(encrypted_str).toUpperCase()

  xml += `<sign><![CDATA[${encrypted_str}]]></sign></xml>`
  return xml
}

async function get_out_trade_no() {
  let num = '';
  for (var i = 0; i < 5; i++) {
    num += Math.floor(Math.random() * 10);
  }
  return `${Date.parse(new Date())}${num}`;
}

async function upgrade(mch_id, key) {
  let wxpay_key = await get_sign_key(mch_id, key)
  if (wxpay_key.sandbox_signkey) {
    wxpay_key = wxpay_key.sandbox_signkey
  }
  else {
    return wxpay_key
  }

  let out_trade_no, out_trade_no_2nd, template, result, xmlJSON

  console.log(`========用例编号1001刷卡========`)
  out_trade_no = await get_out_trade_no()
  let MicroPay_param = {
    'appid': appid,
    'mch_id': mch_id,
    'nonce_str': nonce_str,
    'body': "check",
    'out_trade_no': out_trade_no,
    'total_fee': "501",
    'spbill_create_ip': "8.8.8.8",
    'auth_code': "120061098828009406",
  }
  template = await json2xml(MicroPay_param, wxpay_key);
  result = await requestPost(MicroPay_Url, template)

  console.log(`========用例编号1001刷卡正常支付查询========`)
  let OrderQuery_param = {
    'appid': appid,
    'mch_id': mch_id,
    'nonce_str': nonce_str,
    'out_trade_no': out_trade_no,
  }
  template = await json2xml(OrderQuery_param, wxpay_key);
  result = await requestPost(OrderQuery_Url, template)

  console.log(`========用例编号1002刷卡========`)
  out_trade_no_2nd = await get_out_trade_no()
  MicroPay_param = {
    'appid': appid,
    'mch_id': mch_id,
    'nonce_str': nonce_str,
    'body': "check",
    'out_trade_no': out_trade_no_2nd,
    'total_fee': "502",
    'spbill_create_ip': "8.8.8.8",
    'auth_code': "120061098828009406",
  }
  template = await json2xml(MicroPay_param, wxpay_key);
  result = await requestPost(MicroPay_Url, template)

  console.log(`========用例编号1002刷卡正常支付查询========`)
  OrderQuery_param = {
    'appid': appid,
    'mch_id': mch_id,
    'nonce_str': nonce_str,
    'out_trade_no': out_trade_no_2nd,
  }
  template = await json2xml(OrderQuery_param, wxpay_key);
  result = await requestPost(OrderQuery_Url, template)

  console.log(`========用例编号1002刷卡支付退款========`)
  let ReFund_param = {
    'appid': appid,
    'mch_id': mch_id,
    'nonce_str': nonce_str,
    'out_refund_no': out_trade_no,
    'total_fee': "502",
    'refund_fee': "501",
    'out_trade_no': out_trade_no_2nd,
  }
  template = await json2xml(ReFund_param, wxpay_key);
  result = await requestPost(ReFund_Url, template)

  console.log(`========用例编号1002刷卡支付退款查询========`)
  let RefundQuery_param = {
    'appid': appid,
    'mch_id': mch_id,
    'nonce_str': nonce_str,
    'out_trade_no': out_trade_no_2nd,
  }
  template = await json2xml(RefundQuery_param, wxpay_key);
  result = await requestPost(RefundQuery_Url, template)


  console.log(`========用例编号1003-公众号/APP/扫码正常支付========`)
  out_trade_no = await get_out_trade_no()
  let UnifiedOrder_param = {
    'appid': appid,
    'mch_id': mch_id,
    'nonce_str': nonce_str,
    'body': "check",
    'out_trade_no': out_trade_no,
    'total_fee': "551",
    'notify_url': "https://www.weixin.qq.com/wxpay/pay.php",
    'spbill_create_ip': "8.8.8.8",
    'trade_type': "JSAPI",
  }
  template = await json2xml(UnifiedOrder_param, wxpay_key);
  result = await requestPost(UnifiedOrder_Url, template)

  console.log(`========用例编号1003-公众号/APP/扫码正常支付查询========`)
  OrderQuery_param = {
    'appid': appid,
    'mch_id': mch_id,
    'nonce_str': nonce_str,
    'out_trade_no': out_trade_no,
  }

  template = await json2xml(OrderQuery_param, wxpay_key);
  result = await requestPost(OrderQuery_Url, template)

  console.log(`========用例编号1004-公众号/APP/扫码支付退款========`)
  out_trade_no_2nd = await get_out_trade_no()
  UnifiedOrder_param = {
    'appid': appid,
    'mch_id': mch_id,
    'nonce_str': nonce_str,
    'body': "check",
    'out_trade_no': out_trade_no_2nd,
    'total_fee': "552",
    'notify_url': "https://www.weixin.qq.com/wxpay/pay.php",
    'spbill_create_ip': "8.8.8.8",
    'trade_type': "JSAPI",
  }
  template = await json2xml(UnifiedOrder_param, wxpay_key);
  result = await requestPost(UnifiedOrder_Url, template)

  console.log(`========用例编号1004-公众号/APP/扫码支付退款查询========`)
  OrderQuery_param = {
    'appid': appid,
    'mch_id': mch_id,
    'nonce_str': nonce_str,
    'out_trade_no': out_trade_no_2nd,
  }
  template = await json2xml(OrderQuery_param, wxpay_key);
  result = await requestPost(OrderQuery_Url, template)

  console.log(`========用例编号1004-公众号/APP/扫码支付退款========`)
  ReFund_param = {
    'appid': appid,
    'mch_id': mch_id,
    'nonce_str': nonce_str,
    'out_refund_no': out_trade_no,
    'total_fee': "552",
    'refund_fee': "551",
    'out_trade_no': out_trade_no_2nd,
  }
  template = await json2xml(ReFund_param, wxpay_key);
  result = await requestPost(ReFund_Url, template)

  console.log(`========用例编号1004-公众号/APP/扫码支付退款查询========`)
  RefundQuery_param = {
    'appid': appid,
    'mch_id': mch_id,
    'nonce_str': nonce_str,
    'out_trade_no': out_trade_no_2nd,
  }
  template = await json2xml(RefundQuery_param, wxpay_key);
  result = await requestPost(RefundQuery_Url, template)

  console.log(`========下载交易的对账单========`)
  let DownloadBill_param = {
    'appid': appid,
    'mch_id': mch_id,
    'nonce_str': nonce_str,
    'bill_date': "2021-04-01",
    'bill_type': "ALL"
  }
  template = await json2xml(DownloadBill_param, wxpay_key);
  await requestPost(DownloadBill_Url, template)

  return { retcode: 0 }
}

module.exports = { upgrade }
最后一次编辑于  2021-05-10  
点赞 7
收藏
评论

2 个评论

登录 后发表内容