| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- namespace App\Module\Transfer\Services;
- use App\Module\Transfer\Models\TransferApp;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
- /**
- * 外部API服务
- * 处理与外部应用的API交互
- */
- class ExternalApiService
- {
- /**
- * 创建转出订单
- *
- * @param TransferApp $app 应用配置
- * @param array $data 订单数据
- * @return array
- */
- public static function createOutOrder(TransferApp $app, array $data): array
- {
- try {
- if (empty($app->order_out_create_url)) {
- return ['success' => false, 'message' => '未配置转出创建API'];
- }
- // 添加签名
- $data['timestamp'] = time();
- $data['signature'] = self::generateSignature($data, $app);
- // 发送HTTP请求
- $response = Http::timeout(30)
- ->post($app->order_out_create_url, $data);
- if ($response->successful()) {
- $result = $response->json();
- Log::info('External API create out order success', [
- 'app_id' => $app->id,
- 'url' => $app->order_out_create_url,
- 'request' => $data,
- 'response' => $result
- ]);
- return [
- 'success' => true,
- 'data' => $result,
- 'message' => '创建成功'
- ];
- } else {
- $error = "HTTP {$response->status()}: {$response->body()}";
- Log::error('External API create out order failed', [
- 'app_id' => $app->id,
- 'url' => $app->order_out_create_url,
- 'error' => $error
- ]);
- return [
- 'success' => false,
- 'message' => $error
- ];
- }
- } catch (\Exception $e) {
- Log::error('External API create out order exception', [
- 'app_id' => $app->id,
- 'error' => $e->getMessage()
- ]);
- return [
- 'success' => false,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 查询转出订单状态
- *
- * @param TransferApp $app 应用配置
- * @param string $outOrderId 外部订单ID
- * @return array
- */
- public static function queryOutOrder(TransferApp $app, string $outOrderId): array
- {
- try {
- if (empty($app->order_out_info_url)) {
- return ['success' => false, 'message' => '未配置转出查询API'];
- }
- $params = [
- 'out_order_id' => $outOrderId,
- 'timestamp' => time()
- ];
- $params['signature'] = self::generateSignature($params, $app);
- $response = Http::timeout(30)
- ->get($app->order_out_info_url, $params);
- if ($response->successful()) {
- $result = $response->json();
- return [
- 'success' => true,
- 'data' => $result,
- 'message' => '查询成功'
- ];
- } else {
- return [
- 'success' => false,
- 'message' => "HTTP {$response->status()}: {$response->body()}"
- ];
- }
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 查询转入订单状态
- *
- * @param TransferApp $app 应用配置
- * @param string $outOrderId 外部订单ID
- * @return array
- */
- public static function queryInOrder(TransferApp $app, string $outOrderId): array
- {
- try {
- if (empty($app->order_in_info_url)) {
- return ['success' => false, 'message' => '未配置转入查询API'];
- }
- $params = [
- 'out_order_id' => $outOrderId,
- 'timestamp' => time()
- ];
- $params['signature'] = self::generateSignature($params, $app);
- $response = Http::timeout(30)
- ->get($app->order_in_info_url, $params);
- if ($response->successful()) {
- $result = $response->json();
- return [
- 'success' => true,
- 'data' => $result,
- 'message' => '查询成功'
- ];
- } else {
- return [
- 'success' => false,
- 'message' => "HTTP {$response->status()}: {$response->body()}"
- ];
- }
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 发送回调通知
- *
- * @param TransferApp $app 应用配置
- * @param array $data 回调数据
- * @return array
- */
- public static function sendCallback(TransferApp $app, array $data): array
- {
- try {
- if (empty($app->order_callback_url)) {
- return ['success' => false, 'message' => '未配置回调URL'];
- }
- // 添加签名
- $data['signature'] = self::generateSignature($data, $app);
- $response = Http::timeout(30)
- ->post($app->order_callback_url, $data);
- if ($response->successful()) {
- $result = $response->json();
- Log::info('External API callback success', [
- 'app_id' => $app->id,
- 'url' => $app->order_callback_url,
- 'response' => $result
- ]);
- return [
- 'success' => true,
- 'data' => $result,
- 'message' => '回调成功'
- ];
- } else {
- $error = "HTTP {$response->status()}: {$response->body()}";
- Log::error('External API callback failed', [
- 'app_id' => $app->id,
- 'url' => $app->order_callback_url,
- 'error' => $error
- ]);
- return [
- 'success' => false,
- 'message' => $error
- ];
- }
- } catch (\Exception $e) {
- Log::error('External API callback exception', [
- 'app_id' => $app->id,
- 'error' => $e->getMessage()
- ]);
- return [
- 'success' => false,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 生成API签名
- *
- * @param array $data 数据
- * @param TransferApp $app 应用配置
- * @return string
- */
- private static function generateSignature(array $data, TransferApp $app): string
- {
- // 移除signature字段
- unset($data['signature']);
- // 按key排序
- ksort($data);
- // 拼接字符串
- $string = '';
- foreach ($data as $key => $value) {
- if (is_array($value)) {
- $value = json_encode($value, JSON_UNESCAPED_UNICODE);
- }
- $string .= $key . '=' . $value . '&';
- }
- // 添加密钥(这里使用应用keyname作为密钥,实际项目中应该有专门的密钥字段)
- $string .= 'key=' . $app->keyname;
- // 生成MD5签名
- return md5($string);
- }
- /**
- * 验证API签名
- *
- * @param array $data 数据
- * @param TransferApp $app 应用配置
- * @return bool
- */
- public static function verifySignature(array $data, TransferApp $app): bool
- {
- $signature = $data['signature'] ?? '';
- $expectedSignature = self::generateSignature($data, $app);
- return hash_equals($expectedSignature, $signature);
- }
- }
|