webman/app/Utils/API/PaymentNew.php

169 lines
5.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Utils\API;
use GuzzleHttp\Client;
use support\Log;
/**
* 第三方api转账
*/
class PaymentNew
{
protected static $secret_key = "sZ2wAfh1lMkxBVrlY4uZY8Fj92E4scFf";
protected static $ShopUserLongId = "776ae472-d4fc-435c-9639-be5763138d95";
protected static function generate_encrypt_value($data)
{
// 1. 过滤掉 EncryptValue 和值为 null 的参数
$filtered_data = array_filter($data, function ($value, $key) {
return $key !== 'EncryptValue' && $value !== null;
}, ARRAY_FILTER_USE_BOTH);
// 2. 按照 A~Z 顺序排序 key
ksort($filtered_data);
// 3. 拼接 key=value 形式的字符串,并加上 HashKey=密钥
$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
$param_str .= "&HashKey=" . trim(self::$secret_key);
// 输出拼接后的字符串以供调试(可选)
// echo "Lowercase string: $param_str\n";
// 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, $orderId)
{
$url = "https://mdf.hr5688.com/api/createPaymentOrder";
$PaymentChannelId = 0;
$banks = [strtoupper("bKash"), strtoupper("Nagad")];
if (!in_array(strtoupper($payeeBankName), $banks)) {
return -1;
}
if (strtoupper($payeeBankName) == strtoupper("bKash")) {
$PaymentChannelId = 34;
}
if (strtoupper($payeeBankName) == strtoupper("Nagad")) {
$PaymentChannelId = 35;
}
// Guzzle HTTP client
$client = new Client();
// 请求数据
$payload = [
"Amount" => $money,
"CurrencyId" => 11,
"IsTest" => false,
"PayeeAccountName" => $payeeAccountName,
"PayeeAccountNumber" => $payeeAccountNumber,
"PayeeBankName" => $payeeBankName,
"PayeeIFSCCode" => "",
"PaymentChannelId" => $PaymentChannelId,
"ShopInformUrl" => "http:/38.54.94.131/api/withdraw/callback",
"ShopOrderId" => $orderId . "",
"ShopUserLongId" => "776ae472-d4fc-435c-9639-be5763138d95"
];
// 生成加密值
$encrypt_value = self::generate_encrypt_value($payload);
$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 {
Log::warning("请求支付api失败状态码:" . $httpCode . "responseBody:" . $responseBody);
echo $responseBody;
}
return json_decode($responseBody);
} catch (\GuzzleHttp\Exception\RequestException $e) {
Log::warning("请求支付api失未知失败:" . $e->getMessage());
// 如果是 400 错误,可能有额外的请求错误信息
if ($e->hasResponse()) {
$response = $e->getResponse();
$httpCode = $response->getStatusCode();
if ($httpCode == 400) {
$responseBody = $response->getBody()->getContents();
Log::warning("请求支付api失败状态码 400响应体" . $responseBody);
}
}
return json_decode($responseBody);
}
}
/**
* 查询商户余额
* 返回-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());
return -1;
}
}
}