TransferInHandler.php 6.1 KB

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