253 lines
8.2 KiB
PHP
253 lines
8.2 KiB
PHP
<?php
|
|
/**
|
|
* 华为支付模型
|
|
*/
|
|
|
|
namespace addon\huaweipay\model;
|
|
|
|
use addon\huaweipay\data\sdk\HuaweiPayClient;
|
|
use app\model\BaseModel;
|
|
use app\model\system\Cron;
|
|
use app\model\system\Pay as PayCommon;
|
|
use app\model\system\Pay as PayModel;
|
|
use think\facade\Log;
|
|
|
|
/**
|
|
* 华为支付配置
|
|
*/
|
|
class Pay extends BaseModel
|
|
{
|
|
public $hwpay_client;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param $site_id 站点ID
|
|
*/
|
|
function __construct($site_id)
|
|
{
|
|
try {
|
|
// 获取华为支付参数
|
|
$config_info = (new Config())->getPayConfig($site_id)['data']['value'];
|
|
|
|
if (!empty($config_info)) {
|
|
// 初始化华为支付客户端
|
|
$this->hwpay_client = new HuaweiPayClient($config_info);
|
|
}
|
|
} catch (\Exception $e) {
|
|
return $this->error('', '华为支付配置错误: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 生成支付
|
|
* @param $param 支付参数
|
|
* @return array
|
|
*/
|
|
public function pay($param)
|
|
{
|
|
try {
|
|
// 构造华为支付请求参数
|
|
$parameter = array(
|
|
"out_trade_no" => $param["out_trade_no"],
|
|
"subject" => substr($param["pay_body"], 0, 15),
|
|
"total_amount" => (float)$param["pay_money"],
|
|
"body" => substr($param["pay_body"], 0, 60),
|
|
);
|
|
|
|
// 绑定商户数据
|
|
$pay_model = new PayModel();
|
|
$pay_model->bindMchPay($param["out_trade_no"], []);
|
|
|
|
// 根据不同的应用类型调用不同的华为支付API
|
|
switch ($param["app_type"]) {
|
|
case "h5":
|
|
// H5支付
|
|
$result = $this->hwpay_client->h5Pay($parameter, $param["return_url"], $param["notify_url"]);
|
|
if ($result['code'] == '0') {
|
|
return $this->success([
|
|
'type' => 'url',
|
|
'data' => $result['pay_url']
|
|
]);
|
|
} else {
|
|
return $this->error('', $result['msg'] ?? '华为支付请求失败');
|
|
}
|
|
break;
|
|
case "app":
|
|
// APP支付
|
|
$result = $this->hwpay_client->appPay($parameter, $param["notify_url"]);
|
|
if ($result['code'] == '0') {
|
|
return $this->success([
|
|
'type' => 'params',
|
|
'data' => $result
|
|
]);
|
|
} else {
|
|
return $this->error('', $result['msg'] ?? '华为支付请求失败');
|
|
}
|
|
break;
|
|
default:
|
|
// 默认返回错误
|
|
return $this->error('', '不支持的支付类型');
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::error('华为支付生成支付失败: ' . $e->getMessage());
|
|
return $this->error('', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 支付关闭
|
|
* @param $param 关闭订单参数
|
|
* @return array
|
|
*/
|
|
public function close($param)
|
|
{
|
|
try {
|
|
$parameter = array(
|
|
"out_trade_no" => $param["out_trade_no"]
|
|
);
|
|
|
|
// 调用华为支付关闭订单API
|
|
$result = $this->hwpay_client->closeOrder($parameter);
|
|
|
|
if ($result['code'] == '0') {
|
|
return $this->success();
|
|
} else {
|
|
return $this->error('', $result['msg'] ?? '关闭订单失败');
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::error('华为支付关闭订单失败: ' . $e->getMessage());
|
|
return $this->error('', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 华为支付退款
|
|
* @param array $param 退款参数
|
|
* @return array
|
|
*/
|
|
public function refund($param)
|
|
{
|
|
try {
|
|
$pay_info = $param["pay_info"];
|
|
$refund_no = $param["refund_no"];
|
|
$out_trade_no = $pay_info["out_trade_no"] ?? '';
|
|
$trade_no = $pay_info["trade_no"] ?? '';
|
|
$refund_fee = $param["refund_fee"];
|
|
|
|
$parameter = array(
|
|
'out_trade_no' => $out_trade_no,
|
|
'trade_no' => $trade_no,
|
|
'refund_amount' => sprintf("%.2f", $refund_fee),
|
|
'out_request_no' => $refund_no,
|
|
'refund_reason' => $param["refund_desc"] ?? ''
|
|
);
|
|
|
|
// 调用华为支付退款API
|
|
$result = $this->hwpay_client->refund($parameter);
|
|
|
|
if ($result['code'] == '0') {
|
|
return $this->success();
|
|
} else {
|
|
return $this->error('', $result['msg'] ?? '退款失败');
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::error('华为支付退款失败: ' . $e->getMessage());
|
|
return $this->error('', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 华为支付转账
|
|
* @param $param 转账参数
|
|
* @return array
|
|
*/
|
|
public function transfer($param)
|
|
{
|
|
try {
|
|
// 华为支付转账功能需要根据实际API进行实现
|
|
// 目前华为支付客户端未实现转账功能,需要根据官方文档扩展
|
|
return $this->error('', '华为支付转账功能暂未实现');
|
|
} catch (\Exception $e) {
|
|
Log::error('华为支付转账失败: ' . $e->getMessage());
|
|
return $this->error('', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 异步完成支付
|
|
* @param $param 回调参数
|
|
*/
|
|
public function notify($param)
|
|
{
|
|
try {
|
|
// 验证华为支付回调签名
|
|
$is_valid = $this->hwpay_client->verifyNotify($param);
|
|
|
|
if ($is_valid) { // 验证成功
|
|
$out_trade_no = $param['out_trade_no'];
|
|
// 华为支付交易号
|
|
$trade_no = $param['trade_no'];
|
|
// 交易状态
|
|
$trade_status = $param['trade_status'];
|
|
$pay_common = new PayCommon();
|
|
|
|
if ($trade_status == "TRADE_SUCCESS") {
|
|
$retval = $pay_common->onlinePay($out_trade_no, "huaweipay", $trade_no, "huaweipay");
|
|
}
|
|
echo "success";
|
|
} else {
|
|
// 验证失败
|
|
Log::error('华为支付回调签名验证失败: ' . json_encode($param));
|
|
echo "fail";
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::error('华为支付回调处理失败: ' . $e->getMessage());
|
|
echo "fail";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 授权码支付
|
|
* @param $param 授权码支付参数
|
|
* @return array|mixed|void
|
|
*/
|
|
public function authcodePay($param)
|
|
{
|
|
try {
|
|
// 华为支付授权码支付功能需要根据实际API进行实现
|
|
// 目前华为支付客户端未实现授权码支付功能,需要根据官方文档扩展
|
|
return $this->error('', '华为支付授权码支付功能暂未实现');
|
|
} catch (\Exception $e) {
|
|
Log::error('华为支付授权码支付失败: ' . $e->getMessage());
|
|
return $this->error('', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 查询订单信息
|
|
* @param $param 查询参数
|
|
* @return array
|
|
*/
|
|
public function query($param)
|
|
{
|
|
try {
|
|
// 构造查询请求参数
|
|
$parameter = array(
|
|
"out_trade_no" => $param["out_trade_no"],
|
|
);
|
|
|
|
// 调用华为支付查询订单API
|
|
$result = $this->hwpay_client->queryOrder($parameter);
|
|
|
|
if ($result['code'] == '0') {
|
|
return $this->success($result['data']);
|
|
} else {
|
|
return $this->error('', $result['msg'] ?? '查询订单失败');
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::error('华为支付查询订单失败: ' . $e->getMessage());
|
|
return $this->error('', $e->getMessage());
|
|
}
|
|
}
|
|
}
|