| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- namespace App\Module\OpenAPI\Handlers\Transfer;
- use App\Module\OpenAPI\Handlers\BaseHandler;
- use App\Module\OpenAPI\Enums\SCOPE_TYPE;
- use App\Module\Transfer\Services\TransferService;
- use App\Module\Transfer\Validations\TransferOutValidation;
- use Illuminate\Http\JsonResponse;
- /**
- * 转出处理器
- * 处理用户向外部应用转出资金的请求
- */
- class TransferOutHandler extends BaseHandler
- {
- /**
- * 获取所需权限范围
- *
- * @return array
- */
- public function getRequiredScopes(): array
- {
- return [SCOPE_TYPE::TRANSFER_OUT];
- }
- /**
- * 处理转出请求
- *
- * @param array $data 请求数据
- * @param array $context 上下文信息
- * @return JsonResponse
- */
- public function handle(array $data, array $context = []): JsonResponse
- {
- try {
- // 验证权限
- if (!$this->validatePermissions($context['app']->scopes ?? [], $context)) {
- return $this->errorResponse('权限不足', null, 403);
- }
- // 添加应用ID到数据中
- $data['transfer_app_id'] = $context['app']->id ?? null;
- if (!$data['transfer_app_id']) {
- return $this->errorResponse('应用ID缺失', null, 400);
- }
- // 验证请求数据
- $validation = new TransferOutValidation($data);
- if (!$validation->validate()) {
- return $this->errorResponse('数据验证失败', $validation->getErrors(), 422);
- }
- // 获取验证后的数据
- $validatedData = $validation->getValidatedData();
- // 调用Transfer服务创建转出订单
- $result = TransferService::createTransferOut(
- transferAppId: $validatedData['transfer_app_id'],
- userId: $validatedData['user_id'],
- amount: $validatedData['amount'],
- password: $validatedData['password'],
- googleCode: $validatedData['google_code'] ?? null,
- outUserId: $validatedData['out_user_id'] ?? null,
- remark: $validatedData['remark'] ?? null,
- callbackData: $validatedData['callback_data'] ?? []
- );
- if (is_string($result)) {
- // 返回错误信息
- return $this->errorResponse($result, null, 400);
- }
- // 记录操作日志
- $this->logAction('transfer.out.create', [
- 'order_id' => $result->id,
- 'business_id' => $result->out_order_id,
- 'amount' => $result->amount,
- 'user_id' => $result->user_id
- ], $context);
- // 返回成功响应
- return $this->successResponse('转出订单创建成功', [
- 'order_id' => $result->id,
- 'business_id' => $result->out_order_id,
- 'amount' => $result->amount,
- 'out_amount' => $result->out_amount,
- 'exchange_rate' => $result->exchange_rate,
- 'status' => $result->status->value,
- 'status_text' => $result->status->getDescription(),
- 'created_at' => $result->created_at->toISOString()
- ]);
- } catch (\Exception $e) {
- // 记录错误日志
- $this->logError('transfer.out.error', $e, $context);
- return $this->errorResponse('转出处理失败', [
- 'error' => $e->getMessage()
- ], 500);
- }
- }
- /**
- * 获取Handler描述
- *
- * @return string
- */
- public function getDescription(): string
- {
- return '处理用户向外部应用转出资金的请求';
- }
- /**
- * 获取请求参数说明
- *
- * @return array
- */
- public function getRequestParameters(): array
- {
- return [
- 'business_id' => [
- 'type' => 'string',
- 'required' => true,
- 'description' => '外部业务订单ID,用于防重复提交'
- ],
- 'user_id' => [
- 'type' => 'integer',
- 'required' => true,
- 'description' => '转出用户ID'
- ],
- 'amount' => [
- 'type' => 'string',
- 'required' => true,
- 'description' => '转出金额(内部金额)'
- ],
- 'out_user_id' => [
- 'type' => 'string',
- 'required' => false,
- 'description' => '外部用户ID(可选)'
- ],
- 'remark' => [
- 'type' => 'string',
- 'required' => false,
- 'description' => '备注信息'
- ],
- 'callback_data' => [
- 'type' => 'object',
- 'required' => false,
- 'description' => '回调数据,将在回调时原样返回'
- ]
- ];
- }
- /**
- * 获取响应参数说明
- *
- * @return array
- */
- public function getResponseParameters(): array
- {
- return [
- 'order_id' => [
- 'type' => 'integer',
- 'description' => '内部订单ID'
- ],
- 'business_id' => [
- 'type' => 'string',
- 'description' => '外部业务订单ID'
- ],
- 'amount' => [
- 'type' => 'string',
- 'description' => '内部金额'
- ],
- 'out_amount' => [
- 'type' => 'string',
- 'description' => '外部金额(按汇率转换后)'
- ],
- 'exchange_rate' => [
- 'type' => 'string',
- 'description' => '使用的汇率'
- ],
- 'status' => [
- 'type' => 'integer',
- 'description' => '订单状态值'
- ],
- 'status_text' => [
- 'type' => 'string',
- 'description' => '订单状态描述'
- ],
- 'created_at' => [
- 'type' => 'string',
- 'description' => '创建时间(ISO格式)'
- ]
- ];
- }
- }
|