UrsDepositWebhook.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /**
  7. * URS充值通知Webhook处理器
  8. *
  9. * 专门处理充值相关的Webhook通知
  10. */
  11. class UrsDepositWebhook extends \ThirdParty\Urs\Webhook\WebhookReceiver
  12. {
  13. /**
  14. * 构造函数
  15. *
  16. * @param string $serviceCode 服务代码
  17. * @param Request $request 请求对象
  18. * @param ServiceModel $service 服务配置对象
  19. */
  20. public function __construct(string $serviceCode, Request $request, ServiceModel $service)
  21. {
  22. parent::__construct($serviceCode, $request, $service);
  23. }
  24. /**
  25. * 处理充值通知
  26. *
  27. * @param string $action 操作类型(固定为deposit)
  28. * @param Request $request 请求对象
  29. * @return array
  30. * @throws \Exception
  31. */
  32. protected function handler(string $action, Request $request): array
  33. {
  34. // 验证操作类型
  35. if ($action !== 'deposit') {
  36. throw new \Exception("此处理器只能处理deposit操作,当前操作: {$action}");
  37. }
  38. // 验证必需字段
  39. $this->validateDepositRequest($request);
  40. // 处理充值通知
  41. return $this->processDepositNotification($request->get('user_id'), $request->get('amount'), $request->get('order_id'));
  42. }
  43. /**
  44. * 验证充值请求
  45. *
  46. * @param Request $request 请求对象
  47. * @throws \Exception
  48. */
  49. protected function validateDepositRequest(Request $request): void
  50. {
  51. $requiredFields = [ 'user_id', 'amount', 'order_id' ];
  52. foreach ($requiredFields as $field) {
  53. if (!$request->has($field)) {
  54. throw new \Exception("缺少必需字段: {$field}");
  55. }
  56. }
  57. // 验证用户ID
  58. $userId = $request->input('user_id');
  59. if (!is_numeric($userId) || $userId <= 0) {
  60. throw new \Exception('用户ID格式无效');
  61. }
  62. // 验证金额
  63. $amount = $request->input('amount');
  64. if (!is_numeric($amount) || $amount <= 0) {
  65. throw new \Exception('充值金额必须大于0');
  66. }
  67. // 验证订单ID
  68. $orderId = $request->input('order_id');
  69. if (empty($orderId)) {
  70. throw new \Exception('订单ID不能为空');
  71. }
  72. }
  73. /**
  74. * 处理充值通知
  75. *
  76. * @param Request $request 请求对象
  77. * @return array
  78. */
  79. protected function processDepositNotification($user_id, $amount, $order_id): array
  80. {
  81. $tid = $this->getConfigDto()->
  82. // 记录处理日志
  83. Log::info("URS充值通知处理", [
  84. 'user_id' => $user_id,
  85. 'amount' => $amount,
  86. 'order_id' => $order_id,
  87. 'request_id' => $this->getRequestId(),
  88. ]);
  89. // 先创建充值单,然后返回成功,通过会队列处理充值单
  90. $rorder_id = time();
  91. return [
  92. 'rorder_id' => $rorder_id
  93. ];
  94. }
  95. }