⼩程序⽀付功能完整dome-php
使⽤uniapp开发⽰例,原⽣需将uni标签改为wx即可(前端)quest({ //⽀付统⼀下单
url: this.$api + '/index/user/uniforder',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded',
'token': ken
},
data: {
name:this.obj.name,
uid:this.userInfo.uid,
orderId:'xiuli'+rest.data.data,
price:this.obj.jiage*100
},
success: (res)=> {
if(de==600){
provider: 'wxpay',
timeStamp: res.data.data.timeStamp,
nonceStr: res.Str,
package: res.data.data.package,
signType: res.data.data.signType,
paySign: res.data.data.paySign,
success:(res)=> {
console.log(res)
url: this.$api + '/index/user/buyorder',
method: 'POST',制作查询类小程序
header: {
'content-type': 'application/x-www-form-urlencoded', 'token': ken
},
data: {
orderId:'xiuli'+rest.data.data,
id:rest.data.data
},
success: (res)=> {
if(de==600){
uni.navigateTo({
url: 'order?type'+1
});
}
else{
uni.showToast({
icon:'error',
title: '⽀付失败',
duration: 2000,
success:(res)=>{
uni.navigateTo({
url: 'order?type'+0
});
}
});
}
},
})
},
fail:(err)=> {
uni.showToast({
icon:'error',
title: '⽀付取消',
duration: 2000
});
}
});
}
else{
uni.showToast({
icon:'error',
title: res.data.msg,
duration: 2000
});
}
}
})
PHP接⼝封装(后端)1、统⼀下单
public function uniforder(){
$post = input('post.');
if(!isset($post['uid'])){
return PubClass::jsonErrorData(500,'参数不完整');
}
$user = Db::name('user')->where(['id'=>$post['uid']])->find();
if(!$user){
return PubClass::jsonErrorData(500,'该⽤户不存在');
}
$system = Db::name('system')->order('id desc')->find();
$time = (string)time();
//统⼀下单数组
$dataArr['appid'] = $system['appid'];
$dataArr['body'] = $post['name'];
$dataArr['mch_id'] = $system['shanghuhao'];
$dataArr['nonce_str'] = md5(uniqid(microtime(true),true).rand());
$dataArr['notify_url'] = 'www.weixin.qq/wxpay/pay.php';  $dataArr['openid'] = $user['openid'];
$dataArr['out_trade_no'] = $post['orderId'];
$dataArr['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];
$dataArr['total_fee'] = $post['price'];
$dataArr['trade_type'] = 'JSAPI';
//订单查询数组
$orderArr['appid'] = $system['appid'];
$orderArr['mch_id'] = $system['shanghuhao'];
$orderArr['nonce_str'] = $dataArr['nonce_str'];
$orderArr['out_trade_no'] = $dataArr['out_trade_no'];
$ordertring = ''; //查询订单签名
foreach($orderArr as $key=>$value){
$ordertring .= $key.'='.$value.'&';
}
$ordertring = $ordertring."key=".$system['miyao'];
$orderSign = strtoupper(MD5($ordertring));
$orderArr['sign'] = $orderSign;
$orderxml = PubClass::arr2xml($orderArr);
$orders = PubClass::curl_get('h.weixin.qq/pay/orderquery',$orderxml);//查询订单接⼝  //XML转数组
$xmlorder = json_decode(json_encode(simplexml_load_string($orders, 'SimpleXMLElement',
LIBXML_NOCDATA)), true);
if(isset($xmlorder['transaction_id'])){
return PubClass::jsonErrorData(500,'该订单已⽀付');
}
$string = ''; //统⼀下单签名
foreach($dataArr as $key=>$value){
$string .= $key.'='.$value.'&';
}
$string = $string."key=".$system['miyao'];
$dataArr['sign'] = strtoupper(MD5($string));
$xml = PubClass::arr2xml($dataArr);
$rexml = PubClass::curl_get('h.weixin.qq/pay/unifiedorder',$xml);//统⼀下单接⼝

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。