微信小程序支付功能完整dome-php

使用uniapp开发示例,原生需将uni标签改为wx即可(前端)

uni.request({ //支付统一下单

url: this.$api + '/index/user/uniforder',

method: 'POST',

header: {

'content-type': 'application/x-www-form-urlencoded',

'token': this.userInfo.token

},

data: {

name:this.obj.name,

uid:this.userInfo.uid,

orderId:'xiuli'+rest.data.data,

price:this.obj.jiage*100

},

success: (res)=> {

if(res.data.code==600){

uni.requestPayment({ //唤起支付

    provider: 'wxpay',

    timeStamp: res.data.data.timeStamp,

    nonceStr: res.data.data.nonceStr,

    package: res.data.data.package,

    signType: res.data.data.signType,

    paySign: res.data.data.paySign,

    success:(res)=> {

console.log(res)

uni.request({ //订单支付

url: this.$api + '/index/user/buyorder',

method: 'POST',

header: {

'content-type': 'application/x-www-form-urlencoded',

'token': this.userInfo.token

},

data: {

orderId:'xiuli'+rest.data.data,

id:rest.data.data

},

success: (res)=> {

if(res.data.code==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'] = 'https://www.weixin.qq.com/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('https://api.mch.weixin.qq.com/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('https://api.mch.weixin.qq.com/pay/unifiedorder',$xml);//统一下单接口

  //XML转数组

  $xmlarr = json_decode(json_encode(simplexml_load_string($rexml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);


  if(!isset($xmlarr['prepay_id'])){

return PubClass::jsonErrorData(500,$xmlarr);

  }

  //支付返回数组

  $reData['nonceStr'] = $dataArr['nonce_str'];

  $reData['package'] = 'prepay_id='.$xmlarr['prepay_id'];

  $reData['signType'] = 'MD5';

  $reData['timeStamp'] = $time;


  //二次签名数组

  $newdata['appId'] = $system['appid'];

  $newdata['nonceStr'] = $reData['nonceStr'];

  $newdata['package'] = $reData['package'];

  $newdata['signType'] = $reData['signType'];

  $newdata['timeStamp'] = $reData['timeStamp'];

  $newstring = ''; //支付二次签名

  foreach($newdata as $key=>$value){

    $newstring .= $key.'='.$value.'&';

  }

  $newstring = $newstring."key=".$system['miyao'];

  $paySign = strtoupper(MD5($newstring));

  $reData['paySign'] = $paySign;

  return PubClass::jsonSuccessData($reData);

  }

公共类:数组转xml

public static function arr2xml($data, $root = true){

    $str="";

    if($root)$str .= "";

    foreach($data as $key => $val){

        if(is_array($val)){

            $child = arr2xml($val, false);

            $str .= "<$key>$child";

        }else{

            $str.= "<$key>$val";

        }

    }

    if($root)$str .= "";

    return $str;

}

2、支付订单

public function buyorder(){

      $post = input('post.');

  //微信支付成功订单查询操作

  //订单查询数组

  $system = Db::name('system')->order('id desc')->find();

  $orderArr['appid'] = $system['appid'];

  $orderArr['mch_id'] = $system['shanghuhao'];

  $orderArr['nonce_str'] = md5(uniqid(microtime(true),true).rand());

  $orderArr['out_trade_no'] = $post['orderId'];

  $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('https://api.mch.weixin.qq.com/pay/orderquery',$orderxml);//查询订单接口

  //XML转数组

  $xmlorder = json_decode(json_encode(simplexml_load_string($orders, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

  if(isset($xmlorder['transaction_id'])){

  $zhifuOrder = $xmlorder['transaction_id'];

$data=Db::name('buylog')->where(['id'=>$post['id']])->update(['type'=>1,'transaction_id'=>$zhifuOrder]);


return PubClass::jsonSuccessData($data);

  }else{

return PubClass::jsonErrorData(500,'该订单未支付');

  }

  }

注:基于TP5框架,只给了关键类,其余的公共类自己随便定义

也可使用 V3 版本支付,接口地址:https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi

签名方式支持:V3:RSA    V2:MD5,HMAC-SHA256 ;微信开发文档菜单栏不兼容360浏览器

你可能感兴趣的:(微信小程序支付功能完整dome-php)