完善签名

This commit is contained in:
2020-10-13 16:59:05 +08:00
parent 4d97481515
commit 1dd3503ee8
3 changed files with 39 additions and 16 deletions

View File

@@ -38,10 +38,10 @@ class UnionPayController extends Controller
unset($inputs['sign']); unset($inputs['sign']);
$action = new UnionPay($inputs, $sign); $action = new UnionPay($inputs, $sign);
$action->addLog(); // $action->addLog();
$action->start(); $action->start();
$action->updateLog(); // $action->updateLog();
return $action->respond(); return $action->respond();

View File

@@ -53,8 +53,10 @@ class Init
*/ */
public function checkSign($out = true, $self = false) public function checkSign($out = true, $self = false)
{ {
$sign = hex2bin($this->sign); $sign = $this->hexXbin($this->sign);
if (!$sign) {
throw new \Exception('签名错误');
}
$public_key = $this->getPublic($self); $public_key = $this->getPublic($self);
$pub_key_id = openssl_get_publickey($public_key); $pub_key_id = openssl_get_publickey($public_key);
@@ -72,6 +74,26 @@ class Init
return $result; return $result;
} }
/**
* Notes: 校验sign
* @Author: 玄尘
* @Date : 2020/10/13 15:21
* @param $data
* @param false $types
* @return int|string
*/
public function hexXbin($sign, $types = false)
{
// 过滤非16进制字符
$checkStr = strspn($sign, '0123456789abcdefABCDEF');
//字符串长度不是偶数时pack来处理
if (strlen($checkStr) % 2) {
return pack("H*", $sign);
} else {
return hex2bin($sign);
}
}
/** /**
* Notes: 签名 * Notes: 签名
* @Author: 玄尘 * @Author: 玄尘

View File

@@ -40,10 +40,10 @@ class UnionPay extends Init
{ {
//设置基础数据 //设置基础数据
$this->getOutBaseData(); $this->getOutBaseData();
//校验数据
$this->checkInData();
try {
try {
//校验数据
$this->checkInData();
//查询是否是幂等 就是重复查询 //查询是否是幂等 就是重复查询
$this->idempotent(); $this->idempotent();
//入库请求参数 //入库请求参数
@@ -148,6 +148,7 @@ class UnionPay extends Init
{ {
//验签 //验签
$res = $this->checkSign(false, false); $res = $this->checkSign(false, false);
if ($res !== true) { if ($res !== true) {
$this->msg_rsp_code = 9996; $this->msg_rsp_code = 9996;
$this->msg_rsp_desc = '验签失败'; $this->msg_rsp_desc = '验签失败';
@@ -222,10 +223,7 @@ class UnionPay extends Init
break; break;
//冲正 //冲正
case '002101': case '002101':
$basics = array_merge($basics, [ //撤销
'msg_ver' => 0.1,
]);
//撤销
case '002102': case '002102':
$basics = array_merge($basics, [ $basics = array_merge($basics, [
'msg_ver' => 0.1, 'msg_ver' => 0.1,
@@ -256,12 +254,15 @@ class UnionPay extends Init
//更新返回值 //更新返回值
public function updateOutData() public function updateOutData()
{ {
$this->outdata['sign'] = $this->getSign(); $this->outdata['sign'] = $this->getSign();
$this->model->out_source = $this->outdata; //如果有入库模型
if ($this->outdata['msg_rsp_code'] != '0000') { if ($this->model) {
$this->model->status = 0; $this->model->out_source = $this->outdata;
if ($this->outdata['msg_rsp_code'] != '0000') {
$this->model->status = 0;
}
$this->model->save();
} }
$this->model->save();
} }