一、服务端逻辑:服务端使用jsApi支付生成预支付订单,返回给前端,使用的微信支付v2版本
1.1、下载官方sdk,也可用这个,经过博主修改的laravel版的:https://www.aliyundrive.com/s/mf1jnwagKuk
1.2 、将下载好的微信支付sdk放置到vendor目录下。
1.3 、在订单控制器OrderController引入我们的微信支付sdk
require_once "../vendor/wxpaysdk/lib/WxPay.Api.php"; require_once "../vendor/wxpaysdk/lib/WxPay.NativePay.php"; require_once "../vendor/wxpaysdk/lib/phpqrcode/phpqrcode.php"; require_once "../vendor/wxpaysdk/lib/WxPay.JsApiPay.php"; require_once "../vendor/wxpaysdk/lib/WxPay.Config.php"; require_once "../vendor/wxpaysdk/lib/WxPay.Notify.php";
1.4、 生成预支付订单方法
public function order_create(Request $request){ $data = [ 'goods_id'=>$request->goods_id, 'name'=>$request->name, 'mobile'=>$request->mobile, 'goods_money'=>$request->goods_money, 'member_id'=>$request->member_id, 'openid'=>$request->openid, 'nickName'=>$request->nickName, 'avatar'=>$request->avatar, 'order_no'=>date('Ymd'). substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8), 'create_time'=>time(), ]; $res =Order::create($data); $id = $res->id; //根据id查询订单 $order = Order::where(array('id'=>$id))->first(); try{ //调起微信支付jsApi接口 $tools = new \JsApiPay(); $openId =$request->openid; //②、统一下单 $input = new \WxPayUnifiedOrder(); $input->SetBody($request->goods_name); //商品名称 $input->SetAttach("test"); $input->SetOut_trade_no($order->order_no); $input->SetTotal_fee((string)($request->goods_money*100)); $input->SetTime_start(date("YmdHis",$order->create_time)); //SetTime_start 设置订单生成时间,格式为yyyyMMddHHmmss */ $input->SetTime_expire(date("YmdHis", $order->create_time + 1800)); //SetTime_expire 设置订单失效时间,格式为yyyyMMddHHmmss */ $input->SetGoods_tag("test"); //SetGoods_tag 设置商品标记,代金券或立减优惠功能的参数 */ $input->SetNotify_url("https://shop.example.com/weixin/order_pay_notify"); $input->SetTrade_type("JSAPI"); $input->SetOpenid($openId); $config = new \WxPayConfig(); $orderRes = \WxPayApi::unifiedOrder($config, $input); $resjsApiParameters = $tools->GetJsApiParameters($orderRes); $resultData= json_decode($resjsApiParameters,true); }catch(Exception $e) { Log::channel('payerr')->debug(json_encode($e)); } if($res){ return $this->jsonData(ApiErrDesc::SUCCESS[0],ApiErrDesc::SUCCESS[1],$resultData); }else{ return $this->jsonData(ApiErrDesc::ERROR[0],ApiErrDesc::ERROR[1],''); } }
1.5 支付回调:更新订单状态
/* * 支付回调 * */ public function order_pay_notify(Request $request){ $post= file_get_contents("php://input"); libxml_disable_entity_loader(true); //禁止引用外部xml实体 $xml = simplexml_load_string($post, 'SimpleXMLElement', LIBXML_NOCDATA);//XML转数组 $post_data = (array)$xml; Log::channel('pay')->debug($post_data); if($post_data){ if($post_data['return_code'] == 'SUCCESS' && $post_data['result_code'] == 'SUCCESS'){ $data=[ 'out_trade_no'=>$post_data['transaction_id'], 'pay_time'=>strtotime($post_data['time_end']), 'pay_status'=>1, ]; $updateOrder = Order::where(array('order_no'=>$post_data['out_trade_no']))->update($data); if($updateOrder){ //阻止微信接口反复回调接口 文档地址 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_7&index=7,下面这句非常重要!!! echo '<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> </xml>'; exit('Notify 非法回调'); } } } }
二、 小程序前端:wx.requestPayment(Object object)
详细文档:https://developers.weixin.qq.com/miniprogram/dev/api/payment/wx.requestPayment.html
request({ url: '/weixin/order_create', data:{ goods_id: this.data.pay_detail.id, goods_name: this.data.pay_detail.goods_name, name: e.detail.value.name, mobile: e.detail.value.mobile, mobile: e.detail.value.remarks, goods_money: this.data.pay_detail.goods_price, member_id: this.data.userInfo.id, openid: this.data.userInfo.openid, nickName: this.data.userInfo.nickName, avatar: this.data.userInfo.avatar, }, method: 'POST', }).then(result=>{ if(result.data.code==200){ console.log(result.data.data) wx.requestPayment({ timeStamp: result.data.data.timeStamp, //随机字符串,长度为32个字符以下 nonceStr: result.data.data.nonceStr, package: result.data.data.package, //统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*** signType: result.data.data.signType, paySign: result.data.data.paySign, //签名算法,应与后台下单时的值一致 success (res) { console.log(res) }, fail (res) { console.log(res) } }) }else{ wx.showToast({ title: '网络错误', duration: 2000, icon: 'none' }); } })
文章评论(0)