小程序php支付,先後端分離

小程序端:小程序

xml:api

<button type="default" bindtap="payOrder">支付</button>
js:
payOrder: function (event) {
wx.request({
url: 'http://test.dailingmei.com/api/Mini/miniGetPayMoney',
data: { //發送給後臺的數據
token: wx.getStorageSync('token'),
order_list_id: "88"
},
header: { //請求頭
'content-type': 'application/x-www-form-urlencoded'
},
method: "POST", //get爲默認方法/POST
success: function (res) {
console.log(res);
wx.requestPayment({
timeStamp: res.data.data.result.timeStamp,
nonceStr: res.data.data.result.nonceStr,
package: res.data.data.result.package,
signType: 'MD5',
paySign: res.data.data.result.paySign,
success: function (res) {
// success
console.log(res);
},
fail: function (res) {
// fail
console.log(res);
},
complete: function (res) {
// complete
console.log(res);
}
})
},
fail: function (err) { }, //請求失敗
complete: function () { } //請求完成後執行的函數
})
},
 
後臺:
use think\Config;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Log;

public function miniGetPayMoney(){    if ($this->request->isPost()) {        $validate = new \think\Validate(['order_list_id'=>'require']);        $request_data = $this->request->param();        $check_result = $validate->check($request_data);        if($check_result == true){            $order_list_model = new \app\api\model\order\Orderlist;            $price = $order_list_model->calculatePrice($request_data['order_list_id']);            $price *= 100;            $user_info = $this->auth->getUserinfo();            $user_id = $user_info['id'];            $out_trade_no = $user_id . '-' . time();            $order = [                'out_trade_no' => $out_trade_no,                'body' => '商品支付',                'total_fee' => $price,                'openid' => $this->auth->getUser()->openid,            ];            $result = Pay::wechat(Config::get('wechat.pay'))->miniapp($order);            \app\api\model\pay\Order::create(                [                    'package' => $out_trade_no,                    'order_type' => 'order',                    'order_mes' => $request_data['order_list_id']                ]            );            $this->success('success', ['order_list_id'=>$request_data['order_list_id'],'money'=>$price,'result'=>$result]);        }else{            $this->error('沒傳訂單編號');        }    }    $this->error('提交方式不是POST');}
相關文章
相關標籤/搜索