add clear
This commit is contained in:
+42
-50
@@ -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;
|
||||
@@ -17,23 +18,23 @@ use wchat\wx\SmallProgram;
|
||||
class Cash_Bonus extends SmallProgram
|
||||
{
|
||||
|
||||
public $_cash = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
|
||||
public string $_cash = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
|
||||
|
||||
private array $_errors = [
|
||||
'66228701' => '红包个数超出限制',
|
||||
'66228705' => '总金额超出限制',
|
||||
'66228706' => '总金额不足以按最小金额领取每个红包',
|
||||
'66228707' => '商户签名校验失败',
|
||||
'66228708' => '重入??',
|
||||
'66228709' => 'openid转换uin失败',
|
||||
'66228711' => '商户订单中的商户号有误',
|
||||
'66228712' => '商户订单中的日期超过范围',
|
||||
'66228713' => '余额不足',
|
||||
'66228715' => '用户未关注公众号,发送AIO消息失败',
|
||||
'66229716' => '用户禁用公众号,发AIO消息失败'
|
||||
];
|
||||
private array $_errors = [
|
||||
'66228701' => '红包个数超出限制',
|
||||
'66228705' => '总金额超出限制',
|
||||
'66228706' => '总金额不足以按最小金额领取每个红包',
|
||||
'66228707' => '商户签名校验失败',
|
||||
'66228708' => '重入??',
|
||||
'66228709' => 'openid转换uin失败',
|
||||
'66228711' => '商户订单中的商户号有误',
|
||||
'66228712' => '商户订单中的日期超过范围',
|
||||
'66228713' => '余额不足',
|
||||
'66228715' => '用户未关注公众号,发送AIO消息失败',
|
||||
'66229716' => '用户禁用公众号,发AIO消息失败'
|
||||
];
|
||||
|
||||
private array $_requestParams = [];
|
||||
private array $_requestParams = [];
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
@@ -83,39 +84,30 @@ class Cash_Bonus extends SmallProgram
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function mch_send($mch_billno, $openId, $price)
|
||||
public function mch_send($mch_billno, $openId, $price): Result
|
||||
{
|
||||
$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['retcode'] != 0) {
|
||||
if (empty($json['retmsg']) && isset($this->_errors[$json['retcode']])) {
|
||||
$json['retmsg'] = $this->_errors[$json['retcode']];
|
||||
}
|
||||
$array['code'] = $json['retcode'];
|
||||
$array['message'] = $json['retmsg'];
|
||||
} else {
|
||||
$array['code'] = 0;
|
||||
$array['message'] = $json['retmsg'];
|
||||
$array['data'] = ['listid' => $json['listid']];
|
||||
}
|
||||
$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, message: $json['return_msg'] ?? $json['retmsg']);
|
||||
} else if ($json['retcode'] != 0) {
|
||||
if (empty($json['retmsg']) && isset($this->_errors[$json['retcode']])) {
|
||||
$json['retmsg'] = $this->_errors[$json['retcode']];
|
||||
}
|
||||
return new Result(code: $json['retcode'], message: $json['retmsg']);
|
||||
} else {
|
||||
return new Result(code: 0, data: $json);
|
||||
}
|
||||
return $response->getData('listid');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,7 +116,7 @@ class Cash_Bonus extends SmallProgram
|
||||
* @param $price
|
||||
* @return array
|
||||
*/
|
||||
private function orderConfig($mch_billno, $openId, $price)
|
||||
private function orderConfig($mch_billno, $openId, $price): array
|
||||
{
|
||||
$requestParam['charset'] = 1;
|
||||
$requestParam['nonce_str'] = Help::random(32);
|
||||
@@ -138,7 +130,7 @@ class Cash_Bonus extends SmallProgram
|
||||
$requestParam['total_num'] = 1;
|
||||
$requestParam['notify_url'] = $this->getConfig()->getNotifyUrl();
|
||||
|
||||
if (!empty($this->_requestParams) && is_array($this->_requestParams)) {
|
||||
if (!empty($this->_requestParams)) {
|
||||
$requestParam = array_merge($requestParam, $this->_requestParams);
|
||||
}
|
||||
|
||||
@@ -152,7 +144,7 @@ class Cash_Bonus extends SmallProgram
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function mchOrderNo()
|
||||
public function mchOrderNo(): string
|
||||
{
|
||||
return implode([
|
||||
$this->getConfig()->getMchId(),
|
||||
@@ -168,7 +160,7 @@ class Cash_Bonus extends SmallProgram
|
||||
* @param array $requestParams
|
||||
* @return bool
|
||||
*/
|
||||
public function validator(array $requestParams)
|
||||
public function validator(array $requestParams): bool
|
||||
{
|
||||
$notifySign = $requestParams['sign'];
|
||||
unset($requestParams['sign']);
|
||||
|
||||
Reference in New Issue
Block a user