This commit is contained in:
xl
2023-11-14 01:08:06 +08:00
parent 0d8ddfef54
commit a6a2a0f472
11 changed files with 691 additions and 762 deletions
+20
View File
@@ -14,6 +14,11 @@ class AppConfig
public string $appId = ''; public string $appId = '';
/**
* @var string
*/
private string $host = '';
/** /**
* @var string * @var string
*/ */
@@ -217,5 +222,20 @@ class AppConfig
$this->signType = $signType; $this->signType = $signType;
} }
/**
* @return string
*/
public function getHost(): string
{
return $this->host;
}
/**
* @param string $host
*/
public function setHost(string $host): void
{
$this->host = $host;
}
} }
+13 -7
View File
@@ -22,6 +22,9 @@ abstract class Multiprogramming implements Progaram
protected string $errorMsg = ''; protected string $errorMsg = '';
protected string $host = 'api.weixin.qq.com';
/** /**
* @var AppConfig * @var AppConfig
*/ */
@@ -136,11 +139,12 @@ abstract class Multiprogramming implements Progaram
/** /**
* @param string $requestUrl * @param string $requestUrl
* @param mixed $body * @param mixed $body
* @param string $contentType
* @return Result * @return Result
*/ */
protected function post(string $requestUrl, mixed $body): Result protected function post(string $requestUrl, mixed $body, string $contentType = 'application/application'): Result
{ {
return $this->request('post', $requestUrl, $body); return $this->request('post', $requestUrl, $body, $contentType);
} }
@@ -149,9 +153,9 @@ abstract class Multiprogramming implements Progaram
* @param mixed $body * @param mixed $body
* @return Result * @return Result
*/ */
protected function get(string $requestUrl, mixed $body): Result protected function get(string $requestUrl, mixed $body, string $contentType = 'application/application'): Result
{ {
return $this->request('get', $requestUrl, $body); return $this->request('get', $requestUrl, $body, $contentType);
} }
@@ -170,22 +174,24 @@ abstract class Multiprogramming implements Progaram
* @param string $method * @param string $method
* @param string $requestUrl * @param string $requestUrl
* @param $body * @param $body
* @param string $contentType
* @return Result * @return Result
*/ */
private function request(string $method, string $requestUrl, $body): Result private function request(string $method, string $requestUrl, $body, string $contentType = 'application/application'): Result
{ {
$client = new Client('api.weixin.qq.com', 443, true); $client = new Client($this->host, 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$proxyHost = $this->getConfig()->getProxyHost(); $proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort(); $proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) { if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort); $client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
} }
if ($method == 'post') { if ($method == 'post') {
$client->withHeader(['Content-Type' => $contentType]);
$client->post($requestUrl, $body); $client->post($requestUrl, $body);
} else if ($method == 'upload') { } else if ($method == 'upload') {
$client->upload($requestUrl, $body); $client->upload($requestUrl, $body);
} else { } else {
$client->withHeader(['Content-Type' => $contentType]);
$client->get($requestUrl, $body); $client->get($requestUrl, $body);
} }
$client->close(); $client->close();
+12 -24
View File
@@ -14,30 +14,18 @@ use wchat\common\Result;
class Account extends SmallProgram class Account extends SmallProgram
{ {
/** /**
* @param string $code * @param string $code
* @return Result * @return Result
*/ */
public function login(string $code): Result public function login(string $code): Result
{ {
$param['appid'] = $this->config->getAppid(); $param['appid'] = $this->payConfig->appId;
$param['secret'] = $this->config->getAppsecret(); $param['secret'] = $this->payConfig->appSecret;
$param['js_code'] = $code; $param['js_code'] = $code;
$param['grant_type'] = 'authorization_code'; $param['grant_type'] = 'authorization_code';
$client = new Client('api.q.qq.com', 443, true); return $this->get('/sns/jscode2session', $param);
$client->withHeader(['Content-Type' => 'application/json']); }
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->get('sns/jscode2session', $param);
$client->close();
return Result::init($client);
}
} }
+2 -2
View File
@@ -73,8 +73,8 @@ class Notify extends SmallProgram
$sign = $params['sign']; $sign = $params['sign'];
unset($params['sign']); unset($params['sign']);
$signType = $this->config->getSignType(); $signType = $this->payConfig->getSignType();
$privateKey = $this->config->getKey(); $privateKey = $this->payConfig->getKey();
$nowSign = Help::sign($params, $privateKey, $signType); $nowSign = Help::sign($params, $privateKey, $signType);
if ($sign === $nowSign) { if ($sign === $nowSign) {
return true; return true;
+18 -44
View File
@@ -25,6 +25,8 @@ class Recharge extends SmallProgram
private string $uniformer = '/cgi-bin/pay/qpay_unified_order.cgi'; private string $uniformer = '/cgi-bin/pay/qpay_unified_order.cgi';
protected string $host = 'qpay.qq.com';
/** /**
* @param $value * @param $value
*/ */
@@ -45,36 +47,11 @@ class Recharge extends SmallProgram
if ($money < 0) { if ($money < 0) {
return new Result(code: 500, message: '充值金额不能小于0.'); return new Result(code: 500, message: '充值金额不能小于0.');
} }
$this->money = $money; $this->money = $money;
$this->orderNo = $orderNo; $this->orderNo = $orderNo;
$this->data['openid'] = $openId; $this->data['openid'] = $openId;
$client = new Client('qpay.qq.com', 443, true); return $this->post($this->uniformer, $this->builder(), 'application/xml');
$client->withHeader(['Content-Type' => 'application/xml']);
$client->withBody($this->builder());
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->post($this->uniformer);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$data = Help::toArray($client->getBody());
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
return new Result(code: 503, message: $data['return_msg']);
}
if ($data['result_code'] != 'SUCCESS') {
return new Result(code: 504, message: $data['err_code_des']);
}
return new Result(code: 0, data: $this->reception($data['prepayid']));
} }
@@ -85,11 +62,11 @@ class Recharge extends SmallProgram
public function reception(string $prepay_id): string public function reception(string $prepay_id): string
{ {
return Help::sign([ return Help::sign([
'signType' => $this->config->getSignType(), 'signType' => $this->payConfig->getSignType(),
'package' => 'prepay_id=' . $prepay_id, 'package' => 'prepay_id=' . $prepay_id,
'nonceStr' => Help::random(32), 'nonceStr' => Help::random(32),
'timestamp' => time() 'timestamp' => time()
], $this->getConfig()->getKey(), $this->getConfig()->getSignType()); ], $this->payConfig->pay->qq->mchSecret, $this->payConfig->getSignType());
} }
/** /**
@@ -97,25 +74,22 @@ class Recharge extends SmallProgram
*/ */
protected function builder(): string protected function builder(): string
{ {
$data = [ $data = [
'appid' => $this->config->getAppid(), 'appid' => $this->payConfig->appId,
'mch_id' => $this->config->getMchId(), 'mch_id' => $this->payConfig->pay->qq->mchId,
'nonce_str' => Help::random(32), 'nonce_str' => Help::random(32),
'body' => $this->config->getBody(), 'body' => $this->payConfig->getBody(),
'out_trade_no' => $this->orderNo, 'out_trade_no' => $this->orderNo,
'total_fee' => $this->money, 'total_fee' => $this->money,
'spbill_create_ip' => $this->spill_create_ip, 'spbill_create_ip' => $this->spill_create_ip,
'notify_url' => $this->config->getNotifyUrl(), 'notify_url' => $this->payConfig->getNotifyUrl(),
'trade_type' => $this->config->getTradeType(), 'trade_type' => $this->payConfig->getTradeType(),
]; ];
$this->data = array_merge($data, $this->data);
$this->data = array_merge($data, $this->data); $key = $this->payConfig->pay->qq->mchSecret;
$sign_type = $this->payConfig->getSignType();
$key = $this->config->getKey(); $this->data['sign_type'] = $this->payConfig->getSignType();
$sign_type = $this->config->getSignType(); $this->data['sign'] = Help::sign($this->data, $key, $sign_type);
$this->data['sign_type'] = $this->config->getSignType();
$this->data['sign'] = Help::sign($this->data, $key, $sign_type);
return Help::toXml($this->data); return Help::toXml($this->data);
} }
+300 -301
View File
@@ -10,353 +10,352 @@ use wchat\common\Result;
class Redhat extends SmallProgram class Redhat extends SmallProgram
{ {
private string $charset = 'UTF8'; private string $charset = 'UTF8';
private string $sign = ''; private string $sign = '';
private string $mch_billno = ''; private string $mch_billno = '';
private string $mch_name = ''; private string $mch_name = '';
private string $re_openid = ''; private string $re_openid = '';
private string $total_amount = ''; private string $total_amount = '';
private string $total_num = ''; private string $total_num = '';
private string $wishing = ''; private string $wishing = '';
private string $act_name = ''; private string $act_name = '';
private string $icon_id = ''; private string $icon_id = '';
private string $banner_id = ''; private string $banner_id = '';
private string $not_send_msg = ''; private string $not_send_msg = '';
private string $min_value = ''; private string $min_value = '';
private string $max_value = ''; private string $max_value = '';
private string $sendUrl = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi'; private string $sendUrl = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
private string $searchUrl = '/cgi-bin/mch_query/qpay_hb_mch_list_query.cgi'; private string $searchUrl = '/cgi-bin/mch_query/qpay_hb_mch_list_query.cgi';
/** /**
* @return string * @return string
*/ */
public function getCharset(): string public function getCharset(): string
{ {
return $this->charset; return $this->charset;
} }
/** /**
* @param string $charset * @param string $charset
*/ */
public function setCharset(string $charset): void public function setCharset(string $charset): void
{ {
$this->charset = $charset; $this->charset = $charset;
} }
/** /**
* @return string * @return string
*/ */
public function getSign(): string public function getSign(): string
{ {
return $this->sign; return $this->sign;
} }
/** /**
* @param string $sign * @param string $sign
*/ */
public function setSign(string $sign): void public function setSign(string $sign): void
{ {
$this->sign = $sign; $this->sign = $sign;
} }
/** /**
* @return string * @return string
*/ */
public function getMchBillno(): string public function getMchBillno(): string
{ {
return $this->mch_billno; return $this->mch_billno;
} }
/** /**
* @param string $mch_billno * @param string $mch_billno
*/ */
public function setMchBillno(string $mch_billno): void public function setMchBillno(string $mch_billno): void
{ {
$this->mch_billno = $mch_billno; $this->mch_billno = $mch_billno;
} }
/** /**
* @return string * @return string
*/ */
public function getMchName(): string public function getMchName(): string
{ {
return $this->mch_name; return $this->mch_name;
} }
/** /**
* @param string $mch_name * @param string $mch_name
*/ */
public function setMchName(string $mch_name): void public function setMchName(string $mch_name): void
{ {
$this->mch_name = $mch_name; $this->mch_name = $mch_name;
} }
/** /**
* @return string * @return string
*/ */
public function getReOpenid(): string public function getReOpenid(): string
{ {
return $this->re_openid; return $this->re_openid;
} }
/** /**
* @param string $re_openid * @param string $re_openid
*/ */
public function setReOpenid(string $re_openid): void public function setReOpenid(string $re_openid): void
{ {
$this->re_openid = $re_openid; $this->re_openid = $re_openid;
} }
/** /**
* @return string * @return string
*/ */
public function getTotalAmount(): string public function getTotalAmount(): string
{ {
return $this->total_amount; return $this->total_amount;
} }
/** /**
* @param string $total_amount * @param string $total_amount
*/ */
public function setTotalAmount(string $total_amount): void public function setTotalAmount(string $total_amount): void
{ {
$this->total_amount = $total_amount; $this->total_amount = $total_amount;
} }
/** /**
* @return string * @return string
*/ */
public function getTotalNum(): string public function getTotalNum(): string
{ {
return $this->total_num; return $this->total_num;
} }
/** /**
* @param string $total_num * @param string $total_num
*/ */
public function setTotalNum(string $total_num): void public function setTotalNum(string $total_num): void
{ {
$this->total_num = $total_num; $this->total_num = $total_num;
} }
/** /**
* @return string * @return string
*/ */
public function getWishing(): string public function getWishing(): string
{ {
return $this->wishing; return $this->wishing;
} }
/** /**
* @param string $wishing * @param string $wishing
*/ */
public function setWishing(string $wishing): void public function setWishing(string $wishing): void
{ {
$this->wishing = $wishing; $this->wishing = $wishing;
} }
/** /**
* @return string * @return string
*/ */
public function getActName(): string public function getActName(): string
{ {
return $this->act_name; return $this->act_name;
} }
/** /**
* @param string $act_name * @param string $act_name
*/ */
public function setActName(string $act_name): void public function setActName(string $act_name): void
{ {
$this->act_name = $act_name; $this->act_name = $act_name;
} }
/** /**
* @return string * @return string
*/ */
public function getIconId(): string public function getIconId(): string
{ {
return $this->icon_id; return $this->icon_id;
} }
/** /**
* @param string $icon_id * @param string $icon_id
*/ */
public function setIconId(string $icon_id): void public function setIconId(string $icon_id): void
{ {
$this->icon_id = $icon_id; $this->icon_id = $icon_id;
} }
/** /**
* @return string * @return string
*/ */
public function getBannerId(): string public function getBannerId(): string
{ {
return $this->banner_id; return $this->banner_id;
} }
/** /**
* @param string $banner_id * @param string $banner_id
*/ */
public function setBannerId(string $banner_id): void public function setBannerId(string $banner_id): void
{ {
$this->banner_id = $banner_id; $this->banner_id = $banner_id;
} }
/** /**
* @return string * @return string
*/ */
public function getNotSendMsg(): string public function getNotSendMsg(): string
{ {
return $this->not_send_msg; return $this->not_send_msg;
} }
/** /**
* @param string $not_send_msg * @param string $not_send_msg
*/ */
public function setNotSendMsg(string $not_send_msg): void public function setNotSendMsg(string $not_send_msg): void
{ {
$this->not_send_msg = $not_send_msg; $this->not_send_msg = $not_send_msg;
} }
/** /**
* @return string * @return string
*/ */
public function getMinValue(): string public function getMinValue(): string
{ {
return $this->min_value; return $this->min_value;
} }
/** /**
* @param string $min_value * @param string $min_value
*/ */
public function setMinValue(string $min_value): void public function setMinValue(string $min_value): void
{ {
$this->min_value = $min_value; $this->min_value = $min_value;
} }
/** /**
* @return string * @return string
*/ */
public function getMaxValue(): string public function getMaxValue(): string
{ {
return $this->max_value; return $this->max_value;
} }
/** /**
* @param string $max_value * @param string $max_value
*/ */
public function setMaxValue(string $max_value): void public function setMaxValue(string $max_value): void
{ {
$this->max_value = $max_value; $this->max_value = $max_value;
} }
/** /**
* @return mixed * @return mixed
* 构建请求参数 * 构建请求参数
*/ */
public function generate(): array public function generate(): array
{ {
$requestParam = []; $requestParam = [];
$requestParam['charset'] = $this->getCharset(); $requestParam['charset'] = $this->getCharset();
$requestParam['nonce_str'] = Help::random(30); $requestParam['nonce_str'] = Help::random(30);
$requestParam['mch_billno'] = $this->getMchBillno(); $requestParam['mch_billno'] = $this->getMchBillno();
$requestParam['mch_id'] = $this->config->getMchId(); $requestParam['mch_id'] = $this->payConfig->pay->qq->mchId;
$requestParam['mch_name'] = $this->getMchName(); $requestParam['mch_name'] = $this->getMchName();
$requestParam['qqappid'] = $this->config->getAppid(); $requestParam['qqappid'] = $this->payConfig->appId;
$requestParam['re_openid'] = $this->getReOpenid(); $requestParam['re_openid'] = $this->getReOpenid();
$requestParam['total_amount'] = $this->getTotalAmount() * 100; $requestParam['total_amount'] = $this->getTotalAmount() * 100;
$requestParam['min_value'] = $this->getMinValue() * 100; $requestParam['min_value'] = $this->getMinValue() * 100;
$requestParam['max_value'] = $this->getMaxValue() * 100; $requestParam['max_value'] = $this->getMaxValue() * 100;
$requestParam['total_num'] = $this->getTotalNum(); $requestParam['total_num'] = $this->getTotalNum();
$requestParam['wishing'] = $this->getWishing(); $requestParam['wishing'] = $this->getWishing();
$requestParam['act_name'] = $this->getActName(); $requestParam['act_name'] = $this->getActName();
$requestParam['icon_id'] = $this->getIconId(); $requestParam['icon_id'] = $this->getIconId();
$requestParam['notify_url'] = $this->config->getNotifyUrl(); $requestParam['notify_url'] = $this->payConfig->getNotifyUrl();
$requestParam['sign'] = $this->builderSign($requestParam);
$requestParam['sign'] = $this->builderSign($requestParam); return $requestParam;
}
return $requestParam;
}
/** /**
* @param $requestParam * @param $requestParam
* @return string * @return string
*/ */
private function builderSign($requestParam): string private function builderSign($requestParam): string
{ {
return Help::sign($requestParam, $this->config->getKey(), $this->config->getSignType()); return Help::sign($requestParam, $this->payConfig->pay->qq->mchSecret, $this->payConfig->getSignType());
} }
/** /**
* @return Result * @return Result
*/ */
public function redEnvelopes(): Result public function redEnvelopes(): Result
{ {
$client = new Client('api.qpay.qq.com', 443, true); $client = new Client('api.qpay.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/x-www-form-urlencoded']); $client->withHeader(['Content-Type' => 'application/x-www-form-urlencoded']);
$client->withSslKeyFile($this->config->getSslKey()); $client->withSslKeyFile($this->payConfig->getSslKey());
$client->withSslCertFile($this->config->getSslCert()); $client->withSslCertFile($this->payConfig->getSslCert());
$client->withCa($this->config->getSslCa()); $client->withCa($this->payConfig->getSslCa());
$proxyHost = $this->getConfig()->getProxyHost(); $proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort(); $proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) { if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort); $client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
} }
$client->post($this->sendUrl, http_build_query($this->generate())); $client->post($this->sendUrl, http_build_query($this->generate()));
$client->close(); $client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) { if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody()); return new Result(code: 505, message: $client->getBody());
} }
$json = json_decode($client->getBody(), true); $json = json_decode($client->getBody(), true);
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') { if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
return new Result(code: 500, message: $json['return_msg'] ?? $json['retmsg']); return new Result(code: 500, message: $json['return_msg'] ?? $json['retmsg']);
} }
if ($json['retcode'] != 0) { if ($json['retcode'] != 0) {
return new Result(code: $json['retcode'], message: $json['retmsg']); return new Result(code: $json['retcode'], message: $json['retmsg']);
} else { } else {
return new Result(code: 0, data: $json); return new Result(code: 0, data: $json);
} }
} }
/** /**
* @param $listid * @param $listid
* @param $orderNo * @param $orderNo
* @return Result * @return Result
*/ */
public function searchByOrderNo($listid, $orderNo = null): Result public function searchByOrderNo($listid, $orderNo = null): Result
{ {
$requestParam['nonce_str'] = Help::random(31); $requestParam['nonce_str'] = Help::random(31);
$requestParam['mch_id'] = $this->config->getMchId(); $requestParam['mch_id'] = $this->payConfig->getMchId();
$requestParam['listid'] = $listid; $requestParam['listid'] = $listid;
if (!empty($orderNo)) { if (!empty($orderNo)) {
$requestParam['mch_billno'] = $orderNo; $requestParam['mch_billno'] = $orderNo;
} }
$requestParam['sign'] = $this->builderSign($requestParam); $requestParam['sign'] = $this->builderSign($requestParam);
$client = new Client('qpay.qq.com', 443, true); $client = new Client('qpay.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']); $proxyHost = $this->getConfig()->getProxyHost();
$proxyHost = $this->getConfig()->getProxyHost(); $proxyPort = $this->getConfig()->getProxyPort();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) { if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort); $client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
} }
$client->post($this->searchUrl, $requestParam); $client->withHeader(['Content-Type' => 'application/json']);
$client->close(); $client->post($this->searchUrl, $requestParam);
if (!in_array($client->getStatusCode(), [101, 200, 201])) { $client->close();
return new Result(code: 505, message: $client->getBody()); if (!in_array($client->getStatusCode(), [101, 200, 201])) {
} return new Result(code: 505, message: $client->getBody());
$json = json_decode($client->getBody(), true); }
if (isset($json['result']) && $json['result'] != 'SUCCESS') { $json = json_decode($client->getBody(), true);
return new Result(code: 500, message: $response['res_info'] ?? '订单查询失败!'); if (isset($json['result']) && $json['result'] != 'SUCCESS') {
} else { return new Result(code: 500, message: $response['res_info'] ?? '订单查询失败!');
return new Result(code: 0, data: $json); } else {
} return new Result(code: 0, data: $json);
} }
}
} }
+31 -66
View File
@@ -14,78 +14,43 @@ use wchat\common\Result;
class SecCheck extends SmallProgram class SecCheck extends SmallProgram
{ {
private string $_url = '/api/json/security/ImgSecCheck?access_token='; private string $_url = '/api/json/security/ImgSecCheck?access_token=';
private string $_msgUrl = '/api/json/security/MsgSecCheck?access_token='; private string $_msgUrl = '/api/json/security/MsgSecCheck?access_token=';
/** /**
* @param string $path * @param string $path
* @return Result * @return Result
*/ */
public function image(string $path = ''): Result public function image(string $path = ''): Result
{ {
if (!file_exists($path)) { if (!file_exists($path)) {
return $this->sendError('文件不存在', 404); return $this->sendError('文件不存在', 404);
}
$real_path = new \CURLFile($path);
$data = [
'appid' => $this->config->getAppid(),
'media' => $real_path,
'form-data[filename]' => $path,
'form-data[content-type]' => $real_path->getMimeType()
];
$client = new Client('api.q.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'multipart/form-data']);
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
} }
$client->upload($path, $data);
$client->close(); $real_path = new \CURLFile($path);
if (!in_array($client->getStatusCode(), [101, 200, 201])) { $data = [
return new Result(code: 505, message: $client->getBody()); 'appid' => $this->payConfig->appId,
} 'media' => $real_path,
$body = json_decode($client->getBody(), true); 'form-data[filename]' => $path,
if (isset($body['errCode']) && $body['errCode'] != 0) { 'form-data[content-type]' => $real_path->getMimeType()
return new Result(code: $body['errCode'], message: $body['errMsg']); ];
} else { return $this->upload($path, $data);
return new Result(code: 0, data: $body); }
}
}
/** /**
* @param string $content * @param string $content
* @return Result * @return Result
*/ */
public function text(string $content): Result public function text(string $content): Result
{ {
if (empty($content)) { if (empty($content)) {
return $this->sendError('文件不存在', 404); return $this->sendError('文件不存在', 404);
}
$url = '/' . ltrim($this->_url, '/') . $this->config->getAccessToken();
$client = new Client('api.q.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
} }
$client->post($url, ['appid' => $this->config->getAppid(), 'content' => $content]); $url = '/' . ltrim($this->_url, '/') . $this->payConfig->getAccessToken();
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) { return $this->post($url, ['appid' => $this->payConfig->appId, 'content' => $content]);
return new Result(code: 505, message: $client->getBody()); }
}
$body = json_decode($client->getBody(), true);
if (isset($body['errCode']) && $body['errCode'] != 0) {
return new Result(code: $body['errCode'], message: $body['errMsg']);
} else {
return new Result(code: 0, data: $body);
}
}
} }
+30
View File
@@ -4,9 +4,39 @@
namespace wchat\qq; namespace wchat\qq;
use Kiri\Client;
use wchat\common\Multiprogramming; use wchat\common\Multiprogramming;
class SmallProgram extends Multiprogramming class SmallProgram extends Multiprogramming
{ {
/**
* @var string
*/
protected string $host = 'api.q.qq.com';
/**
* @param string $url
* @param mixed $data
* @return Client
*/
protected function createClient(string $url, mixed $data): Client
{
$client = new Client('api.qpay.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->withSslCertFile($this->payConfig->pay->qq->mchCert);
$client->withSslKeyFile($this->payConfig->pay->qq->mchKey);
$client->withCa($this->payConfig->pay->qq->mchCa);
$proxyHost = $this->payConfig->getProxyHost();
$proxyPort = $this->payConfig->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->post($url, $data);
$client->close();
return $client;
}
} }
+12 -29
View File
@@ -10,34 +10,17 @@ use wchat\common\Result;
class Token extends SmallProgram class Token extends SmallProgram
{ {
/** /**
* @return Result * @return Result
*/ */
public function token(): Result public function token(): Result
{ {
$query = [ $query = [
'grant_type' => 'client_credential', 'grant_type' => 'client_credential',
'appid' => $this->config->getAppid(), 'appid' => $this->payConfig->appId,
'secret' => $this->config->getAppsecret() 'secret' => $this->payConfig->appSecret
]; ];
$client = new Client('api.q.qq.com', 443, true); return $this->get('/api/getToken', $query);
$client->withHeader(['Content-Type' => 'application/json']); }
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->get('/api/getToken', $query);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $body);
}
}
} }
+134 -152
View File
@@ -5,11 +5,9 @@ namespace wchat\qq\pay;
use Exception; use Exception;
use Kiri\Client;
use wchat\common\Help; use wchat\common\Help;
use wchat\common\HttpClient;
use wchat\common\Result; use wchat\common\Result;
use wchat\wx\SmallProgram; use wchat\qq\SmallProgram;
/** /**
* Class Cash_Bonus * Class Cash_Bonus
@@ -18,167 +16,151 @@ use wchat\wx\SmallProgram;
class Cash_Bonus extends SmallProgram class Cash_Bonus extends SmallProgram
{ {
public string $_cash = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi'; public string $_cash = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
private array $_errors = [ private array $_errors = [
'66228701' => '红包个数超出限制', '66228701' => '红包个数超出限制',
'66228705' => '总金额超出限制', '66228705' => '总金额超出限制',
'66228706' => '总金额不足以按最小金额领取每个红包', '66228706' => '总金额不足以按最小金额领取每个红包',
'66228707' => '商户签名校验失败', '66228707' => '商户签名校验失败',
'66228708' => '重入??', '66228708' => '重入??',
'66228709' => 'openid转换uin失败', '66228709' => 'openid转换uin失败',
'66228711' => '商户订单中的商户号有误', '66228711' => '商户订单中的商户号有误',
'66228712' => '商户订单中的日期超过范围', '66228712' => '商户订单中的日期超过范围',
'66228713' => '余额不足', '66228713' => '余额不足',
'66228715' => '用户未关注公众号,发送AIO消息失败', '66228715' => '用户未关注公众号,发送AIO消息失败',
'66229716' => '用户禁用公众号,发AIO消息失败' '66229716' => '用户禁用公众号,发AIO消息失败'
]; ];
private array $_requestParams = []; private array $_requestParams = [];
/** /**
* @param $value * @param $value
*/ */
public function setMchName($value) public function setMchName($value)
{ {
$this->_requestParams['mch_name'] = $value; $this->_requestParams['mch_name'] = $value;
} }
/** /**
* @param $value * @param $value
*/ */
public function setActName($value) public function setActName($value)
{ {
$this->_requestParams['act_name'] = $value; $this->_requestParams['act_name'] = $value;
} }
/** /**
* @param $value * @param $value
*/ */
public function setWishing($value) public function setWishing($value)
{ {
$this->_requestParams['wishing'] = $value; $this->_requestParams['wishing'] = $value;
} }
/** /**
* @param $value * @param $value
*/ */
public function setIconId($value) public function setIconId($value)
{ {
$this->_requestParams['icon_id'] = $value; $this->_requestParams['icon_id'] = $value;
} }
/** /**
* @param $value * @param $value
*/ */
public function setBannerId($value) public function setBannerId($value)
{ {
$this->_requestParams['banner_id'] = $value; $this->_requestParams['banner_id'] = $value;
} }
/** /**
* @param $mch_billno * @param $mch_billno
* @param $openId * @param $openId
* @param $price * @param $price
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function mch_send($mch_billno, $openId, $price): Result public function mch_send($mch_billno, $openId, $price): Result
{ {
$client = new Client('api.qpay.qq.com', 443, true); $client = $this->createClient($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
$client->withHeader(['Content-Type' => 'application/json']); if (!in_array($client->getStatusCode(), [101, 200, 201])) {
$client->withSslCertFile($this->getConfig()->getSslCert()); return new Result(code: 505, message: $client->getBody());
$client->withSslKeyFile($this->getConfig()->getSslKey()); }
$client->withCa($this->getConfig()->getSslCa()); $json = json_decode($client->getBody(), true);
$proxyHost = $this->getConfig()->getProxyHost(); if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
$proxyPort = $this->getConfig()->getProxyPort(); return new Result(code: 500, message: $json['return_msg'] ?? $json['retmsg']);
if (!empty($proxyHost) && $proxyPort > 0) { } else if ($json['retcode'] != 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort); 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);
}
}
/**
* @param $mch_billno
* @param $openId
* @param $price
* @return array
*/
private function orderConfig($mch_billno, $openId, $price): array
{
$requestParam['charset'] = 1;
$requestParam['nonce_str'] = Help::random(32);
$requestParam['mch_billno'] = $mch_billno;
$requestParam['mch_id'] = $this->payConfig->pay->qq->mchId;
$requestParam['qqappid'] = $this->payConfig->appId;
$requestParam['re_openid'] = $openId;
$requestParam['total_amount'] = $price * 100;
$requestParam['min_value'] = $price * 100;
$requestParam['max_value'] = $price * 100;
$requestParam['total_num'] = 1;
$requestParam['notify_url'] = $this->payConfig->getNotifyUrl();
if (!empty($this->_requestParams)) {
$requestParam = array_merge($requestParam, $this->_requestParams);
}
$requestParam['sign'] = Help::sign($requestParam, $this->payConfig->pay->qq->mchSecret, 'MD5');
return $requestParam;
}
/**
* @return string
* @throws Exception
*/
public function mchOrderNo(): string
{
return implode([
$this->payConfig->pay->qq->mchId,
date('Ymd'),
random_int(11, 99),
date('His'),
random_int(11, 99)
]);
}
/**
* @param array $requestParams
* @return bool
*/
public function validator(array $requestParams): bool
{
$notifySign = $requestParams['sign'];
unset($requestParams['sign']);
$sign = Help::sign($requestParams, $this->payConfig->pay->qq->mchSecret, 'MD5');
if ($sign !== $notifySign) {
return false;
} }
$client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price)); return true;
$client->close(); }
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$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);
}
}
/**
* @param $mch_billno
* @param $openId
* @param $price
* @return array
*/
private function orderConfig($mch_billno, $openId, $price): array
{
$requestParam['charset'] = 1;
$requestParam['nonce_str'] = Help::random(32);
$requestParam['mch_billno'] = $mch_billno;
$requestParam['mch_id'] = $this->getConfig()->getMchId();
$requestParam['qqappid'] = $this->getConfig()->getAppid();
$requestParam['re_openid'] = $openId;
$requestParam['total_amount'] = $price * 100;
$requestParam['min_value'] = $price * 100;
$requestParam['max_value'] = $price * 100;
$requestParam['total_num'] = 1;
$requestParam['notify_url'] = $this->getConfig()->getNotifyUrl();
if (!empty($this->_requestParams)) {
$requestParam = array_merge($requestParam, $this->_requestParams);
}
$requestParam['sign'] = Help::sign($requestParam, $this->getConfig()->getKey(), 'MD5');
return $requestParam;
}
/**
* @return string
* @throws Exception
*/
public function mchOrderNo(): string
{
return implode([
$this->getConfig()->getMchId(),
date('Ymd'),
random_int(11, 99),
date('His'),
random_int(11, 99)
]);
}
/**
* @param array $requestParams
* @return bool
*/
public function validator(array $requestParams): bool
{
$notifySign = $requestParams['sign'];
unset($requestParams['sign']);
$sign = Help::sign($requestParams, $this->getConfig()->getKey(), 'MD5');
if ($sign !== $notifySign) {
return false;
}
return true;
}
} }
+119 -137
View File
@@ -5,9 +5,7 @@ namespace wchat\qq\pay;
use Exception; use Exception;
use Kiri\Client;
use wchat\common\Help; use wchat\common\Help;
use wchat\common\HttpClient;
use wchat\common\Result; use wchat\common\Result;
use wchat\qq\SmallProgram; use wchat\qq\SmallProgram;
@@ -17,149 +15,133 @@ use wchat\qq\SmallProgram;
*/ */
class Enterprise_payment extends SmallProgram class Enterprise_payment extends SmallProgram
{ {
public string $_cash = '/cgi-bin/epay/qpay_epay_b2c.cgi'; public string $_cash = '/cgi-bin/epay/qpay_epay_b2c.cgi';
private $_errors = [ private $_errors = [
'SYSTEMERROR' => '系统错误', 'SYSTEMERROR' => '系统错误',
'PARAM_ERROR' => '请求参数未按指引进行填写', 'PARAM_ERROR' => '请求参数未按指引进行填写',
'SIGNERROR' => '参数签名结果不正确', 'SIGNERROR' => '参数签名结果不正确',
'OP_USER_PASSWD_ERROR' => '操作员密码校验失败', 'OP_USER_PASSWD_ERROR' => '操作员密码校验失败',
'OP_USER_AUTH_ERROR' => '操作员权限错误', 'OP_USER_AUTH_ERROR' => '操作员权限错误',
'TRANSFER_FEE_LIMIT_ERROR' => '转账限额错误', 'TRANSFER_FEE_LIMIT_ERROR' => '转账限额错误',
'TRANSFER_FAIL' => '收款用户的账户不支持收款,收款失败', 'TRANSFER_FAIL' => '收款用户的账户不支持收款,收款失败',
'NOTENOUGH' => '商户营销账户的余额不足', 'NOTENOUGH' => '商户营销账户的余额不足',
'ORDERNOTEXIST' => '转账订单不存在', 'ORDERNOTEXIST' => '转账订单不存在',
'APPID_OR_OPENID_ERR' => 'appid 或 openid 非法', 'APPID_OR_OPENID_ERR' => 'appid 或 openid 非法',
'TOTAL_FEE_OUT_OF_LIMIT' => '单笔限额检查失败', 'TOTAL_FEE_OUT_OF_LIMIT' => '单笔限额检查失败',
'SPID_NOT_ALLOW' => '当前商户不支持企业付款', 'SPID_NOT_ALLOW' => '当前商户不支持企业付款',
'REALNAME_CHECK_ERROR' => '实名检查失败', 'REALNAME_CHECK_ERROR' => '实名检查失败',
'RE_USER_NAME_CHECK_ERROR' => '用户真实姓名校验失败', 'RE_USER_NAME_CHECK_ERROR' => '用户真实姓名校验失败',
'INVALID_CERTIFICATE' => '证书非法', 'INVALID_CERTIFICATE' => '证书非法',
]; ];
private $_requestParams = []; private $_requestParams = [];
/** /**
* @param $value * @param $value
*/ */
public function setOpUserId($value) public function setOpUserId($value)
{ {
$this->_requestParams['op_user_id'] = $value; $this->_requestParams['op_user_id'] = $value;
} }
/** /**
* @param $value * @param $value
*/ */
public function setOpUserPassword($value) public function setOpUserPassword($value)
{ {
$this->_requestParams['op_user_passwd'] = $value; $this->_requestParams['op_user_passwd'] = $value;
} }
/** /**
* @param $value * @param $value
*/ */
public function setSpbillCreateIp($value) public function setSpbillCreateIp($value)
{ {
$this->_requestParams['spbill_create_ip'] = $value; $this->_requestParams['spbill_create_ip'] = $value;
} }
/** /**
* @param $mch_billno * @param $mch_billno
* @param $openId * @param $openId
* @param $price * @param $price
* @return mixed * @return Result
* @throws Exception * @throws Exception
*/ */
public function mch_send($mch_billno, $openId, $price) public function mch_send($mch_billno, $openId, $price): Result
{ {
$client = new Client('api.qpay.qq.com', 443, true); $client = $this->createClient($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
$client->withHeader(['Content-Type' => 'application/json']); if (!in_array($client->getStatusCode(), [101, 200, 201])) {
$client->withSslCertFile($this->getConfig()->getSslCert()); return new Result(code: 505, message: $client->getBody());
$client->withSslKeyFile($this->getConfig()->getSslKey()); }
$client->withCa($this->getConfig()->getSslCa()); $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);
}
}
$proxyHost = $this->getConfig()->getProxyHost(); /**
$proxyPort = $this->getConfig()->getProxyPort(); * @param $mch_billno
if (!empty($proxyHost) && $proxyPort > 0) { * @param $openId
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort); * @param $price
* @return array
*/
private function orderConfig($mch_billno, $openId, $price): array
{
$requestParam['input_charset'] = 'UTF-8';
$requestParam['nonce_str'] = Help::random(32);
$requestParam['out_trade_no'] = $mch_billno;
$requestParam['mch_id'] = $this->payConfig->pay->qq->mchId;
$requestParam['appid'] = $this->payConfig->pay->qq->mchSecret;
$requestParam['openid'] = $openId;
$requestParam['fee_type'] = 'CNY';
$requestParam['total_fee'] = $price * 100;
$requestParam['memo'] = $this->payConfig->getBody();
$requestParam['notify_url'] = $this->payConfig->getNotifyUrl();
$requestParam['spbill_create_ip'] = $this->payConfig->getNotifyUrl();
if (!empty($this->_requestParams) && is_array($this->_requestParams)) {
$requestParam = array_merge($requestParam, $this->_requestParams);
}
$requestParam['sign'] = Help::sign($requestParam, $this->payConfig->pay->qq->mchSecret, 'MD5');
return $requestParam;
}
/**
* @return string
* @throws Exception
*/
public function mchOrderNo(): string
{
return implode([
$this->payConfig->pay->qq->mchId,
date('Ymd'),
random_int(11, 99),
date('His'),
random_int(11, 99)
]);
}
/**
* @param array $requestParams
* @return bool
*/
public function validator(array $requestParams): bool
{
$notifySign = $requestParams['sign'];
unset($requestParams['sign']);
$sign = Help::sign($requestParams, $this->payConfig->pay->qq->mchSecret, 'MD5');
if ($sign !== $notifySign) {
return false;
} }
$client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price)); return true;
$client->close(); }
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$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);
}
}
/**
* @param $mch_billno
* @param $openId
* @param $price
* @return array
*/
private function orderConfig($mch_billno, $openId, $price): array
{
$requestParam['input_charset'] = 'UTF-8';
$requestParam['nonce_str'] = Help::random(32);
$requestParam['out_trade_no'] = $mch_billno;
$requestParam['mch_id'] = $this->getConfig()->getMchId();
$requestParam['appid'] = $this->getConfig()->getAppid();
$requestParam['openid'] = $openId;
$requestParam['fee_type'] = 'CNY';
$requestParam['total_fee'] = $price * 100;
$requestParam['memo'] = $this->getConfig()->getBody();
$requestParam['notify_url'] = $this->getConfig()->getNotifyUrl();
$requestParam['spbill_create_ip'] = $this->getConfig()->getNotifyUrl();
if (!empty($this->_requestParams) && is_array($this->_requestParams)) {
$requestParam = array_merge($requestParam, $this->_requestParams);
}
$requestParam['sign'] = Help::sign($requestParam, $this->getConfig()->getKey(), 'MD5');
return $requestParam;
}
/**
* @return string
* @throws Exception
*/
public function mchOrderNo(): string
{
return implode([
$this->getConfig()->getMchId(),
date('Ymd'),
random_int(11, 99),
date('His'),
random_int(11, 99)
]);
}
/**
* @param array $requestParams
* @return bool
*/
public function validator(array $requestParams): bool
{
$notifySign = $requestParams['sign'];
unset($requestParams['sign']);
$sign = Help::sign($requestParams, $this->getConfig()->getKey(), 'MD5');
if ($sign !== $notifySign) {
return false;
}
return true;
}
} }