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;
|
2025-07-26 18:49:58 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2025-07-21 13:29:17 +08:00
|
|
|
use App\Services\Plugin\HookManager;
|
2023-11-17 14:44:01 +08:00
|
|
|
|
|
|
|
|
class PaymentController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function notify($method, $uuid, Request $request)
|
|
|
|
|
{
|
2025-07-21 13:29:17 +08:00
|
|
|
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());
|
2025-07-21 13:29:17 +08:00
|
|
|
if (!$verify) {
|
|
|
|
|
HookManager::call('payment.notify.failed', [$method, $uuid, $request]);
|
2024-04-10 00:51:03 +08:00
|
|
|
return $this->fail([422, 'verify error']);
|
2025-07-21 13:29:17 +08:00
|
|
|
}
|
|
|
|
|
HookManager::call('payment.notify.verified', $verify);
|
2023-11-17 14:44:01 +08:00
|
|
|
if (!$this->handle($verify['trade_no'], $verify['callback_no'])) {
|
2024-04-10 00:51:03 +08:00
|
|
|
return $this->fail([400, 'handle error']);
|
2023-11-17 14:44:01 +08:00
|
|
|
}
|
2024-01-11 01:16:10 +08:00
|
|
|
return (isset($verify['custom_result']) ? $verify['custom_result'] : 'success');
|
2023-11-17 14:44:01 +08:00
|
|
|
} catch (\Exception $e) {
|
2025-07-26 18:49:58 +08:00
|
|
|
Log::error($e);
|
2024-04-10 00:51:03 +08:00
|
|
|
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) {
|
2024-04-10 00:51:03 +08:00
|
|
|
return $this->fail([400202, 'order is not found']);
|
2023-11-17 14:44:01 +08:00
|
|
|
}
|
2024-04-10 00:51:03 +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
|
|
|
|
2025-07-26 18:49:58 +08:00
|
|
|
HookManager::call('payment.notify.success', $order);
|
2023-11-17 14:44:01 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|