$out_trade_no]); if ($result) { file_put_contents($log_file, "成功:使用orderCode参数完成订单\n", FILE_APPEND); echo "success"; exit; } // 方法2:尝试使用orderId参数 if (isset($orderInfo['userOrder']['id'])) { $result = finishOrder($out_trade_no, $token, ["orderId" => $orderInfo['userOrder']['id']]); if ($result) { file_put_contents($log_file, "成功:使用orderId参数完成订单\n", FILE_APPEND); echo "success"; exit; } } // 方法3:使用完整的订单数据 if (isset($orderInfo['userOrder'])) { $orderData = $orderInfo['userOrder']; $orderData['isFinished'] = true; $result = finishOrder($out_trade_no, $token, $orderData); if ($result) { file_put_contents($log_file, "成功:使用完整订单数据完成订单\n", FILE_APPEND); echo "success"; exit; } } // 如果所有方法都失败,尝试多种不同API路径 $apiPaths = [ "/UserOrderService/finishUserOrder", "/UserOrderService/updateUserOrder", "/UserOrderService/updateOrderFinished", "/UserOrderService/payUserOrder" ]; foreach ($apiPaths as $apiPath) { $result = callAPI($apiPath, ["orderCode" => $out_trade_no], $token); file_put_contents($log_file, "尝试API $apiPath 结果: " . json_encode($result) . "\n", FILE_APPEND); if ($result && isset($result['code']) && $result['code'] == 200) { file_put_contents($log_file, "成功:使用 $apiPath 完成订单\n", FILE_APPEND); echo "success"; exit; } } // 如果前面的方法都失败,返回错误 file_put_contents($log_file, "错误:所有方法都无法完成订单\n", FILE_APPEND); die("error"); } catch (Exception $e) { file_put_contents($log_file, "异常:" . $e->getMessage() . "\n", FILE_APPEND); die("error"); } // 获取访问令牌 function getAccessToken() { global $config, $log_file; $api_url = $config['goedge_api_url'] . "/APIAccessTokenService/getAPIAccessToken"; $auth_data = [ "type" => "admin", "accessKeyId" => $config['goedge_access_id'], "accessKey" => $config['goedge_access_key'] ]; file_put_contents($log_file, "请求访问令牌: " . $api_url . "\n", FILE_APPEND); $ch = curl_init($api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($auth_data)); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $response = curl_exec($ch); $errno = curl_errno($ch); curl_close($ch); if ($errno) { return false; } $result = json_decode($response, true); if (!$result || !isset($result['data']['token'])) { return false; } file_put_contents($log_file, "成功获取访问令牌\n", FILE_APPEND); return $result['data']['token']; } // 查询订单 function findOrder($orderCode, $token) { global $config, $log_file; $api_url = $config['goedge_api_url'] . "/UserOrderService/findEnabledUserOrder"; $data = [ "code" => $orderCode ]; file_put_contents($log_file, "查询订单: " . $api_url . "\n", FILE_APPEND); file_put_contents($log_file, "查询参数: " . json_encode($data) . "\n", FILE_APPEND); $ch = curl_init($api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'X-Edge-Access-Token: ' . $token ]); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $response = curl_exec($ch); $errno = curl_errno($ch); curl_close($ch); if ($errno) { file_put_contents($log_file, "查询订单失败: cURL错误\n", FILE_APPEND); return false; } file_put_contents($log_file, "查询订单响应: " . $response . "\n", FILE_APPEND); $result = json_decode($response, true); if (!$result || $result['code'] != 200 || !isset($result['data'])) { file_put_contents($log_file, "查询订单失败: 无效响应\n", FILE_APPEND); return false; } return $result['data']; } // 完成订单 function finishOrder($orderCode, $token, $data) { global $config, $log_file; $api_url = $config['goedge_api_url'] . "/UserOrderService/finishUserOrder"; file_put_contents($log_file, "完成订单: " . $api_url . "\n", FILE_APPEND); file_put_contents($log_file, "订单数据: " . json_encode($data) . "\n", FILE_APPEND); $result = callAPI($api_url, $data, $token); if (!$result || !isset($result['code']) || $result['code'] != 200) { file_put_contents($log_file, "完成订单失败: " . json_encode($result) . "\n", FILE_APPEND); return false; } return true; } // 调用API function callAPI($url, $data, $token) { global $config, $log_file; if (strpos($url, 'http') !== 0) { $url = $config['goedge_api_url'] . $url; } $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'X-Edge-Access-Token: ' . $token ]); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $response = curl_exec($ch); $errno = curl_errno($ch); $error = curl_error($ch); curl_close($ch); if ($errno) { file_put_contents($log_file, "API调用失败: ($errno) $error\n", FILE_APPEND); return false; } return json_decode($response, true); } ?>