评论

纯属意外,还可以这么起底微信支付接口对接

接续上一扒,起底一下 wechatpay-axios-plugin 重构之后的“变态”用法。

上一扒讲到的,从设计初是按照URL.pathname切分出实体entity做属性树扩展,map服务端服务接口;重构完后,再写测试用例的时候,竟然发现了有如下“变态”用法,这不得不也得分享出来,以供絮叨。

再提一次树

以目标函数 [Function: /v2/pay/micropay] 为例,这里有一种更简洁的构建方式,即Object.property方式构建,可以把层叠树平展展成类似一层key/value结构,树构建代码如下:

// 实例化不表了,这里直接以wxpay代指实例化handler

wxpay['v3/pay/transactions/native']
wxpay['v3/pay/transactions/id/{transaction_id}']
wxpay['v3/pay/transactions/out-trade-no/{out_trade_no}']
wxpay['v3/pay/partner/transactions/native']
wxpay['v3/marketing/busifavor/users/{openid}/coupons/{coupon_code}/appids/{appid}']
wxpay['v3/marketing/favor/stocks/{stock_id}/use-flow']
wxpay['v3/marketing/partnerships/build']
wxpay['v3/marketing/favor/media/image-upload']
wxpay['v2/pay/micropay']
wxpay['v2/risk/getpublickey']
wxpay['v2/secapi/pay/refund']
wxpay['v2/mmpaymkttransfers/promotion/transfers']
wxpay['v2/mmpaymkttransfers/promotion/paywwsptrans2pocket']
wxpay['v2/mmpaymkttransfers/sendworkwxredpack']
// ...
;console.info(wxpay)

打印出的结构如下:

[Function (anonymous)] {
  'v3/pay/transactions/native': [Function: /v3/pay/transactions/native],
  'v3/pay/transactions/id/{transaction_id}': [Function: /v3/pay/transactions/id/{transaction_id}],
  'v3/pay/transactions/out-trade-no/{out_trade_no}': [Function: /v3/pay/transactions/out-trade-no/{out_trade_no}],
  'v3/pay/partner/transactions/native': [Function: /v3/pay/partner/transactions/native],
  'v3/marketing/busifavor/users/{openid}/coupons/{coupon_code}/appids/{appid}': [Function: /v3/marketing/busifavor/users/{openid}/coupons/{coupon_code}/appids/{appid}],
  'v3/marketing/favor/stocks/{stock_id}/use-flow': [Function: /v3/marketing/favor/stocks/{stock_id}/use-flow],
  'v3/marketing/partnerships/build': [Function: /v3/marketing/partnerships/build],
  'v3/marketing/favor/media/image-upload': [Function: /v3/marketing/favor/media/image-upload],
  'v3/bill/tradebill': [Function: /v3/bill/tradebill],
  'v3/billdownload/file': [Function: /v3/billdownload/file],
  'v2/pay/micropay': [Function: /v2/pay/micropay],
  'v2/risk/getpublickey': [Function: /v2/risk/getpublickey],
  'v2/secapi/pay/refund': [Function: /v2/secapi/pay/refund],
  'v2/mmpaymkttransfers/promotion/transfers': [Function: /v2/mmpaymkttransfers/promotion/transfers],
  'v2/mmpaymkttransfers/promotion/paywwsptrans2pocket': [Function: /v2/mmpaymkttransfers/promotion/paywwsptrans2pocket],
  'v2/mmpaymkttransfers/sendworkwxredpack': [Function: /v2/mmpaymkttransfers/sendworkwxredpack]
}

毫无疑问,这种方式结构看起来相对更简洁一些,相应地,编程语法糖,可以这么写了~

APIv3 Native下单

wxpay['v3/pay/transactions/native']({
  /*文档参数放这里就好*/
})
.then(({data: {code_url}}) => console.info(code_url))
.catch(console.error)

APIv3 查询订单

wxpay['v3/pay/transactions/id/{transaction_id}'].get({
  params: {mchid: '1230000109'},
  transaction_id: '1217752501201407033233368018'
})
.then(({data}) => console.info(data))
.catch(console.error)

APIv3 关单

