前提要有https的支持。支付没那么复杂,原理想清楚后就基本可以实现了。这个实例是全部微信的敏感数据都走后台处理,微信js只负责调用接口。
代码写的可能有点乱,但是只在于原理不在于形式。
以下为js文件,接收订单信息进行Submit处理。
formSubmit: function (event) {
var that=this;
var form_data = event.detail.value;
var radioValue=wx.getStorageSync('radioValue');
form_data['radioValue']=radioValue;//是否需要合同
//console.log(form_data);
this.getLogin(that,form_data);//
}
getLogin:function(that,form_data){//申请拿code
wx.login({//这里不同于拿用户所有信息那个,好像小楼昨夜又秋风大神说的,那个和这个其实是区分开的
success: function(res){
if (res.code) {
//发起网络请求
that.api_user_code(that,res,form_data);
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
}
})
}
api_user_code: function (that,res,form_data) {//后台处理微信获取用户那个code的处理
//console.log(res.code);
wx.request({
url: 'https://xxxxxxxxxxxxxx',//你的后台处理地址,我的是php后台处理
data: {
code: res.code
},
method: 'POST',
header: { 'content-type': 'application/json' },
success: function (res) {
var openid= res.data.openid;
//console.log(openid);
that.orderChuli(that,openid,form_data);
//that.paypay(openid);
}
})
}
public function api_user_code(){//php后台的微信拿code处理,跟着微信做就好
$user_code = file_get_contents('php://input', 'r');
$user_code= json_decode($user_code,true);
$code=$user_code['code'];
$appid="xxxxxxxx";
$secret="xxxxxxxxxxx";
$url="https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
$curl = curl_init();//这个curl一开始总是不成功,关键点是你走去的url是https,这个要自行百度
curl_setopt($curl, CURLOPT_URL, $url);
//curl_setopt($curl, CURLOPT_HEADER, array("Content-Type: application/json; charset=utf-8"));
//$data = array('name' =>"lijunsheng");
//$data=json_encode($data);
//$data_one=array('newlyIncrease'=>$newlyIncrease);
//print_r($data_one);
//curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$res = curl_exec($curl);
curl_close($curl);
$res= json_decode($res,true);
echo $res['data'];
//echo $url;
//$gotofind = array('code' => $code,'appid' => $appid,'secret' => $secret);
//echo json_encode($gotofind);
}
再回到小程序的js中
orderChuli:function(that,openid,form_data){//本人项目的一些购物车处理,没什么好看的,可以忽略,成功的话都是走去做支付功能的步骤
//console.log(form_data);
if(form_data.toCity ==0 ||form_data.toAddress ==0||form_data.toName ==0||form_data.toMobile ==0){
wx.showModal({
title: '提示',
content: '你的收货地址并没完整,系统将带你去到添加收货地址页面',
success: function (res) {
if (res.confirm) {
wx.navigateTo({
url: "../address/address"
})
}
}
})
}else{
var num = Math.random() * 10000 + 100;
num = parseInt(num, 10);
form_data.orderId = num;
form_data.providerCode = openid;
form_data.senderCode = "www.EF.com";
//console.log(form_data.total_money);
var total_money=form_data.total_money;
wx.showModal({
title: '提示',
content: '确定提交订单?',
success: function (res) {
if (res.confirm) {
var qingling_arr = wx.getStorageSync('qingling_arr');
//console.log(qingling_arr);
wx.request({
url: 'https://xxxxxxxxxxxxxxxx',
data: {
goods_arr: qingling_arr,
goods_address: form_data
},
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: { 'content-type': 'json' },
success: function (res) {
if(total_money == 0){//有会员码的情况下,直接后台录入数据
wx.clearStorage();
wx.showToast({
title: '成功',
icon: 'success',
duration: 1000
});
setTimeout(function () {
wx.switchTab({
url: "../index/index"
})
}, 2000);
}else{
var orderId = res.data;
that.paypay(that,openid,total_money,orderId);//关键来啦,把openid和钱和订单走去支付啦!
}
//console.log(id);
//console.log(total_money);
//console.log(res);
/*
if (res.data != "not") {
wx.clearStorage();
wx.showToast({
title: '成功',
icon: 'success',
duration: 1000
});
setTimeout(function () {
wx.switchTab({
url: "../index/index"
})
}, 2000);
}else{
wx.showToast({
title: '请填写正确会员码',
icon: 'loading',
duration: 1000
});
}
*/
}
})
}
}
})
}
}
关键点来啦!切记切记,可以拿回以前微信的支付demo来套入,就是统一支付接口那!不行就试多几次,肯定可以的!都是后台来处理的
paypay:function(that,openid,total_money,orderId){
wx.request({
url: 'https://xxxxxxxxx',//后台语言的处理
data: {
openid:openid,
total_money:total_money
},
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: { 'content-type': 'application/json' },
success: function(res){
console.log(res);
var nonceStr=res.data.nonce_str;
var appId=res.data.appid;
var pkg = 'prepay_id='+ res.data.prepay_id;
var timeStamp =res.data.timeStamp;
var paySign = res.data.paySign;
var sign = res.data.sign;
//console.log(pkg);
wx.requestPayment({
timeStamp: timeStamp,
nonceStr: nonceStr,
package: pkg,
signType: 'MD5',
paySign: paySign,
success: function(res){
that.changeOrderIdPay(orderId);
}
})
}
});
}
php后台
public function ef_wx_pay(){
include_once("efwxpay/WxPayPubHelper/WxPayPubHelper.php");//微信demo的文件,帮你处理所有的数据
$openid = file_get_contents('php://input', 'r');
$openid= json_decode($openid,true);
$total_money = file_get_contents('php://input', 'r');
$total_money= json_decode($total_money,true);
$openid=$openid['openid'];
$total_money=$total_money['total_money'];
//使用统一支付接口
$unifiedOrder = new UnifiedOrder_pub();
//echo $res;
//设置统一支付接口参数
//设置必填参数
//appid已填,商户无需重复填写
//mch_id已填,商户无需重复填写
//noncestr已填,商户无需重复填写
//spbill_create_ip已填,商户无需重复填写
//sign已填,商户无需重复填写
$unifiedOrder->setParameter("body","EF礼品订购");//商品描述
//自定义订单号,此处仅作举例
$timeStamp = time();
$out_trade_no = WxPayConf_pub::APPID."$timeStamp";
$unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号
$unifiedOrder->setParameter("total_fee","$total_money"."00");//总金额
//$unifiedOrder->setParameter("total_fee","$res"."00");//总金额
$unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址
$unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
//非必填参数,商户可根据实际情况选填
$unifiedOrder->setParameter("openid","$openid");//用户标识
//获取统一支付接口结果
$unifiedOrderResult = $unifiedOrder->getResult();
//商户根据实际情况设置相应的处理流程
if ($unifiedOrderResult["return_code"] == "FAIL")
{
//商户自行增加处理流程
echo "通信出错:".$unifiedOrderResult['return_msg']."<br>";
}
elseif($unifiedOrderResult["result_code"] == "FAIL")
{
//商户自行增加处理流程
echo "错误代码:".$unifiedOrderResult['err_code']."<br>";
echo "错误代码描述:".$unifiedOrderResult['err_code_des']."<br>";
}else{
//echo json_encode($unifiedOrderResult);
//$paySign=$this->againQ($unifiedOrderResult);
//$unifiedOrderResult['paySign']=$paySign;
$nonce_str=$unifiedOrderResult['nonce_str'];
$mch_id=$unifiedOrderResult['mch_id'];
$appid=$unifiedOrderResult['appid'];
$prepay_id=$unifiedOrderResult['prepay_id'];
$key="xkF5KMzLR8vjZmJDBV34uqPcdXHbUhAs";
$stringA="appId=$appid&nonceStr=$nonce_str&package=".'prepay_id='."$prepay_id&signType=MD5&timeStamp=$timeStamp";//这里记得顺序和大小写!切记!
$stringSignTemp="$stringA&key=$key";
$paySign=MD5($stringSignTemp);
$paySign=strtoupper($paySign);
$unifiedOrderResult['paySign']=$paySign;
$unifiedOrderResult['timeStamp']="$timeStamp";
echo json_encode($unifiedOrderResult);
}
}
小程序js最后收尾,没什么好写的,就处理下完成订单后的动作
changeOrderIdPay:function(orderId){
var url="https://xxxxxx";
wx.request({
url: url,
data: {
orderId:orderId
},
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: { 'content-type': 'application/json' },
success: function(res){
wx.clearStorage();
}
})
}
版权声明:本文为sinat_23900111原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。