TransferInHandler.php 5.7 KB

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