From 3ace17af4d1fcd59b3d7f035084b50a295a40af9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E7=8B=BC?= <2468023037@qq.com> Date: Tue, 18 Feb 2025 11:08:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=BD=AC=E8=B4=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Utils/API/Payment.php | 61 ++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/app/Utils/API/Payment.php b/app/Utils/API/Payment.php index aa2c264..003d947 100644 --- a/app/Utils/API/Payment.php +++ b/app/Utils/API/Payment.php @@ -9,15 +9,40 @@ use GuzzleHttp\Client; */ class Payment { + function generate_encrypt_value($data, $secret_key) { + // 1. 过滤掉 EncryptValue 和值为 null 的参数 + $filtered_data = array_filter($data, function($v, $k) { + return $k !== "EncryptValue" && $v !== null; + }, ARRAY_FILTER_USE_BOTH); + + // 2. 按照 A~Z 顺序排序 key + ksort($filtered_data); + + // 3. 拼接 key=value 形式的字符串,并加上 HashKey=密钥 + $param_str = http_build_query($filtered_data) . "&HashKey=$secret_key"; + + // 4. 转换为小写 + $param_str = strtolower($param_str); + + // 5. 计算 sha256 哈希 + $hash_value = hash('sha256', $param_str); + + // 6. 转换为大写返回 + return strtoupper($hash_value); + } /** * 转账 */ public static function pushMoney($money,$payeeAccountName,$payeeAccountNumber,$payeeBankName) { - $client = new Client(); // 创建 Guzzle 客户端 - + $secret_key = "sZ2wAfh1lMkxBVrlY4uZY8Fj92E4scFf"; + $url = "https://mdf.hr5688.com/api/createPaymentOrder"; + $headers = [ + "accept" => "application/json", + "content-type" => "application/json" + ]; // 请求数据 - $data = [ + $payload = [ "Amount" => $money, "CurrencyId" => 11, "IsTest" => false, @@ -31,15 +56,29 @@ class Payment "ShopRemark" => "", // 留空时可不给此参数 "ShopUserLongId" => "776ae472-d4fc-435c-9639-be5763138d95" ]; + $secret_key = "sZ2wAfh1lMkxBVrlY4uZY8Fj92E4scFf"; + $encrypt_value = Payment::generate_encrypt_value($payload, $secret_key); + $payload["EncryptValue"] = $encrypt_value; - // 发送 POST 请求 - $response = $client->post('https://example.com/api/createPaymentOrder', [ - 'json' => $data, // 以 JSON 格式发送数据 - ]); + $json_payload = json_encode($payload); - // 获取响应体内容 - $body = $response->getBody(); - $responseData = json_decode($body, true); // 如果返回的是 JSON 格式,解析它 - return $responseData; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $json_payload); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $response = curl_exec($ch); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + if ($http_code == 200) { + echo $response; + } else { + echo "Error: HTTP Code $http_code\n"; + echo $response; + } + curl_close($ch); + return $response; } }