diff --git a/config.php b/config.php new file mode 100644 index 0000000..dfea67e --- /dev/null +++ b/config.php @@ -0,0 +1,9 @@ +"",//易支付地址 + "api_pid"=>"",//易支付PID + "api_key"=>"",//易支付KEY + "goedge_api_url"=>"",//GoEdge系统API URL + "goedge_access_id"=>"",//GoEdge系统Access ID + "goedge_access_key"=>"",//GoEdge系统中Access KEY +); \ No newline at end of file diff --git a/notify.php b/notify.php new file mode 100644 index 0000000..c0622a5 --- /dev/null +++ b/notify.php @@ -0,0 +1,225 @@ + $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); +} +?> \ No newline at end of file diff --git a/pay.php b/pay.php new file mode 100644 index 0000000..9280ca2 --- /dev/null +++ b/pay.php @@ -0,0 +1,135 @@ + "admin", + "accessKeyId"=> $config['goedge_access_id'], + "accessKey"=> $config['goedge_access_key'] + ]; + $access_token_json = sendPostJson($config['goedge_api_url']."/APIAccessTokenService/getAPIAccessToken",json_encode($auth_info,true),"none"); + $access_token = json_decode($access_token_json,true); + $token = $access_token['data']['token']; + $code = [ + "code"=>$orderCode, + ]; + $status = sendPostJson($config['goedge_api_url']."/UserOrderService/findEnabledUserOrder",json_encode($code,true),$token); + $status_decode = json_decode($status,true); + if ($status_decode['code'] != 200 or $status_decode['data']['userOrder']['code'] != $orderCode) { + exit("验证失败"); + } +} +function callPaymentApi($orderCode, $orderAmount,$type) { + // 商户ID、密钥 + global $config; + $merchant_id = $config['api_pid']; // 请替换为您的商户ID + $merchant_key = $config['api_key']; // 替换为真实密钥 + + // 支付接口URL + $api_url = $config['api_url']."/submit.php"; + // 请求参数 + $params = [ + 'pid' => $merchant_id, + 'type' => $type, // 支付方式,您可以选择 'wxpay' 或其他方式 + 'out_trade_no' => $orderCode, // 商户订单号 + 'notify_url' => "https://gateway.tools.tf/api".'/notify.php', // 异步通知地址 请注意这里应该改为当前网站!! + 'return_url' => "https://gateway.tools.tf/api".'/return.php', // 页面跳转通知地址 请注意这里应该改为当前网站!! + 'name' => 'DokiDoki CDN', // 商品名称 + 'money' => $orderAmount, // 订单金额 + 'sign_type' => "MD5" + ]; + + // 生成签名 + $params['sign'] = generateSign($params, $merchant_key); + + // 发起POST请求 + $response = sendPostRequest($api_url, $params); + + // 解析返回的JSON数据 + $result = json_decode($response, true); + echo "
"; + echo ""; +} + +// 生成签名的函数 +function generateSign($params, $key) { + // 1. 按照参数名的ASCII码排序 + ksort($params); + + // 2. 拼接成键值对的字符串 + $signStr = ''; + foreach ($params as $k => $v) { + if ($k != "sign" && $k != "sign_type" && $v != '') { + $signStr .= $k . '=' . $v . '&'; + } + } + // 3. 将密钥拼接到字符串后 + $signStr = substr($signStr,0,-1); + $signStr .= $key; + + // 4. MD5加密并返回小写签名 + return md5($signStr); +} +// 发送POST请求的函数 +function sendPostRequest($url, $data) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + curl_close($ch); + + return $response; +} +function sendPostJson($url, $json,$token) { + // 初始化cURL + $ch = curl_init($url); + + // 设置cURL选项 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Content-Type: application/json', // 设置请求头为JSON + 'Content-Length: ' . strlen($json), // 设置请求体长度 + 'X-Edge-Access-Token: ' . $token + )); + curl_setopt($ch, CURLOPT_POSTFIELDS, $json); // 传递JSON数据 + + // 执行请求并获取响应 + $response = curl_exec($ch); + + // 错误处理 + if (curl_errno($ch)) { + echo 'Error:' . curl_error($ch); + } + + // 关闭cURL会话 + curl_close($ch); + + // 返回响应 + return $response; +} +?> diff --git a/return.php b/return.php new file mode 100644 index 0000000..b458d7b --- /dev/null +++ b/return.php @@ -0,0 +1,198 @@ +支付过程中出现了问题,请发送工单联系管理员处理。'; + $button_text = '联系管理员'; + $button_link = 'mailto:pghkipy@gmail.com'; + } +} else { + // 签名验证失败 + $message = '签名无效'; + $alert_class = 'alert-danger'; + $icon = 'fas fa-exclamation-circle'; + $additional_info = '签名验证失败,请联系管理员。'; + $button_text = '联系管理员'; + $button_link = 'mailto:pghkipy@gmail.com'; +} + +// HTML 页面开始 +?> + + + + + +