Skip to content

Commit

Permalink
all
Browse files Browse the repository at this point in the history
  • Loading branch information
tongjingcheng committed Dec 10, 2019
1 parent 013a545 commit 17e996e
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 6 deletions.
25 changes: 25 additions & 0 deletions example/precard/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
require "../../vendor/autoload.php";

use Kltong\Pay\Config\Channel;
use Kltong\Pay\Client\Charge;
use Kltong\Pay\Exception\PayException;

$pre_card_config = new \Kltong\Pay\Config\PreCardConfig();
$pre_card_config->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());
}
73 changes: 73 additions & 0 deletions src/Kltong/Pay/Biz/CardWithPassBiz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
namespace Kltong\Pay\Biz;

use Kltong\Pay\Common\PreCardProcess;
use Kltong\Pay\Request\CardPassContent;
use Kltong\Pay\Utils\ArrayParams;
use Kltong\Pay\Utils\RSA;

/**
* Class CardWithPassBiz
* @package Kltong\Pay\Biz
*/
class CardWithPassBiz extends PreCardProcess
{

public $head;
public $content;


private function process_data(){
$cardPassContent = new CardPassContent();
$cardPassContent->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;

}



}
9 changes: 7 additions & 2 deletions src/Kltong/Pay/ChargeContext.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace Kltong\Pay;
use Kltong\Pay\Biz\CardWithPassBiz;
use Kltong\Pay\Charge\Precard\PreCardCharge;
use Kltong\Pay\Common\BaseStrategy;
use Kltong\Pay\Config\Channel;
use Kltong\Pay\Exception\PayException;

/**
Expand All @@ -24,8 +26,11 @@ class ChargeContext{
public function initContext($channel, $config){
switch ($channel){
case Channel::PRECARD_MAGNETICANDPWD;
$this->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");
}
Expand Down
5 changes: 3 additions & 2 deletions src/Kltong/Pay/Client/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -17,7 +17,8 @@ class Charge
* @var array
*/
private static $support_channel=[
Channel::PRECARD_MAGNETICANDPWD
Channel::PRECARD_MAGNETICANDPWD,
Channel::PRECARD_CARDWITHPASS,
];

/**
Expand Down
82 changes: 82 additions & 0 deletions src/Kltong/Pay/Common/PreCardProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
namespace Kltong\Pay\Common;

use GuzzleHttp\Client;
use Kltong\Pay\Config\PreCardConfig;
use Kltong\Pay\Exception\PayException;

class PreCardProcess implements BaseStrategy
{
/**
* 商户配置
* @var PreCardConfig|null
*/
public $pre_card_config = null;

public $pre_card_data = array();

public $cardPassContent = array();
/**
* 请求参数
* @var array
*/
public $requestData = array();


public function __construct(PreCardConfig $config)
{
if(empty($config->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
);
}


}
2 changes: 1 addition & 1 deletion src/Kltong/Pay/Config/Channel.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Kltong\Pay;
namespace Kltong\Pay\Config;

/**
* 常量
Expand Down
130 changes: 130 additions & 0 deletions src/Kltong/Pay/Config/PreCardConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
namespace Kltong\Pay\Config;

/**
* 商户信信配置
* Class PreCardConfig
* @package Kltong\Pay\Config
*/
class PreCardConfig
{
public $merchantId;
public $pkcs12;
public $pkcsPassword;
public $signType = 2;
public $dev = 1;
public $version = '1.0';

/**
* @return mixed
*/
public function getMerchantId()
{
return $this->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;
}


}
Loading

0 comments on commit 17e996e

Please sign in to comment.