Commit 36d3fc63 by yeran

fix the tlpay notify/cancel bug 20180504 09

1 parent b92f0c1f
......@@ -11,6 +11,7 @@ require_once __DIR__ . '/testNotify.php';
use Payment\Common\PayException;
use Payment\Client\Notify;
use Payment\Utils\ArrayUtil;
date_default_timezone_set('Asia/Shanghai');
......@@ -36,9 +37,9 @@ try {
if (!empty($config)){
// $retData = Notify::getNotifyData($type, $config);// 获取第三方的原始数据,未进行签名检查
// $retData = "{}";
$ret = Notify::run($type, $config, $callback);// 处理回调,内部进行了签名检查
$retData = "{\"acct\":\"odNM242ffm2FaJlLIpkcwM1O3YsE\",\"appid\":\"00020281\",\"chnltrxid\":\"4200000135201805042268978922\",\"cusid\":\"55079104816PJXP\",\"cusorderid\":\"20180504091414288930545254561086\",\"outtrxid\":\"20180504091414288930545254561086\",\"paytime\":\"20180504091423\",\"sign\":\"AD9FB6CD0E1455574C690F8AFE26F2A1\",\"termauthno\":\"CFT\",\"termrefnum\":\"4200000135201805042268978922\",\"termtraceno\":\"0\",\"trxamt\":\"1\",\"trxcode\":\"VSP501\",\"trxdate\":\"20180504\",\"trxid\":\"111817080000227489\",\"trxreserved\":\"通联支付测试备注信息\",\"trxstatus\":\"0000\"}";
$ret = ArrayUtil::ValidSign(json_decode($retData,true), $tlConfig['md5_key']);
echo $ret;
}
} catch (PayException $e) {
echo $e->errorMessage();
......
<?php
/**
* 公众号支付
* Created by PhpStorm.
* User: helei
* Date: 2017/4/30
* Time: 下午3:33
*/
require_once __DIR__ . '/../../autoload.php';
use Payment\Common\PayException;
use Payment\Client\Charge;
use Payment\Config;
date_default_timezone_set('Asia/Shanghai');
$tlConfig = require_once __DIR__ . '/../TLConfig.php';
$orderNo = 12345678;//time() . rand(1000, 9999);
// 订单信息
$payData = [
'body' => '商品名称',
'reqsn' => $orderNo,
// 'timeout_express' => time() + 600,// 表示必须 600s 内付款
'trxamt' => '1',// 微信沙箱模式,需要金额固定为3.01
'randomstr' => '1450432107647',//md5(uniqid(md5(microtime(true)), true)),
'remark' => '备注信息',
// 如果是服务商,请提供以下参数
'validtime' => 6,//微信分配的子商户公众账号ID
'acct' => 'odNM24283i2wwQPzqy8Fxyax2y8U',// 微信支付分配的子商户号
'limit_pay' => 'no_credit'
];
$params = array();
$params["trxamt"] = "1";
$params["reqsn"] = "123456";
//$params["oldreqsn"] = "123456";//原来订单号
$params['oldtrxid']='111817080000227489';
$params["randomstr"] = "1450432107647";//
try {
$ret = \Payment\Client\Cancel::run(Config::TL_CHANNEL_LITE, $tlConfig, $params);
} catch (PayException $e) {
echo $e->errorMessage();
exit;
}
echo json_encode($ret, JSON_UNESCAPED_UNICODE);
\ No newline at end of file
......@@ -21,7 +21,7 @@ $wxConfig = require_once __DIR__ . '/../TLConfig.php';
$data = [
'randomstr' => 'asdasda',
'reqsn' => '14935385689468',
'oldreqsn'=> '123456',
'oldtrxid'=>'111817080000227489',
'trxamt' => '100',
'remark' => 'dasd',
......
......@@ -142,8 +142,8 @@
return FALSE;
}
pay();
//cancel();
//refund();
// pay();
// cancel();
refund();
//query();
?>
\ No newline at end of file
......@@ -7,7 +7,7 @@
namespace Payment;
use Payment\Charge\Wx\TLCancel;
use Payment\Charge\TLpay\TLCancel;
use Payment\Common\BaseStrategy;
use Payment\Common\PayException;
......@@ -39,7 +39,7 @@ class CancelContext
try {
switch ($channel) {
case Config::TL_CANCEL:
case Config::TL_CHANNEL_LITE:
$this->cancelHandler = new TLCancel($config);
break;
default:
......
......@@ -6,7 +6,7 @@
* Time: 下午5:44
*/
namespace Payment\Charge\Wx;
namespace Payment\Charge\TLpay;
use Payment\Common\TLConfig;
use Payment\Common\TLpay\Data\Cancel\TLCancelData;
......
<?php
/**
* Created by PhpStorm.
* User: helei
* Date: 2017/3/4
* Time: 下午5:40
*/
namespace Payment\Client;
use Payment\CancelContext;
use Payment\Common\PayException;
use Payment\Config;
class Cancel
{
private static $supportChannel = [
Config::ALI_CHANNEL_APP,// 支付宝 APP 支付
Config::ALI_CHANNEL_WAP, // 支付宝手机网页支付
Config::ALI_CHANNEL_WEB, // 支付宝电脑网站支付
Config::ALI_CHANNEL_QR, // 支付宝当面付-扫码支付
Config::ALI_CHANNEL_BAR,// 支付宝当面付-条码支付
Config::WX_CHANNEL_APP,// 微信 APP 支付
Config::WX_CHANNEL_PUB,// 微信公众号支付
Config::WX_CHANNEL_QR,// 微信公众号扫码支付
Config::WX_CHANNEL_BAR,// 微信刷卡支付
Config::WX_CHANNEL_WAP,// 微信 WAP 支付(此渠道仅针对特定客户开放)
Config::WX_CHANNEL_LITE,// 微信小程序支付
Config::CMB_CHANNEL_APP,// 招行一网通
'applepay_upacp',// Apple Pay
Config::TL_CHANNEL_LITE,
];
/**
* 异步通知类
* @var CancelContext
*/
protected static $instance;
protected static function getInstance($channel, $config)
{
if (is_null(self::$instance)) {
static::$instance = new CancelContext();
try {
static::$instance->initCancelHandler($channel, $config);
} catch (PayException $e) {
throw $e;
}
}
return static::$instance;
}
/**
* @param string $channel
* @param array $config
* @param array $metadata
*
* @return mixed
* @throws PayException
*/
public static function run($channel, $config, $metadata)
{
if (! in_array($channel, self::$supportChannel)) {
throw new PayException('sdk当前不支持该支付渠道,当前仅支持:' . implode(',', self::$supportChannel));
}
try {
$instance = self::getInstance($channel, $config);
$ret = $instance->cancel($metadata);
} catch (PayException $e) {
throw $e;
}
return $ret;
}
}
\ No newline at end of file
......@@ -56,7 +56,7 @@ class RefundData extends TLBaseData
throw new PayException('必须提供通联支付交易号或商户网站唯一订单号。建议使用通联支付交易号');
}
if ($trxamt<=1) {
if ($trxamt<1) {
throw new PayException('退款金额异常');
}
......
......@@ -21,7 +21,7 @@ class TLCancelData extends TLBaseData
$oldreqsn = $this->oldreqsn;//原交易的商户交易单号
$oldtrxid = $this->oldtrxid;//原交易的收银宝平台流水
$reqsn = $this->reqsn;//商户的退款交易订单号,商户平台唯一
if (empty($oldreqsn) || empty($oldtrxid) || empty($reqsn)) {
if ((empty($oldreqsn) && empty($oldtrxid)) || empty($reqsn)) {
throw new PayException('单号数据缺失');
}
}
......
......@@ -41,18 +41,19 @@ class TLNotify extends NotifyStrategy
*/
public function getNotifyData()
{
// php://input 带来的内存压力更小
$data = @file_get_contents('php://input');// 等同于微信提供的:$GLOBALS['HTTP_RAW_POST_DATA']
// 将xml数据格式化为数组
$arrData = json_decode($data,true);
if (empty($arrData)) {
$params = array();
foreach($_POST as $key=>$val) {//动态遍历获取所有收到的参数,此步非常关键,因为收银宝以后可能会加字段,动态获取可以兼容由于收银宝加字段而引起的签名异常
$params[$key] = $val;
}
if(count($params)<1){//如果参数为空,则不进行处理
return false;
}
// 移除值中的空格 xml转化为数组时,CDATA 数据会被带入额外的空格。
$arrData = ArrayUtil::paraFilter($arrData);
if(!$this->checkNotifyData($params)){
return false;
}
return $arrData;
return $params;
}
/**
......@@ -76,20 +77,14 @@ class TLNotify extends NotifyStrategy
*/
protected function verifySign(array $retData)
{
$retSign = $retData['sign'];
$values = ArrayUtil::removeKeys($retData, ['sign', 'sign_type']);
$values = ArrayUtil::paraFilter($values);
$values = ArrayUtil::arraySort($values);
$signStr = ArrayUtil::createLinkstring($values);
$signStr .= '&key=' . $this->config->md5Key;
$sign = md5(StrUtil::getBytes($signStr));
if(ArrayUtil::ValidSign($retData, $this->config->md5Key)){//验签成功
//此处进行业务逻辑处理
return true;
}
else{
return false;
}
return strtoupper($sign) === $retSign;
}
/**
......
......@@ -141,8 +141,8 @@ class ArrayUtil
/**
* 校验签名
* @param array|参数 $array
* @param array 参数
* @param unknown_type appkey
* @return bool
*/
public static function ValidSign(array $array,$appkey){
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!