欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序 支付简单实例及注意事项

程序员文章站 2023-12-04 13:18:40
微信小程序 支付 微信小程序的支付和微信公众号的支付是类似的,对比起来还比公众号支付简单了一些,我们只需要调用微信的统一下单接口获取prepay_id之后我们在调用微信的...

微信小程序 支付

微信小程序的支付和微信公众号的支付是类似的,对比起来还比公众号支付简单了一些,我们只需要调用微信的统一下单接口获取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,这个因人而异,可以根据自己需要来。我们需要请求https://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

感谢阅读,希望能帮助到大家,谢谢大家对本站 的支持!