内容详情 您现在的位置是: 首页> Js
微信小程序支付流程操作及源码讲解
发布时间:2021-01-24 18:26 已围观:3518
摘要微信小程序详细支付流程,操作及源码讲解
首先有自己的小程序以及商户平台;
-
进入小程序后台,绑定商户平台,获得appid和mch_id,两者需具备绑定关系,以此来使用微信支付提供的开放接口;
根据图操作,进入商户平台:
商户平台中添加小程序appid,绑定成功后,进入小程序后台确定;
微信官方文档 https://pay.weixin.qq.com/static/pay_setting/appid_protocol.shtml
-
代码操作:
在小程序的代码中调用wx.login()来向微信获取到code,拿到了之后把code通过request传给商户服务器,再由服务器来跟微信服务器要session_key和openID
小程序中 ,buy.js
buyVip: function() {
var a = this;
dataApi.wxPay({
uid: wx.getStorageSync("uid"),//用户id
id: 15,
type: 2,//类型
auth_selected_str:a.data.auth_selected_str,//选择的支付套餐标识
}).then(function(t) {
console.log(t);//获取设置的支付参数
var orderid=t.data.orderid;
wx.requestPayment({
timeStamp: String(t.data.timeStamp),//时间戳
nonceStr: t.data.nonceStr,//随机字符串
package: t.data.package,//统一下单接口返回的 prepay_id 参数值
signType: t.data.signType,//签名算法
paySign: t.data.paySign,//签名
success: function(t) {
wx.showToast({
icon: "success",
title: "支付成功"
});
wx.switchTab({//支付成功,跳转
url: "../payOk/payOk"
});
},
fail: function(t) {
wx.showToast({
icon: "none",
title: "支付失败,请重试~"
});
}
});
}).catch(function(t) {
console.log(t);
})
},
服务器后端:
public function dopageWxPay()
{
global $_W;
global $_GPC;
$uid = intval($_GPC['uid']);
$type = intval($_GPC['type']);
$id = intval($_GPC['id']);
$auth_selected_str =$_GPC['auth_selected_str'];
//获取支付设置
$setting = pdo_get('g_setting', ['weid' => $_W['uniacid']], ['pay_open','auth']);
$pay_open=$setting?$setting['pay_open']:'';
if ($pay_open != 1) {
return $this->result(1, '未开启支付', []);
}
//支付套餐数组
$auth=$setting?unserialize($setting['auth']):'';
$price=0;//根据标识获取价格
if($auth){
foreach ($auth as $k=>$v){
if($k==$auth_selected_str){
$price=$v;
}
}
}
if ($price>0) {
$openid = pdo_getcolumn('g_user', ['weid' => $_W['uniacid'], 'id' => $uid], 'openid');
$order_sn = getNonceStr(8) . date('his') . getNonceStr(8);//随机订单号
$insert = [];
$insert['weid'] = $_W['uniacid'];
$insert['type'] = $type;
$insert['out_trade_no'] = $order_sn;
$insert['userid'] = $uid;
$insert['openid'] = $openid;
$insert['order_status'] = 0;//支付状态
$insert['all_money'] = $price;
$insert['true_money'] = $price;
$insert['createtime'] = TIMESTAMP;
$insert['msg'] = '微信支付';
$insert['dataid'] = $id;
pdo_insert('g_order', $insert);//添加订单记录
$orderid = pdo_insertid();
$sys = pdo_fetch('SELECT mchid,pay_secret FROM ' . tablename('g_setting') . ' WHERE weid = :weid ', [':weid' => $_W['uniacid']]);
$nonce_str = getNonceStr(32);
$fee = $price * 100;
$spbill_create_ip = $_SERVER['REMOTE_ADDR'];
$notify = $_W['siteroot'] . 'addons/go/wxresult.php';
$orderinfo = [
'appid' => $_W['account']['key'],
'mch_id' => $sys['mchid'],
'nonce_str' => $nonce_str,
'body' => '资料购买',
'out_trade_no' => $order_sn,
'total_fee' => $fee,
'spbill_create_ip' => $spbill_create_ip,
'notify_url' => $notify,
'trade_type' => 'JSAPI',
'openid' => $openid
];
$sign = getSign($orderinfo, $sys['pay_secret']);
$orderinfo['sign'] = $sign;
$xml = arrayToXml($orderinfo);
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$res = api_notice_increment($url, $xml);
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
$returnarr = json_decode(json_encode($xmlstring), true);
$data = [];
if (($returnarr['return_code'] == 'SUCCESS') && ($returnarr['result_code'] == 'SUCCESS')) {
$prepay_id = $returnarr['prepay_id'];
$update = [];
$update['transaction_sn'] = $prepay_id;
pdo_update('g_order', $update, ['id' => $orderid, 'weid' => $_W['uniacid']]);
$data = [
'appId' => $_W['account']['key'],
'timeStamp' => TIMESTAMP,
'nonceStr' => $nonce_str,
'package' => 'prepay_id=' . $prepay_id,
'signType' => 'MD5'
];
$paySign = getSign($data, $sys['pay_secret']);
$data['paySign'] = $paySign;
$data['orderid'] =$orderid;
$data['price'] =$price;
return $this->result(0, '获取支付参数', $data);
} else {
$data['msg'] = $returnarr['return_msg'];
return $this->result(1, '支付失败,请重试', $data);
}
}else{
return $this->result(1, '不需要购买', []);
赞一个 (29)
上一篇: 小程序登录、用户信息相关接口调整说明
下一篇: 微信小程序直播