基于nodejs实现微信支付功能
程序员文章站
2022-07-28 19:38:54
本文实例为大家分享了nodejs实现微信支付的具体代码,供大家参考,具体内容如下
通过nodejs使用微信支付最重要的就是微信的签名,在这里md5后的字符必须转化为大...
本文实例为大家分享了nodejs实现微信支付的具体代码,供大家参考,具体内容如下
通过nodejs使用微信支付最重要的就是微信的签名,在这里md5后的字符必须转化为大写
一、回复微信通知消息模版
message.ejs
<xml> <return_code><![cdata[<%-return_code%>]]></return_code> <return_msg><![cdata[<%=return_msg%>]]></return_msg> </xml>
二、微信支付model文件代码
wxpay.js
var config = require('../config'); //配置文件 appid 等信息 var q = require("q"); var request = require("request"); var crypto = require('crypto'); var ejs = require('ejs'); var fs = require('fs'); var key = "此处为申请微信支付的api密码"; var messagetpl = fs.readfilesync(__dirname + '/message.ejs', 'utf-8'); var wxpay = { getxmlnodevalue: function(node_name, xml) { var tmp = xml.split("<" + node_name + ">"); var _tmp = tmp[1].split("</" + node_name + ">"); return _tmp[0]; }, raw: function(args) { var keys = object.keys(args); keys = keys.sort() var newargs = {}; keys.foreach(function(key) { newargs[key] = args[key]; }); var string = ''; for (var k in newargs) { string += '&' + k + '=' + newargs[k]; } string = string.substr(1); return string; }, paysignjs: function(appid, noncestr, package, signtype, timestamp) { var ret = { appid: appid, noncestr: noncestr, package: package, signtype: signtype, timestamp: timestamp }; var string = this.raw(ret); string = string + '&key=' + key; var sign = crypto.createhash('md5').update(string, 'utf8').digest('hex'); return sign.touppercase(); }, paysignjsapi: function(appid, attach, body, mch_id, nonce_str, notify_url, openid, out_trade_no, spbill_create_ip, total_fee, trade_type) { var ret = { appid: appid, attach: attach, body: body, mch_id: mch_id, nonce_str: nonce_str, notify_url: notify_url, openid: openid, out_trade_no: out_trade_no, spbill_create_ip: spbill_create_ip, total_fee: total_fee, trade_type: trade_type }; var string = this.raw(ret); string = string + '&key=' + key; //key为在微信商户平台(pay.weixin.qq.com)-->账户设置-->api安全-->密钥设置 var crypto = require('crypto'); var sign = crypto.createhash('md5').update(string, 'utf8').digest('hex'); return sign.touppercase(); }, // 随机字符串产生函数 createnoncestr: function() { return math.random().tostring(36).substr(2, 15); }, // 时间戳产生函数 createtimestamp: function() { return parseint(new date().gettime() / 1000) + ''; },
// 此处的attach不能为空值 否则微信提示签名错误 order: function(attach, body, mch_id, openid, bookingno, total_fee, notify_url) { var deferred = q.defer(); var appid = config.member_config.appid; var nonce_str = this.createnoncestr(); var timestamp = this.createtimestamp(); var url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; var formdata = "<xml>"; formdata += "<appid>" + appid + "</appid>"; //appid formdata += "<attach>" + attach + "</attach>"; //附加数据 formdata += "<body>" + body + "</body>"; formdata += "<mch_id>" + mch_id + "</mch_id>"; //商户号 formdata += "<nonce_str>" + nonce_str + "</nonce_str>"; //随机字符串,不长于32位。 formdata += "<notify_url>" + notify_url + "</notify_url>"; formdata += "<openid>" + openid + "</openid>"; formdata += "<out_trade_no>" + bookingno + "</out_trade_no>"; formdata += "<spbill_create_ip>61.50.221.43</spbill_create_ip>"; formdata += "<total_fee>" + total_fee + "</total_fee>"; formdata += "<trade_type>jsapi</trade_type>"; formdata += "<sign>" + this.paysignjsapi(appid, attach, body, mch_id, nonce_str, notify_url, openid, bookingno, '61.50.221.43', total_fee, 'jsapi') + "</sign>"; formdata += "</xml>"; var self = this; request({ url: url, method: 'post', body: formdata }, function(err, response, body) { if (!err && response.statuscode == 200) { console.log(body); var prepay_id = self.getxmlnodevalue('prepay_id', body.tostring("utf-8")); var tmp = prepay_id.split('['); var tmp1 = tmp[2].split(']'); //签名 var _paysignjs = self.paysignjs(appid, nonce_str, 'prepay_id=' + tmp1[0], 'md5', timestamp); var args = { appid: appid, timestamp: timestamp, noncestr: nonce_str, signtype: "md5", package: tmp1[0], paysign: _paysignjs }; deferred.resolve(args); } else { console.log(body); } }); return deferred.promise; }, //支付回调通知 notify: function(obj) { var output = ""; if (obj.return_code == "success") { var reply = { return_code: "success", return_msg: "ok" }; } else { var reply = { return_code: "fail", return_msg: "fail" }; } output = ejs.render(messagetpl, reply); return output; }, }; module.exports = wxpay;
三、在express router中调用wxpay
//微信支付demo router.get('/order', function(req, res, next){ var attach = "1276687601"; var body = "测试支付"; var mch_id = "1111111"; //商户id var openid = "111111"; var bookingno = "201501806125346"; //订单号 var total_fee = 10; var notify_url = "http://localhost/wxpay/notify"; //通知地址 wxpay.order(attach, body, mch_id, openid, bookingno, total_fee, notify_url).then(function(data){ res.render('wxpay', {args: data}); }); });
//微信回调通知 采用数据流形式读取微信返回的xml数据 此处不在累赘 router.post('/notify', function(req, res, next){ });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读