feat: 转账

This commit is contained in:
陈狼 2025-02-18 11:08:46 +08:00
parent 8fab1c0c3d
commit 3ace17af4d
1 changed files with 50 additions and 11 deletions

View File

@ -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;
}
}