Files
ntonpower/extend/utils/UnionpayB2C.php
2026-10-14 14:00:29 +08:00

147 lines
4.7 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
/**
* unionpay在线网关支付B2C业务
*/
class UnionpayB2C
{
protected $pay_type = 'unionpay_b2c';
#版本号
protected $version;
#编码方式
protected $encoding = 'UTF-8';
#交易币种列表
protected $currencyCode_list = [
'CNY' => '156'
];
protected $currencyCode;
#交易子类型列表,支付接口专用
protected $txnSubType_list = [
'zizhu' => '01',
'fenqi' => '03',
];
protected $txnSubType;
#接入类型
protected $accessType = '0'; //商户直连
#签名算法
protected $signMethod;
#渠道类型列表
protected $channelType_list = [
'pc' => '07',
'mobile' => '08'
];
protected $channelType;
#商户号
// protected $merId = '777290058181532'; //test
protected $merId = '802310057320534'; //线上
#接口url列表
protected $dev_url_list = [
'pay' => 'https://gateway.test.95516.com/gateway/api/frontTransReq.do',
'query' => 'https://gateway.test.95516.com/gateway/api/queryTrans.do',
];
protected $prod_url_list = [
'pay' => 'https://gateway.95516.com/gateway/api/frontTransReq.do',
'query' => 'https://gateway.95516.com/gateway/api/queryTrans.do',
];
protected $url_list;
private $logger;
public function __construct()
{
parent::__construct();
$this->init();
}
/**
* 初始化部分参数
*/
private function init()
{
require_once app()->getRootPath() . '/extend/payment/unionpay/sdk/AcpService.php';
$this->version = com\unionpay\acp\sdk\SDKConfig::getSDKConfig()->version;
$this->front_url = com\unionpay\acp\sdk\SDKConfig::getSDKConfig()->frontUrl;
$this->back_url = com\unionpay\acp\sdk\SDKConfig::getSDKConfig()->backUrl;
$this->signMethod = com\unionpay\acp\sdk\SDKConfig::getSDKConfig()->signMethod;
// $this->url_list = $this->dev_url_list;
$this->url_list = $this->prod_url_list;
$date = explode('-', date('Y-m-d'));
$filepath = APP_PATH . 'Lib/pay/log/' . $date[0] . '-' . $date[1] . '/' . $date[2];
$filename = $this->pay_type . '.log';
import('ORG.Util.FileLog');
$this->logger = new FileLog($filepath, $filename);
}
/**
* 查询支付状态
* @param $pay_log === pay_log表记录
*/
public function query($pay_log)
{
$out_trade_no = $pay_log['out_trade_no'];
$txnTime = date('YmdHis', strtotime($pay_log['create_time']));
$this->bizType = $this->bizType_list['query'][$pay_log['pay_type']];
$this->channelType = $this->channelType_list[$pay_log['source']];
$params = [
'version' => $this->version, //版本号
'encoding' => $this->encoding, //编码方式
'signMethod' => $this->signMethod, //签名方法
'txnType' => '00', //交易类型
'txnSubType' => '00', //交易子类
'bizType' => '000000', //业务类型
'accessType' => $this->accessType, //接入类型
'channelType' => $this->channelType, //渠道类型
'orderId' => $out_trade_no, //请修改被查询的交易的订单号,8-32位数字字母,不能含“-”或“_”,此处默认取demo演示页面传递的参数
'merId' => $this->merId, //商户代码,请改自己的测试商户号,此处默认取demo演示页面传递的参数
'txnTime' => $txnTime, //请修改被查询的交易的订单发送时间,格式为YYYYMMDDhhmmss,此处默认取demo演示页面传递的参数
];
com\unionpay\acp\sdk\AcpService::sign ( $params ); // 签名
$url = $this->url_list['query'];
$this->logger->write('查询请求参数: ' . json_encode($params) . '=====url: ' . $url, 'suc');
$result_arr = com\unionpay\acp\sdk\AcpService::post ( $params, $url);
if (count($result_arr) > 0 &&
com\unionpay\acp\sdk\AcpService::validate ($result_arr) &&
$result_arr["respCode"] == "00" &&
$result_arr["origRespCode"] == "00"
)
{
$this->logger->write('查询完成,交易成功,交易单号: ' . $out_trade_no, 'suc');
return $result_arr;
}
$this->logger->write('查询完成,交易失败,交易单号: ' . $out_trade_no);
return false;
}
private function http_success()
{
header("HTTP/1.1 200 OK");
exit;
}
private function http_fail()
{
header("HTTP/1.1 500 ERROR");
exit;
}
}