在Easywechat官方文档中,微信公众号网页支付和扫码支付都有代码示例,小程序端的示例缺失,我根据微信官方文档和lavarl的示例,整理出TP5+Easywechat小程序支付功能,以下是小程序端代码:
wxpay: function (e) {
var that = this;
var data = {
"ctl": 'pay',
"act": "min",
"productid": 1,
"licenseid": 1,
"buynum": 1,
"customerid": 1,
"order_amount": 1,
"openid": wx.getStorageSync("openid"),
'hash': app.globalData.hash
};
util.minRequest(data, function (res) {
var payres = res.data.data;
if (payres.result_code == "FAIL") {
wx.showModal({
title: payres.err_code,
content: payres.err_code_des,
})
return false;
}
var payconfig = payres.data;
// 唤起支付
wx.requestPayment({
timeStamp: payconfig['timeStamp'],
nonceStr: payconfig['nonceStr'],
package: payconfig['package'],
signType: payconfig['signType'],
paySign: payconfig['paySign'],
'success': function (res) {
},
'fail': function (res) {
},
'complete': function (res) {
}
})
}, "http://localhost/api/minprogram");
},
在网站后端实现支付参数的装配
$app = Factory::payment($config);
$res = $app->order->unify([
'product_id' => $order['productid'],
'body' => $order['subject'], 'out_trade_no' => $orderlog['out_trade_no'], 'total_fee' => $order['total_fee']*100,
'trade_type' => 'JSAPI',
'openid' => $data['openid']
]);
if (array_key_exists('result_code', $res) && $res['result_code'] == 'FAIL') {
return getJsonCode($res);
}
$prepay_id = $res['prepay_id'];
$paysign = $app->jssdk->sdkConfig($prepay_id);//如果这里是app的配置生成,改为 $app->jssdk->appConfig($prepay_id);
return getJsonCode($paysign);