<?php
namespace addon\jhzf\model;
use app\model\system\Cron;
use app\model\system\RechargeOrder;
use Rtgm\sm\RtSm2;
use Rtgm\util\FormatSign;
use app\model\system\Pay as PayModel;
use addon\weapp\model\Config as WeappConfig;
use addon\wechat\model\Config as WechatConfig;
use think\Exception;
class Jhzf
{
public $app_id ='';// 招行提供 appid
public $secrt ='';/// 招行提供 appscript
public $merId ='';// 商户号:
public $userid = '';// 收银员编号
public $encoding =''; // 文档对应
public $version = ''; // 文档对应
public $signMethod = ''; // 02 = SM2国密方式
public $privateKey = ''; // 私钥
public $pub_key = ''; // 公钥
public $t_start = ''; // 公钥头
public $notifyUrl = ''; //
public $host = 'https://api.cmburl.cn:8065';
private $config = [];
private $is_weapp = 0;
/**
* 微信支付接口版本
* @var string
*/
private $api = 'v2';
private $jichu =[
'version'=>'0.0.1',
'encoding'=>'UTF-8',
'signMethod'=>'02'
];
/**
* 站点id
* @var
*/
private $site_id;
public function pay()
{
$pay_data['subOpenId'] = '';//用户openid
$pay_data['merId'] = $this->config["mch_id"];
$pay_data['subAppId'] = $this->config["subAppId"];//小程序或者公众号 App的appid
$pay_data['orderId'] ='';//订单号
$pay_data['tradeType'] ="JSAPI";
$pay_data['userId'] = $this->userid;
$pay_data['body'] = '';
$pay_data['notifyUrl'] = $this->notifyUrl;
$pay_data['spbillCreateIp'] = $_SERVER['REMOTE_ADDR'];//用户的ip
$pay_data['txnAmt'] = intval(1 * 100); //支付金额
$datta = $this->jichu;
ksort($pay_data);
$datta['biz_content'] = json_encode($pay_data);
ksort($datta);
$sign = $this->getSign($datta);
$datta['sign'] = $sign;
$url = $this->host.'/polypay/v1.0/mchorders/onlinepay';
ksort($datta);
$re = $this->curl_post($url,$datta,$this->getHead($sign));
return success(0,'',$re);
}
public function getQrcode()
{
$data = [
'orderId'=>'',
// 'orderId'=>'20240117110500439',
'notifyUrl'=>$this->notifyUrl,
'payValidTime'=>strval(60*60*2),
'userId'=> $this->userid,
'txnAmt'=> strval(intval(1 * 100)),
'body'=> '',
'currencyCode'=>'156',
'mchReserved'=>'1'
];
$data = array_filter($data);
$re = $this->gathering($data);
}
/** 参数排序拼接
* @param $array
* @return string
*/
public function ToUrlParams(array $array)
{
$buff = "";
foreach ($array as $k => $v) {
if ($v != "" && !is_array($v)) {
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
/**
* @param $sign
* @return array设置头部信息
*/
public function getHead($sign)
{
$time = time();
$head = [
'appid'=>$this->config['appid'],
'secret'=>$this->config['app_secrect'],
'sign'=>$sign,
'timestamp'=>$time
];
ksort($head);
$appid = $this->config['appid'];
$apisign = md5(rawurldecode(http_build_query($head)));
$headers[] = "Content-Type: application/json" ;
$headers[] = "appid: $appid" ;
$headers[] = "timestamp: $time" ;
$headers[] = "apisign: $apisign";
return $headers;
}
/**
* @param $data
* @return array|void退款接口
*/
public function refund($data){
try {
$data =[
'merId'=>$this->config['mch_id'],
'orderId'=>$data['refund_no'],//退款订单号
'userId'=>$this->userid,
'origOrderId'=>'',//商户订单号
'txnAmt'=>intval(1 * 100),//实际支付金额
'refundAmt'=>intval(1 * 100),//退款金额
];
$data = array_filter($data);
$list = $this->jichu;
$list['biz_content'] = json_encode($data,JSON_UNESCAPED_SLASHES);
$sign = $this->getSign($list);
$list['sign'] = $sign;
//$lsit['sign'] = $data['sign'];
$RechargeOrder = new RechargeOrder();
$cron = new Cron();
$url = $this->host.'/polypay/v1.0/mchorders/orderquery';
ksort($list);
$re = $this->curl_post($url,$list,$this->getHead($sign));
if ($re['returnCode'] == 'SUCCESS'){
if ($re['respCode'] == 'SUCCESS'){
if ($re['refundState'] == 'P'){
//退款中,生成定时查询
}else if ($re['refundState'] == 'S'){
//退款成功 调用成功接口
}else if ($re['refundState'] == 'F'){
//退款失败
throw new Exception($re['failureReason']);
}
}else if ( $re['respCode'] == 'SYSTERM_ERROR'){
//聚合内部问题 建议轮询查询订单状态
}else if ( $re['respCode'] == 'FAIL'){
throw new Exception('退款失敗');
}
}
}catch (Exception $e){
return error(-1,$e->getMessage());
}
}
public function payNotify()
{
$data = input();
//myLogWrite(json_encode($data),'jhpayNotify');
$re= $this->ValidSign($data);
$re = true;
if ($re){
//说明验签通过
try {
$content = json_decode($data['biz_content'],true);
//实现自己的逻辑
$da = $this->jichu;
$da['returnCode'] = 'SUCCESS';
$da['respCode'] = 'SUCCESS';
$data['sign'] = $this->getSign($da);
ksort($data);
return json_encode($data);
}catch (Exception $e){
$da = $this->jichu;
$da['returnCode'] = 'SUCCESS';
$da['respCode'] = 'FAIL';
$data['sign'] = $this->getSign($da);
ksort($data);
return json_encode($data);
}
}else{
$da = $this->jichu;
$da['returnCode'] = 'FAIL';
$data['sign'] = $this->getSign($da);
ksort($data);
return json_encode($data);
}
}
/**
* @xk 签名校验
* @method ValidSign
* @param array $array [description]
* @Version:V6.0
*/
public function ValidSign($array = [])
{
$sign =$array['sign'];
unset($array['sign']);
ksort($array);
$document = $this->ToUrlParams($array);
$sm2 = new RtSm2('base64');
// $pub_key = ''; // 配置中的公钥
$bstr=base64_decode($this->pub_key);//base64解码公钥
$pk=bin2hex($bstr);//转16进制
//去公钥头
//
$pkarr=explode(strtolower($this->t_start), $pk);
$pubkey=$pkarr[1];//实际公钥
$sm_userId = "1234567812345678";
$signh = $this->getSign($array);
return $sm2->verifySign($document, $signh, $pubkey, $sm_userId);
}
//订单状态
public function getrefundstatus()
{
$data =[
'merId'=>$this->config['mch_id'],
'orderId'=>'',//商户订单号
'userId'=>$this->userid,
];
$data = array_filter($data);
$list = $this->jichu;
$dat
tp6对接招商银行聚合 sm2国密 api
需积分: 5 120 浏览量
2024-01-18
10:16:21
上传
评论
收藏 3KB ZIP 举报
水浅不摸鱼
- 粉丝: 1
- 资源: 13