E企盈营销工具技术服务商 热线:4006-838-530

小程序支付

E企盈直播平台营销卖货系统

1.开通微信支付和微信商户号这个过程就和开通服务号的微信支付过程一样,没有什么可以说的。 2.获得用户的openid首页我们需要在小程序的客户端js中获取当前用户的openid,通过调用wx.login方法可以得到用户的code,然后开发者服务器使用登录凭证 code 获取 openid。wx.login({      success: function(res) {        if (res.code) {          //发起网络请求          wx.request({            url: ‘https://qifeng.site/onLogin’,            method: ‘POST’,            data: {              code: res.code            },            success: function(res) {                var openid = res.data.openid;            },            fail: function(err) {                console.log(err)            }          })        } else {          console.log(‘获取用户登录态失败!’ + res.errMsg)        }      }    });3.获取prepay_id和支付签名验证paySign这一步的过程就和服务号里的微信支付过程一样,分为客户端和服务器端 首先来看一下客户端js 在服务号里,我们是通过如下的代码来调起支付功能///在小程序里,我们是通过wx.requestPayment方法来调起支付功能,当然在这之前,我们先要获取prepay_id。wx.request({     url: ‘https://qifeng.site/service/getPay’,      method: ‘POST’,     data: {       pkg:pkg,  /*订单号*/       total_fee:total_fee,   /*订单金额*/       nonceStr:nonceStr       openid:openid     },     header: {         ‘content-type’: ‘application/json’     },     success: function(res) {        wx.requestPayment({           ‘timeStamp’: ”,//即当前的时间           ‘nonceStr’: ”,//随机字符串,长度为32个字符以下。           ‘package’: ”,//统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*           ‘signType’: ‘MD5’,//签名算法,暂支持 MD5           ‘paySign’: ”,//签名,具体签名方案参见[小程序支付接口文档;](https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=3)           ‘success’:function(res){//调用支付成功           },           ‘fail’:function(res){//           },           complete(res){//接口调用结束的回调函数(调用成功、失败都会执行)           }        })     },     fail: function(err) {         console.log(err)     } })//在服务号里,通过如下的代码来调起支付功能function jsApiCall() {      WeixinJSBridge.invoke(          ‘getBrandWCPayRequest’,          {             “appId”:””,     //公众号名称,由商户传入                  “timeStamp”:””,         //时间戳,自1970年以来的秒数                  “nonceStr”:””, //随机串                  “package”:”prepay_id=%=prepay_id%”,                  “signType”:”MD5″,         //微信签名方式:                  “paySign”:”%=_paySignjs%” //微信签名          },          function(res){              WeixinJSBridge.log(res.err_msg);              if( res.err_msg ==”get_brand_wcpay_request:ok”){                  alert(“支付成功!”);              }else{                  alert(“支付失败!”);              }          }      );  }那在服务器端主要要实现的是prepay_id的获取和签名paySignvar bookingNo = req.param(“bookingNo”); var total_fee = req.param(“total_fee”); var openid = req.param(“openid”); var body = “费用说明”; var url = “https://api.mch.weixin.qq.com/pay/unifiedorder”; var formData = “xml”; formData += “appidappid/appid”; //appid formData += “attachtest/attach”; formData += “body” + body + “/body”; formData += “mch_idmch_id/mch_id”; //商户号 formData += “nonce_strnonce_str/nonce_str”; formData += “notify_urlnotify_url/notify_url”; formData += “openid” + openid + “/openid”; formData += “out_trade_no” + bookingNo + “/out_trade_no”; formData += “spbill_create_ipspbill_create_ip/spbill_create_ip”; formData += “total_fee” + total_fee + “/total_fee”; formData += “trade_typeJSAPI/trade_type”; formData += “sign” + paysignjsapi(appid, attach, body, mch_id, nonce_str, notify_url, openid, bookingNo, spbill_create_ip, total_fee, ‘JSAPI’) + “/sign”; formData += “/xml”; request({     url: url,     method: ‘POST’,     body: formData }, function(err, response, body) {     if(!err && response.statusCode == 200) {         var prepay_id = getXMLNodeValue(‘prepay_id’, body.toString(“utf-8″));         var tmp = prepay_id.split(‘[‘);         var tmp1 = tmp[2].split(‘]’);         //签名         var _paySignjs = paysignjs(appid, mch_id, ‘prepay_id=’ + tmp1[0], ‘MD5’,timeStamp);         var o = {             prepay_id: tmp1[0],             _paySignjs: _paySignjs         }         res.send(o);     } });////用到的一些其他的方法function paysignjs(appid, nonceStr, package, signType, timeStamp) {    var ret = {        appId: appid,        nonceStr: nonceStr,        package: package,        signType: signType,        timeStamp: timeStamp    };    var string = raw1(ret);    string = string + ‘&key=’+key;    console.log(string);    var crypto = require(‘crypto’);    return crypto.createHash(‘md5’).update(string, ‘utf8’).digest(‘hex’);};function raw1(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;};function paysignjsapi(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 = raw(ret);    string = string + ‘&key=’+key;    var crypto = require(‘crypto’);    return crypto.createHash(‘md5’).update(string, ‘utf8’).digest(‘hex’);};function raw(args) {    var keys = Object.keys(args);    keys = keys.sort()    var newArgs = {};    keys.forEach(function(key) {        newArgs[key.toLowerCase()] = args[key];    });    var string = ”;    for(var k in newArgs) {        string += ‘&’ + k + ‘=’ + newArgs[k];    }    string = string.substr(1);    return string;};function getXMLNodeValue(node_name, xml) {    var tmp = xml.split(“” + node_name + “”);    var _tmp = tmp[1].split(“/” + node_name + “”);    return _tmp[0];}支付就这样了本文转载自简书https://www.jianshu.com/p/72f5c1e3f8a5 感谢网友贡献

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 小程序支付
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