This commit is contained in:
xl
2023-11-14 00:15:27 +08:00
parent fbdbcbccd9
commit e721fe4f36
53 changed files with 1 additions and 1 deletions
+43
View File
@@ -0,0 +1,43 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/19 0019
* Time: 16:12
*/
namespace wchat\qq;
use Kiri\Client;
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';
$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);
}
}
+94
View File
@@ -0,0 +1,94 @@
<?php
namespace wchat\qq;
use Exception;
use wchat\common\Help;
/**
* Class Notify
* @package wchat\qq
*/
class Notify extends SmallProgram
{
public mixed $appid = null;
public mixed $mch_id = null;
public mixed $nonce_str = null;
public mixed $sign = null;
public mixed $device_info = null;
public mixed $trade_type = null;
public mixed $trade_state = null;
public mixed $bank_type = null;
public mixed $fee_type = null;
public mixed $total_fee = null;
public mixed $cash_fee = null;
public mixed $coupon_fee = null;
public mixed $transaction_id = null;
public mixed $out_trade_no = null;
public mixed $attach = null;
public mixed $time_end = null;
public mixed $openid = null;
/**
* @return bool
* 判断是否完成支付
*/
public function isSuccess(): bool
{
return $this->trade_state === 'SUCCESS';
}
/**
* @param array $params
* @return $this
* @throws Exception
*/
public function setPayNotifyData(array $params): static
{
if (!$this->validation($params)) {
throw new Exception('签名错误!');
}
foreach ($params as $key => $val) {
$this->$key = $val;
}
return $this;
}
public function __set(string $name, $value): void
{
if (property_exists($this, $name)) {
$this->{$name} = $value;
}
}
/**
* @param array $params
* @return bool
*/
public function validation(array $params): bool
{
$sign = $params['sign'];
unset($params['sign']);
$signType = $this->config->getSignType();
$privateKey = $this->config->getKey();
$nowSign = Help::sign($params, $privateKey, $signType);
if ($sign === $nowSign) {
return true;
}
return false;
}
/**
* @return null
*/
public function getAppid()
{
return $this->appid;
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace wchat\wx;
use Kiri\Di\Container;
use ReflectionException;
use wchat\common\Config;
class QqFactory
{
/**
* @param $class
* @param Config $config
* @return mixed
* @throws ReflectionException
*/
public static function get($class, Config $config): mixed
{
$container = Container::instance();
$object = $container->get($class);
$object->setConfig($config);
return $object;
}
}
+122
View File
@@ -0,0 +1,122 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/26 0026
* Time: 10:22
*/
namespace wchat\qq;
use Exception;
use Kiri\Client;
use wchat\common\Result;
use wchat\common\Help;
class Recharge extends SmallProgram
{
private float $money = 0;
private string $orderNo;
private array $data = [];
private string $spill_create_ip = '';
private string $uniformer = '/cgi-bin/pay/qpay_unified_order.cgi';
/**
* @param $value
*/
public function setSpillCreateIp($value)
{
$this->spill_create_ip = $value;
}
/**
* @param int $money
* @param string $orderNo
* @param string $openId
* @return Result
* @throws
*/
public function recharge(int $money, string $orderNo, string $openId = ''): Result
{
if ($money < 0) {
return new Result(code: 500, message: '充值金额不能小于0.');
}
$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']));
}
/**
* @param string $prepay_id
* @return string
*/
public function reception(string $prepay_id): string
{
return Help::sign([
'signType' => $this->config->getSignType(),
'package' => 'prepay_id=' . $prepay_id,
'nonceStr' => Help::random(32),
'timestamp' => time()
], $this->getConfig()->getKey(), $this->getConfig()->getSignType());
}
/**
* @return string
*/
protected function builder(): string
{
$data = [
'appid' => $this->config->getAppid(),
'mch_id' => $this->config->getMchId(),
'nonce_str' => Help::random(32),
'body' => $this->config->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(),
];
$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);
return Help::toXml($this->data);
}
}
+362
View File
@@ -0,0 +1,362 @@
<?php
namespace wchat\qq;
use Kiri\Client;
use wchat\common\Help;
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 $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;
}
/**
* @param string $charset
*/
public function setCharset(string $charset): void
{
$this->charset = $charset;
}
/**
* @return string
*/
public function getSign(): string
{
return $this->sign;
}
/**
* @param string $sign
*/
public function setSign(string $sign): void
{
$this->sign = $sign;
}
/**
* @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;
}
/**
* @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;
}
/**
* @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;
}
/**
* @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;
}
/**
* @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;
}
/**
* @return string
*/
public function getWishing(): string
{
return $this->wishing;
}
/**
* @param string $wishing
*/
public function setWishing(string $wishing): void
{
$this->wishing = $wishing;
}
/**
* @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;
}
/**
* @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;
}
/**
* @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;
}
/**
* @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;
}
/**
* @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;
}
/**
* @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;
}
/**
* @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();
$requestParam['sign'] = $this->builderSign($requestParam);
return $requestParam;
}
/**
* @param $requestParam
* @return string
*/
private function builderSign($requestParam): string
{
return Help::sign($requestParam, $this->config->getKey(), $this->config->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());
$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);
}
}
/**
* @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();
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);
}
}
}
+91
View File
@@ -0,0 +1,91 @@
<?php
namespace wchat\qq;
use Kiri\Client;
use wchat\common\Result;
/**
* Class SecCheck
* @package qq
*/
class SecCheck extends SmallProgram
{
private string $_url = '/api/json/security/ImgSecCheck?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);
}
$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);
}
}
/**
* @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);
}
$client->post($url, ['appid' => $this->config->getAppid(), 'content' => $content]);
$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);
}
}
}
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace wchat\qq;
use wchat\common\Multiprogramming;
class SmallProgram extends Multiprogramming
{
}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace wchat\qq;
use wchat\common\Result;
/**
* Class Subject
* @package wchat\qq
*/
class Subject extends \wchat\base\Subject
{
/**
* @return string
*/
public function getUrl(): string
{
return '/api/json/subscribe/SendSubscriptionMessage';
}
/**
* @return string
*/
public function getHost(): string
{
return 'api.q.qq.com';
}
}
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace wchat\qq;
use Kiri\Client;
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);
}
}
}
+184
View File
@@ -0,0 +1,184 @@
<?php
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;
/**
* Class Cash_Bonus
* @package wchat\qq
*/
class Cash_Bonus extends SmallProgram
{
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 $_requestParams = [];
/**
* @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 setWishing($value)
{
$this->_requestParams['wishing'] = $value;
}
/**
* @param $value
*/
public function setIconId($value)
{
$this->_requestParams['icon_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());
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$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;
}
}
+165
View File
@@ -0,0 +1,165 @@
<?php
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;
/**
* Class Enterprise_payment
* @package wchat\qq\pay
*/
class Enterprise_payment extends SmallProgram
{
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 $_requestParams = [];
/**
* @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 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());
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$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;
}
}
+102
View File
@@ -0,0 +1,102 @@
<?php
namespace wchat\qq\result;
/**
* Class RedHatResult
* @package qq\result
*/
class RedHatResult
{
public mixed $result;
public mixed $res_info;
public mixed $listid;
public mixed $state;
public mixed $total_num;
public mixed $recv_num;
public mixed $total_amount;
public mixed $recv_amount;
public mixed $recv_details;
public mixed $uin;
const RED_HAT_STATUS_PAID = 1;
const RED_HAT_STATUS_SNATCHED = 2;
const RED_HAT_STATUS_EXPIRED = 3;
const RED_HAT_STATUS_REFUNDED = 4;
/**
* RedHatResult constructor.
* @param array $array
*/
public function __construct(array $array)
{
$this->init($array);
}
/**
* @param $array
* @return void
*/
private function init($array): void
{
foreach ($array as $key => $value) {
if (!property_exists($this, $key)) {
continue;
}
$this->{$key} = $value;
}
}
/**
* @param $field
* @return mixed
*/
public function get($field): mixed
{
return $this->$field;
}
/**
* @return bool
* 是否已完成支付
*/
public function isPaid(): bool
{
return $this->state == self::RED_HAT_STATUS_PAID;
}
/**
* @return bool
* 是否已抢完
*/
public function isSnatched(): bool
{
return $this->state == self::RED_HAT_STATUS_SNATCHED;
}
/**
* @return bool
*/
public function isExpired(): bool
{
return $this->state == self::RED_HAT_STATUS_EXPIRED;
}
/**
* @return bool
*/
public function isRefunded(): bool
{
return $this->state == self::RED_HAT_STATUS_REFUNDED;
}
}