UrsCheckWebhook.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace ThirdParty\Urs\Webhook;
  3. use App\Module\ThirdParty\Models\ThirdPartyService as ServiceModel;
  4. use Illuminate\Http\Request;
  5. /**
  6. * URS余额检查Webhook处理器
  7. *
  8. * 专门处理余额检查相关的Webhook通知
  9. */
  10. class UrsCheckWebhook extends WebhookReceiver
  11. {
  12. /**
  13. * 构造函数
  14. *
  15. * @param string $serviceCode 服务代码
  16. * @param Request $request 请求对象
  17. * @param ServiceModel $service 服务配置对象
  18. */
  19. public function __construct(string $serviceCode, Request $request, ServiceModel $service)
  20. {
  21. parent::__construct($serviceCode, $request, $service);
  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->processCheck($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', 'amount' ];
  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. public function processCheck()
  63. {
  64. // 是否允许,钻石余额 ,本金总数,手续费总数,所需总数
  65. return [
  66. 'check' => false, // 是否允许
  67. 'diamond_balance' => 100, // 钻石余额
  68. 'principal_total' => 500, // 本金总数
  69. 'fee_total' => 50, // 手续费总数
  70. 'required_total' => 550, // 所需总数
  71. ];
  72. }
  73. }