UrsWithdrawWebhook.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace ThirdParty\Urs\Webhook;
  3. use App\Module\ThirdParty\Models\ThirdPartyService as ServiceModel;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\DB;
  7. /**
  8. * URS提取通知Webhook处理器
  9. *
  10. * 专门处理提取相关的Webhook通知
  11. */
  12. class UrsWithdrawWebhook 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 操作类型(固定为withdraw)
  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 !== 'withdraw') {
  37. throw new \Exception("此处理器只能处理withdraw操作,当前操作: {$action}");
  38. }
  39. // 验证必需字段
  40. $this->validateWithdrawRequest($request);
  41. // 获取请求参数
  42. $userId = $request->input('user_id');
  43. $amount = $request->input('amount');
  44. $orderId = $request->input('order_id');
  45. // 处理提取通知
  46. return $this->processWithdrawNotification($userId, $amount, $orderId);
  47. }
  48. /**
  49. * 处理提取通知的核心逻辑
  50. *
  51. * @param string $userId URS用户ID
  52. * @param string $amount 提取金额
  53. * @param string $orderId URS订单ID
  54. * @return array
  55. * @throws \Exception
  56. */
  57. protected function processWithdrawNotification(string $userId, string $amount, string $orderId): array
  58. {
  59. try {
  60. /**
  61. * 三方应用ID
  62. * @var int $thirdPartyAppId
  63. */
  64. $thirdPartyAppId = $this->service->id;
  65. // 1. 根据URS用户ID获取农场用户ID
  66. $farmUserId = \App\Module\UrsPromotion\Services\UrsUserMappingService::getFarmUserId($userId);
  67. if (!$farmUserId) {
  68. Log::error("URS提取失败:未找到用户映射关系", [
  69. 'urs_user_id' => $userId,
  70. 'order_id' => $orderId,
  71. ]);
  72. throw new \Exception("用户未进入农场系统,无法完成提取");
  73. }
  74. // 2. 开启数据库事务并使用Transfer模块完成提取钻石操作
  75. $result = DB::transaction(function () use ($thirdPartyAppId, $farmUserId, $userId, $amount, $orderId) {
  76. return \App\Module\Transfer\Services\TransferThirdPartyService::createWithdrawOrder(
  77. thirdPartyAppId: $thirdPartyAppId,
  78. farmUserId: $farmUserId,
  79. thirdPartyUserId: (string)$userId,
  80. thirdPartyAmount: $amount,
  81. remark: "URS提取 - 订单号: {$orderId}",
  82. callbackData: [
  83. 'urs_order_id' => $orderId,
  84. 'urs_user_id' => $userId,
  85. 'source' => 'urs_withdraw_webhook'
  86. ]
  87. );
  88. });
  89. if (is_string($result)) {
  90. // 提取失败
  91. Log::error("URS提取失败", [
  92. 'urs_user_id' => $userId,
  93. 'farm_user_id' => $farmUserId,
  94. 'third_party_app_id' => $thirdPartyAppId,
  95. 'amount' => $amount,
  96. 'order_id' => $orderId,
  97. 'error' => $result
  98. ]);
  99. throw new \Exception("提取失败: {$result}");
  100. }
  101. // 提取成功
  102. Log::info("URS提取成功", [
  103. 'urs_user_id' => $userId,
  104. 'farm_user_id' => $farmUserId,
  105. 'third_party_app_id' => $thirdPartyAppId,
  106. 'amount' => $amount,
  107. 'order_id' => $orderId,
  108. 'transfer_order_id' => $result->id,
  109. 'rorder_id' => $result->out_order_id
  110. ]);
  111. return [
  112. 'rorder_id' => $result->out_order_id,
  113. 'transfer_order_id' => $result->id,
  114. 'actual_amount' => $result->actual_amount,
  115. 'status' => 'success'
  116. ];
  117. } catch (\Exception $e) {
  118. Log::error("URS提取处理异常", [
  119. 'urs_user_id' => $userId,
  120. 'amount' => $amount,
  121. 'order_id' => $orderId,
  122. 'error' => $e->getMessage(),
  123. 'trace' => $e->getTraceAsString()
  124. ]);
  125. throw $e;
  126. }
  127. }
  128. /**
  129. * 验证提取请求
  130. *
  131. * @param Request $request 请求对象
  132. * @throws \Exception
  133. */
  134. protected function validateWithdrawRequest(Request $request): void
  135. {
  136. $requiredFields = ['user_id', 'amount', 'order_id'];
  137. foreach ($requiredFields as $field) {
  138. if (!$request->has($field)) {
  139. throw new \Exception("缺少必需字段: {$field}");
  140. }
  141. }
  142. // 验证用户ID
  143. $userId = $request->input('user_id');
  144. if (!is_numeric($userId) || $userId <= 0) {
  145. throw new \Exception('用户ID格式无效');
  146. }
  147. // 验证金额
  148. $amount = $request->input('amount');
  149. if (!is_numeric($amount) || $amount <= 0) {
  150. throw new \Exception('提取金额必须大于0');
  151. }
  152. // 验证订单ID
  153. $orderId = $request->input('order_id');
  154. if (empty($orderId)) {
  155. throw new \Exception('订单ID不能为空');
  156. }
  157. }
  158. }