diff --git a/example/precard/test.php b/example/precard/test.php new file mode 100644 index 0000000..fc61a3d --- /dev/null +++ b/example/precard/test.php @@ -0,0 +1,25 @@ +setDev(1)->setMerchantId('903310112340001') + ->setPkcs12(dirname(__FILE__) . DIRECTORY_SEPARATOR . '903310112340001-kltong.pfx') + ->setPkcsPassword('123456'); +$data = [ + 'cardId'=>'8889990010000028810', + 'txAt'=>1, + 'reqSeq'=>date("mdHis").random_int(1,100000), + 'pin1'=>'580058', + +]; +try{ + $result = Charge::execute(Channel::PRECARD_CARDWITHPASS,$pre_card_config,$data); +}catch (PayException $e){ + exit($e->getMessage()); +}catch(Exception $e){ + exit($e->getMessage()); +} \ No newline at end of file diff --git a/src/Kltong/Pay/Biz/CardWithPassBiz.php b/src/Kltong/Pay/Biz/CardWithPassBiz.php new file mode 100644 index 0000000..969210e --- /dev/null +++ b/src/Kltong/Pay/Biz/CardWithPassBiz.php @@ -0,0 +1,73 @@ +merchantId = $this->pre_card_config->getMerchantId(); + $cardPassContent->version = $this->pre_card_config->getVersion(); + $cardPassContent->signType = $this->pre_card_config->getSignType(); + $this->cardPassContent = $cardPassContent; + } + + private function process_content(){ + $this->cardPassContent->cardId = $this->pre_card_data['cardId']; + $this->cardPassContent->pin1 = $this->pre_card_data['pin1']; + $this->cardPassContent->txAt = $this->pre_card_data['txAt']; + $this->cardPassContent->reqDt = date("Ymd"); + $this->cardPassContent->reqTm = date("His"); + $this->cardPassContent->reqSeq = $this->pre_card_data['reqSeq']; + + } + + public function process(){ + $this->process_data(); + $this->process_content(); + //处理加答前参数 + $string = ArrayParams::get_sign_string($this->cardPassContent->toArray()); + $sign = RSA::getSign($string,file_get_contents($this->pre_card_config->getPkcs12()),$this->pre_card_config->getPkcsPassword()); + $this->cardPassContent->sign = $sign ; + + $head = ""; + $content = ''; + $content = array( + 'cardId'=>$this->cardPassContent->cardId, + 'txAt'=>$this->cardPassContent->txAt, + 'reqDt'=>$this->cardPassContent->reqDt, + 'reqTm'=>$this->cardPassContent->reqTm, + 'reqSeq'=>$this->cardPassContent->reqSeq, + 'pin1'=>$this->cardPassContent->pin1, + ); + $newContent = array(); + foreach ($content as $k=>$val){ + if(!empty($val)){ + $newContent[$k]=$val; + } + } + $head = $this->get_header(); + $requet_data = array( + 'head'=>$head, + 'content'=> $newContent + ); + return $requet_data; + + } + + + +} \ No newline at end of file diff --git a/src/Kltong/Pay/ChargeContext.php b/src/Kltong/Pay/ChargeContext.php index 4a99b23..219a632 100644 --- a/src/Kltong/Pay/ChargeContext.php +++ b/src/Kltong/Pay/ChargeContext.php @@ -1,7 +1,9 @@ channel = new PreCardCharge($config); - break; + $this->channel = new PreCardCharge($config); + break; + case Channel::PRECARD_CARDWITHPASS; + $this->channel = new CardWithPassBiz($config); + break; default : throw new PayException("传入的支付通道不支持!请检查channel"); } diff --git a/src/Kltong/Pay/Client/Charge.php b/src/Kltong/Pay/Client/Charge.php index 5dfecb3..a1407f3 100644 --- a/src/Kltong/Pay/Client/Charge.php +++ b/src/Kltong/Pay/Client/Charge.php @@ -2,7 +2,7 @@ namespace Kltong\Pay\Client; use Kltong\Pay\ChargeContext; -use Kltong\Pay\Channel; +use Kltong\Pay\Config\Channel; use Kltong\Pay\Exception\PayException; /** @@ -17,7 +17,8 @@ class Charge * @var array */ private static $support_channel=[ - Channel::PRECARD_MAGNETICANDPWD + Channel::PRECARD_MAGNETICANDPWD, + Channel::PRECARD_CARDWITHPASS, ]; /** diff --git a/src/Kltong/Pay/Common/PreCardProcess.php b/src/Kltong/Pay/Common/PreCardProcess.php new file mode 100644 index 0000000..631c424 --- /dev/null +++ b/src/Kltong/Pay/Common/PreCardProcess.php @@ -0,0 +1,82 @@ +merchantId)){ + throw new PayException("未设置商户号"); + } + if(empty($config->pkcs12)){ + throw new PayException("未设置证书"); + } + if(empty($config->pkcsPassword)){ + throw new PayException("未设置证书密码"); + } + $this->pre_card_config = $config; + } + + /** + * @inheritDoc + */ + public function handle(array $data) + { + $this->pre_card_data = $data; + $data = $this->process(); + if($this->pre_card_config->getDev() =="1"){ + $url = 'https://ipay.chinasmartpay.cn/openapi/LianxinCard/pay'; + }else{ + $url = 'https://openapi.openepay.com/openapi/LianxinCard/pay'; + } + + $client = new Client([ + 'timeout' => '10.0' + ]); + $response = $client->request('POST', $url, ['timeout'=>5,'verify'=>false,'json'=>$data]); + if ($response->getStatusCode() != '200') { + throw new PayException('网络发生错误,请稍后再试curl返回码:' . $response->getReasonPhrase()); + } + $body = $response->getBody(); + // 格式化为数组 + $retData = json_decode($body,true); + if (isset($retData['responseCode']) && $retData['responseCode']!= 'SUCCESS') { + throw new PayException('支付失败:' . $retData['responseMsg']); + } + return $retData; + + + } + + + public function get_header(){ + return array( + 'version'=>$this->cardPassContent->version, + 'merchantId'=>$this->cardPassContent->merchantId, + 'signType'=>$this->cardPassContent->signType, + 'sign' => $this->cardPassContent->sign + ); + } + + +} \ No newline at end of file diff --git a/src/Kltong/Pay/Config/Channel.php b/src/Kltong/Pay/Config/Channel.php index ca6fd89..013a0af 100644 --- a/src/Kltong/Pay/Config/Channel.php +++ b/src/Kltong/Pay/Config/Channel.php @@ -1,5 +1,5 @@ merchantId; + } + + /** + * @param $merchantId + * @return $this + */ + public function setMerchantId($merchantId) + { + $this->merchantId = $merchantId; + return $this; + } + + /** + * @return mixed + */ + public function getPkcs12() + { + return $this->pkcs12; + } + + /** + * @param $pkcs12 + * @return $this + */ + public function setPkcs12($pkcs12) + { + $this->pkcs12 = $pkcs12; + return $this; + } + + /** + * @return mixed + */ + public function getPkcsPassword() + { + return $this->pkcsPassword; + } + + /** + * @param $pkcsPassword + * @return $this + */ + public function setPkcsPassword($pkcsPassword) + { + $this->pkcsPassword = $pkcsPassword; + return $this; + } + + /** + * @return int + */ + public function getSignType() + { + return $this->signType; + } + + /** + * @param $signType + * @return $this + */ + public function setSignType($signType) + { + if(empty($signType)){ + return $this; + } + $this->signType = $signType == 1 ? 1 :2; + return $this; + } + + /** + * @return int + */ + public function getDev() + { + return $this->dev; + } + + /** + * @param $dev + * @return $this + */ + public function setDev($dev) + { + $this->dev = $dev == 1 ? 1 :2; + return $this; + } + + /** + * @return string + */ + public function getVersion() + { + return $this->version; + } + + /** + * @param $version + * @return $this + */ + public function setVersion($version) + { + $this->version = $version; + return $this; + } + + +} \ No newline at end of file diff --git a/src/Kltong/Pay/Request/CardPassContent.php b/src/Kltong/Pay/Request/CardPassContent.php new file mode 100644 index 0000000..d407f9b --- /dev/null +++ b/src/Kltong/Pay/Request/CardPassContent.php @@ -0,0 +1,47 @@ +$val) { - if(!empty($val) && !in_array($k,array('sign','app_cert','app_pass'))){ + if(!empty($val) && !in_array($k,array('dev','sign','app_cert','app_pass'))){ $pre_array[] = $k.'='.$val; } }