UrsCheckWebhook.php 7.5 KB

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