webman/app/Utils/API/PaymentNew.php

158 lines
5.2 KiB
PHP
Raw Normal View History

2025-02-18 11:35:00 +08:00
<?php
namespace App\Utils\API;
use GuzzleHttp\Client;
use support\Log;
/**
* 第三方api转账
*/
class PaymentNew
{
2025-02-20 21:00:55 +08:00
protected static $secret_key = "sZ2wAfh1lMkxBVrlY4uZY8Fj92E4scFf";
protected static $ShopUserLongId = "776ae472-d4fc-435c-9639-be5763138d95";
protected static function generate_encrypt_value($data)
2025-02-18 11:35:00 +08:00
{
2025-02-20 21:00:55 +08:00
// 1. 过滤掉 EncryptValue 和值为 null 的参数
$filtered_data = array_filter($data, function ($value, $key) {
2025-02-18 17:22:37 +08:00
return $key !== 'EncryptValue' && $value !== null;
2025-02-18 11:35:00 +08:00
}, ARRAY_FILTER_USE_BOTH);
// 2. 按照 A~Z 顺序排序 key
ksort($filtered_data);
// 3. 拼接 key=value 形式的字符串,并加上 HashKey=密钥
2025-02-18 17:22:37 +08:00
$param_str = '';
foreach ($filtered_data as $key => $value) {
// 确保键名和值没有多余的空格
$key = trim($key);
// 处理布尔值
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
} else {
$value = trim($value);
}
$param_str .= "$key=$value&";
}
// 移除最后一个多余的 &
$param_str = rtrim($param_str, '&');
// 添加 HashKey
2025-02-20 21:00:55 +08:00
$param_str .= "&HashKey=" . trim(self::$secret_key);
2025-02-18 17:22:37 +08:00
// 输出拼接后的字符串以供调试(可选)
2025-02-20 21:00:55 +08:00
// echo "Lowercase string: $param_str\n";
2025-02-18 11:35:00 +08:00
// 4. 转换为小写
$param_str = strtolower($param_str);
// 5. 计算 sha256 哈希
$hash_value = hash('sha256', $param_str);
// 6. 转换为大写返回
return strtoupper($hash_value);
}
/**
* 转账
*/
2025-02-20 21:00:55 +08:00
public static function pushMoney($money, $payeeAccountName, $payeeAccountNumber, $payeeBankName, $orderId)
2025-02-18 11:35:00 +08:00
{
$url = "https://mdf.hr5688.com/api/createPaymentOrder";
2025-02-18 17:22:37 +08:00
$PaymentChannelId = 0;
2025-02-20 22:25:31 +08:00
$banks = [strtoupper("bKash"), strtoupper("Nagad")];
if (!in_array(strtoupper($payeeBankName), $banks)) {
return -1;
}
if (strtoupper($payeeBankName) == strtoupper("bKash")) {
2025-02-18 17:22:37 +08:00
$PaymentChannelId = 34;
2025-02-20 22:25:31 +08:00
}
if (strtoupper($payeeBankName) == strtoupper("Nagad")) {
2025-02-18 17:22:37 +08:00
$PaymentChannelId = 35;
}
2025-02-18 11:35:00 +08:00
// Guzzle HTTP client
$client = new Client();
// 请求数据
$payload = [
"Amount" => $money,
"CurrencyId" => 11,
"IsTest" => false,
"PayeeAccountName" => $payeeAccountName,
"PayeeAccountNumber" => $payeeAccountNumber,
"PayeeBankName" => $payeeBankName,
2025-02-18 17:22:37 +08:00
"PayeeIFSCCode" => "",
"PaymentChannelId" => $PaymentChannelId,
2025-02-20 23:45:28 +08:00
"ShopInformUrl" => "http:/38.54.94.131/api/withdraw/callback",
2025-02-20 21:00:55 +08:00
"ShopOrderId" => $orderId . "",
2025-02-18 11:35:00 +08:00
"ShopUserLongId" => "776ae472-d4fc-435c-9639-be5763138d95"
];
// 生成加密值
2025-02-20 21:00:55 +08:00
$encrypt_value = self::generate_encrypt_value($payload);
2025-02-18 11:35:00 +08:00
$payload["EncryptValue"] = $encrypt_value;
try {
// 发送 POST 请求
$response = $client->post($url, [
'json' => $payload,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
]
]);
// 获取响应内容
$responseBody = $response->getBody()->getContents();
$httpCode = $response->getStatusCode();
if ($httpCode == 200) {
echo $responseBody;
} else {
2025-02-20 21:00:55 +08:00
Log::warning("请求支付api失败状态码:" . $httpCode . "responseBody:" . $responseBody);
2025-02-18 11:35:00 +08:00
echo $responseBody;
}
return $responseBody;
} catch (\GuzzleHttp\Exception\RequestException $e) {
2025-02-20 21:00:55 +08:00
Log::warning("请求支付api失未知失败:" . $e->getMessage());
}
}
/**
* 查询商户余额
* 返回-1则是失败 其他是余额
*/
public static function shopGetBalance()
{
$url = "https://mdf.hr5688.com/api/shopGetBalance";
$payload = [
"CurrencyId" => 11,
"ShopUserLongId" => "776ae472-d4fc-435c-9639-be5763138d95"
];
$encrypt_value = self::generate_encrypt_value($payload);
$payload["EncryptValue"] = $encrypt_value;
$client = new Client();
try {
// 发送 POST 请求
$response = $client->post($url, [
'json' => $payload,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
]
]);
// 获取响应内容
$responseBody = $response->getBody()->getContents();
$httpCode = $response->getStatusCode();
if ($httpCode == 200) {
return json_decode($responseBody)->AmountAvailable;
} else {
Log::warning("请求支付api失败状态码:" . $httpCode . "responseBody:" . $responseBody);
return -1;
}
} catch (\GuzzleHttp\Exception\RequestException $e) {
Log::warning("请求支付api失未知失败:" . $e->getMessage());
2025-02-18 11:35:00 +08:00
}
}
}