[更新]修正项目类名

This commit is contained in:
邹景立 2018-02-01 09:48:44 +08:00 committed by Anyon
parent 34650e09bb
commit c6a20ad2d6
5 changed files with 38 additions and 39 deletions

View File

@ -20,7 +20,7 @@ use ArrayAccess;
* Class Config * Class Config
* @package Wechat * @package Wechat
*/ */
class Config implements ArrayAccess class DataArray implements ArrayAccess
{ {
/** /**

View File

@ -45,19 +45,19 @@ class Request
/** /**
* 当前公众号配置对象 * 当前公众号配置对象
* @var Config * @var DataArray
*/ */
protected $config; protected $config;
/** /**
* 公众号的推送请求参数 * 公众号的推送请求参数
* @var Config * @var DataArray
*/ */
protected $params; protected $params;
/** /**
* 公众号推送内容对象 * 公众号推送内容对象
* @var Config * @var DataArray
*/ */
protected $receive; protected $receive;
@ -84,8 +84,8 @@ class Request
throw new InvalidArgumentException("Missing Config -- [token]"); throw new InvalidArgumentException("Missing Config -- [token]");
} }
// 参数初始化 // 参数初始化
$this->config = new Config($options); $this->config = new DataArray($options);
$this->params = new Config($_REQUEST); $this->params = new DataArray($_REQUEST);
$this->appid = $this->config->get('appid'); $this->appid = $this->config->get('appid');
// 推送消息处理 // 推送消息处理
if ($_SERVER['REQUEST_METHOD'] == "POST") { if ($_SERVER['REQUEST_METHOD'] == "POST") {
@ -106,7 +106,7 @@ class Request
} }
list($this->postxml, $this->appid) = [$array[1], $array[2]]; list($this->postxml, $this->appid) = [$array[1], $array[2]];
} }
$this->receive = new Config(Tools::xml2arr($this->postxml)); $this->receive = new DataArray(Tools::xml2arr($this->postxml));
} elseif ($_SERVER['REQUEST_METHOD'] == "GET" && $this->checkSignature()) { } elseif ($_SERVER['REQUEST_METHOD'] == "GET" && $this->checkSignature()) {
@ob_clean(); @ob_clean();
exit($this->params->get('echostr')); exit($this->params->get('echostr'));

View File

@ -66,22 +66,6 @@ class Tools
return $method(join('&', $params)); return $method(join('&', $params));
} }
/**
* 生成支付签名
* @param array $data
* @param string $mchKey 商户密钥
* @return string
*/
public static function getPaySign(array $data, $mchKey)
{
ksort($data);
$string = "";
foreach ($data as $key => $value) {
$string .= "{$key}={$value}&";
}
return strtoupper(hash_hmac('SHA256', "{$string}key={$mchKey}", $mchKey));
}
/** /**
* 根据文件后缀获取文件MINE * 根据文件后缀获取文件MINE
* @param array $ext 文件后缀 * @param array $ext 文件后缀
@ -343,6 +327,6 @@ class Tools
} }
self::$cache_path = rtrim(self::$cache_path, '/\\') . DIRECTORY_SEPARATOR; self::$cache_path = rtrim(self::$cache_path, '/\\') . DIRECTORY_SEPARATOR;
file_exists(self::$cache_path) || mkdir(self::$cache_path, 0755, true); file_exists(self::$cache_path) || mkdir(self::$cache_path, 0755, true);
return self::$cache_path . md5($name); return self::$cache_path . $name;
} }
} }

View File

@ -26,7 +26,7 @@ class WeChat
/** /**
* 当前微信配置 * 当前微信配置
* @var Config * @var DataArray
*/ */
public $config; public $config;
@ -60,7 +60,7 @@ class WeChat
if (empty($options['appsecret'])) { if (empty($options['appsecret'])) {
throw new InvalidArgumentException("Missing Config -- [appsecret]"); throw new InvalidArgumentException("Missing Config -- [appsecret]");
} }
$this->config = new Config($options); $this->config = new DataArray($options);
} }
/** /**
@ -98,7 +98,6 @@ class WeChat
return Tools::delCache($this->config->get('appid') . '_accesstoken'); return Tools::delCache($this->config->get('appid') . '_accesstoken');
} }
/** /**
* 以GET获取接口数据并转为数组 * 以GET获取接口数据并转为数组
* @param string $url 接口地址 * @param string $url 接口地址

View File

@ -14,7 +14,7 @@
namespace WeChat; namespace WeChat;
use WeChat\Contracts\Config; use WeChat\Contracts\DataArray;
use WeChat\Contracts\Tools; use WeChat\Contracts\Tools;
use WeChat\Exceptions\InvalidArgumentException; use WeChat\Exceptions\InvalidArgumentException;
use WeChat\Exceptions\InvalidResponseException; use WeChat\Exceptions\InvalidResponseException;
@ -27,17 +27,18 @@ use WeChat\Exceptions\InvalidResponseException;
class Pay class Pay
{ {
/**
* 商户配置
* @var DataArray
*/
protected $config;
/** /**
* 当前请求数据 * 当前请求数据
* @var Config * @var DataArray
*/ */
protected $params; protected $params;
/**
* 商户配置
* @var Config
*/
protected $config;
/** /**
* WeChat constructor. * WeChat constructor.
@ -50,8 +51,8 @@ class Pay
throw new InvalidArgumentException("Missing Config -- [{$key}]", '0'); throw new InvalidArgumentException("Missing Config -- [{$key}]", '0');
} }
} }
$this->config = new Config($options); $this->config = new DataArray($options);
$this->params = new Config([ $this->params = new DataArray([
'appid' => $this->config->get('appid'), 'appid' => $this->config->get('appid'),
'mch_id' => $this->config->get('mch_id'), 'mch_id' => $this->config->get('mch_id'),
'nonce_str' => Tools::createNoncestr(), 'nonce_str' => Tools::createNoncestr(),
@ -85,7 +86,7 @@ class Pay
* @param string $out_trade_no 商户订单号 * @param string $out_trade_no 商户订单号
* @return array * @return array
*/ */
public function close($out_trade_no) public function closeOrder($out_trade_no)
{ {
$url = 'https://api.mch.weixin.qq.com/pay/closeorder'; $url = 'https://api.mch.weixin.qq.com/pay/closeorder';
return $this->callPostApi($url, ['out_trade_no' => $out_trade_no]); return $this->callPostApi($url, ['out_trade_no' => $out_trade_no]);
@ -178,13 +179,28 @@ class Pay
{ {
$data = Tools::xml2arr(file_get_contents('php://input')); $data = Tools::xml2arr(file_get_contents('php://input'));
if (!empty($data['sign'])) { if (!empty($data['sign'])) {
if (Tools::getPaySign($data, $this->config->get('mch_key')) === $data['sign']) { if ($this->getPaySign($data) === $data['sign']) {
return $data; return $data;
} }
} }
throw new InvalidResponseException('Invalid Notify.', '0'); throw new InvalidResponseException('Invalid Notify.', '0');
} }
/**
* 生成支付签名
* @param array $data
* @return string
*/
public function getPaySign(array $data)
{
ksort($data);
list($key, $str) = [$this->config->get('mch_key'), ''];
foreach ($data as $key => $value) {
$str .= "{$key}={$value}&";
}
return strtoupper(hash_hmac('SHA256', "{$str}key={$key}", $key));
}
/** /**
* 以Post请求接口 * 以Post请求接口
* @param string $url 请求 * @param string $url 请求
@ -206,7 +222,7 @@ class Pay
} }
$params = $this->params->merge($data); $params = $this->params->merge($data);
$params['sign_type'] = 'HMAC-SHA256'; $params['sign_type'] = 'HMAC-SHA256';
$params['sign'] = Tools::getPaySign($params, $this->config->get('mch_key')); $params['sign'] = $this->getPaySign($params);
return Tools::xml2arr(Tools::post($url, Tools::arr2xml($params), $option)); return Tools::xml2arr(Tools::post($url, Tools::arr2xml($params), $option));
} }
} }