diff --git a/src/Charge/Cmb/CmbCharge.php b/src/Charge/Cmb/CmbCharge.php index c2c06358..2317090f 100644 --- a/src/Charge/Cmb/CmbCharge.php +++ b/src/Charge/Cmb/CmbCharge.php @@ -1,5 +1,4 @@ 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']); } @@ -108,21 +122,6 @@ protected function sendReq($json) 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 @@ -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; + } } \ No newline at end of file diff --git a/src/Helper/Cmb/PubKeyHelper.php b/src/Helper/Cmb/PubKeyHelper.php index 25a7b174..52811040 100644 --- a/src/Helper/Cmb/PubKeyHelper.php +++ b/src/Helper/Cmb/PubKeyHelper.php @@ -1,11 +1,4 @@ retry = 0; - $this->default = array( - 'CURLOPT_TIMEOUT' => 30, - 'CURLOPT_ENCODING' => '', - 'CURLOPT_IPRESOLVE' => 1, - 'CURLOPT_RETURNTRANSFER' => true, - 'CURLOPT_SSL_VERIFYPEER' => false, - 'CURLOPT_CONNECTTIMEOUT' => 10, - 'CURLOPT_HEADER' => 0 - ); - } - - /** - * 静态实例化 - * @return Curl - */ - public static function init() - { - if (static::$instance === null) { - static::$instance = new static; - } - return static::$instance; - } - - /** - * 提交GET请求 - * @param string $url - * @return array - */ - public function get($url) - { - return $this->set('CURLOPT_URL', $url)->exec(); - } - - /** - * 设置POST信息 - * @param array|string $data - * @param string $value - * @return $this - */ - public function post($data, $value = '') - { - if (is_array($data)) { - foreach ($data as $key => $value) { - $this->post[$key] = $value; - } - } elseif ($value) { - $this->post[$data] = $value; - } else { - $this->post = $data; - } - return $this; - } - - /** - * 设置文件上传 - * @param string $field - * @param string $path - * @param string $type - * @param string $name - * @return $this - */ - public function upload($field, $path, $type, $name) - { - $name = basename($name); - if (class_exists('CURLFile')) { - $this->set('CURLOPT_SAFE_UPLOAD', true); - $file = curl_file_create($path, $type, $name); - } else { - $file = "@{$path};type={$type};filename={$name}"; - } - return $this->post($field, $file); - } - - /** - * 提交POST请求 - * @param string $url - * @return array - */ - public function submit($url) - { - if (! $this->post) { - return array( - 'error' => 1, - 'message' => '未设置POST信息' - ); - } - return $this->set('CURLOPT_URL', $url)->exec(); - } - - /** - * 设置下载地址 - * @param string $url - * @return $this - */ - public function download($url) - { - $this->download = true; - return $this->set('CURLOPT_URL', $url); - } - - /** - * 下载保存文件 - * @param string $path - * @return array - */ - public function save($path) - { - if (! $this->download) { - return array( - 'error' => 1, - 'message' => '未设置下载地址' - ); - } - - $result = $this->exec(); - if ($result['error'] === 0) { - $fp = @fopen($path, 'w'); - fwrite($fp, $result['body']); - fclose($fp); - } - return $result; - } - - /** - * 配置Curl操作 - * @param array|string $item - * @param string $value - * @return $this - */ - public function set($item, $value = '') - { - if (is_array($item)) { - foreach ($item as $key => &$value) { - $this->option[$key] = $value; - } - } else { - $this->option[$item] = $value; - } - return $this; - } - - /** - * 出错自动重试 - * @param int $times - * @return $this - */ - public function retry($times = 0) - { - $this->retry = $times; - return $this; - } - - /** - * 执行Curl操作 - * @param int $retry - * @return array - */ - private function exec($retry = 0) - { - // 初始化句柄 - $ch = curl_init(); - - // 配置选项 - $options = array_merge($this->default, $this->option); - foreach ($options as $key => $val) { - if (is_string($key)) { - $key = constant(strtoupper($key)); - } - curl_setopt($ch, $key, $val); - } - - // POST选项 - if ($this->post) { - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postFieldsBuild($this->post)); - } - - // 运行句柄 - $body = curl_exec($ch); - $info = curl_getinfo($ch); - - // 检查错误 - $errno = curl_errno($ch); - if ($errno === 0 && $info['http_code'] >= 400) { - $errno = $info['http_code']; - } - - // 注销句柄 - curl_close($ch); - - // 自动重试 - if ($errno && $retry < $this->retry) { - $this->exec($retry + 1); - } - - // 注销配置 - $this->post = null; - $this->retry = null; - $this->option = null; - $this->download = null; - - // 返回结果 - return array( - 'error' => $errno ? 1 : 0, - 'message' => $errno, - 'body' => $body, - 'info' => $info - ); - } - - /** - * 一维化POST信息 - * @param array $input - * @param string $pre - * @return array - */ - private function postFieldsBuild($input, $pre = null) - { - if (is_array($input)) { - $output = array(); - foreach ($input as $key => $value) { - $index = is_null($pre) ? $key : "{$pre}[{$key}]"; - if (is_array($value)) { - $output = array_merge($output, $this->postFieldsBuild($value, $index)); - } else { - $output[$index] = $value; - } - } - return $output; - } - return $input; - } -}