您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何基于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){ });
感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何基于nodejs實現微信支付功能”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。