| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace ThirdParty\Urs\Webhook;
- use App\Module\ThirdParty\Models\ThirdPartyService as ServiceModel;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\DB;
- /**
- * 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 int $user_id URS用户ID
- * @param string $amount 充值金额
- * @param string $order_id URS订单ID
- * @return array
- */
- protected function processDepositNotification($user_id, $amount, $order_id): array
- {
- // 记录处理日志
- Log::info("URS充值通知处理开始", [
- 'urs_user_id' => $user_id,
- 'amount' => $amount,
- 'order_id' => $order_id,
- 'request_id' => $this->getRequestId(),
- ]);
- try {
- /**
- * 三方应用ID
- * @var int $thirdPartyAppId
- */
- $thirdPartyAppId = $this->service->id;
- // 1. 根据URS用户ID获取农场用户ID
- $farmUserId = \App\Module\UrsPromotion\Services\UrsUserMappingService::getFarmUserId($user_id);
- if (!$farmUserId) {
- Log::error("URS充值失败:未找到用户映射关系", [
- 'urs_user_id' => $user_id,
- 'order_id' => $order_id,
- ]);
- throw new \Exception("用户未进入农场系统,无法完成充值");
- }
- // 2. 开启数据库事务并使用Transfer模块完成充值钻石操作
- $result = DB::transaction(function () use ($thirdPartyAppId, $farmUserId, $user_id, $amount, $order_id) {
- return \App\Module\Transfer\Services\TransferThirdPartyService::createRechargeOrder(
- thirdPartyAppId: $thirdPartyAppId,
- farmUserId: $farmUserId,
- thirdPartyUserId: (string)$user_id,
- thirdPartyAmount: $amount,
- order_id: $order_id,
- remark: "URS充值 - 订单号: {$order_id}",
- callbackData: [
- 'urs_order_id' => $order_id,
- 'urs_user_id' => $user_id,
- 'source' => 'urs_deposit_webhook'
- ]
- );
- });
- // 3. 检查充值结果
- if (is_string($result)) {
- // 充值失败
- Log::error("URS充值失败", [
- 'urs_user_id' => $user_id,
- 'farm_user_id' => $farmUserId,
- 'third_party_app_id' => $thirdPartyAppId,
- 'amount' => $amount,
- 'order_id' => $order_id,
- 'error' => $result,
- ]);
- throw new \Exception("充值失败: {$result}");
- }
- // 4. 充值成功,记录成功日志
- Log::info("URS充值成功", [
- 'urs_user_id' => $user_id,
- 'farm_user_id' => $farmUserId,
- 'third_party_app_id' => $thirdPartyAppId,
- 'amount' => $amount,
- 'order_id' => $order_id,
- 'transfer_order_id' => $result->id,
- 'business_id' => $result->out_order_id,
- 'actual_amount' => $result->out_amount,
- ]);
- // 5. 返回成功响应
- return [
- 'rorder_id' => $result->out_order_id, // 返回Transfer模块生成的业务订单ID
- 'transfer_order_id' => $result->id, // Transfer模块的内部订单ID
- 'actual_amount' => $result->out_amount, // 实际到账金额
- 'status' => 'success'
- ];
- } catch (\Exception $e) {
- // 记录异常日志
- Log::error("URS充值处理异常", [
- 'urs_user_id' => $user_id,
- 'amount' => $amount,
- 'order_id' => $order_id,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString(),
- ]);
- // 重新抛出异常,让上层处理
- throw $e;
- }
- }
- }
|