Skip to content

Commit

Permalink
merge to dev: 完善微信支付网关与文档
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda committed Aug 17, 2017
2 parents e8b1a13 + e8765c9 commit 2decb4d
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 52 deletions.
8 changes: 4 additions & 4 deletions src/Contracts/GatewayInterface.php
Expand Up @@ -15,7 +15,7 @@ interface GatewayInterface
*
* @param array $config_biz [description]
*
* @return [type] [description]
* @return mixed [description]
*/
public function pay(array $config_biz);

Expand All @@ -28,7 +28,7 @@ public function pay(array $config_biz);
*
* @param array $config_biz [description]
*
* @return [type] [description]
* @return boolean [description]
*/
public function refund(array $config_biz);

Expand All @@ -41,7 +41,7 @@ public function refund(array $config_biz);
*
* @param array $config_biz [description]
*
* @return [type] [description]
* @return boolean [description]
*/
public function close(array $config_biz);

Expand All @@ -56,7 +56,7 @@ public function close(array $config_biz);
* @param string $sign 签名字符串-支付宝服务器发送过来的原始串
* @param bool $sync 是否同步验证
*
* @return [type] [description]
* @return boolean [description]
*/
public function verify($data, $sign = null, $sync = false);
}
4 changes: 2 additions & 2 deletions src/Exceptions/GatewayException.php
Expand Up @@ -21,8 +21,8 @@ class GatewayException extends Exception
*
* @version 2017-07-28
*
* @param [type] $message [description]
* @param [type] $code [description]
* @param string $message [description]
* @param string|int $code [description]
*/
public function __construct($message, $code, $raw = [])
{
Expand Down
49 changes: 24 additions & 25 deletions src/Gateways/Alipay/Alipay.php
Expand Up @@ -78,7 +78,7 @@ public function __construct(array $config)
*
* @param array $config_biz [description]
*
* @return [type] [description]
* @return string [description]
*/
public function pay(array $config_biz = [])
{
Expand All @@ -100,14 +100,11 @@ public function pay(array $config_biz = [])
*
* @param array $config_biz [description]
*
* @return [type] [description]
* @return boolean [description]
*/
public function refund(array $config_biz = [])
{
$this->config['biz_content'] = json_encode($config_biz, JSON_UNESCAPED_UNICODE);
$this->config['method'] = 'alipay.trade.refund';

return $this->getResult('alipay_trade_refund_response');
return $this->getResult($config_biz, 'alipay.trade.refund');
}

/**
Expand All @@ -119,14 +116,11 @@ public function refund(array $config_biz = [])
*
* @param array $config_biz [description]
*
* @return [type] [description]
* @return boolean [description]
*/
public function close(array $config_biz = [])
{
$this->config['biz_content'] = json_encode($config_biz, JSON_UNESCAPED_UNICODE);
$this->config['method'] = 'alipay.trade.close';

return $this->getResult('alipay_trade_close_response');
return $this->getResult($config_biz, 'alipay.trade.close');
}

/**
Expand All @@ -140,7 +134,7 @@ public function close(array $config_biz = [])
* @param string $sign 签名字符串-支付宝服务器发送过来的原始串
* @param bool $sync 是否同步验证
*
* @return [type] [description]
* @return boolean [description]
*/
public function verify($data, $sign = null, $sync = false)
{
Expand Down Expand Up @@ -170,7 +164,7 @@ public function verify($data, $sign = null, $sync = false)
*
* @version 2017-08-10
*
* @return [type] [description]
* @return string [description]
*/
abstract protected function getPayMethod();

Expand All @@ -181,7 +175,7 @@ abstract protected function getPayMethod();
*
* @version 2017-08-10
*
* @return [type] [description]
* @return string [description]
*/
abstract protected function getPayProductCode();

Expand All @@ -192,17 +186,17 @@ abstract protected function getPayProductCode();
*
* @version 2017-08-11
*
* @return [type] [description]
* @return string [description]
*/
protected function buildPayHtml()
{
$sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".$this->gateway."?charset=utf-8' method='POST'>";
$sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='" . $this->gateway . "?charset=utf-8' method='POST'>";
while (list ($key, $val) = each ($this->config)) {
$val = str_replace("'","&apos;",$val);
$sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>";
$sHtml .= "<input type='hidden' name='" . $key . "' value='" . $val . "'/>";
}
$sHtml = $sHtml."<input type='submit' value='ok' style='display:none;''></form>";
$sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>";
$sHtml .= "<input type='submit' value='ok' style='display:none;''></form>";
$sHtml .= "<script>document.forms['alipaysubmit'].submit();</script>";

return $sHtml;
}
Expand All @@ -214,17 +208,22 @@ protected function buildPayHtml()
*
* @version 2017-08-12
*
* @param [type] $method [description]
* @param array $config_biz [description]
* @param string $method [description]
*
* @return [type] [description]
* @return boolean [description]
*/
protected function getResult($method)
protected function getResult($config_biz, $method)
{
$this->config['biz_content'] = json_encode($config_biz, JSON_UNESCAPED_UNICODE);
$this->config['method'] = $method;
$this->config['sign'] = $this->getSign();

$method = str_replace('.', '_', $method) . '_response';

$data = json_decode($this->post($this->gateway, $this->config), true);

if (! isset($data[$method]['code']) || $data[$method]['code'] !== '10000') {
if (!isset($data[$method]['code']) || $data[$method]['code'] !== '10000') {
throw new GatewayException(
'get result error:' . $data[$method]['msg'] . ' - ' . $data[$method]['sub_msg'],
$data[$method]['code'],
Expand All @@ -241,7 +240,7 @@ protected function getResult($method)
*
* @version 2017-08-10
*
* @return [type] [description]
* @return string [description]
*/
protected function getSign()
{
Expand All @@ -268,7 +267,7 @@ protected function getSign()
* @param array $toBeSigned [description]
* @param boolean $verify 是否异步同时验证签名
*
* @return [type] [description]
* @return string [description]
*/
protected function getSignContent(array $toBeSigned, $verify = false)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Gateways/Wechat/WapGateway.php
Expand Up @@ -38,7 +38,7 @@ public function pay(array $config_biz = [])

$data = $this->preOrder();

return is_null($this->user_config->get('return_url')) ? $data['MWEB_URL'] : $data['MWEB_URL'].
'&redirect_url='. urlencode($this->config['return_url']);
return is_null($this->user_config->get('return_url')) ? $data['MWEB_URL'] : $data['MWEB_URL'] .
'&redirect_url=' . urlencode($this->config['return_url']);
}
}
22 changes: 11 additions & 11 deletions src/Gateways/Wechat/Wechat.php
Expand Up @@ -66,7 +66,7 @@ public function __construct(array $config)
*
* @version 2017-08-15
*
* @param array $cofnig_biz [description]
* @param array $config_biz [description]
*
* @return [type] [description]
*/
Expand Down Expand Up @@ -147,7 +147,7 @@ protected function preOrder()

if (!isset($data['return_code']) || $data['return_code'] !== 'SUCCESS' || $data['result_code'] !== 'SUCCESS') {
$error = 'preOrder error:' . $data['return_msg'];
$error .= isset($data['err_code_des']) ? ' - ' . $data['err_code_des'] : '';
$error .= isset($data['err_code_des']) ? ' - ' . $data['err_code_des'] : '';

throw new GatewayException(
$error,
Expand Down Expand Up @@ -206,7 +206,7 @@ protected function getSignContent($data)

foreach ($data as $k => $v)
{
if($k != "sign" && $v != "" && !is_array($v)){
if ($k != "sign" && $v != "" && !is_array($v)) {
$buff .= $k . "=" . $v . "&";
}
}
Expand Down Expand Up @@ -248,19 +248,19 @@ protected function createNonceStr($length = 16) {
*/
protected function toXml($data)
{
if(!is_array($data) || count($data) <= 0){
if (!is_array($data) || count($data) <= 0) {
throw new InvalidArgumentException("convert to xml error!invalid array!");
}

$xml = "<xml>";
foreach ($data as $key => $val){
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
foreach ($data as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml.="</xml>";
$xml .= "</xml>";

return $xml;
}
Expand All @@ -278,7 +278,7 @@ protected function toXml($data)
*/
protected function fromXml($xml)
{
if(!$xml){
if (!$xml) {
throw new InvalidArgumentException("convert to array error !invalid xml");
}

Expand Down
8 changes: 4 additions & 4 deletions src/Pay.php
Expand Up @@ -80,7 +80,7 @@ public function driver($driver)
*/
public function gateway($gateway = 'web')
{
if (! isset($this->drivers)) {
if (!isset($this->drivers)) {
throw new InvalidArgumentException("Driver is not defined.");
}

Expand All @@ -96,13 +96,13 @@ public function gateway($gateway = 'web')
*
* @version 2017-08-10
*
* @param [type] $gateway [description]
* @param string $gateway [description]
*
* @return [type] [description]
*/
private function createGateway($gateway)
{
if (! file_exists(__DIR__ . '/Gateways/' . ucfirst($this->drivers) . '/' . ucfirst($gateway) . 'Gateway.php')) {
if (!file_exists(__DIR__ . '/Gateways/' . ucfirst($this->drivers) . '/' . ucfirst($gateway) . 'Gateway.php')) {
throw new InvalidArgumentException("Gateway [$gateway] is not supported.");
}

Expand All @@ -118,7 +118,7 @@ private function createGateway($gateway)
*
* @version 2017-07-30
*
* @param [type] $gateway [description]
* @param string $gateway [description]
*
* @return [type] [description]
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Support/Config.php
Expand Up @@ -32,8 +32,8 @@ public function __construct(array $config = [])
*
* @version 2017-07-28
*
* @param [type] $key [description]
* @param [type] $default [description]
* @param string $key [description]
* @param string $default [description]
*
* @return [type] [description]
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/HasHttpRequest.php
Expand Up @@ -45,7 +45,7 @@ protected function get($endpoint, $query = [], $headers = [])
* @param string $body
* @param array $headers
*
* @return array
* @return string
*/
protected function post($endpoint, $params = [], $body = null, $headers = [])
{
Expand All @@ -54,7 +54,7 @@ protected function post($endpoint, $params = [], $body = null, $headers = [])
'form_params' => $params,
];

if (! is_null($body)) {
if (!is_null($body)) {
$options = [
'headers' => $headers,
'body' => $body,
Expand Down

0 comments on commit 2decb4d

Please sign in to comment.