TransferOutHandler.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Module\OpenAPI\Handlers\Transfer;
  3. use App\Module\OpenAPI\Handlers\BaseHandler;
  4. use App\Module\OpenAPI\Enums\SCOPE_TYPE;
  5. use App\Module\Transfer\Services\TransferService;
  6. use App\Module\Transfer\Validations\TransferOutValidation;
  7. use Illuminate\Http\JsonResponse;
  8. /**
  9. * 转出处理器
  10. * 处理用户向外部应用转出资金的请求
  11. */
  12. class TransferOutHandler extends BaseHandler
  13. {
  14. /**
  15. * 获取所需权限范围
  16. *
  17. * @return array
  18. */
  19. public function getRequiredScopes(): array
  20. {
  21. return [SCOPE_TYPE::TRANSFER_OUT];
  22. }
  23. /**
  24. * 处理转出请求
  25. *
  26. * @param array $data 请求数据
  27. * @param array $context 上下文信息
  28. * @return JsonResponse
  29. */
  30. public function handle(array $data, array $context = []): JsonResponse
  31. {
  32. try {
  33. // 验证权限
  34. if (!$this->validatePermissions($context['app']->scopes ?? [], $context)) {
  35. return $this->errorResponse('权限不足', null, 403);
  36. }
  37. // 添加应用ID到数据中
  38. $data['transfer_app_id'] = $context['app']->id ?? null;
  39. if (!$data['transfer_app_id']) {
  40. return $this->errorResponse('应用ID缺失', null, 400);
  41. }
  42. // 验证请求数据
  43. $validation = new TransferOutValidation($data);
  44. if (!$validation->validate()) {
  45. return $this->errorResponse('数据验证失败', $validation->getErrors(), 422);
  46. }
  47. // 获取验证后的数据
  48. $validatedData = $validation->getValidatedData();
  49. // 调用Transfer服务创建转出订单
  50. $result = TransferService::createTransferOut(
  51. transferAppId: $validatedData['transfer_app_id'],
  52. userId: $validatedData['user_id'],
  53. amount: $validatedData['amount'],
  54. password: $validatedData['password'],
  55. googleCode: $validatedData['google_code'] ?? null,
  56. outUserId: $validatedData['out_user_id'] ?? null,
  57. remark: $validatedData['remark'] ?? null,
  58. callbackData: $validatedData['callback_data'] ?? []
  59. );
  60. if (is_string($result)) {
  61. // 返回错误信息
  62. return $this->errorResponse($result, null, 400);
  63. }
  64. // 记录操作日志
  65. $this->logAction('transfer.out.create', [
  66. 'order_id' => $result->id,
  67. 'business_id' => $result->out_order_id,
  68. 'amount' => $result->amount,
  69. 'user_id' => $result->user_id
  70. ], $context);
  71. // 返回成功响应
  72. return $this->successResponse('转出订单创建成功', [
  73. 'order_id' => $result->id,
  74. 'business_id' => $result->out_order_id,
  75. 'amount' => $result->amount,
  76. 'out_amount' => $result->out_amount,
  77. 'exchange_rate' => $result->exchange_rate,
  78. 'status' => $result->status->value,
  79. 'status_text' => $result->status->getDescription(),
  80. 'created_at' => $result->created_at->toISOString()
  81. ]);
  82. } catch (\Exception $e) {
  83. // 记录错误日志
  84. $this->logError('transfer.out.error', $e, $context);
  85. return $this->errorResponse('转出处理失败', [
  86. 'error' => $e->getMessage()
  87. ], 500);
  88. }
  89. }
  90. /**
  91. * 获取Handler描述
  92. *
  93. * @return string
  94. */
  95. public function getDescription(): string
  96. {
  97. return '处理用户向外部应用转出资金的请求';
  98. }
  99. /**
  100. * 获取请求参数说明
  101. *
  102. * @return array
  103. */
  104. public function getRequestParameters(): array
  105. {
  106. return [
  107. 'business_id' => [
  108. 'type' => 'string',
  109. 'required' => true,
  110. 'description' => '外部业务订单ID,用于防重复提交'
  111. ],
  112. 'user_id' => [
  113. 'type' => 'integer',
  114. 'required' => true,
  115. 'description' => '转出用户ID'
  116. ],
  117. 'amount' => [
  118. 'type' => 'string',
  119. 'required' => true,
  120. 'description' => '转出金额(内部金额)'
  121. ],
  122. 'out_user_id' => [
  123. 'type' => 'string',
  124. 'required' => false,
  125. 'description' => '外部用户ID(可选)'
  126. ],
  127. 'remark' => [
  128. 'type' => 'string',
  129. 'required' => false,
  130. 'description' => '备注信息'
  131. ],
  132. 'callback_data' => [
  133. 'type' => 'object',
  134. 'required' => false,
  135. 'description' => '回调数据,将在回调时原样返回'
  136. ]
  137. ];
  138. }
  139. /**
  140. * 获取响应参数说明
  141. *
  142. * @return array
  143. */
  144. public function getResponseParameters(): array
  145. {
  146. return [
  147. 'order_id' => [
  148. 'type' => 'integer',
  149. 'description' => '内部订单ID'
  150. ],
  151. 'business_id' => [
  152. 'type' => 'string',
  153. 'description' => '外部业务订单ID'
  154. ],
  155. 'amount' => [
  156. 'type' => 'string',
  157. 'description' => '内部金额'
  158. ],
  159. 'out_amount' => [
  160. 'type' => 'string',
  161. 'description' => '外部金额(按汇率转换后)'
  162. ],
  163. 'exchange_rate' => [
  164. 'type' => 'string',
  165. 'description' => '使用的汇率'
  166. ],
  167. 'status' => [
  168. 'type' => 'integer',
  169. 'description' => '订单状态值'
  170. ],
  171. 'status_text' => [
  172. 'type' => 'string',
  173. 'description' => '订单状态描述'
  174. ],
  175. 'created_at' => [
  176. 'type' => 'string',
  177. 'description' => '创建时间(ISO格式)'
  178. ]
  179. ];
  180. }
  181. }