多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## ***laravel5.5 使用三方支付扩展:```yansongda/laravel-pay```** ##### <blockquote class="danger"><p>如需了解更多,请访问php专属扩展库链接:Packgist[链接](http://packagist.p2hp.com/packages/yansongda/laravel-pay) -------------------------------------------------------------------------------------------------------------------- 1. laravel5.5 使用 微信支付【事先引入扩展类:```use Yansongda\LaravelPay\Facades\Pay;```】 1.1:使用默认配置文件,发起微信支付 使用代码【微信H5支付:返回支付链接地址】: ``` try { $response = Pay::wechat()->wap($data); if ($url = $response->getTargetUrl()) { $client = new Client([ 'timeout' => 5.0, 'headers' => [ 'Referer' => config('app.url') ] ]); $contents = $client->get($url)->getBody()->getContents(); Log::debug('微信H5支付返回:', ['data' => $data, 'response' => $contents]); $match = []; if (0 === preg_match('/var\surl="(.*?)"/', (string)$contents, $match) || empty($match[1])) { return failed('system.busy'); } Log::debug('微信H5支付url:', ['data' => $data, 'url' => $match[1]]); return success(['url' => $match[1]]); } return failed('system.busy'); } catch (\Exception $e) { Log::debug('微信H5支付出错:' . $e->getMessage(), $data); return failed(); } ``` 1.2:使用自定义配置文件,发起微信支付 使用代码【微信H5支付:返回支付链接地址】: ``` $data = [ 'out_trade_no' => $request->number, 'body' => $order->props_name ?? '', 'total_fee' => $money * 100 ]; try { $config = config('pay.wolf'); Log::info("北极狼新商户号:".json_encode($config)); $wxPay = \Yansongda\Pay\Pay::wechat($config); $response = $wxPay->wap($data); if ($url = $response->getTargetUrl()) { $client = new Client([ 'timeout' => 5.0, 'headers' => [ 'Referer' => $config['url'] ] ]); $contents = $client->get($url)->getBody()->getContents(); Log::debug('北极狼微信H5支付返回:', ['data' => $data, 'response' => $contents]); $match = []; if (0 === preg_match('/var\surl="(.*?)"/', (string)$contents, $match) || empty($match[1])) { return failed('system.busy'); } Log::debug('北极狼微信H5url:', ['data' => $data, 'url' => $match[1]]); return success(['url' => $match[1]]); } return failed('system.busy'); } catch (\Exception $e) { Log::debug('北极狼微信H5支付出错:' . $e->getMessage(), $data); return failed(); } ``` 1.3:异步回调通知代码【使用默认配置,则不需要传入config配置】: ``` $config = config('pay.wolf'); $wxPay = \Yansongda\Pay\Pay::wechat($config); try { $data = $wxPay->verify(); if (!$order = Order::unPayedByNumber($data->out_trade_no)) { throw new \Exception("订单不存在或已完成支付"); } // ------折扣[根据是否有折扣判断金额是否相同]Start------ if ($order->pay_currency_code != PayType::PAY_CNY_CURRENCY_CODE) { return failed('H5微信支付币种有误!!!'); } $discount = Game::checkDiscount($order->game_id); $order->real_order_money = !empty($discount) ? $order->real_order_money * ($discount / 100) : $order->real_order_money; $order->real_order_money = number_format($order->real_order_money, 2, '.', ''); // ------折扣[根据是否有折扣判断金额是否相同]End-------- if ($order->real_order_money != floatval($data->total_fee / 100)) { throw new \Exception("订单金额不符"); } $params = [ 'real_pay_currency_code' => 'CNY', 'real_pay_money' => floatval($data->total_fee / 100), 'pay_type_id' => PayType::WX_PAY_ID ]; if (!Order::orderPaySuccessWithOutMoney($data->out_trade_no, $data->transaction_id, $params)) { throw new \Exception("订单状态更新失败"); } event(new PaySuccess($data->out_trade_no)); } catch (\Exception $e) { Log::error('微信支付成功通知出错:' . $e->getMessage(), $request->all()); } return $wxPay->success(); ``` 2. laravel5.5 使用 支付宝支付【事先引入扩展类:```use Yansongda\LaravelPay\Facades\Pay;```】 2.1:使用默认配置文件,发起支付宝支付 使用代码【支付宝H5支付:返回支付form表单】: ``` $this->validate($request, [ 'number' => 'string|required' ]); if (!$order = Order::unPayedByNumberUser($request->number, Member::id())) { return failed('order.non_existent'); } Order::orderPaying($order, PayType::Ali_PAY_H5); if (!($money = RmbContrastGoods::convertRmb($order->pay_currency_code, $order->money)) || ($money <= 0)) { return failed('system.busy'); } $data = [ 'out_trade_no' => $request->number, 'subject' => $order->props_name ?? '', 'total_amount' => $money ]; try { $response = Pay::alipay()->wap($data); return success(['form' => $response->getContent()]); } catch (\Exception $e) { Log::debug('支付宝H5支付出错:' . $e->getMessage(), $data); return failed(); } ``` 2.2:使用自定义配置文件,发起支付宝支付 使用代码【支付宝H5支付:返回支付form表单】: ``` $data = [ 'out_trade_no' => $request->number, 'subject' => $order->props_name ?? '', 'total_amount' => 0.01, 'product_code' => 'FAST_INSTANT_TRADE_PAY' ]; try { $response = \Yansongda\Pay\Pay::alipay(config('pay.alipay_sandbox'))->web($data); return success(['form' => $response->getContent()]); } catch (\Exception $e) { Log::error('Sandbox 支付宝web支付出错:' . $e->getMessage(), $data); return failed(); } ``` 2.3:异步回调通知代码【使用默认配置,则不需要传入config配置】: ``` Log::debug('支付宝支付成功通知(同步)', $request->all()); $aliPay = Pay::alipay(); try { $data = $aliPay->verify(); if (!$order = Order::unPayedByNumber($data->out_trade_no)) { throw new \Exception("订单不存在或已完成支付"); } if ((RmbContrastGoods::convertRmb($order->pay_currency_code, $order->real_order_money) != floatval($data->total_amount)) && floatval($data->total_amount) != 0.01) { throw new \Exception("订单金额不符"); } $params = [ 'real_pay_currency_code' => 'CNY', 'real_pay_money' => $data->total_amount, 'pay_type_id' => \App\Models\PayType::ALI_PAY_ID ]; if (!Order::orderPaySuccessWithOutMoney($data->out_trade_no, $data->trade_no, $params)) { throw new \Exception("订单状态更新失败"); } event(new PaySuccess($data->out_trade_no)); } catch (\Exception $e) { Log::error('支付宝支付成功通知出错(同步):' . $e->getMessage(), $request->all()); } return $aliPay->success(); ```