second
This commit is contained in:
225
notify.php
Normal file
225
notify.php
Normal file
@@ -0,0 +1,225 @@
|
||||
<?php
|
||||
// 包含配置文件
|
||||
$config = include('config.php');
|
||||
|
||||
// 记录所有请求数据
|
||||
$log_file = 'notify.log';
|
||||
file_put_contents($log_file, date('Y-m-d H:i:s') . " 收到请求:\n", FILE_APPEND);
|
||||
file_put_contents($log_file, "GET: " . json_encode($_GET) . "\n", FILE_APPEND);
|
||||
file_put_contents($log_file, "POST: " . json_encode($_POST) . "\n", FILE_APPEND);
|
||||
|
||||
// 获取订单号
|
||||
$out_trade_no = $_GET['out_trade_no'] ?? '';
|
||||
if (empty($out_trade_no)) {
|
||||
file_put_contents($log_file, "错误:缺少订单号\n", FILE_APPEND);
|
||||
die("错误:无效的请求参数。");
|
||||
}
|
||||
|
||||
try {
|
||||
// 获取访问令牌
|
||||
$token = getAccessToken();
|
||||
if (!$token) {
|
||||
file_put_contents($log_file, "错误:无法获取访问令牌\n", FILE_APPEND);
|
||||
die("error");
|
||||
}
|
||||
|
||||
// 先查询订单详情
|
||||
$orderInfo = findOrder($out_trade_no, $token);
|
||||
if (!$orderInfo) {
|
||||
file_put_contents($log_file, "错误:无法找到订单 $out_trade_no\n", FILE_APPEND);
|
||||
die("error");
|
||||
}
|
||||
|
||||
// 记录订单详情
|
||||
file_put_contents($log_file, "订单详情: " . json_encode($orderInfo) . "\n", FILE_APPEND);
|
||||
|
||||
// 尝试不同的参数组合完成订单
|
||||
$result = false;
|
||||
|
||||
// 方法1:使用orderCode参数
|
||||
$result = finishOrder($out_trade_no, $token, ["orderCode" => $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);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user