本文主要是介绍nodejs开发微信支付之接收退款申请通知,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
nodejs开发微信支付之退款结果通知url
- 前言
- 步骤
- 1.先对加密串A进行base64解密
- 2.对商户key做md5,得到32位小写key
- 3.用key*对加密串B做AES-256-ECB解密
前言
退款成功后返回的xml格式示例
步骤
加密字符串是req_info
解密之前,我们需要把xml格式转换为json格式:
const notificationXML = ctx.request.body.xmlconst notificationJSON = {}for (let [key, value] of Object.entries(notificationXML)) {notificationJSON[key] = value[0]}let notionResult = notificationJSON['req_info']
1.先对加密串A进行base64解密
let resultInfo = Buffer.from(notionResult, 'base64')
2.对商户key做md5,得到32位小写key
const key = 'KmfuRzZM9lfxHJL'const md5Key = await crypto.createHash('md5').update(key).digest('hex')
3.用key*对加密串B做AES-256-ECB解密
我们将解密过程封装成一个方法,这样调用起来比较方便
/*** aes解密微信回调通知* @param data 待解密内容* @param key 必须为32位私钥* @returns {string}*/
exports.decryption = function (data, key, iv) {if (!data) {return ''}iv = iv || ''var clearEncoding = 'utf8'var cipherEncoding = 'base64'var cipherChunks = []var decipher = crypto.createDecipheriv('aes-256-ecb', key, iv)decipher.setAutoPadding(true)cipherChunks.push(decipher.update(data, cipherEncoding, clearEncoding))cipherChunks.push(decipher.final(clearEncoding))return cipherChunks.join('')
}
解密方法封装好了,那就开始调用
const fxp = require('fast-xml-parser')let iv = Buffer.alloc(0) // 设置偏移量let decxml = exports.decryption(resultInfo, md5Key, iv) // 解码console.log(decxml)let reg = new RegExp('root>', 'g')decxml = decxml.replace(reg, 'xml>') // 转化为xml格式console.log(decxml)const xml2json = fxp.parse(decxml) // xml转对象console.log(xml2json)
剩下的就看自己的业务需求了,值都取出来了想干嘛干嘛,服务号的通知什么的也很容易,退款通知这种,下一个博客见。
来自小仙女的代码之路.
附带大神的链接 https://www.oecom.cn/nodejs-wechat-pay-4/
这篇关于nodejs开发微信支付之接收退款申请通知的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!