diff --git a/app/Api/Controllers/UnionPayController.php b/app/Api/Controllers/UnionPayController.php index 2c3b935..e2a471a 100644 --- a/app/Api/Controllers/UnionPayController.php +++ b/app/Api/Controllers/UnionPayController.php @@ -38,10 +38,10 @@ class UnionPayController extends Controller unset($inputs['sign']); $action = new UnionPay($inputs, $sign); - $action->addLog(); + // $action->addLog(); $action->start(); - $action->updateLog(); + // $action->updateLog(); return $action->respond(); diff --git a/packages/unionpay/src/Action/Init.php b/packages/unionpay/src/Action/Init.php index 3912264..2394fc4 100644 --- a/packages/unionpay/src/Action/Init.php +++ b/packages/unionpay/src/Action/Init.php @@ -53,8 +53,10 @@ class Init */ 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); $pub_key_id = openssl_get_publickey($public_key); @@ -72,6 +74,26 @@ class Init 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: 签名 * @Author: 玄尘 diff --git a/packages/unionpay/src/UnionPay.php b/packages/unionpay/src/UnionPay.php index 83ccc4b..62b704d 100644 --- a/packages/unionpay/src/UnionPay.php +++ b/packages/unionpay/src/UnionPay.php @@ -40,10 +40,10 @@ class UnionPay extends Init { //设置基础数据 $this->getOutBaseData(); - //校验数据 - $this->checkInData(); - try { + try { + //校验数据 + $this->checkInData(); //查询是否是幂等 就是重复查询 $this->idempotent(); //入库请求参数 @@ -148,6 +148,7 @@ class UnionPay extends Init { //验签 $res = $this->checkSign(false, false); + if ($res !== true) { $this->msg_rsp_code = 9996; $this->msg_rsp_desc = '验签失败'; @@ -222,10 +223,7 @@ class UnionPay extends Init break; //冲正 case '002101': - $basics = array_merge($basics, [ - 'msg_ver' => 0.1, - ]); - //撤销 + //撤销 case '002102': $basics = array_merge($basics, [ 'msg_ver' => 0.1, @@ -256,12 +254,15 @@ class UnionPay extends Init //更新返回值 public function updateOutData() { - $this->outdata['sign'] = $this->getSign(); - $this->model->out_source = $this->outdata; - if ($this->outdata['msg_rsp_code'] != '0000') { - $this->model->status = 0; + $this->outdata['sign'] = $this->getSign(); + //如果有入库模型 + if ($this->model) { + $this->model->out_source = $this->outdata; + if ($this->outdata['msg_rsp_code'] != '0000') { + $this->model->status = 0; + } + $this->model->save(); } - $this->model->save(); }