wxpay['v3/pay/transactions/out-trade-no/{out_trade_no}']({
  mchid: '1230000109'
  }, {
    out_trade_no: '1217752501201407033233368018'
  }
)
.then(({status, statusText}) => console.info(status, statusText))
.catch(console.error)

APIv3 对账单下载及解析

const assert = require('assert')
const {Hash: {sha1}} = require('wechatpay-axios-plugin')

wxpay['v3/bill/tradebill'].get({
  params: {
    bill_date: '2021-02-12',
    bill_type: 'ALL',
  }
}).then(({data: {download_url, hash_value}}) => wxpay['v3/billdownload/file'].get({
  params: (new URL(download_url)).searchParams,
  signed: hash_value,
  responseType: 'arraybuffer',
})).then(res => {
  assert(sha1(res.data.toString()) === res.config.signed, 'verify the SHA1 digest failed.')
  console.info(Formatter.castCsvBill(res.data))
}).catch(console.error)

APIv2 付款码(刷卡)支付

wxpay['v2/pay/micropay']({
  appid: 'wx8888888888888888',
  mch_id: '1900000109',
  nonce_str: Formatter.nonce(),
  sign_type: 'HMAC-SHA256',
  body: 'image形象店-深圳腾大-QQ公仔',
  out_trade_no: '1217752501201407033233368018',
  total_fee: 888,
  fee_type: 'CNY',
  spbill_create_ip: '8.8.8.8',
  auth_code: '120061098828009406',
})
.then(res => console.info(res.data))
.catch(console.error)

现金红包

wxpay['v2/mmpaymkttransfers/sendredpack']({
  nonce_str: Formatter.nonce(),
  mch_billno: '10000098201411111234567890',
  mch_id: '10000098',
  wxappid: 'wx8888888888888888',
  send_name: '鹅企支付',
  re_openid: 'oxTWIuGaIt6gTKsQRLau2M0yL16E',
  total_amount: 1000,
  total_num: 1,
  wishing: 'HAPPY BIRTHDAY',
  client_ip: '192.168.0.1',
  act_name: '回馈活动',
  remark: '会员回馈活动',
  scene_id: 'PRODUCT_4',
})
.then(res => console.info(res.data))
.catch(console.error)

企业付款到零钱

wxpay['v2/mmpaymkttransfers/promotion/transfers']({
  appid: 'wx8888888888888888',
  mch_id: '1900000109',
  partner_trade_no: '10000098201411111234567890',
  openid: 'oxTWIuGaIt6gTKsQRLau2M0yL16E',
  check_name: 'FORCE_CHECK',
  re_user_name: '王小王',
  amount: 10099,
  desc: '理赔',
  spbill_create_ip: '192.168.0.1',
  nonce_str: Formatter.nonce(),
})
.then(res => console.info(res.data))
.catch(console.error)

企业付款到银行卡-获取RSA公钥

这个用法,是测试用例发掘出来的,注意请求带了第二个参数 {baseURL:'https://fraud.mch.weixin.qq.com'}

wxpay['v2/risk/getpublickey']({
  mch_id: '1900000109',
  sign_type: 'MD5',
  nonce_str: Formatter.nonce(),
}, {
  baseURL: 'https://fraud.mch.weixin.qq.com'
})
.then(res => console.info(res.data))
.catch(console.error)

发放企业红包

wxpay['v2/mmpaymkttransfers/sendworkwxredpack']({
  mch_billno: '123456',
  wxappid: 'wx8888888888888888',
  sender_name: 'XX活动',
  sender_header_media_id: '1G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu9V5w7o8K0',
  re_openid: 'oxTWIuGaIt6gTKsQRLau2M0yL16E',
  total_amount: 1000,
  wishing: '感谢您参加猜灯谜活动,祝您元宵节快乐!',
  act_name: '猜灯谜抢红包活动',
  remark: '猜越多得越多,快来抢!',
  mch_id: '1900000109',
  nonce_str: Formatter.nonce(),
})
.then(res => console.info(res.data))
.catch(console.error)

这么搞下去。。。还愁啥对接啊!一致开发体验,尽在: https://github.com/TheNorthMemory/wechatpay-axios-plugin

最后一次编辑于  2021-03-09  
点赞 3
收藏
评论

1 个评论

登录 后发表内容