| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace ThirdParty\Urs;
- use App\Module\ThirdParty\Services\BaseWebhook;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- /**
- * URS余额检查Webhook处理器
- *
- * 专门处理余额检查相关的Webhook通知
- */
- class UrsCheckWebhook extends BaseWebhook
- {
- /**
- * 构造函数
- *
- * @param string $serviceCode 服务代码
- * @param Request $request 请求对象
- */
- public function __construct(string $serviceCode, Request $request)
- {
- parent::__construct($serviceCode, $request);
- }
-
- /**
- * 处理余额检查通知
- *
- * @param string $action 操作类型(固定为check)
- * @param Request $request 请求对象
- * @return array
- * @throws \Exception
- */
- protected function handler(string $action, Request $request): array
- {
- // 验证操作类型
- if ($action !== 'check') {
- throw new \Exception("此处理器只能处理check操作,当前操作: {$action}");
- }
-
- // 验证必需字段
- $this->validateCheckRequest($request);
-
- // 处理余额检查通知
- return $this->processCheckNotification($request);
- }
-
- /**
- * 验证余额检查请求
- *
- * @param Request $request 请求对象
- * @throws \Exception
- */
- protected function validateCheckRequest(Request $request): void
- {
- $requiredFields = ['user_id', 'check_type'];
-
- foreach ($requiredFields as $field) {
- if (!$request->has($field)) {
- throw new \Exception("缺少必需字段: {$field}");
- }
- }
-
- // 验证用户ID
- $userId = $request->input('user_id');
- if (!is_numeric($userId) || $userId <= 0) {
- throw new \Exception('用户ID格式无效');
- }
-
- // 验证检查类型
- $checkType = $request->input('check_type');
- if (!in_array($checkType, ['balance', 'transaction', 'status', 'limit'])) {
- throw new \Exception('检查类型无效,必须是: balance, transaction, status, limit');
- }
- }
-
- /**
- * 处理余额检查通知
- *
- * @param Request $request 请求对象
- * @return array
- */
- protected function processCheckNotification(Request $request): array
- {
- $userId = $request->input('user_id');
- $checkType = $request->input('check_type');
- $requestId = $request->input('request_id', '');
-
- // 记录处理日志
- Log::info("URS余额检查通知处理", [
- 'user_id' => $userId,
- 'check_type' => $checkType,
- 'request_id' => $requestId,
- 'webhook_request_id' => $this->getRequestId(),
- ]);
-
- // 根据检查类型执行不同的处理逻辑
- switch ($checkType) {
- case 'balance':
- return $this->handleBalanceCheck($userId, $requestId);
- case 'transaction':
- return $this->handleTransactionCheck($userId, $requestId);
- case 'status':
- return $this->handleStatusCheck($userId, $requestId);
- case 'limit':
- return $this->handleLimitCheck($userId, $requestId);
- default:
- throw new \Exception("未知的检查类型: {$checkType}");
- }
- }
-
- /**
- * 处理余额检查
- *
- * @param int $userId 用户ID
- * @param string $requestId 请求ID
- * @return array
- */
- protected function handleBalanceCheck(int $userId, string $requestId): array
- {
- // 这里可以调用Fund模块获取用户余额
- // 例如:$balance = FundService::getBalance($userId);
-
- // 模拟余额数据
- $balanceData = [
- 'total_balance' => 1000.50,
- 'available_balance' => 950.00,
- 'frozen_balance' => 50.50,
- 'currency' => 'CNY',
- ];
-
- return [
- 'message' => '余额检查处理完成',
- 'user_id' => $userId,
- 'request_id' => $requestId,
- 'check_type' => 'balance',
- 'data' => $balanceData,
- 'status' => 'success',
- 'processed_at' => now()->toISOString(),
- ];
- }
-
- /**
- * 处理交易记录检查
- *
- * @param int $userId 用户ID
- * @param string $requestId 请求ID
- * @return array
- */
- protected function handleTransactionCheck(int $userId, string $requestId): array
- {
- // 这里可以调用Fund模块获取用户交易记录
- // 例如:$transactions = FundService::getTransactions($userId);
-
- // 模拟交易数据
- $transactionData = [
- 'recent_transactions' => [
- [
- 'id' => 'TXN001',
- 'type' => 'deposit',
- 'amount' => 100.00,
- 'status' => 'success',
- 'created_at' => '2025-06-14 14:30:00',
- ],
- [
- 'id' => 'TXN002',
- 'type' => 'withdraw',
- 'amount' => 50.00,
- 'status' => 'pending',
- 'created_at' => '2025-06-14 15:00:00',
- ],
- ],
- 'total_count' => 25,
- ];
-
- return [
- 'message' => '交易记录检查处理完成',
- 'user_id' => $userId,
- 'request_id' => $requestId,
- 'check_type' => 'transaction',
- 'data' => $transactionData,
- 'status' => 'success',
- 'processed_at' => now()->toISOString(),
- ];
- }
-
- /**
- * 处理状态检查
- *
- * @param int $userId 用户ID
- * @param string $requestId 请求ID
- * @return array
- */
- protected function handleStatusCheck(int $userId, string $requestId): array
- {
- // 这里可以调用相关模块获取用户状态
- // 例如:$status = UserService::getStatus($userId);
-
- // 模拟状态数据
- $statusData = [
- 'account_status' => 'active',
- 'kyc_status' => 'verified',
- 'risk_level' => 'low',
- 'last_login' => '2025-06-14 16:00:00',
- ];
-
- return [
- 'message' => '状态检查处理完成',
- 'user_id' => $userId,
- 'request_id' => $requestId,
- 'check_type' => 'status',
- 'data' => $statusData,
- 'status' => 'success',
- 'processed_at' => now()->toISOString(),
- ];
- }
-
- /**
- * 处理限额检查
- *
- * @param int $userId 用户ID
- * @param string $requestId 请求ID
- * @return array
- */
- protected function handleLimitCheck(int $userId, string $requestId): array
- {
- // 这里可以调用相关模块获取用户限额信息
- // 例如:$limits = UserService::getLimits($userId);
-
- // 模拟限额数据
- $limitData = [
- 'daily_deposit_limit' => 10000.00,
- 'daily_withdraw_limit' => 5000.00,
- 'used_deposit_today' => 1500.00,
- 'used_withdraw_today' => 200.00,
- 'remaining_deposit' => 8500.00,
- 'remaining_withdraw' => 4800.00,
- ];
-
- return [
- 'message' => '限额检查处理完成',
- 'user_id' => $userId,
- 'request_id' => $requestId,
- 'check_type' => 'limit',
- 'data' => $limitData,
- 'status' => 'success',
- 'processed_at' => now()->toISOString(),
- ];
- }
- }
|