UrsCheckWebhook.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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->processCheck($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. public function processCheck()
  70. {
  71. // 是否允许,钻石余额 ,本金总数,手续费总数,所需总数
  72. // 调用 充值模块 来完成
  73. return [
  74. 'check' => true,
  75. ];
  76. }
  77. }