Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda committed Dec 3, 2018
1 parent 994d23a commit 0182e7c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 59 deletions.
4 changes: 1 addition & 3 deletions src/Gateways/Alipay.php
Expand Up @@ -62,12 +62,10 @@ class Alipay implements GatewayApplicationInterface
* @author yansongda <me@yansongda.cn>
*
* @param Config $config
*
* @throws \Yansongda\Pay\Exceptions\InvalidArgumentException
*/
public function __construct(Config $config)
{
$this->gateway = Support::getInstance($config)->getBaseUri();
$this->gateway = Support::create($config)->getBaseUri();
$this->payload = [
'app_id' => $config->get('app_id'),
'method' => '',
Expand Down
87 changes: 31 additions & 56 deletions src/Gateways/Alipay/Support.php
Expand Up @@ -3,7 +3,6 @@
namespace Yansongda\Pay\Gateways\Alipay;

use Yansongda\Pay\Exceptions\GatewayException;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
use Yansongda\Pay\Exceptions\InvalidConfigException;
use Yansongda\Pay\Exceptions\InvalidSignException;
use Yansongda\Pay\Gateways\Alipay;
Expand Down Expand Up @@ -60,57 +59,40 @@ private function __construct(Config $config)
{
$this->baseUri = Alipay::URL[$config->get('mode', Alipay::MODE_NORMAL)];
$this->config = $config;

$this->setHttpOptions();
}

/**
* __get.
* create.
*
* @author yansongda <me@yansongda.cn>
*
* @param $key
* @param Config $config
*
* @return mixed|null|Config
* @return Support
*/
public function __get($key)
public static function create(Config $config)
{
return $this->getConfig($key);
}
if (php_sapi_name() === 'cli' || !(self::$instance instanceof self)) {
self::$instance = new self($config);
}

/**
* Get Base Uri.
*
* @author yansongda <me@yansongda.cn>
*
* @return string
*/
public function getBaseUri()
{
return $this->baseUri;
return self::$instance;
}

/**
* Get instance.
* __get.
*
* @author yansongda <me@yansongda.cn>
*
* @param Config|null $config
*
* @throws InvalidArgumentException
* @param $key
*
* @return self
* @return mixed|null|Config
*/
public static function getInstance($config = null): self
public function __get($key)
{
if ((!(self::$instance instanceof self)) && is_null($config)) {
throw new InvalidArgumentException('Must Initialize Support With Config Before Using');
}

if (!(self::$instance instanceof self)) {
self::$instance = new self($config);
}

return self::$instance;
return $this->getConfig($key);
}

/**
Expand All @@ -123,19 +105,18 @@ public static function getInstance($config = null): self
* @throws GatewayException
* @throws InvalidConfigException
* @throws InvalidSignException
* @throws InvalidArgumentException
*
* @return Collection
*/
public static function requestApi(array $data): Collection
{
Log::debug('Request To Alipay Api', [self::getInstance()->getBaseUri(), $data]);
Log::debug('Request To Alipay Api', [self::$instance->getBaseUri(), $data]);

$data = array_filter($data, function ($value) {
return ($value == '' || is_null($value)) ? false : true;
});

$result = mb_convert_encoding(self::getInstance()->post('', $data), 'utf-8', 'gb2312');
$result = mb_convert_encoding(self::$instance->post('', $data), 'utf-8', 'gb2312');
$result = json_decode($result, true);

Log::debug('Result Of Alipay Api', $result);
Expand Down Expand Up @@ -167,13 +148,12 @@ public static function requestApi(array $data): Collection
* @param array $params
*
* @throws InvalidConfigException
* @throws InvalidArgumentException
*
* @return string
*/
public static function generateSign(array $params): string
{
$privateKey = self::getInstance()->private_key;
$privateKey = self::$instance->private_key;

if (is_null($privateKey)) {
throw new InvalidConfigException('Missing Alipay Config -- [private_key]');
Expand Down Expand Up @@ -206,13 +186,12 @@ public static function generateSign(array $params): string
* @param string|null $sign
*
* @throws InvalidConfigException
* @throws InvalidArgumentException
*
* @return bool
*/
public static function verifySign(array $data, $sync = false, $sign = null): bool
{
$publicKey = self::getInstance()->ali_public_key;
$publicKey = self::$instance->ali_public_key;

if (is_null($publicKey)) {
throw new InvalidConfigException('Missing Alipay Config -- [ali_public_key]');
Expand Down Expand Up @@ -282,19 +261,20 @@ public static function encoding($data, $to = 'utf-8', $from = 'gb2312'): array
}

/**
* Initialize.
* Set Http options.
*
* @author yansongda <me@yansongda.cn>
*
* @param Config $config
*
* @throws InvalidArgumentException
*
* @return Support
* @return self
*/
public static function initialize(Config $config): self
protected function setHttpOptions(): self
{
return self::getInstance($config);
if ($this->config->has('http') && is_array($this->config->get('http'))) {
$this->config->forget('http.base_uri');
$this->httpOptions = $this->config->get('http');
}

return $this;
}

/**
Expand All @@ -321,19 +301,14 @@ public function getConfig($key = null, $default = null)
}

/**
* Set Http options.
* Get Base Uri.
*
* @author yansongda <me@yansongda.cn>
*
* @return self
* @return string
*/
protected function setHttpOptions(): self
public function getBaseUri()
{
if ($this->config->has('http') && is_array($this->config->get('http'))) {
$this->config->forget('http.base_uri');
$this->httpOptions = $this->config->get('http');
}

return $this;
return $this->baseUri;
}
}

0 comments on commit 0182e7c

Please sign in to comment.