UrsCheckWebhook.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace ThirdParty\Urs\Webhook;
  3. use App\Module\ThirdParty\Models\ThirdPartyService as ServiceModel;
  4. use App\Module\ThirdParty\Services\WebhookReceiver;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * URS余额检查Webhook处理器
  9. *
  10. * 专门处理余额检查相关的Webhook通知
  11. */
  12. class UrsCheckWebhook extends WebhookReceiver
  13. {
  14. /**
  15. * 构造函数
  16. *
  17. * @param string $serviceCode 服务代码
  18. * @param Request $request 请求对象
  19. * @param ServiceModel $service 服务配置对象
  20. */
  21. public function __construct(string $serviceCode, Request $request, ServiceModel $service)
  22. {
  23. parent::__construct($serviceCode, $request, $service);
  24. }
  25. /**
  26. * 处理余额检查通知
  27. *
  28. * @param string $action 操作类型(固定为check)
  29. * @param Request $request 请求对象
  30. * @return array
  31. * @throws \Exception
  32. */
  33. protected function handler(string $action, Request $request): array
  34. {
  35. // 验证操作类型
  36. if ($action !== 'check') {
  37. throw new \Exception("此处理器只能处理check操作,当前操作: {$action}");
  38. }
  39. // 验证必需字段
  40. $this->validateCheckRequest($request);
  41. // 处理余额检查通知
  42. return $this->processCheckNotification($request);
  43. }
  44. /**
  45. * 验证余额检查请求
  46. *
  47. * @param Request $request 请求对象
  48. * @throws \Exception
  49. */
  50. protected function validateCheckRequest(Request $request): void
  51. {
  52. $requiredFields = ['user_id', 'check_type'];
  53. foreach ($requiredFields as $field) {
  54. if (!$request->has($field)) {
  55. throw new \Exception("缺少必需字段: {$field}");
  56. }
  57. }
  58. // 验证用户ID
  59. $userId = $request->input('user_id');
  60. if (!is_numeric($userId) || $userId <= 0) {
  61. throw new \Exception('用户ID格式无效');
  62. }
  63. // 验证检查类型
  64. $checkType = $request->input('check_type');
  65. if (!in_array($checkType, ['balance', 'transaction', 'status', 'limit'])) {
  66. throw new \Exception('检查类型无效,必须是: balance, transaction, status, limit');
  67. }
  68. }
  69. /**
  70. * 处理余额检查通知
  71. *
  72. * @param Request $request 请求对象
  73. * @return array
  74. */
  75. protected function processCheckNotification(Request $request): array
  76. {
  77. $userId = $request->input('user_id');
  78. $checkType = $request->input('check_type');
  79. $requestId = $request->input('request_id', '');
  80. // 记录处理日志
  81. Log::info("URS余额检查通知处理", [
  82. 'user_id' => $userId,
  83. 'check_type' => $checkType,
  84. 'request_id' => $requestId,
  85. 'webhook_request_id' => $this->getRequestId(),
  86. ]);
  87. // 根据检查类型执行不同的处理逻辑
  88. switch ($checkType) {
  89. case 'balance':
  90. return $this->handleBalanceCheck($userId, $requestId);
  91. case 'transaction':
  92. return $this->handleTransactionCheck($userId, $requestId);
  93. case 'status':
  94. return $this->handleStatusCheck($userId, $requestId);
  95. case 'limit':
  96. return $this->handleLimitCheck($userId, $requestId);
  97. default:
  98. throw new \Exception("未知的检查类型: {$checkType}");
  99. }
  100. }
  101. /**
  102. * 处理余额检查
  103. *
  104. * @param int $userId 用户ID
  105. * @param string $requestId 请求ID
  106. * @return array
  107. */
  108. protected function handleBalanceCheck(int $userId, string $requestId): array
  109. {
  110. // 这里可以调用Fund模块获取用户余额
  111. // 例如:$balance = FundService::getBalance($userId);
  112. // 模拟余额数据
  113. $balanceData = [
  114. 'total_balance' => 1000.50,
  115. 'available_balance' => 950.00,
  116. 'frozen_balance' => 50.50,
  117. 'currency' => 'CNY',
  118. ];
  119. return [
  120. 'message' => '余额检查处理完成',
  121. 'user_id' => $userId,
  122. 'request_id' => $requestId,
  123. 'check_type' => 'balance',
  124. 'data' => $balanceData,
  125. 'status' => 'success',
  126. 'processed_at' => now()->toISOString(),
  127. ];
  128. }
  129. /**
  130. * 处理交易记录检查
  131. *
  132. * @param int $userId 用户ID
  133. * @param string $requestId 请求ID
  134. * @return array
  135. */
  136. protected function handleTransactionCheck(int $userId, string $requestId): array
  137. {
  138. // 这里可以调用Fund模块获取用户交易记录
  139. // 例如:$transactions = FundService::getTransactions($userId);
  140. // 模拟交易数据
  141. $transactionData = [
  142. 'recent_transactions' => [
  143. [
  144. 'id' => 'TXN001',
  145. 'type' => 'deposit',
  146. 'amount' => 100.00,
  147. 'status' => 'success',
  148. 'created_at' => '2025-06-14 14:30:00',
  149. ],
  150. [
  151. 'id' => 'TXN002',
  152. 'type' => 'withdraw',
  153. 'amount' => 50.00,
  154. 'status' => 'pending',
  155. 'created_at' => '2025-06-14 15:00:00',
  156. ],
  157. ],
  158. 'total_count' => 25,
  159. ];
  160. return [
  161. 'message' => '交易记录检查处理完成',
  162. 'user_id' => $userId,
  163. 'request_id' => $requestId,
  164. 'check_type' => 'transaction',
  165. 'data' => $transactionData,
  166. 'status' => 'success',
  167. 'processed_at' => now()->toISOString(),
  168. ];
  169. }
  170. /**
  171. * 处理状态检查
  172. *
  173. * @param int $userId 用户ID
  174. * @param string $requestId 请求ID
  175. * @return array
  176. */
  177. protected function handleStatusCheck(int $userId, string $requestId): array
  178. {
  179. // 这里可以调用相关模块获取用户状态
  180. // 例如:$status = UserService::getStatus($userId);
  181. // 模拟状态数据
  182. $statusData = [
  183. 'account_status' => 'active',
  184. 'kyc_status' => 'verified',
  185. 'risk_level' => 'low',
  186. 'last_login' => '2025-06-14 16:00:00',
  187. ];
  188. return [
  189. 'message' => '状态检查处理完成',
  190. 'user_id' => $userId,
  191. 'request_id' => $requestId,
  192. 'check_type' => 'status',
  193. 'data' => $statusData,
  194. 'status' => 'success',
  195. 'processed_at' => now()->toISOString(),
  196. ];
  197. }
  198. /**
  199. * 处理限额检查
  200. *
  201. * @param int $userId 用户ID
  202. * @param string $requestId 请求ID
  203. * @return array
  204. */
  205. protected function handleLimitCheck(int $userId, string $requestId): array
  206. {
  207. // 这里可以调用相关模块获取用户限额信息
  208. // 例如:$limits = UserService::getLimits($userId);
  209. // 模拟限额数据
  210. $limitData = [
  211. 'daily_deposit_limit' => 10000.00,
  212. 'daily_withdraw_limit' => 5000.00,
  213. 'used_deposit_today' => 1500.00,
  214. 'used_withdraw_today' => 200.00,
  215. 'remaining_deposit' => 8500.00,
  216. 'remaining_withdraw' => 4800.00,
  217. ];
  218. return [
  219. 'message' => '限额检查处理完成',
  220. 'user_id' => $userId,
  221. 'request_id' => $requestId,
  222. 'check_type' => 'limit',
  223. 'data' => $limitData,
  224. 'status' => 'success',
  225. 'processed_at' => now()->toISOString(),
  226. ];
  227. }
  228. }