| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?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 UrsWithdrawWebhook 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 操作类型(固定为withdraw)
- * @param Request $request 请求对象
- * @return array
- * @throws \Exception
- */
- protected function handler(string $action, Request $request): array
- {
- // 验证操作类型
- if ($action !== 'withdraw') {
- throw new \Exception("此处理器只能处理withdraw操作,当前操作: {$action}");
- }
- // 验证必需字段
- $this->validateWithdrawRequest($request);
- // 获取请求参数
- $userId = $request->input('user_id');
- $amount = $request->input('amount');
- $orderId = $request->input('order_id');
- // 处理提取通知
- return $this->processWithdrawNotification($userId, $amount, $orderId);
- }
- /**
- * 处理提取通知的核心逻辑
- *
- * @param string $userId URS用户ID
- * @param string $amount 提取金额(外部金额)
- * @param string $orderId URS订单ID
- * @return array
- * @throws \Exception
- */
- protected function processWithdrawNotification(string $userId, string $amount, string $orderId): array
- {
- try {
- /**
- * 三方应用ID
- * @var int $thirdPartyAppId
- */
- $thirdPartyAppId = $this->service->id;
- // 1. 根据URS用户ID获取农场用户ID
- $farmUserId = \App\Module\UrsPromotion\Services\UrsUserMappingService::getFarmUserId($userId);
- if (!$farmUserId) {
- Log::error("URS提取失败:未找到用户映射关系", [
- 'urs_user_id' => $userId,
- 'order_id' => $orderId,
- ]);
- throw new \Exception("用户未进入农场系统,无法完成提取");
- }
- // 2. 开启数据库事务并使用Transfer模块完成提取钻石操作
- $result = DB::transaction(function () use ($thirdPartyAppId, $farmUserId, $userId, $amount, $orderId) {
- return \App\Module\Transfer\Services\TransferThirdPartyService::createWithdrawOrder(
- thirdPartyAppId: $thirdPartyAppId,
- farmUserId: $farmUserId,
- thirdPartyUserId: (string)$userId,
- thirdPartyAmount: $amount,
- order_id: $orderId,
- remark: "URS提取 - 订单号: {$orderId}",
- callbackData: [
- 'urs_order_id' => $orderId,
- 'urs_user_id' => $userId,
- 'source' => 'urs_withdraw_webhook'
- ]
- );
- });
- if (is_string($result)) {
- // 提取失败
- Log::error("URS提取失败", [
- 'urs_user_id' => $userId,
- 'farm_user_id' => $farmUserId,
- 'third_party_app_id' => $thirdPartyAppId,
- 'amount' => $amount,
- 'order_id' => $orderId,
- 'error' => $result
- ]);
- throw new \Exception("提取失败: {$result}");
- }
- // 提取成功
- Log::info("URS提取成功", [
- 'urs_user_id' => $userId,
- 'farm_user_id' => $farmUserId,
- 'third_party_app_id' => $thirdPartyAppId,
- 'amount' => $amount,
- 'order_id' => $orderId,
- 'transfer_order_id' => $result->id,
- 'rorder_id' => $result->out_order_id
- ]);
- return [
- 'rorder_id' => $result->out_order_id,
- 'transfer_order_id' => $result->id,
- 'actual_amount' => $result->actual_amount,
- 'status' => 'success'
- ];
- } catch (\Exception $e) {
- Log::error("URS提取处理异常", [
- 'urs_user_id' => $userId,
- 'amount' => $amount,
- 'order_id' => $orderId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- throw $e;
- }
- }
- /**
- * 验证提取请求
- *
- * @param Request $request 请求对象
- * @throws \Exception
- */
- protected function validateWithdrawRequest(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不能为空');
- }
- }
- }
|