add clear

This commit is contained in:
2022-09-09 16:42:55 +08:00
parent ad76b45b45
commit 7273fe1ce5
63 changed files with 2625 additions and 5287 deletions
+22 -31
View File
@@ -5,6 +5,7 @@ namespace wchat\qq\pay;
use Exception;
use Kiri\Client;
use wchat\common\Help;
use wchat\common\HttpClient;
use wchat\common\Result;
@@ -16,7 +17,7 @@ use wchat\qq\SmallProgram;
*/
class Enterprise_payment extends SmallProgram
{
public $_cash = '/cgi-bin/epay/qpay_epay_b2c.cgi';
public string $_cash = '/cgi-bin/epay/qpay_epay_b2c.cgi';
private $_errors = [
'SYSTEMERROR' => '系统错误',
@@ -71,34 +72,24 @@ class Enterprise_payment extends SmallProgram
*/
public function mch_send($mch_billno, $openId, $price)
{
$client = HttpClient::NewRequest();
$client->setIsSSL(true);
$client->setSslCertFile($this->getConfig()->getSslCert());
$client->setSslKeyFile($this->getConfig()->getSslKey());
$client->setCa($this->getConfig()->getSslCa());
$client->setUseSwoole($this->getConfig()->isUsrSwoole());
$client->setHost('api.qpay.qq.com');
$client->setCallback(function ($response, $requestParam) {
$json = json_decode($response, true);
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
$array['code'] = 500;
$array['message'] = $json['return_msg'] ?? $json['retmsg'];
} else if ($json['result_code'] != 'SUCCESS') {
$array['code'] = 500;
$array['message'] = $this->_errors[$json['err_code']] ?? $json['err_code_desc'];
} else {
$array['code'] = 0;
$array['message'] = 'system success';
$array['data'] = $json;
}
$array['data']['request'] = $requestParam;
return new Result($array);
});
$response = $client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
if (!$response->isResultsOK()) {
throw new Exception($response->getMessage());
$client = new Client('api.qpay.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->withSslCertFile($this->getConfig()->getSslCert());
$client->withSslKeyFile($this->getConfig()->getSslKey());
$client->withCa($this->getConfig()->getSslCa());
$client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: 'network error.');
}
$json = json_decode($client->getBody(), true);
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
return new Result(code: 500, data: $json['return_msg'] ?? $json['retmsg']);
} else if ($json['result_code'] != 'SUCCESS') {
return new Result(code: 500, data: $this->_errors[$json['err_code']] ?? $json['err_code_desc']);
} else {
return new Result(code: 0, data: $json);
}
return $response->getData();
}
/**
@@ -107,7 +98,7 @@ class Enterprise_payment extends SmallProgram
* @param $price
* @return array
*/
private function orderConfig($mch_billno, $openId, $price)
private function orderConfig($mch_billno, $openId, $price): array
{
$requestParam['input_charset'] = 'UTF-8';
$requestParam['nonce_str'] = Help::random(32);
@@ -135,7 +126,7 @@ class Enterprise_payment extends SmallProgram
* @return string
* @throws Exception
*/
public function mchOrderNo()
public function mchOrderNo(): string
{
return implode([
$this->getConfig()->getMchId(),
@@ -151,7 +142,7 @@ class Enterprise_payment extends SmallProgram
* @param array $requestParams
* @return bool
*/
public function validator(array $requestParams)
public function validator(array $requestParams): bool
{
$notifySign = $requestParams['sign'];
unset($requestParams['sign']);