UrsDepositWebhook.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 UrsDepositWebhook 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 操作类型(固定为deposit)
  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 !== 'deposit') {
  37. throw new \Exception("此处理器只能处理deposit操作,当前操作: {$action}");
  38. }
  39. // 验证必需字段
  40. $this->validateDepositRequest($request);
  41. // 处理充值通知
  42. return $this->processDepositNotification($request);
  43. }
  44. /**
  45. * 验证充值请求
  46. *
  47. * @param Request $request 请求对象
  48. * @throws \Exception
  49. */
  50. protected function validateDepositRequest(Request $request): void
  51. {
  52. $requiredFields = ['user_id', 'amount', 'order_id', 'status'];
  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. $amount = $request->input('amount');
  65. if (!is_numeric($amount) || $amount <= 0) {
  66. throw new \Exception('充值金额必须大于0');
  67. }
  68. // 验证订单ID
  69. $orderId = $request->input('order_id');
  70. if (empty($orderId)) {
  71. throw new \Exception('订单ID不能为空');
  72. }
  73. // 验证状态
  74. $status = $request->input('status');
  75. if (!in_array($status, ['success', 'failed', 'pending'])) {
  76. throw new \Exception('状态值无效,必须是: success, failed, pending');
  77. }
  78. }
  79. /**
  80. * 处理充值通知
  81. *
  82. * @param Request $request 请求对象
  83. * @return array
  84. */
  85. protected function processDepositNotification(Request $request): array
  86. {
  87. $userId = $request->input('user_id');
  88. $amount = $request->input('amount');
  89. $orderId = $request->input('order_id');
  90. $status = $request->input('status');
  91. $message = $request->input('message', '');
  92. // 记录处理日志
  93. Log::info("URS充值通知处理", [
  94. 'user_id' => $userId,
  95. 'amount' => $amount,
  96. 'order_id' => $orderId,
  97. 'status' => $status,
  98. 'message' => $message,
  99. 'request_id' => $this->getRequestId(),
  100. ]);
  101. // 根据状态执行不同的处理逻辑
  102. switch ($status) {
  103. case 'success':
  104. return $this->handleSuccessfulDeposit($userId, $amount, $orderId);
  105. case 'failed':
  106. return $this->handleFailedDeposit($userId, $amount, $orderId, $message);
  107. case 'pending':
  108. return $this->handlePendingDeposit($userId, $amount, $orderId);
  109. default:
  110. throw new \Exception("未知的充值状态: {$status}");
  111. }
  112. }
  113. /**
  114. * 处理充值成功
  115. *
  116. * @param int $userId 用户ID
  117. * @param float $amount 充值金额
  118. * @param string $orderId 订单ID
  119. * @return array
  120. */
  121. protected function handleSuccessfulDeposit(int $userId, float $amount, string $orderId): array
  122. {
  123. // 这里可以调用Fund模块的充值逻辑
  124. // 例如:FundService::deposit($userId, $amount, $orderId);
  125. return [
  126. 'message' => '充值成功通知处理完成',
  127. 'user_id' => $userId,
  128. 'amount' => $amount,
  129. 'order_id' => $orderId,
  130. 'status' => 'success',
  131. 'actions' => [
  132. 'balance_updated' => true,
  133. 'transaction_recorded' => true,
  134. 'user_notified' => true,
  135. ],
  136. 'processed_at' => now()->toISOString(),
  137. ];
  138. }
  139. /**
  140. * 处理充值失败
  141. *
  142. * @param int $userId 用户ID
  143. * @param float $amount 充值金额
  144. * @param string $orderId 订单ID
  145. * @param string $message 失败原因
  146. * @return array
  147. */
  148. protected function handleFailedDeposit(int $userId, float $amount, string $orderId, string $message): array
  149. {
  150. // 这里可以调用相关的失败处理逻辑
  151. // 例如:记录失败原因、退款处理等
  152. return [
  153. 'message' => '充值失败通知处理完成',
  154. 'user_id' => $userId,
  155. 'amount' => $amount,
  156. 'order_id' => $orderId,
  157. 'status' => 'failed',
  158. 'failure_reason' => $message,
  159. 'actions' => [
  160. 'failure_logged' => true,
  161. 'refund_initiated' => true,
  162. 'user_notified' => true,
  163. ],
  164. 'processed_at' => now()->toISOString(),
  165. ];
  166. }
  167. /**
  168. * 处理充值待处理
  169. *
  170. * @param int $userId 用户ID
  171. * @param float $amount 充值金额
  172. * @param string $orderId 订单ID
  173. * @return array
  174. */
  175. protected function handlePendingDeposit(int $userId, float $amount, string $orderId): array
  176. {
  177. // 这里可以调用待处理相关的逻辑
  178. // 例如:加入处理队列、发送通知等
  179. return [
  180. 'message' => '充值待处理通知处理完成',
  181. 'user_id' => $userId,
  182. 'amount' => $amount,
  183. 'order_id' => $orderId,
  184. 'status' => 'pending',
  185. 'actions' => [
  186. 'added_to_processing_queue' => true,
  187. 'admin_notified' => true,
  188. ],
  189. 'processed_at' => now()->toISOString(),
  190. ];
  191. }
  192. }