您好,登錄后才能下訂單哦!
這篇文章主要介紹小程序怎么實現支付功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
微信小程序的支付和微信公眾號的支付是類似的,對比起來還比公眾號支付簡單了一些,我們只需要調用微信的統一下單接口獲取prepay_id之后我們在調用微信的支付即可。
今天我們來封裝一般node的支付接口!!!
首先調用統一下單接口我們需要知道一些信息
var bookingNo = 'davdian' + this.createNonceStr() + this.createTimeStamp() var deferred = Q.defer() var appid = 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) { 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
這個是一個統一下單接口的代碼,我們需要appid小程序公眾號id,mch_id商戶號id,openid小程序的唯一標實,key支付用的密碼,剩下的參數都是訂單的信息和價格之類的,本人require進q模塊使用promise,這個因人而異,可以根據自己需要來。我們需要請求api.mch.weixin.qq.com/pay/unifiedorder接口
注意:這里我們傳遞的formdata是一個xml而不是json
然后我們需要簽名方法,這里我們需要封裝兩個方法,一個是簽名方法調用統一下單接口會用到,另一個是調用小程序支付用到
統一下單接口sign:
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 var crypto = require('crypto') var sign = crypto.createHash('md5').update(string, 'utf8').digest('hex') return sign.toUpperCase() 支付sign:
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() 注意加密的時候我們獲取的是string而不是一個json,所以我們需要吧json轉換成string,代碼如下:
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 統一下單接口返回的是帶有prepay_id的xml,所以我們需要一個方法進行解析,代碼如下:
var tmp = xml.split("<" + node_name + ">") var _tmp = tmp[1].split("</" + node_name + ">") return _tmp[0] 最后我們只需要把這些連接到一起就是可以獲取所有微信支付所需參數,代碼如下:
//微信小程序支付封裝,暫支持md5加密,不支持sha1 /** ***create order by jianchep 2016/11/22 **/ var config = require('../config/weapp.js') var Q = require("q") var request = require("request") var crypto = require('crypto') var ejs = require('ejs') var fs = require('fs') var key = config.key module.exports = { // 獲取prepay_id getXMLNodeValue: function(node_name, xml) { var tmp = xml.split("<" + node_name + ">") var _tmp = tmp[1].split("</" + node_name + ">") return _tmp[0] }, // object-->string 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 }, // 隨機字符串產生函數 createNonceStr: function() { return Math.random().toString(36).substr(2, 15) }, // 時間戳產生函數 createTimeStamp: function() { return parseInt(new Date().getTime() / 1000) + '' }, // 支付md5加密獲取sign 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() }, // 統一下單接口加密獲取sign 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 var crypto = require('crypto') var sign = crypto.createHash('md5').update(string, 'utf8').digest('hex') return sign.toUpperCase() }, // 下單接口 order: function(attach, body, mch_id, openid, total_fee, notify_url) { var bookingNo = 'davdian' + this.createNonceStr() + this.createTimeStamp() var deferred = Q.defer() var appid = 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) { 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 } } 之后我們封裝下單接口:
unifiedorder: function (req, res) { var body = "測試支付" var openid = "openid" var total_fee = 1 var notify_url = "http://localhost/notify" var mch_id = config.shopId var attach = "測試" wxpay.order(attach, body, mch_id, openid, total_fee, notify_url) .then(function(data){ console.log('data--->', data, 123123) res.json(data) }) },
然后我們只需要在小程序里面調用這個接口,就會獲取到所有的支付需要信息,再掉微信支付即可。
這里說幾個小程序支付的坑:
1.統一下單接口是xml(這個不只是小程序,公眾號也是),返回值也是xml格式需要自己獲取prepay_id,
2.簽名算法要帶上key,最后要轉換成大些
3.微信支付的sign算法也要帶上appid(這個不科學,深坑)
4.簽名算法一定不要用json拼接key
以上是“小程序怎么實現支付功能”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。