## ***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();
```
- Laravel5.5總結
- 項目管理
- Manager
- Vip專屬鏈接管理
- Api
- Vip專屬鏈接管理(Api)
- php artisan route:list 路由顯示不全
- 數據遷移和填充
- Laravel5.5事件監聽機制(注冊-監聽-任務調度)
- 章節1:未啟用任務調度
- 章節2:啟用任務調度
- 使用記錄
- 數據遷移使用記錄
- 安裝laravel5.5日志觀察器
- Laravel5.5消息隊列(rabbitmq)
- 1:laravel自帶消息隊列
- 2:RabbitMq隊列使用
- 第三方支付擴展:yansongda/laravel-pay
- 安裝指引
- 控制器內使用
- 分表查詢(mysql+mongo)
- 前端Vue按鈕導出問題
- 單元測試
- 模型使用
- laravel9數據填充
- laravel9子查詢