| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace ThirdParty\Urs\Webhook;
- use App\Module\ThirdParty\Models\ThirdPartyService as ServiceModel;
- use App\Module\ThirdParty\Services\WebhookReceiver;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- /**
- * URS充值通知Webhook处理器
- *
- * 专门处理充值相关的Webhook通知
- */
- class UrsDepositWebhook extends 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);
- }
- /**
- * 验证充值请求
- *
- * @param Request $request 请求对象
- * @throws \Exception
- */
- protected function validateDepositRequest(Request $request): void
- {
- $requiredFields = ['user_id', 'amount', 'order_id', 'status'];
- 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不能为空');
- }
- // 验证状态
- $status = $request->input('status');
- if (!in_array($status, ['success', 'failed', 'pending'])) {
- throw new \Exception('状态值无效,必须是: success, failed, pending');
- }
- }
- /**
- * 处理充值通知
- *
- * @param Request $request 请求对象
- * @return array
- */
- protected function processDepositNotification(Request $request): array
- {
- $userId = $request->input('user_id');
- $amount = $request->input('amount');
- $orderId = $request->input('order_id');
- $status = $request->input('status');
- $message = $request->input('message', '');
- // 记录处理日志
- Log::info("URS充值通知处理", [
- 'user_id' => $userId,
- 'amount' => $amount,
- 'order_id' => $orderId,
- 'status' => $status,
- 'message' => $message,
- 'request_id' => $this->getRequestId(),
- ]);
- // 根据状态执行不同的处理逻辑
- switch ($status) {
- case 'success':
- return $this->handleSuccessfulDeposit($userId, $amount, $orderId);
- case 'failed':
- return $this->handleFailedDeposit($userId, $amount, $orderId, $message);
- case 'pending':
- return $this->handlePendingDeposit($userId, $amount, $orderId);
- default:
- throw new \Exception("未知的充值状态: {$status}");
- }
- }
- /**
- * 处理充值成功
- *
- * @param int $userId 用户ID
- * @param float $amount 充值金额
- * @param string $orderId 订单ID
- * @return array
- */
- protected function handleSuccessfulDeposit(int $userId, float $amount, string $orderId): array
- {
- // 这里可以调用Fund模块的充值逻辑
- // 例如:FundService::deposit($userId, $amount, $orderId);
- return [
- 'message' => '充值成功通知处理完成',
- 'user_id' => $userId,
- 'amount' => $amount,
- 'order_id' => $orderId,
- 'status' => 'success',
- 'actions' => [
- 'balance_updated' => true,
- 'transaction_recorded' => true,
- 'user_notified' => true,
- ],
- 'processed_at' => now()->toISOString(),
- ];
- }
- /**
- * 处理充值失败
- *
- * @param int $userId 用户ID
- * @param float $amount 充值金额
- * @param string $orderId 订单ID
- * @param string $message 失败原因
- * @return array
- */
- protected function handleFailedDeposit(int $userId, float $amount, string $orderId, string $message): array
- {
- // 这里可以调用相关的失败处理逻辑
- // 例如:记录失败原因、退款处理等
- return [
- 'message' => '充值失败通知处理完成',
- 'user_id' => $userId,
- 'amount' => $amount,
- 'order_id' => $orderId,
- 'status' => 'failed',
- 'failure_reason' => $message,
- 'actions' => [
- 'failure_logged' => true,
- 'refund_initiated' => true,
- 'user_notified' => true,
- ],
- 'processed_at' => now()->toISOString(),
- ];
- }
- /**
- * 处理充值待处理
- *
- * @param int $userId 用户ID
- * @param float $amount 充值金额
- * @param string $orderId 订单ID
- * @return array
- */
- protected function handlePendingDeposit(int $userId, float $amount, string $orderId): array
- {
- // 这里可以调用待处理相关的逻辑
- // 例如:加入处理队列、发送通知等
- return [
- 'message' => '充值待处理通知处理完成',
- 'user_id' => $userId,
- 'amount' => $amount,
- 'order_id' => $orderId,
- 'status' => 'pending',
- 'actions' => [
- 'added_to_processing_queue' => true,
- 'admin_notified' => true,
- ],
- 'processed_at' => now()->toISOString(),
- ];
- }
- }
|