Files
Xboard/app/Http/Controllers/V1/Guest/PaymentController.php

53 lines
1.7 KiB
PHP
Raw Normal View History

2023-11-17 14:44:01 +08:00
<?php
namespace App\Http\Controllers\V1\Guest;
use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Services\OrderService;
use App\Services\PaymentService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Services\Plugin\HookManager;
2023-11-17 14:44:01 +08:00
class PaymentController extends Controller
{
public function notify($method, $uuid, Request $request)
{
HookManager::call('payment.notify.before', [$method, $uuid, $request]);
2023-11-17 14:44:01 +08:00
try {
$paymentService = new PaymentService($method, null, $uuid);
$verify = $paymentService->notify($request->input());
if (!$verify) {
HookManager::call('payment.notify.failed', [$method, $uuid, $request]);
return $this->fail([422, 'verify error']);
}
HookManager::call('payment.notify.verified', $verify);
2023-11-17 14:44:01 +08:00
if (!$this->handle($verify['trade_no'], $verify['callback_no'])) {
return $this->fail([400, 'handle error']);
2023-11-17 14:44:01 +08:00
}
return (isset($verify['custom_result']) ? $verify['custom_result'] : 'success');
2023-11-17 14:44:01 +08:00
} catch (\Exception $e) {
Log::error($e);
return $this->fail([500, 'fail']);
2023-11-17 14:44:01 +08:00
}
}
private function handle($tradeNo, $callbackNo)
{
$order = Order::where('trade_no', $tradeNo)->first();
if (!$order) {
return $this->fail([400202, 'order is not found']);
2023-11-17 14:44:01 +08:00
}
if ($order->status !== Order::STATUS_PENDING)
return true;
2023-11-17 14:44:01 +08:00
$orderService = new OrderService($order);
if (!$orderService->paid($callbackNo)) {
return false;
}
2024-06-08 17:51:38 +08:00
HookManager::call('payment.notify.success', $order);
2023-11-17 14:44:01 +08:00
return true;
}
}