This commit is contained in:
yansongda 2017-08-11 12:17:38 +08:00
parent 3ab13fa19f
commit 816fa772b9
2 changed files with 137 additions and 6 deletions

View File

@ -3,6 +3,7 @@
namespace Yansongda\Pay\Gateways\Alipay;
use Yansongda\Pay\Support\Config;
use Yansongda\Pay\Trait\HasHttpRequest;
use Yansongda\Pay\Contracts\GatewayInterface;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
@ -11,6 +12,8 @@ use Yansongda\Pay\Exceptions\InvalidArgumentException;
*/
abstract class Alipay implements GatewayInterface
{
use HasHttpRequest;
/**
* 支付宝支付网关
* @var string
@ -37,7 +40,7 @@ abstract class Alipay implements GatewayInterface
*/
public function __construct($config)
{
$this->user_config = new Config(array_merge($this->user_config, $config));
$this->user_config = new Config($config);
$this->config = [
'app_id' => $this->user_config->get('app_id', ''),
@ -52,6 +55,10 @@ abstract class Alipay implements GatewayInterface
'sign' => '',
'biz_content' => '',
];
if ($this->config['app_id'] === '') {
throw new InvalidArgumentException("Missing Config -- [app_id]");
}
}
@ -71,7 +78,8 @@ abstract class Alipay implements GatewayInterface
$this->config['biz_content'] = json_encode($config_biz, JSON_UNESCAPED_UNICODE);
$this->config['sign'] = $this->getSign();
return $this->buildPayHtml();
return $this->post($this->gateway, $this->config);
//return $this->buildPayHtml();
}
/**
@ -86,8 +94,7 @@ abstract class Alipay implements GatewayInterface
$this->config['biz_content'] = json_encode($config_biz, JSON_UNESCAPED_UNICODE);
$this->config['sign'] = $this->getSign();
// TODO
return true;
return $this->post($this->gateway, $this->config);
}
/**
@ -102,8 +109,7 @@ abstract class Alipay implements GatewayInterface
$this->config['biz_content'] = json_encode($config_biz, JSON_UNESCAPED_UNICODE);
$this->config['sign'] = $this->getSign();
// TODO
return true;
return $this->post($this->gateway, $this->config);
}
/**
@ -161,6 +167,10 @@ abstract class Alipay implements GatewayInterface
*/
protected function getSign()
{
if (is_null($this->user_config->get('private_key'))) {
throw new InvalidArgumentException("Missing Config -- [private_key]");
}
$res = "-----BEGIN RSA PRIVATE KEY-----\n" .
wordwrap($this->user_config->get('private_key'), 64, "\n", true) .
"\n-----END RSA PRIVATE KEY-----";

View File

@ -0,0 +1,121 @@
<?php
/*
* This file is part of the overtrue/easy-sms.
*
* (c) overtrue <i@overtrue.me>
*
* Modified By yansongda <me@yansongda.cn>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Yansongda\Pay\Trait;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
/**
* trait HttpClient.
*/
trait HasHttpRequest
{
/**
* Make a get request.
*
* @param string $endpoint
* @param array $query
* @param array $headers
*
* @return array
*/
protected function get($endpoint, $query = [], $headers = [])
{
return $this->request('get', $endpoint, [
'headers' => $headers,
'query' => $query,
]);
}
/**
* Make a post request.
*
* @param string $endpoint
* @param array $params
* @param array $headers
*
* @return array
*/
protected function post($endpoint, $params = [], $headers = [])
{
return $this->request('post', $endpoint, [
'headers' => $headers,
'form_params' => $params,
]);
}
/**
* Make a http request.
*
* @param string $method
* @param string $endpoint
* @param array $options http://docs.guzzlephp.org/en/latest/request-options.html
*
* @return array
*/
protected function request($method, $endpoint, $options = [])
{
return $this->unwrapResponse($this->getHttpClient($this->getBaseOptions())->{$method}($endpoint, $options));
}
/**
* Return base Guzzle options.
*
* @return array
*/
protected function getBaseOptions()
{
$options = [
'base_uri' => method_exists($this, 'getBaseUri') ? $this->getBaseUri() : '',
'timeout' => property_exists($this, 'timeout') ? $this->timeout : 5.0,
];
return $options;
}
/**
* Return http client.
*
* @param array $options
*
* @return \GuzzleHttp\Client
*
* @codeCoverageIgnore
*/
protected function getHttpClient(array $options = [])
{
return new Client($options);
}
/**
* Convert response contents to json.
*
* @param \Psr\Http\Message\ResponseInterface $response
*
* @return array
*/
protected function unwrapResponse(ResponseInterface $response)
{
$contentType = $response->getHeaderLine('Content-Type');
$contents = $response->getBody()->getContents();
if (false !== stripos($contentType, 'json') || stripos($contentType, 'javascript')) {
return json_decode($contents, true);
} elseif (false !== stripos($contentType, 'xml')) {
return json_decode(json_encode(simplexml_load_string($contents)), true);
}
return $contents;
}
}