<?php
namespace Payment\Common\Weixin; use GuzzleHttp\Client; use Payment\Common\PayException; use Payment\Common\Weixin\Data\WxBaseData; use Payment\Common\WxConfig; use Payment\Utils\Curl; use Payment\Utils\DataParser; use Payment\Utils\StrUtil; class WechatHelper extends WxBaseData { const SANDBOX_URL = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey'; public function getSandboxSignKey() { $this->setSign(); $xml = DataParser::toXml($this->getData()); $url = self::SANDBOX_URL; $client = new Client([ 'timeout' => '10.0' ]); $options = [ 'body' => $xml, 'http_errors' => false ]; $response = $client->request('POST', self::SANDBOX_URL, $options); if ($response->getStatusCode() != '200') { throw new PayException('网络发生错误，请稍后再试curl返回码：' . $response->getReasonPhrase()); } $retData = DataParser::toArray($response->getBody()->getContents()); if (strtoupper($retData['return_code']) != 'SUCCESS') { throw new PayException('微信返回错误提示:' . $retData['retmsg']); } return $retData['sandbox_signkey']; } protected function buildData() { $this->retData = [ 'mch_id' => $this->mchId, 'nonce_str' => StrUtil::getNonceStr(), ]; } protected function checkDataParam() { } } 