ExternalApiService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace App\Module\Transfer\Services;
  3. use App\Module\Transfer\Models\TransferApp;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Support\Facades\Log;
  6. /**
  7. * 外部API服务
  8. * 处理与外部应用的API交互
  9. */
  10. class ExternalApiService
  11. {
  12. /**
  13. * 创建转出订单
  14. *
  15. * @param TransferApp $app 应用配置
  16. * @param array $data 订单数据
  17. * @return array
  18. */
  19. public static function createOutOrder(TransferApp $app, array $data): array
  20. {
  21. try {
  22. if (empty($app->order_out_create_url)) {
  23. return ['success' => false, 'message' => '未配置转出创建API'];
  24. }
  25. // 添加签名
  26. $data['timestamp'] = time();
  27. $data['signature'] = self::generateSignature($data, $app);
  28. // 发送HTTP请求
  29. $response = Http::timeout(30)
  30. ->post($app->order_out_create_url, $data);
  31. if ($response->successful()) {
  32. $result = $response->json();
  33. Log::info('External API create out order success', [
  34. 'app_id' => $app->id,
  35. 'url' => $app->order_out_create_url,
  36. 'request' => $data,
  37. 'response' => $result
  38. ]);
  39. return [
  40. 'success' => true,
  41. 'data' => $result,
  42. 'message' => '创建成功'
  43. ];
  44. } else {
  45. $error = "HTTP {$response->status()}: {$response->body()}";
  46. Log::error('External API create out order failed', [
  47. 'app_id' => $app->id,
  48. 'url' => $app->order_out_create_url,
  49. 'error' => $error
  50. ]);
  51. return [
  52. 'success' => false,
  53. 'message' => $error
  54. ];
  55. }
  56. } catch (\Exception $e) {
  57. Log::error('External API create out order exception', [
  58. 'app_id' => $app->id,
  59. 'error' => $e->getMessage()
  60. ]);
  61. return [
  62. 'success' => false,
  63. 'message' => $e->getMessage()
  64. ];
  65. }
  66. }
  67. /**
  68. * 查询转出订单状态
  69. *
  70. * @param TransferApp $app 应用配置
  71. * @param string $outOrderId 外部订单ID
  72. * @return array
  73. */
  74. public static function queryOutOrder(TransferApp $app, string $outOrderId): array
  75. {
  76. try {
  77. if (empty($app->order_out_info_url)) {
  78. return ['success' => false, 'message' => '未配置转出查询API'];
  79. }
  80. $params = [
  81. 'out_order_id' => $outOrderId,
  82. 'timestamp' => time()
  83. ];
  84. $params['signature'] = self::generateSignature($params, $app);
  85. $response = Http::timeout(30)
  86. ->get($app->order_out_info_url, $params);
  87. if ($response->successful()) {
  88. $result = $response->json();
  89. return [
  90. 'success' => true,
  91. 'data' => $result,
  92. 'message' => '查询成功'
  93. ];
  94. } else {
  95. return [
  96. 'success' => false,
  97. 'message' => "HTTP {$response->status()}: {$response->body()}"
  98. ];
  99. }
  100. } catch (\Exception $e) {
  101. return [
  102. 'success' => false,
  103. 'message' => $e->getMessage()
  104. ];
  105. }
  106. }
  107. /**
  108. * 查询转入订单状态
  109. *
  110. * @param TransferApp $app 应用配置
  111. * @param string $outOrderId 外部订单ID
  112. * @return array
  113. */
  114. public static function queryInOrder(TransferApp $app, string $outOrderId): array
  115. {
  116. try {
  117. if (empty($app->order_in_info_url)) {
  118. return ['success' => false, 'message' => '未配置转入查询API'];
  119. }
  120. $params = [
  121. 'out_order_id' => $outOrderId,
  122. 'timestamp' => time()
  123. ];
  124. $params['signature'] = self::generateSignature($params, $app);
  125. $response = Http::timeout(30)
  126. ->get($app->order_in_info_url, $params);
  127. if ($response->successful()) {
  128. $result = $response->json();
  129. return [
  130. 'success' => true,
  131. 'data' => $result,
  132. 'message' => '查询成功'
  133. ];
  134. } else {
  135. return [
  136. 'success' => false,
  137. 'message' => "HTTP {$response->status()}: {$response->body()}"
  138. ];
  139. }
  140. } catch (\Exception $e) {
  141. return [
  142. 'success' => false,
  143. 'message' => $e->getMessage()
  144. ];
  145. }
  146. }
  147. /**
  148. * 发送回调通知
  149. *
  150. * @param TransferApp $app 应用配置
  151. * @param array $data 回调数据
  152. * @return array
  153. */
  154. public static function sendCallback(TransferApp $app, array $data): array
  155. {
  156. try {
  157. if (empty($app->order_callback_url)) {
  158. return ['success' => false, 'message' => '未配置回调URL'];
  159. }
  160. // 添加签名
  161. $data['signature'] = self::generateSignature($data, $app);
  162. $response = Http::timeout(30)
  163. ->post($app->order_callback_url, $data);
  164. if ($response->successful()) {
  165. $result = $response->json();
  166. Log::info('External API callback success', [
  167. 'app_id' => $app->id,
  168. 'url' => $app->order_callback_url,
  169. 'response' => $result
  170. ]);
  171. return [
  172. 'success' => true,
  173. 'data' => $result,
  174. 'message' => '回调成功'
  175. ];
  176. } else {
  177. $error = "HTTP {$response->status()}: {$response->body()}";
  178. Log::error('External API callback failed', [
  179. 'app_id' => $app->id,
  180. 'url' => $app->order_callback_url,
  181. 'error' => $error
  182. ]);
  183. return [
  184. 'success' => false,
  185. 'message' => $error
  186. ];
  187. }
  188. } catch (\Exception $e) {
  189. Log::error('External API callback exception', [
  190. 'app_id' => $app->id,
  191. 'error' => $e->getMessage()
  192. ]);
  193. return [
  194. 'success' => false,
  195. 'message' => $e->getMessage()
  196. ];
  197. }
  198. }
  199. /**
  200. * 生成API签名
  201. *
  202. * @param array $data 数据
  203. * @param TransferApp $app 应用配置
  204. * @return string
  205. */
  206. private static function generateSignature(array $data, TransferApp $app): string
  207. {
  208. // 移除signature字段
  209. unset($data['signature']);
  210. // 按key排序
  211. ksort($data);
  212. // 拼接字符串
  213. $string = '';
  214. foreach ($data as $key => $value) {
  215. if (is_array($value)) {
  216. $value = json_encode($value, JSON_UNESCAPED_UNICODE);
  217. }
  218. $string .= $key . '=' . $value . '&';
  219. }
  220. // 添加密钥(这里使用应用keyname作为密钥,实际项目中应该有专门的密钥字段)
  221. $string .= 'key=' . $app->keyname;
  222. // 生成MD5签名
  223. return md5($string);
  224. }
  225. /**
  226. * 验证API签名
  227. *
  228. * @param array $data 数据
  229. * @param TransferApp $app 应用配置
  230. * @return bool
  231. */
  232. public static function verifySignature(array $data, TransferApp $app): bool
  233. {
  234. $signature = $data['signature'] ?? '';
  235. $expectedSignature = self::generateSignature($data, $app);
  236. return hash_equals($expectedSignature, $signature);
  237. }
  238. }