2023-11-17 01:44:01 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\V1\Guest;
|
|
|
|
|
2023-12-04 07:40:49 -05:00
|
|
|
use App\Exceptions\ApiException;
|
2023-11-17 01:44:01 -05:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Order;
|
2024-06-13 12:05:20 -04:00
|
|
|
use App\Models\Payment;
|
2023-11-17 01:44:01 -05:00
|
|
|
use App\Services\OrderService;
|
|
|
|
use App\Services\PaymentService;
|
|
|
|
use App\Services\TelegramService;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class PaymentController extends Controller
|
|
|
|
{
|
|
|
|
public function notify($method, $uuid, Request $request)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$paymentService = new PaymentService($method, null, $uuid);
|
|
|
|
$verify = $paymentService->notify($request->input());
|
2024-04-09 12:51:03 -04:00
|
|
|
if (!$verify)
|
|
|
|
return $this->fail([422, 'verify error']);
|
2023-11-17 01:44:01 -05:00
|
|
|
if (!$this->handle($verify['trade_no'], $verify['callback_no'])) {
|
2024-04-09 12:51:03 -04:00
|
|
|
return $this->fail([400, 'handle error']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
2024-01-10 12:16:10 -05:00
|
|
|
return (isset($verify['custom_result']) ? $verify['custom_result'] : 'success');
|
2023-11-17 01:44:01 -05:00
|
|
|
} catch (\Exception $e) {
|
2024-01-10 12:16:10 -05:00
|
|
|
\Log::error($e);
|
2024-04-09 12:51:03 -04:00
|
|
|
return $this->fail([500, 'fail']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handle($tradeNo, $callbackNo)
|
|
|
|
{
|
|
|
|
$order = Order::where('trade_no', $tradeNo)->first();
|
|
|
|
if (!$order) {
|
2024-04-09 12:51:03 -04:00
|
|
|
return $this->fail([400202, 'order is not found']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
2024-04-09 12:51:03 -04:00
|
|
|
if ($order->status !== Order::STATUS_PENDING)
|
|
|
|
return true;
|
2023-11-17 01:44:01 -05:00
|
|
|
$orderService = new OrderService($order);
|
|
|
|
if (!$orderService->paid($callbackNo)) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-06-08 05:51:38 -04:00
|
|
|
|
|
|
|
$payment = Payment::where('id', $order->payment_id)->first();
|
2023-11-17 01:44:01 -05:00
|
|
|
$telegramService = new TelegramService();
|
|
|
|
$message = sprintf(
|
2024-06-08 05:51:38 -04:00
|
|
|
"💰成功收款%s元\n" .
|
|
|
|
"———————————————\n" .
|
|
|
|
"支付接口:%s\n" .
|
|
|
|
"支付渠道:%s\n" .
|
|
|
|
"本站订单:`%s`"
|
|
|
|
,
|
2023-11-17 01:44:01 -05:00
|
|
|
$order->total_amount / 100,
|
2024-06-08 05:51:38 -04:00
|
|
|
$payment->payment,
|
|
|
|
$payment->name,
|
2023-11-17 01:44:01 -05:00
|
|
|
$order->trade_no
|
|
|
|
);
|
2024-06-17 10:58:53 -04:00
|
|
|
|
2023-11-17 01:44:01 -05:00
|
|
|
$telegramService->sendMessageWithAdmin($message);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|