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

51 lines
1.7 KiB
PHP
Raw Normal View History

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;
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());
2023-12-04 07:40:49 -05:00
if (!$verify) throw new ApiException(500, 'verify error');
2023-11-17 01:44:01 -05:00
if (!$this->handle($verify['trade_no'], $verify['callback_no'])) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, 'handle error');
2023-11-17 01:44:01 -05:00
}
return(isset($verify['custom_result']) ? $verify['custom_result'] : 'success');
} catch (\Exception $e) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, 'fail');
2023-11-17 01:44:01 -05:00
}
}
private function handle($tradeNo, $callbackNo)
{
$order = Order::where('trade_no', $tradeNo)->first();
if (!$order) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, 'order is not found');
2023-11-17 01:44:01 -05:00
}
if ($order->status !== 0) return true;
$orderService = new OrderService($order);
if (!$orderService->paid($callbackNo)) {
return false;
}
$telegramService = new TelegramService();
$message = sprintf(
"💰成功收款%s元\n———————————————\n订单号:%s",
$order->total_amount / 100,
$order->trade_no
);
$telegramService->sendMessageWithAdmin($message);
return true;
}
}