使用easywechat
demo:
<?php namespace App\Modules\Mall\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use DB; use App\Wxconfig; /*小程序支付*/ class PayController extends Controller { protected $pay; protected $gid; protected $wxconfig; public function __construct(Wxconfig $wxconfig, Request $request) { $this->gid = $request->input('gid'); if ($this->gid) { $this->wxconfig = $wxconfig; $this->pay = $this->wxconfig->WxMiniPay($this->gid); } else { exit('gid no exist'); } } public function pay(Request $request) { $jssdk = $this->pay->jssdk; $openid = $request->input('openid'); $order_no = 'O' . time() . rand(10000, 99999); $order = DB::table('goods_orders')->where('order_no', 'O157066840848073')->first(); $result = $this->pay->order->unify([ 'body' => '测试小程序微信支付', 'out_trade_no' => $order->order_no, 'total_fee' => 1,//总价 'notify_url' => 'http://' . $_SERVER['SERVER_NAME'] . '/mall/api/payNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址 'trade_type' => 'JSAPI', 'openid' => $openid, ]); if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') { $prepayId = $result['prepay_id']; $pay_config = $jssdk->sdkConfig($prepayId); return $pay_config; } else { return '签名错误'; } } public function payNotify() { $response = $this->pay->handlePaidNotify(function ($message, $fail) { // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单 $order = DB::table('goods_orders')->where('order_no', $message['out_trade_no'])->first(); //订单不存在或者订单已支付 if (!$order || $order->pay_time) { return true; } if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态 if (array_get($message, 'result_code') === 'SUCCESS') { // 用户是否支付成功 $order_data = [ 'pay_time' => time(), 'pay_status' => 2, 'transaction_id' => $message['transaction_id'], //微信交易号 ]; // $order->pay_time = time(); // 更新支付时间为当前时间 // $order->pay_status = 20; //支付状态 成功 // $order->transaction_id = $message['transaction_id']; //微信交易号 } elseif (array_get($message, 'result_code') === 'FAIL') { // 用户支付失败 $order['pay_status'] = 3; //支付状态 失败 } } else { return $fail('通信失败,请稍后再通知我'); } $order = DB::table('goods_orders')->where('order_no', $message['out_trade_no'])->update($order_data); // 保存订单 return true; // 返回处理完成 }); return ['code' => 1, 'msg' => '订单支付成功!', 'data' => $response]; } public function orderPayResult(Request $request) { $order_no = $request->post('order_no'); //根据商品订单号查询 $wx_order = $this->wxpay->order->queryByOutTradeNumber($order_no); if ($wx_order && $wx_order['trade_state'] === 'SUCCESS') { return ['code' => 1, 'msg' => '交易成功!']; } return ['code' => 0, 'msg' => '支付失败!', 'data' => $wx_order]; } }
小程序端
//支付接口 paypay: function() { var openid = wx.getStorageSync('openid'); var api = this.globalData.api; var gid = this.globalData.gid; console.log(openid) wx.request({ url: api + '/pay', data: { gid: gid, openid: openid, //order_id: order_id//订单ID }, method: 'POST', header: { 'content-type': 'application/json' }, success: function(res) { console.log(res.data); wx.requestPayment({ 'timeStamp': res.data.timestamp, 'nonceStr': res.data.nonceStr, 'package': res.data.package, 'signType': res.data.signType, 'paySign': res.data.paySign, 'success': function(res) { if (res.errMsg == "requestPayment:ok") { wx.showToast({ title: '支付成功' }) } }, 'fail': function(res) { } }) }, fail: function(res) { console.log(res.data.errmsg); console.log(res.data.errcode); }, complete: function(res) {} }) },