Skip to content

Commit

Permalink
采用guzzle 请求招行
Browse files Browse the repository at this point in the history
移除自己的curl
  • Loading branch information
helei112g committed Sep 13, 2017
1 parent fbbcd3e commit a0e72d7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 284 deletions.
4 changes: 3 additions & 1 deletion src/Charge/Cmb/CmbCharge.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php

namespace Payment\Charge\Cmb;

use Payment\Common\Cmb\CmbBaseStrategy;
use Payment\Common\Cmb\Data\Charge\ChargeData;

/**
* 一网通支付API 接口
*
* Created by PhpStorm.
* User: helei
* Date: 2017/4/27
* Time: 下午12:36
*
* @link http://121.15.180.72/OpenAPI2/DOC/ToDevelop5.aspx
*/
class CmbCharge extends CmbBaseStrategy
{
Expand Down
52 changes: 31 additions & 21 deletions src/Common/Cmb/CmbBaseStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Payment\Common\Cmb;

use GuzzleHttp\Client;
use Payment\Common\BaseData;
use Payment\Common\BaseStrategy;
use Payment\Common\CmbConfig;
use Payment\Common\PayException;
use Payment\Config;
use Payment\Utils\Curl;

/**
* Created by PhpStorm.
Expand Down Expand Up @@ -93,36 +93,35 @@ protected function retData(array $ret)
*/
protected function sendReq($json)
{
$responseTxt = $this->curlPost($json, $this->config->getewayUrl);
if ($responseTxt['error']) {
throw new PayException('网络发生错误,请稍后再试curl返回码:' . $responseTxt['message']);
$client = new Client([
'timeout' => '10.0'
]);
// @note: 微信部分接口并不需要证书支持。这里为了统一,全部携带证书进行请求
$options = [
'body' => $json,
'http_errors' => false
];
$response = $client->request('POST', $this->config->getewayUrl, $options);
if ($response->getStatusCode() != '200') {
throw new PayException('网络发生错误,请稍后再试curl返回码:' . $response->getReasonPhrase());
}

$body = json_decode($responseTxt['body'], true);
$rspData = $body['rspData'];
$body = $response->getBody()->getContents();
$data = json_decode($body, true);
// TODO 检查返回的数据是否被篡改
$flag = $this->verifySign($data);
if (!$flag) {
throw new PayException('微信返回数据被篡改。请检查网络是否安全!');
}

$rspData = $data['rspData'];
if ($rspData['rspCode'] !== CmbConfig::SUCC_TAG) {
throw new PayException('招商返回错误提示:' . $rspData['rspMsg']);
}

return $rspData;
}

/**
* 父类仅提供基础的post请求,子类可根据需要进行重写
* @param string $json
* @param string $url
* @return array
* @author helei
*/
protected function curlPost($json, $url)
{
$curl = new Curl();
return $curl->set([
'CURLOPT_HEADER' => 0,
])->post($json)->submit($url);
}

/**
* 返回统一的交易状态 做一些转化,方便处理
* @param $status
Expand All @@ -143,4 +142,15 @@ protected function getTradeStatus($status)
return Config::TRADE_STATUS_FAILD;// 以上状态全部设置为失败
}
}

/**
* 检查返回的数据是否正确
* @param array $retData
* @return bool
*/
protected function verifySign(array $retData)
{
// todo
return true;
}
}
7 changes: 0 additions & 7 deletions src/Helper/Cmb/PubKeyHelper.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: helei
* Date: 2017/4/29
* Time: 上午9:50
*/

namespace Payment\Helper\Cmb;

use Payment\Common\Cmb\CmbBaseStrategy;
Expand Down
255 changes: 0 additions & 255 deletions src/Utils/Curl.php

This file was deleted.

0 comments on commit a0e72d7

Please sign in to comment.