这篇文章主要介绍了如何基于nodejs实现微信支付功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
创新互联是一家专注于成都网站设计、成都网站制作与策划设计,临湘网站建设哪家好?创新互联做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:临湘等地区。临湘做网站价格咨询:18980820575通过nodejs使用微信支付最重要的就是微信的签名,在这里md5后的字符必须转化为大写
一、回复微信通知消息模版
message.ejs
]]> ]]>
二、微信支付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 = ""; formData += " "; 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;" + appid + " "; //appid formData += "" + attach + " "; //附加数据 formData += "" + body + ""; formData += "" + mch_id + " "; //商户号 formData += "" + nonce_str + " "; //随机字符串,不长于32位。 formData += "" + notify_url + " "; formData += "" + openid + " "; formData += "" + bookingNo + " "; formData += "61.50.221.43 "; formData += "" + total_fee + " "; formData += "JSAPI "; formData += "" + this.paysignjsapi(appid, attach, body, mch_id, nonce_str, notify_url, openid, bookingNo, '61.50.221.43', total_fee, 'JSAPI') + " "; formData += "
三、在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){ });
感谢你能够认真阅读完这篇文章,希望小编分享的“如何基于nodejs实现微信支付功能”这篇文章对大家有帮助,同时也希望大家多多支持创新互联成都网站设计公司,关注创新互联成都网站设计公司行业资讯频道,更多相关知识等着你来学习!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、网站设计器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。