first commit
This commit is contained in:
46
app/Http/Controllers/OrderNotifyController.php
Normal file
46
app/Http/Controllers/OrderNotifyController.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\OrderPayment;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class OrderNotifyController extends Controller
|
||||
{
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
|
||||
$app = app('wechat.payment');
|
||||
$response = $app->handlePaidNotify(function ($message, $fail) {
|
||||
|
||||
$payment = OrderPayment::where('out_trade_no', $message['out_trade_no'])->first();
|
||||
if ($payment && $message['return_code'] === 'SUCCESS') {
|
||||
if (array_get($message, 'result_code') === 'SUCCESS') {
|
||||
try {
|
||||
// 支付结果
|
||||
$payment->transaction_id = $message['transaction_id'];
|
||||
$payment->end_at = strtotime($message['time_end']);
|
||||
$payment->paid_amount = $message['total_fee'] / 100;
|
||||
$payment->save();
|
||||
// 如果订单可以支付,未支付状态或者未关闭的订单
|
||||
// 确认订单支付状态
|
||||
// 否则直接退款
|
||||
if ($payment->order->canPay()) {
|
||||
$payment->order->paid();
|
||||
}
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
return $fail($e->getMessage());
|
||||
}
|
||||
} elseif (array_get($message, 'result_code') === 'FAIL') {
|
||||
return $fail('支付失败');
|
||||
}
|
||||
} else {
|
||||
return $fail('通信失败,请稍后再通知我');
|
||||
}
|
||||
});
|
||||
return $response;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user