From a6a2a0f47264f2debc7702c6e9af6d567d98bd7a Mon Sep 17 00:00:00 2001 From: xl Date: Tue, 14 Nov 2023 01:08:06 +0800 Subject: [PATCH] eee --- common/AppConfig.php | 20 ++ common/Multiprogramming.php | 20 +- qq/Account.php | 36 +- qq/Notify.php | 4 +- qq/Recharge.php | 62 +--- qq/Redhat.php | 601 +++++++++++++++++----------------- qq/SecCheck.php | 97 ++---- qq/SmallProgram.php | 30 ++ qq/Token.php | 41 +-- qq/pay/Cash_Bonus.php | 286 ++++++++-------- qq/pay/Enterprise_payment.php | 256 +++++++-------- 11 files changed, 691 insertions(+), 762 deletions(-) diff --git a/common/AppConfig.php b/common/AppConfig.php index fa32227..dfa94b6 100644 --- a/common/AppConfig.php +++ b/common/AppConfig.php @@ -14,6 +14,11 @@ class AppConfig public string $appId = ''; + /** + * @var string + */ + private string $host = ''; + /** * @var string */ @@ -217,5 +222,20 @@ class AppConfig $this->signType = $signType; } + /** + * @return string + */ + public function getHost(): string + { + return $this->host; + } + + /** + * @param string $host + */ + public function setHost(string $host): void + { + $this->host = $host; + } } \ No newline at end of file diff --git a/common/Multiprogramming.php b/common/Multiprogramming.php index 8c462c1..1ea5ce7 100644 --- a/common/Multiprogramming.php +++ b/common/Multiprogramming.php @@ -22,6 +22,9 @@ abstract class Multiprogramming implements Progaram protected string $errorMsg = ''; + protected string $host = 'api.weixin.qq.com'; + + /** * @var AppConfig */ @@ -136,11 +139,12 @@ abstract class Multiprogramming implements Progaram /** * @param string $requestUrl * @param mixed $body + * @param string $contentType * @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 * @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 $requestUrl * @param $body + * @param string $contentType * @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->withHeader(['Content-Type' => 'application/json']); + $client = new Client($this->host, 443, true); $proxyHost = $this->getConfig()->getProxyHost(); $proxyPort = $this->getConfig()->getProxyPort(); if (!empty($proxyHost) && $proxyPort > 0) { $client->withProxyHost($proxyHost)->withProxyPort($proxyPort); } if ($method == 'post') { + $client->withHeader(['Content-Type' => $contentType]); $client->post($requestUrl, $body); } else if ($method == 'upload') { $client->upload($requestUrl, $body); } else { + $client->withHeader(['Content-Type' => $contentType]); $client->get($requestUrl, $body); } $client->close(); diff --git a/qq/Account.php b/qq/Account.php index a65bf05..a1e900e 100644 --- a/qq/Account.php +++ b/qq/Account.php @@ -14,30 +14,18 @@ use wchat\common\Result; class Account extends SmallProgram { - /** - * @param string $code - * @return Result - */ - public function login(string $code): Result - { - $param['appid'] = $this->config->getAppid(); - $param['secret'] = $this->config->getAppsecret(); - $param['js_code'] = $code; - $param['grant_type'] = 'authorization_code'; + /** + * @param string $code + * @return Result + */ + public function login(string $code): Result + { + $param['appid'] = $this->payConfig->appId; + $param['secret'] = $this->payConfig->appSecret; + $param['js_code'] = $code; + $param['grant_type'] = 'authorization_code'; - $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->get('sns/jscode2session', $param); - $client->close(); - - return Result::init($client); - } + return $this->get('/sns/jscode2session', $param); + } } diff --git a/qq/Notify.php b/qq/Notify.php index 20fbc7c..e3817da 100644 --- a/qq/Notify.php +++ b/qq/Notify.php @@ -73,8 +73,8 @@ class Notify extends SmallProgram $sign = $params['sign']; unset($params['sign']); - $signType = $this->config->getSignType(); - $privateKey = $this->config->getKey(); + $signType = $this->payConfig->getSignType(); + $privateKey = $this->payConfig->getKey(); $nowSign = Help::sign($params, $privateKey, $signType); if ($sign === $nowSign) { return true; diff --git a/qq/Recharge.php b/qq/Recharge.php index c04ec32..dea1bda 100644 --- a/qq/Recharge.php +++ b/qq/Recharge.php @@ -25,6 +25,8 @@ class Recharge extends SmallProgram private string $uniformer = '/cgi-bin/pay/qpay_unified_order.cgi'; + protected string $host = 'qpay.qq.com'; + /** * @param $value */ @@ -45,36 +47,11 @@ class Recharge extends SmallProgram if ($money < 0) { return new Result(code: 500, message: '充值金额不能小于0.'); } - $this->money = $money; - $this->orderNo = $orderNo; + $this->money = $money; + $this->orderNo = $orderNo; $this->data['openid'] = $openId; - $client = new Client('qpay.qq.com', 443, true); - $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'])); + return $this->post($this->uniformer, $this->builder(), 'application/xml'); } @@ -85,11 +62,11 @@ class Recharge extends SmallProgram public function reception(string $prepay_id): string { return Help::sign([ - 'signType' => $this->config->getSignType(), + 'signType' => $this->payConfig->getSignType(), 'package' => 'prepay_id=' . $prepay_id, 'nonceStr' => Help::random(32), '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 { - $data = [ - 'appid' => $this->config->getAppid(), - 'mch_id' => $this->config->getMchId(), + $data = [ + 'appid' => $this->payConfig->appId, + 'mch_id' => $this->payConfig->pay->qq->mchId, 'nonce_str' => Help::random(32), - 'body' => $this->config->getBody(), + 'body' => $this->payConfig->getBody(), 'out_trade_no' => $this->orderNo, 'total_fee' => $this->money, 'spbill_create_ip' => $this->spill_create_ip, - 'notify_url' => $this->config->getNotifyUrl(), - 'trade_type' => $this->config->getTradeType(), + 'notify_url' => $this->payConfig->getNotifyUrl(), + 'trade_type' => $this->payConfig->getTradeType(), ]; - - $this->data = array_merge($data, $this->data); - - $key = $this->config->getKey(); - $sign_type = $this->config->getSignType(); - - $this->data['sign_type'] = $this->config->getSignType(); - $this->data['sign'] = Help::sign($this->data, $key, $sign_type); + $this->data = array_merge($data, $this->data); + $key = $this->payConfig->pay->qq->mchSecret; + $sign_type = $this->payConfig->getSignType(); + $this->data['sign_type'] = $this->payConfig->getSignType(); + $this->data['sign'] = Help::sign($this->data, $key, $sign_type); return Help::toXml($this->data); } diff --git a/qq/Redhat.php b/qq/Redhat.php index da8b036..eae16cb 100644 --- a/qq/Redhat.php +++ b/qq/Redhat.php @@ -10,353 +10,352 @@ use wchat\common\Result; class Redhat extends SmallProgram { - private string $charset = 'UTF8'; - private string $sign = ''; - private string $mch_billno = ''; - private string $mch_name = ''; - private string $re_openid = ''; - private string $total_amount = ''; - private string $total_num = ''; - private string $wishing = ''; - private string $act_name = ''; - private string $icon_id = ''; - private string $banner_id = ''; - private string $not_send_msg = ''; - private string $min_value = ''; - private string $max_value = ''; + private string $charset = 'UTF8'; + private string $sign = ''; + private string $mch_billno = ''; + private string $mch_name = ''; + private string $re_openid = ''; + private string $total_amount = ''; + private string $total_num = ''; + private string $wishing = ''; + private string $act_name = ''; + private string $icon_id = ''; + private string $banner_id = ''; + private string $not_send_msg = ''; + private string $min_value = ''; + private string $max_value = ''; - 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 $sendUrl = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi'; + private string $searchUrl = '/cgi-bin/mch_query/qpay_hb_mch_list_query.cgi'; - /** - * @return string - */ - public function getCharset(): string - { - return $this->charset; - } + /** + * @return string + */ + public function getCharset(): string + { + return $this->charset; + } - /** - * @param string $charset - */ - public function setCharset(string $charset): void - { - $this->charset = $charset; - } + /** + * @param string $charset + */ + public function setCharset(string $charset): void + { + $this->charset = $charset; + } - /** - * @return string - */ - public function getSign(): string - { - return $this->sign; - } + /** + * @return string + */ + public function getSign(): string + { + return $this->sign; + } - /** - * @param string $sign - */ - public function setSign(string $sign): void - { - $this->sign = $sign; - } + /** + * @param string $sign + */ + public function setSign(string $sign): void + { + $this->sign = $sign; + } - /** - * @return string - */ - public function getMchBillno(): string - { - return $this->mch_billno; - } + /** + * @return string + */ + public function getMchBillno(): string + { + return $this->mch_billno; + } - /** - * @param string $mch_billno - */ - public function setMchBillno(string $mch_billno): void - { - $this->mch_billno = $mch_billno; - } + /** + * @param string $mch_billno + */ + public function setMchBillno(string $mch_billno): void + { + $this->mch_billno = $mch_billno; + } - /** - * @return string - */ - public function getMchName(): string - { - return $this->mch_name; - } + /** + * @return string + */ + public function getMchName(): string + { + return $this->mch_name; + } - /** - * @param string $mch_name - */ - public function setMchName(string $mch_name): void - { - $this->mch_name = $mch_name; - } + /** + * @param string $mch_name + */ + public function setMchName(string $mch_name): void + { + $this->mch_name = $mch_name; + } - /** - * @return string - */ - public function getReOpenid(): string - { - return $this->re_openid; - } + /** + * @return string + */ + public function getReOpenid(): string + { + return $this->re_openid; + } - /** - * @param string $re_openid - */ - public function setReOpenid(string $re_openid): void - { - $this->re_openid = $re_openid; - } + /** + * @param string $re_openid + */ + public function setReOpenid(string $re_openid): void + { + $this->re_openid = $re_openid; + } - /** - * @return string - */ - public function getTotalAmount(): string - { - return $this->total_amount; - } + /** + * @return string + */ + public function getTotalAmount(): string + { + return $this->total_amount; + } - /** - * @param string $total_amount - */ - public function setTotalAmount(string $total_amount): void - { - $this->total_amount = $total_amount; - } + /** + * @param string $total_amount + */ + public function setTotalAmount(string $total_amount): void + { + $this->total_amount = $total_amount; + } - /** - * @return string - */ - public function getTotalNum(): string - { - return $this->total_num; - } + /** + * @return string + */ + public function getTotalNum(): string + { + return $this->total_num; + } - /** - * @param string $total_num - */ - public function setTotalNum(string $total_num): void - { - $this->total_num = $total_num; - } + /** + * @param string $total_num + */ + public function setTotalNum(string $total_num): void + { + $this->total_num = $total_num; + } - /** - * @return string - */ - public function getWishing(): string - { - return $this->wishing; - } + /** + * @return string + */ + public function getWishing(): string + { + return $this->wishing; + } - /** - * @param string $wishing - */ - public function setWishing(string $wishing): void - { - $this->wishing = $wishing; - } + /** + * @param string $wishing + */ + public function setWishing(string $wishing): void + { + $this->wishing = $wishing; + } - /** - * @return string - */ - public function getActName(): string - { - return $this->act_name; - } + /** + * @return string + */ + public function getActName(): string + { + return $this->act_name; + } - /** - * @param string $act_name - */ - public function setActName(string $act_name): void - { - $this->act_name = $act_name; - } + /** + * @param string $act_name + */ + public function setActName(string $act_name): void + { + $this->act_name = $act_name; + } - /** - * @return string - */ - public function getIconId(): string - { - return $this->icon_id; - } + /** + * @return string + */ + public function getIconId(): string + { + return $this->icon_id; + } - /** - * @param string $icon_id - */ - public function setIconId(string $icon_id): void - { - $this->icon_id = $icon_id; - } + /** + * @param string $icon_id + */ + public function setIconId(string $icon_id): void + { + $this->icon_id = $icon_id; + } - /** - * @return string - */ - public function getBannerId(): string - { - return $this->banner_id; - } + /** + * @return string + */ + public function getBannerId(): string + { + return $this->banner_id; + } - /** - * @param string $banner_id - */ - public function setBannerId(string $banner_id): void - { - $this->banner_id = $banner_id; - } + /** + * @param string $banner_id + */ + public function setBannerId(string $banner_id): void + { + $this->banner_id = $banner_id; + } - /** - * @return string - */ - public function getNotSendMsg(): string - { - return $this->not_send_msg; - } + /** + * @return string + */ + public function getNotSendMsg(): string + { + return $this->not_send_msg; + } - /** - * @param string $not_send_msg - */ - public function setNotSendMsg(string $not_send_msg): void - { - $this->not_send_msg = $not_send_msg; - } + /** + * @param string $not_send_msg + */ + public function setNotSendMsg(string $not_send_msg): void + { + $this->not_send_msg = $not_send_msg; + } - /** - * @return string - */ - public function getMinValue(): string - { - return $this->min_value; - } + /** + * @return string + */ + public function getMinValue(): string + { + return $this->min_value; + } - /** - * @param string $min_value - */ - public function setMinValue(string $min_value): void - { - $this->min_value = $min_value; - } + /** + * @param string $min_value + */ + public function setMinValue(string $min_value): void + { + $this->min_value = $min_value; + } - /** - * @return string - */ - public function getMaxValue(): string - { - return $this->max_value; - } + /** + * @return string + */ + public function getMaxValue(): string + { + return $this->max_value; + } - /** - * @param string $max_value - */ - public function setMaxValue(string $max_value): void - { - $this->max_value = $max_value; - } + /** + * @param string $max_value + */ + public function setMaxValue(string $max_value): void + { + $this->max_value = $max_value; + } - /** - * @return mixed - * 构建请求参数 - */ - public function generate(): array - { - $requestParam = []; - $requestParam['charset'] = $this->getCharset(); - $requestParam['nonce_str'] = Help::random(30); - $requestParam['mch_billno'] = $this->getMchBillno(); - $requestParam['mch_id'] = $this->config->getMchId(); - $requestParam['mch_name'] = $this->getMchName(); - $requestParam['qqappid'] = $this->config->getAppid(); - $requestParam['re_openid'] = $this->getReOpenid(); - $requestParam['total_amount'] = $this->getTotalAmount() * 100; - $requestParam['min_value'] = $this->getMinValue() * 100; - $requestParam['max_value'] = $this->getMaxValue() * 100; - $requestParam['total_num'] = $this->getTotalNum(); - $requestParam['wishing'] = $this->getWishing(); - $requestParam['act_name'] = $this->getActName(); - $requestParam['icon_id'] = $this->getIconId(); - $requestParam['notify_url'] = $this->config->getNotifyUrl(); + /** + * @return mixed + * 构建请求参数 + */ + public function generate(): array + { + $requestParam = []; + $requestParam['charset'] = $this->getCharset(); + $requestParam['nonce_str'] = Help::random(30); + $requestParam['mch_billno'] = $this->getMchBillno(); + $requestParam['mch_id'] = $this->payConfig->pay->qq->mchId; + $requestParam['mch_name'] = $this->getMchName(); + $requestParam['qqappid'] = $this->payConfig->appId; + $requestParam['re_openid'] = $this->getReOpenid(); + $requestParam['total_amount'] = $this->getTotalAmount() * 100; + $requestParam['min_value'] = $this->getMinValue() * 100; + $requestParam['max_value'] = $this->getMaxValue() * 100; + $requestParam['total_num'] = $this->getTotalNum(); + $requestParam['wishing'] = $this->getWishing(); + $requestParam['act_name'] = $this->getActName(); + $requestParam['icon_id'] = $this->getIconId(); + $requestParam['notify_url'] = $this->payConfig->getNotifyUrl(); + $requestParam['sign'] = $this->builderSign($requestParam); - $requestParam['sign'] = $this->builderSign($requestParam); - - return $requestParam; - } + return $requestParam; + } - /** - * @param $requestParam - * @return string - */ - private function builderSign($requestParam): string - { - return Help::sign($requestParam, $this->config->getKey(), $this->config->getSignType()); - } + /** + * @param $requestParam + * @return string + */ + private function builderSign($requestParam): string + { + return Help::sign($requestParam, $this->payConfig->pay->qq->mchSecret, $this->payConfig->getSignType()); + } - /** - * @return Result - */ - public function redEnvelopes(): Result - { - $client = new Client('api.qpay.qq.com', 443, true); - $client->withHeader(['Content-Type' => 'application/x-www-form-urlencoded']); - $client->withSslKeyFile($this->config->getSslKey()); - $client->withSslCertFile($this->config->getSslCert()); - $client->withCa($this->config->getSslCa()); + /** + * @return Result + */ + public function redEnvelopes(): Result + { + $client = new Client('api.qpay.qq.com', 443, true); + $client->withHeader(['Content-Type' => 'application/x-www-form-urlencoded']); + $client->withSslKeyFile($this->payConfig->getSslKey()); + $client->withSslCertFile($this->payConfig->getSslCert()); + $client->withCa($this->payConfig->getSslCa()); $proxyHost = $this->getConfig()->getProxyHost(); $proxyPort = $this->getConfig()->getProxyPort(); if (!empty($proxyHost) && $proxyPort > 0) { $client->withProxyHost($proxyHost)->withProxyPort($proxyPort); } - $client->post($this->sendUrl, http_build_query($this->generate())); - $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']); - } - if ($json['retcode'] != 0) { - return new Result(code: $json['retcode'], message: $json['retmsg']); - } else { - return new Result(code: 0, data: $json); - } - } + $client->post($this->sendUrl, http_build_query($this->generate())); + $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']); + } + if ($json['retcode'] != 0) { + return new Result(code: $json['retcode'], message: $json['retmsg']); + } else { + return new Result(code: 0, data: $json); + } + } - /** - * @param $listid - * @param $orderNo - * @return Result - */ - public function searchByOrderNo($listid, $orderNo = null): Result - { - $requestParam['nonce_str'] = Help::random(31); - $requestParam['mch_id'] = $this->config->getMchId(); - $requestParam['listid'] = $listid; - if (!empty($orderNo)) { - $requestParam['mch_billno'] = $orderNo; - } - $requestParam['sign'] = $this->builderSign($requestParam); - $client = new Client('qpay.qq.com', 443, true); - $client->withHeader(['Content-Type' => 'application/json']); - $proxyHost = $this->getConfig()->getProxyHost(); - $proxyPort = $this->getConfig()->getProxyPort(); + /** + * @param $listid + * @param $orderNo + * @return Result + */ + public function searchByOrderNo($listid, $orderNo = null): Result + { + $requestParam['nonce_str'] = Help::random(31); + $requestParam['mch_id'] = $this->payConfig->getMchId(); + $requestParam['listid'] = $listid; + if (!empty($orderNo)) { + $requestParam['mch_billno'] = $orderNo; + } + $requestParam['sign'] = $this->builderSign($requestParam); + $client = new Client('qpay.qq.com', 443, true); + $proxyHost = $this->getConfig()->getProxyHost(); + $proxyPort = $this->getConfig()->getProxyPort(); if (!empty($proxyHost) && $proxyPort > 0) { $client->withProxyHost($proxyHost)->withProxyPort($proxyPort); } - $client->post($this->searchUrl, $requestParam); - $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['result']) && $json['result'] != 'SUCCESS') { - return new Result(code: 500, message: $response['res_info'] ?? '订单查询失败!'); - } else { - return new Result(code: 0, data: $json); - } - } + $client->withHeader(['Content-Type' => 'application/json']); + $client->post($this->searchUrl, $requestParam); + $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['result']) && $json['result'] != 'SUCCESS') { + return new Result(code: 500, message: $response['res_info'] ?? '订单查询失败!'); + } else { + return new Result(code: 0, data: $json); + } + } } diff --git a/qq/SecCheck.php b/qq/SecCheck.php index b9a559e..e4646a0 100644 --- a/qq/SecCheck.php +++ b/qq/SecCheck.php @@ -14,78 +14,43 @@ use wchat\common\Result; 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 - * @return Result - */ - public function image(string $path = ''): Result - { - if (!file_exists($path)) { - 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); + /** + * @param string $path + * @return Result + */ + public function image(string $path = ''): Result + { + if (!file_exists($path)) { + return $this->sendError('文件不存在', 404); } - $client->upload($path, $data); - $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); - } - } + + $real_path = new \CURLFile($path); + $data = [ + 'appid' => $this->payConfig->appId, + 'media' => $real_path, + 'form-data[filename]' => $path, + 'form-data[content-type]' => $real_path->getMimeType() + ]; + return $this->upload($path, $data); + } - /** - * @param string $content - * @return Result - */ - public function text(string $content): Result - { - if (empty($content)) { - 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); + /** + * @param string $content + * @return Result + */ + public function text(string $content): Result + { + if (empty($content)) { + return $this->sendError('文件不存在', 404); } - $client->post($url, ['appid' => $this->config->getAppid(), 'content' => $content]); - $client->close(); + $url = '/' . ltrim($this->_url, '/') . $this->payConfig->getAccessToken(); - 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); - } - } + return $this->post($url, ['appid' => $this->payConfig->appId, 'content' => $content]); + } } diff --git a/qq/SmallProgram.php b/qq/SmallProgram.php index e2aaf9c..4a20f96 100644 --- a/qq/SmallProgram.php +++ b/qq/SmallProgram.php @@ -4,9 +4,39 @@ namespace wchat\qq; +use Kiri\Client; use wchat\common\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; + } + } diff --git a/qq/Token.php b/qq/Token.php index cb65c30..71f7107 100644 --- a/qq/Token.php +++ b/qq/Token.php @@ -10,34 +10,17 @@ use wchat\common\Result; class Token extends SmallProgram { - /** - * @return Result - */ - public function token(): Result - { - $query = [ - 'grant_type' => 'client_credential', - 'appid' => $this->config->getAppid(), - 'secret' => $this->config->getAppsecret() - ]; - $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->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); - } - } + /** + * @return Result + */ + public function token(): Result + { + $query = [ + 'grant_type' => 'client_credential', + 'appid' => $this->payConfig->appId, + 'secret' => $this->payConfig->appSecret + ]; + return $this->get('/api/getToken', $query); + } } diff --git a/qq/pay/Cash_Bonus.php b/qq/pay/Cash_Bonus.php index 9ad4ff5..d1a6e7d 100644 --- a/qq/pay/Cash_Bonus.php +++ b/qq/pay/Cash_Bonus.php @@ -5,11 +5,9 @@ namespace wchat\qq\pay; use Exception; -use Kiri\Client; use wchat\common\Help; -use wchat\common\HttpClient; use wchat\common\Result; -use wchat\wx\SmallProgram; +use wchat\qq\SmallProgram; /** * Class Cash_Bonus @@ -18,167 +16,151 @@ use wchat\wx\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 = [ - '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 - */ - public function setMchName($value) - { - $this->_requestParams['mch_name'] = $value; - } + /** + * @param $value + */ + public function setMchName($value) + { + $this->_requestParams['mch_name'] = $value; + } - /** - * @param $value - */ - public function setActName($value) - { - $this->_requestParams['act_name'] = $value; - } + /** + * @param $value + */ + public function setActName($value) + { + $this->_requestParams['act_name'] = $value; + } - /** - * @param $value - */ - public function setWishing($value) - { - $this->_requestParams['wishing'] = $value; - } + /** + * @param $value + */ + public function setWishing($value) + { + $this->_requestParams['wishing'] = $value; + } - /** - * @param $value - */ - public function setIconId($value) - { - $this->_requestParams['icon_id'] = $value; - } + /** + * @param $value + */ + public function setIconId($value) + { + $this->_requestParams['icon_id'] = $value; + } - /** - * @param $value - */ - public function setBannerId($value) - { - $this->_requestParams['banner_id'] = $value; - } + /** + * @param $value + */ + public function setBannerId($value) + { + $this->_requestParams['banner_id'] = $value; + } - /** - * @param $mch_billno - * @param $openId - * @param $price - * @return mixed - * @throws Exception - */ - public function mch_send($mch_billno, $openId, $price): Result - { - $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()); + /** + * @param $mch_billno + * @param $openId + * @param $price + * @return mixed + * @throws Exception + */ + public function mch_send($mch_billno, $openId, $price): Result + { + $client = $this->createClient($this->_cash, $this->orderConfig($mch_billno, $openId, $price)); + if (!in_array($client->getStatusCode(), [101, 200, 201])) { + return new Result(code: 505, message: $client->getBody()); + } + $json = json_decode($client->getBody(), true); - $proxyHost = $this->getConfig()->getProxyHost(); - $proxyPort = $this->getConfig()->getProxyPort(); - if (!empty($proxyHost) && $proxyPort > 0) { - $client->withProxyHost($proxyHost)->withProxyPort($proxyPort); + 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->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)); - $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; - } + return true; + } } diff --git a/qq/pay/Enterprise_payment.php b/qq/pay/Enterprise_payment.php index 648e29a..3f1f52a 100644 --- a/qq/pay/Enterprise_payment.php +++ b/qq/pay/Enterprise_payment.php @@ -5,9 +5,7 @@ namespace wchat\qq\pay; use Exception; -use Kiri\Client; use wchat\common\Help; -use wchat\common\HttpClient; use wchat\common\Result; use wchat\qq\SmallProgram; @@ -17,149 +15,133 @@ use wchat\qq\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 = [ - 'SYSTEMERROR' => '系统错误', - 'PARAM_ERROR' => '请求参数未按指引进行填写', - 'SIGNERROR' => '参数签名结果不正确', - 'OP_USER_PASSWD_ERROR' => '操作员密码校验失败', - 'OP_USER_AUTH_ERROR' => '操作员权限错误', - 'TRANSFER_FEE_LIMIT_ERROR' => '转账限额错误', - 'TRANSFER_FAIL' => '收款用户的账户不支持收款,收款失败', - 'NOTENOUGH' => '商户营销账户的余额不足', - 'ORDERNOTEXIST' => '转账订单不存在', - 'APPID_OR_OPENID_ERR' => 'appid 或 openid 非法', - 'TOTAL_FEE_OUT_OF_LIMIT' => '单笔限额检查失败', - 'SPID_NOT_ALLOW' => '当前商户不支持企业付款', - 'REALNAME_CHECK_ERROR' => '实名检查失败', - 'RE_USER_NAME_CHECK_ERROR' => '用户真实姓名校验失败', - 'INVALID_CERTIFICATE' => '证书非法', - ]; + private $_errors = [ + 'SYSTEMERROR' => '系统错误', + 'PARAM_ERROR' => '请求参数未按指引进行填写', + 'SIGNERROR' => '参数签名结果不正确', + 'OP_USER_PASSWD_ERROR' => '操作员密码校验失败', + 'OP_USER_AUTH_ERROR' => '操作员权限错误', + 'TRANSFER_FEE_LIMIT_ERROR' => '转账限额错误', + 'TRANSFER_FAIL' => '收款用户的账户不支持收款,收款失败', + 'NOTENOUGH' => '商户营销账户的余额不足', + 'ORDERNOTEXIST' => '转账订单不存在', + 'APPID_OR_OPENID_ERR' => 'appid 或 openid 非法', + 'TOTAL_FEE_OUT_OF_LIMIT' => '单笔限额检查失败', + 'SPID_NOT_ALLOW' => '当前商户不支持企业付款', + 'REALNAME_CHECK_ERROR' => '实名检查失败', + 'RE_USER_NAME_CHECK_ERROR' => '用户真实姓名校验失败', + 'INVALID_CERTIFICATE' => '证书非法', + ]; - private $_requestParams = []; + private $_requestParams = []; - /** - * @param $value - */ - public function setOpUserId($value) - { - $this->_requestParams['op_user_id'] = $value; - } + /** + * @param $value + */ + public function setOpUserId($value) + { + $this->_requestParams['op_user_id'] = $value; + } - /** - * @param $value - */ - public function setOpUserPassword($value) - { - $this->_requestParams['op_user_passwd'] = $value; - } + /** + * @param $value + */ + public function setOpUserPassword($value) + { + $this->_requestParams['op_user_passwd'] = $value; + } - /** - * @param $value - */ - public function setSpbillCreateIp($value) - { - $this->_requestParams['spbill_create_ip'] = $value; - } + /** + * @param $value + */ + public function setSpbillCreateIp($value) + { + $this->_requestParams['spbill_create_ip'] = $value; + } - /** - * @param $mch_billno - * @param $openId - * @param $price - * @return mixed - * @throws Exception - */ - public function mch_send($mch_billno, $openId, $price) - { - $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()); + /** + * @param $mch_billno + * @param $openId + * @param $price + * @return Result + * @throws Exception + */ + public function mch_send($mch_billno, $openId, $price): Result + { + $client = $this->createClient($this->_cash, $this->orderConfig($mch_billno, $openId, $price)); + 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); + } + } - $proxyHost = $this->getConfig()->getProxyHost(); - $proxyPort = $this->getConfig()->getProxyPort(); - if (!empty($proxyHost) && $proxyPort > 0) { - $client->withProxyHost($proxyHost)->withProxyPort($proxyPort); + /** + * @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->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)); - $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; - } + return true; + } }