| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace ThirdParty\Urs\Webhook;
- use App\Module\ThirdParty\Models\ThirdPartyService as ServiceModel;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- /**
- * URS充值通知Webhook处理器
- *
- * 专门处理充值相关的Webhook通知
- */
- class UrsDepositWebhook extends \ThirdParty\Urs\Webhook\WebhookReceiver
- {
- /**
- * 构造函数
- *
- * @param string $serviceCode 服务代码
- * @param Request $request 请求对象
- * @param ServiceModel $service 服务配置对象
- */
- public function __construct(string $serviceCode, Request $request, ServiceModel $service)
- {
- parent::__construct($serviceCode, $request, $service);
- }
- /**
- * 处理充值通知
- *
- * @param string $action 操作类型(固定为deposit)
- * @param Request $request 请求对象
- * @return array
- * @throws \Exception
- */
- protected function handler(string $action, Request $request): array
- {
- // 验证操作类型
- if ($action !== 'deposit') {
- throw new \Exception("此处理器只能处理deposit操作,当前操作: {$action}");
- }
- // 验证必需字段
- $this->validateDepositRequest($request);
- // 处理充值通知
- return $this->processDepositNotification($request->get('user_id'), $request->get('amount'), $request->get('order_id'));
- }
- /**
- * 验证充值请求
- *
- * @param Request $request 请求对象
- * @throws \Exception
- */
- protected function validateDepositRequest(Request $request): void
- {
- $requiredFields = [ 'user_id', 'amount', 'order_id' ];
- foreach ($requiredFields as $field) {
- if (!$request->has($field)) {
- throw new \Exception("缺少必需字段: {$field}");
- }
- }
- // 验证用户ID
- $userId = $request->input('user_id');
- if (!is_numeric($userId) || $userId <= 0) {
- throw new \Exception('用户ID格式无效');
- }
- // 验证金额
- $amount = $request->input('amount');
- if (!is_numeric($amount) || $amount <= 0) {
- throw new \Exception('充值金额必须大于0');
- }
- // 验证订单ID
- $orderId = $request->input('order_id');
- if (empty($orderId)) {
- throw new \Exception('订单ID不能为空');
- }
- }
- /**
- * 处理充值通知
- *
- * @param Request $request 请求对象
- * @return array
- */
- protected function processDepositNotification($user_id, $amount, $order_id): array
- {
- $tid = $this->getConfigDto()->
- // 记录处理日志
- Log::info("URS充值通知处理", [
- 'user_id' => $user_id,
- 'amount' => $amount,
- 'order_id' => $order_id,
- 'request_id' => $this->getRequestId(),
- ]);
- // 先创建充值单,然后返回成功,通过会队列处理充值单
- $rorder_id = time();
- return [
- 'rorder_id' => $rorder_id
- ];
- }
- }
|