Compare commits
97 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 24655a4644 | |||
| 4ca64c8205 | |||
| d9dbfb3ca6 | |||
| 1ff0203511 | |||
| 561b105d31 | |||
| 3d423acec2 | |||
| d76680a7a2 | |||
| aeb4f3d7fb | |||
| a3aa8e8ac3 | |||
| de4f58d3e3 | |||
| 76bf8cc2fc | |||
| d9713d00e7 | |||
| 0c89617b00 | |||
| d16adba08e | |||
| 6438d8affb | |||
| 857237c2bc | |||
| faccc08c86 | |||
| b1cf009069 | |||
| 4ea7c71bbb | |||
| 5516f78983 | |||
| 03e8d0e181 | |||
| 7550d6dcfc | |||
| 8af14d1b3b | |||
| 3e7de322df | |||
| 9b13746b02 | |||
| d8214fe810 | |||
| e76f4a7b95 | |||
| 441f3fed31 | |||
| 246ca43a83 | |||
| 4303983818 | |||
| 4f3eed59a0 | |||
| daa4034e39 | |||
| 6c1e4dbbae | |||
| 581da74115 | |||
| e7220b6aba | |||
| 70222819b9 | |||
| f263014948 | |||
| f92fdab692 | |||
| c3326a9b5a | |||
| a962c00223 | |||
| 09cb764dad | |||
| 2f67a0145e | |||
| 64749d2365 | |||
| 2114f08eda | |||
| 21303e5363 | |||
| a0fc4d5a30 | |||
| dfc59ab88d | |||
| 4f2b9367ae | |||
| 93dc51b02c | |||
| 77aa01185b | |||
| a6a2a0f472 | |||
| 0d8ddfef54 | |||
| e721fe4f36 | |||
| fbdbcbccd9 | |||
| c97302ae76 | |||
| d64ffe4b07 | |||
| 494a7e48c0 | |||
| 0eb8aab1d2 | |||
| ac4c6c1e27 | |||
| e010910a25 | |||
| f35f3e69a6 | |||
| e8bd6f3ae0 | |||
| 59456a4759 | |||
| 0d1cb506aa | |||
| 90c65a8c5f | |||
| 693ec4fa65 | |||
| 389f5261c7 | |||
| cf03ebd6dc | |||
| e0d1811a00 | |||
| 1a78bdb173 | |||
| c5f8b19a83 | |||
| 227ff1c166 | |||
| b8960deda5 | |||
| ac70bd3df5 | |||
| 9264f05a49 | |||
| a2dc66ec84 | |||
| d0a6787b85 | |||
| 7f2bdbdb4f | |||
| 459d298983 | |||
| a876b780f5 | |||
| 885c902be8 | |||
| 43f504b2ad | |||
| dbdae7c6c5 | |||
| b87232bbc0 | |||
| d9528679ff | |||
| 09544ee4a2 | |||
| a7c6939a9f | |||
| 0578ba384b | |||
| 7abbaaca46 | |||
| d574ce2c42 | |||
| 2275ed92d1 | |||
| 48467b88ce | |||
| e87728266b | |||
| ae67dce77b | |||
| 2ff7ceb66b | |||
| c74c3b004b | |||
| b92ea7df9f |
@@ -1,3 +1,4 @@
|
||||
/.idea/php.xml
|
||||
vendor/
|
||||
composer.lock
|
||||
.idea/*
|
||||
-101
@@ -1,101 +0,0 @@
|
||||
<?php
|
||||
|
||||
use wchat\common\Config;
|
||||
use wchat\common\Progaram;
|
||||
|
||||
/**
|
||||
* Class Container
|
||||
*/
|
||||
class Container
|
||||
{
|
||||
|
||||
/** @var Container */
|
||||
private static $_instance;
|
||||
|
||||
private $config;
|
||||
|
||||
private $container = [];
|
||||
|
||||
const QQ_SMALL_PROGRAM = 'QQ_SMALL_PROGRAM';
|
||||
const QQ_LITTLE_GAME = 'QQ_LITTLE_GAME';
|
||||
const WX_LITTLE_GAME = 'WX_LITTLE_GAME';
|
||||
const WX_SMALL_PROGRAM = 'WX_SMALL_PROGRAM';
|
||||
const WX_OFFICIAL_ACCOUNT = 'WX_OFFICIAL_ACCOUNT';
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $config
|
||||
* @return mixed|object|ReflectionClass
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public static function newInstance($class, $config)
|
||||
{
|
||||
if (!(static::$_instance instanceof Container)) {
|
||||
static::$_instance = new Container();
|
||||
}
|
||||
static::$_instance->generate($config);
|
||||
|
||||
if (static::$_instance->exists($class)) {
|
||||
$newInstance = static::$_instance->get($class);
|
||||
if (method_exists($newInstance, 'setConfig')) {
|
||||
$newInstance->setConfig($config);
|
||||
}
|
||||
return $newInstance;
|
||||
} else {
|
||||
return static::$_instance->createObject($class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param wchat\common\Config $config
|
||||
* @return $this
|
||||
*/
|
||||
public function generate(Config $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @return object|ReflectionClass
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function createObject($class)
|
||||
{
|
||||
$newInstance = new \ReflectionClass($class);
|
||||
if (!$newInstance->isInstantiable()) {
|
||||
throw new Exception('Class Con\'t instance.');
|
||||
}
|
||||
$newInstance = $newInstance->newInstance();
|
||||
if (method_exists($newInstance, 'setConfig')) {
|
||||
$newInstance->setConfig($this->config);
|
||||
}
|
||||
|
||||
$this->container[$class] = $newInstance;
|
||||
return $newInstance;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed|object|ReflectionClass
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
return $this->container[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($name)
|
||||
{
|
||||
return array_key_exists($name, $this->container) && $this->container[$name] instanceof Progaram;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\base;
|
||||
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Multiprogramming;
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class Subject
|
||||
* @package wchat\base
|
||||
*/
|
||||
abstract class Subject extends Multiprogramming
|
||||
{
|
||||
|
||||
|
||||
private array $keywords = [];
|
||||
private string $templateId = '';
|
||||
private string $openId = '';
|
||||
private string $page = 'pages/index/index';
|
||||
private string $emphasis_keyword = '';
|
||||
private string $oac_appid = '';
|
||||
private string $use_robot = '';
|
||||
|
||||
/**
|
||||
* @param array $keywords
|
||||
*/
|
||||
public function setKeywords(array $keywords): void
|
||||
{
|
||||
$this->keywords = $keywords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $templateId
|
||||
*/
|
||||
public function setTemplateId($templateId): void
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $openId
|
||||
*/
|
||||
public function setOpenId($openId): void
|
||||
{
|
||||
$this->openId = $openId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $page
|
||||
*/
|
||||
public function setPage($page): void
|
||||
{
|
||||
$this->page = $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $emphasis_keyword
|
||||
*/
|
||||
public function setEmphasisKeyword($emphasis_keyword): void
|
||||
{
|
||||
$this->emphasis_keyword = $emphasis_keyword;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $index
|
||||
* @param $context
|
||||
* @param string $color
|
||||
*/
|
||||
public function replaceKeyword($index, $context, string $color = ''): void
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . $index] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $color
|
||||
* @param $context
|
||||
*/
|
||||
public function addKeyword($context, $color = null): void
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . (count($this->keywords) + 1)] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
abstract public function getUrl();
|
||||
|
||||
abstract public function getHost();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getOacAppid(): string
|
||||
{
|
||||
return $this->oac_appid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $oac_appid
|
||||
*/
|
||||
public function setOacAppid(string $oac_appid): void
|
||||
{
|
||||
$this->oac_appid = $oac_appid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUseRobot(): string
|
||||
{
|
||||
return $this->use_robot;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $use_robot
|
||||
*/
|
||||
public function setUseRobot(string $use_robot): void
|
||||
{
|
||||
$this->use_robot = $use_robot;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws
|
||||
*/
|
||||
public function sendTemplate(): Result
|
||||
{
|
||||
$access_token = $this->payConfig->getAccessToken();
|
||||
if (empty($access_token)) {
|
||||
throw new \Exception('request access_token con\'t null.');
|
||||
}
|
||||
$params = [
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"page" => $this->page,
|
||||
"data" => $this->keywords,
|
||||
];
|
||||
if (!empty($this->emphasis_keyword)) {
|
||||
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
||||
}
|
||||
if (!empty($this->oac_appid)) {
|
||||
$params['oac_appid'] = $this->oac_appid;
|
||||
}
|
||||
if (!empty($this->use_robot)) {
|
||||
$params['use_robot'] = $this->use_robot;
|
||||
}
|
||||
$this->reset();
|
||||
|
||||
$client = new Client($this->getHost(), 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json; charset=utf-8']);
|
||||
|
||||
$proxyHost = $this->payConfig->getProxyHost();
|
||||
$proxyPort = $this->payConfig->getProxyPort();
|
||||
if (!empty($proxyHost) && $proxyPort > 0) {
|
||||
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
|
||||
}
|
||||
|
||||
$client->post($this->getUrl() . '?access_token=' . $access_token, $params);
|
||||
$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 void
|
||||
*/
|
||||
public function reset(): void
|
||||
{
|
||||
$this->openId = '';
|
||||
$this->keywords = [];
|
||||
$this->templateId = '';
|
||||
$this->page = '';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\base;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Multiprogramming;
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class Template
|
||||
*/
|
||||
abstract class Template extends Multiprogramming
|
||||
{
|
||||
|
||||
|
||||
private array $keywords = [];
|
||||
private string $templateId = '';
|
||||
private string $formId = '';
|
||||
private string $openId = '';
|
||||
private string $page = 'pages/index/index';
|
||||
private string $emphasis_keyword = '';
|
||||
|
||||
/**
|
||||
* @param array $keywords
|
||||
*/
|
||||
public function setKeywords(array $keywords): void
|
||||
{
|
||||
$this->keywords = $keywords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $templateId
|
||||
*/
|
||||
public function setTemplateId($templateId): void
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $formId
|
||||
*/
|
||||
public function setFormId($formId): void
|
||||
{
|
||||
$this->formId = $formId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $openId
|
||||
*/
|
||||
public function setOpenId($openId): void
|
||||
{
|
||||
$this->openId = $openId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $page
|
||||
*/
|
||||
public function setPage($page): void
|
||||
{
|
||||
$this->page = $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $emphasis_keyword
|
||||
*/
|
||||
public function setEmphasisKeyword($emphasis_keyword): void
|
||||
{
|
||||
$this->emphasis_keyword = $emphasis_keyword;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $index
|
||||
* @param $context
|
||||
* @param string $color
|
||||
*/
|
||||
public function replaceKeyword($index, $context, string $color = ''): void
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . $index] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $color
|
||||
* @param $context
|
||||
*/
|
||||
public function addKeyword($context, $color = null): void
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . (count($this->keywords) + 1)] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
abstract public function getUrl();
|
||||
|
||||
abstract public function getHost();
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws
|
||||
*
|
||||
* 奴隶交易通知
|
||||
*/
|
||||
public function sendTemplate(): Result
|
||||
{
|
||||
$access_token = $this->payConfig->getAccessToken();
|
||||
if (empty($access_token)) {
|
||||
throw new \Exception('request access_token con\'t null.');
|
||||
}
|
||||
|
||||
$params = [
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"page" => $this->page,
|
||||
"form_id" => $this->formId,
|
||||
"data" => $this->keywords,
|
||||
];
|
||||
|
||||
if (!empty($this->emphasis_keyword)) {
|
||||
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
||||
}
|
||||
|
||||
$this->reset();
|
||||
|
||||
$client = new Client($this->getHost(), 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json; charset=utf-8']);
|
||||
|
||||
$proxyHost = $this->payConfig->getProxyHost();
|
||||
$proxyPort = $this->payConfig->getProxyPort();
|
||||
if (!empty($proxyHost) && $proxyPort > 0) {
|
||||
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
|
||||
}
|
||||
|
||||
$client->post($this->getUrl() . '?access_token=' . $access_token, $params);
|
||||
$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']);
|
||||
}
|
||||
return new Result(code: 0, data: $body);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function reset(): void
|
||||
{
|
||||
$this->openId = '';
|
||||
$this->keywords = [];
|
||||
$this->formId = '';
|
||||
$this->templateId = '';
|
||||
$this->page = '';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\common;
|
||||
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use wchat\common\libs\Aliyun;
|
||||
use wchat\common\libs\Qq;
|
||||
use wchat\common\libs\Wx;
|
||||
|
||||
class AppConfig
|
||||
{
|
||||
|
||||
|
||||
const int TYPE_WCHAT_PROJECT = 0;
|
||||
const int TYPE_ALI_GAME_OR_APPLET = 3;
|
||||
const int TYPE_QQ_GAME_OR_APPLET = 1;
|
||||
const int TYPE_APP_PROJECT = 2;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $appId = '';
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $host = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $appSecret = '';
|
||||
|
||||
|
||||
/**
|
||||
* @var PayConfig
|
||||
*/
|
||||
public PayConfig $pay;
|
||||
|
||||
|
||||
private string $proxyHost = '';
|
||||
private int $proxyPort = 0;
|
||||
private string $notifyUrl = '';
|
||||
private string $body = '';
|
||||
private string $currency = 'CNY';
|
||||
private string $remoteIp = '';
|
||||
private string $accessToken = '';
|
||||
private string $tradeType = 'JSAPI';
|
||||
private string $signType = 'MD5';
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public int $type;
|
||||
|
||||
|
||||
/**
|
||||
* @var array|string[]
|
||||
*/
|
||||
#[ArrayShape(['token' => 'string', 'secret' => 'string', 'unionId' => 'string'])]
|
||||
public array $notice = ['token' => '', 'secret' => '', 'unionId' => ''];
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->pay = new PayConfig();
|
||||
$this->pay->wx = new Wx();
|
||||
$this->pay->qq = new Qq();
|
||||
$this->pay->ali = new Aliyun();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param object $app
|
||||
* @return static
|
||||
*/
|
||||
public static function instance(object $app): static
|
||||
{
|
||||
$model = new static();
|
||||
$model->appId = $app->appId;
|
||||
$model->appSecret = $app->appSecret;
|
||||
$model->type = $app->type;
|
||||
|
||||
PayConfig::parse($app->pay, $model->pay);
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function typeIsApp(): bool
|
||||
{
|
||||
return $this->type === self::TYPE_APP_PROJECT;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getProxyHost(): string
|
||||
{
|
||||
return $this->proxyHost;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $proxyHost
|
||||
*/
|
||||
public function setProxyHost(string $proxyHost): void
|
||||
{
|
||||
$this->proxyHost = $proxyHost;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getProxyPort(): int
|
||||
{
|
||||
return $this->proxyPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $proxyPort
|
||||
*/
|
||||
public function setProxyPort(int $proxyPort): void
|
||||
{
|
||||
$this->proxyPort = $proxyPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getNotifyUrl(): string
|
||||
{
|
||||
return $this->notifyUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $notifyUrl
|
||||
*/
|
||||
public function setNotifyUrl(string $notifyUrl): void
|
||||
{
|
||||
$this->notifyUrl = $notifyUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBody(): string
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $body
|
||||
*/
|
||||
public function setBody(string $body): void
|
||||
{
|
||||
$this->body = $body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCurrency(): string
|
||||
{
|
||||
return $this->currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $currency
|
||||
*/
|
||||
public function setCurrency(string $currency): void
|
||||
{
|
||||
$this->currency = $currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRemoteIp(): string
|
||||
{
|
||||
return $this->remoteIp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $remoteIp
|
||||
*/
|
||||
public function setRemoteIp(string $remoteIp): void
|
||||
{
|
||||
$this->remoteIp = $remoteIp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAccessToken(): string
|
||||
{
|
||||
return $this->accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accessToken
|
||||
*/
|
||||
public function setAccessToken(string $accessToken): void
|
||||
{
|
||||
$this->accessToken = $accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTradeType(): string
|
||||
{
|
||||
return $this->tradeType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $tradeType
|
||||
*/
|
||||
public function setTradeType(string $tradeType): void
|
||||
{
|
||||
$this->tradeType = $tradeType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSignType(): string
|
||||
{
|
||||
return $this->signType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $signType
|
||||
*/
|
||||
public function setSignType(string $signType): void
|
||||
{
|
||||
$this->signType = $signType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHost(): string
|
||||
{
|
||||
return $this->host;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $host
|
||||
*/
|
||||
public function setHost(string $host): void
|
||||
{
|
||||
$this->host = $host;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/26 0026
|
||||
* Time: 10:23
|
||||
*/
|
||||
|
||||
namespace wchat\common;
|
||||
|
||||
use Kiri\Client;
|
||||
|
||||
abstract class Multiprogramming implements Progaram
|
||||
{
|
||||
protected static ?Multiprogramming $instance = null;
|
||||
|
||||
protected int $errorCode = 0;
|
||||
protected string $errorMsg = '';
|
||||
|
||||
|
||||
/**
|
||||
* @var AppConfig
|
||||
*/
|
||||
protected AppConfig $payConfig;
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param int $code
|
||||
* @return Result
|
||||
*/
|
||||
protected function sendError($message, int $code = 500): Result
|
||||
{
|
||||
return new Result(code: $code, message: $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $code
|
||||
*/
|
||||
public function setErrorCode($code): void
|
||||
{
|
||||
$this->errorCode = $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
*/
|
||||
public function setErrorMessage($message): void
|
||||
{
|
||||
$this->errorMsg = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getErrorCode(): int
|
||||
{
|
||||
return $this->errorCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getErrorMessage(): string
|
||||
{
|
||||
return $this->errorMsg;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AppConfig
|
||||
*/
|
||||
public function getPayConfig(): AppConfig
|
||||
{
|
||||
return $this->payConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AppConfig $payConfig
|
||||
*/
|
||||
public function setPayConfig(AppConfig $payConfig): void
|
||||
{
|
||||
$this->payConfig = $payConfig;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $host
|
||||
* @param string $requestUrl
|
||||
* @param mixed $body
|
||||
* @param string $contentType
|
||||
* @return Result
|
||||
*/
|
||||
protected function post(string $host, string $requestUrl, mixed $body, string $contentType = 'application/application'): Result
|
||||
{
|
||||
return $this->request($host, 'post', $requestUrl, $body, $contentType);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $host
|
||||
* @param string $requestUrl
|
||||
* @param mixed $body
|
||||
* @param string $contentType
|
||||
* @return Result
|
||||
*/
|
||||
protected function get(string $host, string $requestUrl, mixed $body, string $contentType = 'application/application'): Result
|
||||
{
|
||||
return $this->request($host, 'get', $requestUrl, $body, $contentType);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $host
|
||||
* @param string $requestUrl
|
||||
* @param mixed $body
|
||||
* @return Result
|
||||
*/
|
||||
protected function upload(string $host, string $requestUrl, mixed $body): Result
|
||||
{
|
||||
return $this->request($host, 'upload', $requestUrl, $body);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $host
|
||||
* @param string $method
|
||||
* @param string $requestUrl
|
||||
* @param $body
|
||||
* @param string $contentType
|
||||
* @return Result
|
||||
*/
|
||||
private function request(string $host, string $method, string $requestUrl, $body, string $contentType = 'application/application'): Result
|
||||
{
|
||||
$client = new Client($host, 80, true);
|
||||
$proxyHost = $this->payConfig->getProxyHost();
|
||||
$proxyPort = $this->payConfig->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();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$body = json_decode($client->getBody(), true);
|
||||
if (is_null($body) || (isset($body['errcode']) && $body['errcode'] != 0)) {
|
||||
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||
} else {
|
||||
return new Result(code: 0, data: $body);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\common;
|
||||
|
||||
use wchat\common\libs\Aliyun;
|
||||
use wchat\common\libs\Qq;
|
||||
use wchat\common\libs\Wx;
|
||||
|
||||
class PayConfig
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var Qq
|
||||
*/
|
||||
public Qq $qq;
|
||||
|
||||
|
||||
/**
|
||||
* @var Wx
|
||||
*/
|
||||
public Wx $wx;
|
||||
|
||||
|
||||
/**
|
||||
* @var Aliyun
|
||||
*/
|
||||
public Aliyun $ali;
|
||||
|
||||
|
||||
/**
|
||||
* @param string|array $pay
|
||||
* @param PayConfig|null $model
|
||||
* @return static
|
||||
*/
|
||||
public static function parse(string|array $pay, ?PayConfig $model = null): static
|
||||
{
|
||||
if (is_string($pay)) {
|
||||
$pay = json_decode($pay, true);
|
||||
}
|
||||
if ($model === null) {
|
||||
$model = new static();
|
||||
$model->ali = \Kiri::configure(new Aliyun(), $pay['ali']);
|
||||
$model->qq = \Kiri::configure(new Qq(), $pay['qq']);
|
||||
$model->wx = \Kiri::configure(new Wx(), $pay['wx']);
|
||||
} else {
|
||||
$model->ali = \Kiri::configure($model->ali, $pay['ali']);
|
||||
$model->qq = \Kiri::configure($model->qq, $pay['qq']);
|
||||
$model->wx = \Kiri::configure($model->wx, $pay['wx']);
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -57,7 +57,7 @@ class Result
|
||||
public static function init(Client $client): static
|
||||
{
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$body = json_decode($client->getBody(), true);
|
||||
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||
@@ -107,7 +107,7 @@ class Result
|
||||
* @param $key
|
||||
* @param $data
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function setAttr($key, $data): static
|
||||
{
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\common\libs;
|
||||
|
||||
class Aliyun
|
||||
{
|
||||
|
||||
// APPID
|
||||
public string $appId;
|
||||
|
||||
// APP公钥
|
||||
public string $appKey;
|
||||
|
||||
// APP私钥
|
||||
public string $appSecret;
|
||||
|
||||
// APP公钥证书
|
||||
public string $appPubSecret;
|
||||
|
||||
// 阿里云公钥证书
|
||||
public string $aliPubSecret;
|
||||
|
||||
// 阿里云根证书
|
||||
public string $aliRootSecret;
|
||||
|
||||
// 是否开启证书模式
|
||||
public int $openFileState;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\common\libs;
|
||||
|
||||
class Qq
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $appId;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $mchCa;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $mchId;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $mchKey;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $mchCert;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $appSecret;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $mchSecret;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\common\libs;
|
||||
|
||||
class Wx
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $appId;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $mchId;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $schema = 'WECHATPAY2-SHA256-RSA2048';
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $mchKey;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $secret;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $mchCert;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $appSecret;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $SerialNumber;
|
||||
|
||||
}
|
||||
+3
-5
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "wchat/wchat",
|
||||
"name": "game-worker/kiri-wchat",
|
||||
"description": "一个参数效验扩展",
|
||||
"keywords": [
|
||||
"validate"
|
||||
@@ -13,16 +13,14 @@
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"wchat\\": "./wchat/"
|
||||
"wchat\\": "./"
|
||||
},
|
||||
"files": [
|
||||
"Container.php"
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"php": ">= 8.0",
|
||||
"game-worker/kiri-client": "~v2.6",
|
||||
"game-worker/kiri-container": "~v1.9",
|
||||
"game-worker/kiri-client": "~v2.0",
|
||||
"psr/container": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/19 0019
|
||||
* Time: 16:12
|
||||
*/
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
use wchat\common\Result;
|
||||
|
||||
class Account extends SmallProgram
|
||||
{
|
||||
|
||||
/**
|
||||
* @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';
|
||||
|
||||
return $this->get('api.q.qq.com', '/sns/jscode2session', $param);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
* @return Result
|
||||
*/
|
||||
public function appLogin(string $code): Result
|
||||
{
|
||||
$param['appid'] = $this->payConfig->appId;
|
||||
$param['secret'] = $this->payConfig->appSecret;
|
||||
$param['js_code'] = $code;
|
||||
$param['grant_type'] = 'authorization_code';
|
||||
|
||||
$this->host = 'graph.qq.com';
|
||||
|
||||
return $this->get('api.q.qq.com', '/user/get_user_info', $param);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
*/
|
||||
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->payConfig->getSignType();
|
||||
$privateKey = $this->payConfig->pay->qq->mchSecret;
|
||||
$nowSign = Help::sign($params, $privateKey, $signType);
|
||||
if ($sign === $nowSign) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
*/
|
||||
public function getAppid(): mixed
|
||||
{
|
||||
return $this->appid;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Kiri\Di\Container;
|
||||
use wchat\common\AppConfig;
|
||||
|
||||
class QqFactory
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param AppConfig $config
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public static function get($class, AppConfig $config): mixed
|
||||
{
|
||||
$container = Container::instance();
|
||||
$object = $container->get($class);
|
||||
$object->setPayConfig($config);
|
||||
return $object;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/26 0026
|
||||
* Time: 10:22
|
||||
*/
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
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';
|
||||
|
||||
protected string $host = 'qpay.qq.com';
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setSpillCreateIp(string $value): void
|
||||
{
|
||||
$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;
|
||||
|
||||
return $this->post('api.q.qq.com', $this->uniformer, $this->builder(), 'application/xml');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $prepay_id
|
||||
* @return string
|
||||
*/
|
||||
public function reception(string $prepay_id): string
|
||||
{
|
||||
return Help::sign([
|
||||
'signType' => $this->payConfig->getSignType(),
|
||||
'package' => 'prepay_id=' . $prepay_id,
|
||||
'nonceStr' => Help::random(32),
|
||||
'timestamp' => time()
|
||||
], $this->payConfig->pay->qq->mchSecret, $this->payConfig->getSignType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function builder(): string
|
||||
{
|
||||
$data = [
|
||||
'appid' => $this->payConfig->appId,
|
||||
'mch_id' => $this->payConfig->pay->qq->mchId,
|
||||
'nonce_str' => Help::random(32),
|
||||
'body' => $this->payConfig->getBody(),
|
||||
'out_trade_no' => $this->orderNo,
|
||||
'total_fee' => $this->money,
|
||||
'spbill_create_ip' => $this->spill_create_ip,
|
||||
'notify_url' => $this->payConfig->getNotifyUrl(),
|
||||
'trade_type' => $this->payConfig->getTradeType(),
|
||||
];
|
||||
$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);
|
||||
}
|
||||
}
|
||||
+361
@@ -0,0 +1,361 @@
|
||||
<?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->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);
|
||||
|
||||
return $requestParam;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $requestParam
|
||||
* @return string
|
||||
*/
|
||||
private function builderSign(array $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->payConfig->pay->qq->mchKey);
|
||||
$client->withSslCertFile($this->payConfig->pay->qq->mchCert);
|
||||
$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($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 string $listid
|
||||
* @param string|null $orderNo
|
||||
* @return Result
|
||||
*/
|
||||
public function searchByOrderNo(string $listid, ?string $orderNo = null): Result
|
||||
{
|
||||
$requestParam['nonce_str'] = Help::random(31);
|
||||
$requestParam['mch_id'] = $this->payConfig->pay->qq->mchId;
|
||||
$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->payConfig->getProxyHost();
|
||||
$proxyPort = $this->payConfig->getProxyPort();
|
||||
if (!empty($proxyHost) && $proxyPort > 0) {
|
||||
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
|
||||
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->payConfig->appId,
|
||||
'media' => $real_path,
|
||||
'form-data[filename]' => $path,
|
||||
'form-data[content-type]' => $real_path->getMimeType()
|
||||
];
|
||||
return $this->upload('api.q.qq.com', $this->_url, $data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @return Result
|
||||
*/
|
||||
public function text(string $content): Result
|
||||
{
|
||||
if (empty($content)) {
|
||||
return $this->sendError('文件不存在', 404);
|
||||
}
|
||||
$url = '/' . ltrim($this->_msgUrl, '/') . $this->payConfig->getAccessToken();
|
||||
|
||||
return $this->post('api.q.qq.com', $url, ['appid' => $this->payConfig->appId, 'content' => $content]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,6 @@
|
||||
namespace wchat\qq;
|
||||
|
||||
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class Subject
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
|
||||
use wchat\common\Result;
|
||||
|
||||
class Token extends SmallProgram
|
||||
{
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
*/
|
||||
public function token(): Result
|
||||
{
|
||||
$query = [
|
||||
'grant_type' => 'client_credential',
|
||||
'appid' => $this->payConfig->appId,
|
||||
'secret' => $this->payConfig->appSecret
|
||||
];
|
||||
return $this->get('api.q.qq.com', '/api/getToken', $query);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq\pay;
|
||||
|
||||
|
||||
use Exception;
|
||||
use wchat\common\Help;
|
||||
use wchat\common\Result;
|
||||
use wchat\qq\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 string $value
|
||||
*/
|
||||
public function setMchName(string $value): void
|
||||
{
|
||||
$this->_requestParams['mch_name'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setActName(string $value): void
|
||||
{
|
||||
$this->_requestParams['act_name'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setWishing(string $value): void
|
||||
{
|
||||
$this->_requestParams['wishing'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function setIconId(int $value): void
|
||||
{
|
||||
$this->_requestParams['icon_id'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function setBannerId(int $value): void
|
||||
{
|
||||
$this->_requestParams['banner_id'] = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $mch_billno
|
||||
* @param string $openId
|
||||
* @param float $price
|
||||
* @return mixed
|
||||
*/
|
||||
public function mch_send(string $mch_billno, string $openId, float $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, 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 string $mch_billno
|
||||
* @param string $openId
|
||||
* @param float $price
|
||||
* @return array
|
||||
*/
|
||||
private function orderConfig(string $mch_billno, string $openId, float $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;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq\pay;
|
||||
|
||||
|
||||
use Exception;
|
||||
use wchat\common\Help;
|
||||
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 array $_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 array $_requestParams = [];
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setOpUserId(string $value): void
|
||||
{
|
||||
$this->_requestParams['op_user_id'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setOpUserPassword(string $value): void
|
||||
{
|
||||
$this->_requestParams['op_user_passwd'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setSpbillCreateIp(string $value): void
|
||||
{
|
||||
$this->_requestParams['spbill_create_ip'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $mch_billno
|
||||
* @param string $openId
|
||||
* @param float $price
|
||||
* @return Result
|
||||
*/
|
||||
public function mch_send(string $mch_billno, string $openId, float $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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $mch_billno
|
||||
* @param string $openId
|
||||
* @param float $price
|
||||
* @return array
|
||||
*/
|
||||
private function orderConfig(string $mch_billno, string $openId, float $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;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 int RED_HAT_STATUS_PAID = 1;
|
||||
const int RED_HAT_STATUS_SNATCHED = 2;
|
||||
const int RED_HAT_STATUS_EXPIRED = 3;
|
||||
const int RED_HAT_STATUS_REFUNDED = 4;
|
||||
|
||||
|
||||
/**
|
||||
* RedHatResult constructor.
|
||||
* @param array $array
|
||||
*/
|
||||
public function __construct(array $array)
|
||||
{
|
||||
$this->init($array);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @return void
|
||||
*/
|
||||
private function init(array $array): void
|
||||
{
|
||||
foreach ($array as $key => $value) {
|
||||
if (!property_exists($this, $key)) {
|
||||
continue;
|
||||
}
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,193 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\base;
|
||||
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Multiprogramming;
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class Subject
|
||||
* @package wchat\base
|
||||
*/
|
||||
abstract class Subject extends Multiprogramming
|
||||
{
|
||||
|
||||
|
||||
private array $keywords = [];
|
||||
private string $templateId = '';
|
||||
private string $openId = '';
|
||||
private string $page = 'pages/index/index';
|
||||
private string $emphasis_keyword = '';
|
||||
private string $oac_appid = '';
|
||||
private string $use_robot = '';
|
||||
|
||||
/**
|
||||
* @param array $keywords
|
||||
*/
|
||||
public function setKeywords(array $keywords)
|
||||
{
|
||||
$this->keywords = $keywords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $templateId
|
||||
*/
|
||||
public function setTemplateId($templateId)
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $openId
|
||||
*/
|
||||
public function setOpenId($openId)
|
||||
{
|
||||
$this->openId = $openId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $page
|
||||
*/
|
||||
public function setPage($page)
|
||||
{
|
||||
$this->page = $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $emphasis_keyword
|
||||
*/
|
||||
public function setEmphasisKeyword($emphasis_keyword)
|
||||
{
|
||||
$this->emphasis_keyword = $emphasis_keyword;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $index
|
||||
* @param $context
|
||||
* @param string $color
|
||||
*/
|
||||
public function replaceKeyword($index, $context, string $color = '')
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . $index] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $color
|
||||
* @param $context
|
||||
*/
|
||||
public function addKeyword($context, $color = null)
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . (count($this->keywords) + 1)] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
abstract public function getUrl();
|
||||
|
||||
abstract public function getHost();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getOacAppid(): string
|
||||
{
|
||||
return $this->oac_appid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $oac_appid
|
||||
*/
|
||||
public function setOacAppid(string $oac_appid): void
|
||||
{
|
||||
$this->oac_appid = $oac_appid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUseRobot(): string
|
||||
{
|
||||
return $this->use_robot;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $use_robot
|
||||
*/
|
||||
public function setUseRobot(string $use_robot): void
|
||||
{
|
||||
$this->use_robot = $use_robot;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendTemplate(): Result
|
||||
{
|
||||
$access_token = $this->config->getAccessToken();
|
||||
if (empty($access_token)) {
|
||||
throw new \Exception('request access_token con\'t null.');
|
||||
}
|
||||
$params = [
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"page" => $this->page,
|
||||
"data" => $this->keywords,
|
||||
];
|
||||
if (!empty($this->emphasis_keyword)) {
|
||||
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
||||
}
|
||||
if (!empty($this->oac_appid)) {
|
||||
$params['oac_appid'] = $this->oac_appid;
|
||||
}
|
||||
if (!empty($this->use_robot)) {
|
||||
$params['use_robot'] = $this->use_robot;
|
||||
}
|
||||
$this->reset($result);
|
||||
|
||||
$client = new Client($this->getHost(), 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json; charset=utf-8']);
|
||||
$client->post($this->getUrl() . '?access_token=' . $access_token, $params);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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 $result
|
||||
* @return mixed
|
||||
*/
|
||||
public function reset($result)
|
||||
{
|
||||
$this->openId = '';
|
||||
$this->keywords = [];
|
||||
$this->templateId = '';
|
||||
$this->page = '';
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,162 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\base;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Multiprogramming;
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class Template
|
||||
*/
|
||||
abstract class Template extends Multiprogramming
|
||||
{
|
||||
|
||||
|
||||
private array $keywords = [];
|
||||
private string $templateId = '';
|
||||
private string $formId = '';
|
||||
private string $openId = '';
|
||||
private string $page = 'pages/index/index';
|
||||
private string $emphasis_keyword = '';
|
||||
|
||||
/**
|
||||
* @param array $keywords
|
||||
*/
|
||||
public function setKeywords(array $keywords)
|
||||
{
|
||||
$this->keywords = $keywords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $templateId
|
||||
*/
|
||||
public function setTemplateId($templateId)
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $formId
|
||||
*/
|
||||
public function setFormId($formId)
|
||||
{
|
||||
$this->formId = $formId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $openId
|
||||
*/
|
||||
public function setOpenId($openId)
|
||||
{
|
||||
$this->openId = $openId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $page
|
||||
*/
|
||||
public function setPage($page)
|
||||
{
|
||||
$this->page = $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $emphasis_keyword
|
||||
*/
|
||||
public function setEmphasisKeyword($emphasis_keyword)
|
||||
{
|
||||
$this->emphasis_keyword = $emphasis_keyword;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $index
|
||||
* @param $context
|
||||
* @param $color
|
||||
*/
|
||||
public function replaceKeyword($index, $context, string $color = '')
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . $index] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $color
|
||||
* @param $context
|
||||
*/
|
||||
public function addKeyword($context, $color = null)
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . (count($this->keywords) + 1)] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
abstract public function getUrl();
|
||||
|
||||
abstract public function getHost();
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*
|
||||
* 奴隶交易通知
|
||||
*/
|
||||
public function sendTemplate(): Result
|
||||
{
|
||||
$access_token = $this->config->getAccessToken();
|
||||
if (empty($access_token)) {
|
||||
throw new \Exception('request access_token con\'t null.');
|
||||
}
|
||||
|
||||
$params = [
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"page" => $this->page,
|
||||
"form_id" => $this->formId,
|
||||
"data" => $this->keywords,
|
||||
];
|
||||
|
||||
if (!empty($this->emphasis_keyword)) {
|
||||
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
||||
}
|
||||
|
||||
$this->reset($result);
|
||||
|
||||
$client = new Client($this->getHost(), 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json; charset=utf-8']);
|
||||
$client->post($this->getUrl() . '?access_token=' . $access_token, $params);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$body = json_decode($client->getBody(), true);
|
||||
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||
}
|
||||
return new Result(code: 0, data: $body);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
* @return void
|
||||
*/
|
||||
private function reset($result): void
|
||||
{
|
||||
$this->openId = '';
|
||||
$this->keywords = [];
|
||||
$this->formId = '';
|
||||
$this->templateId = '';
|
||||
$this->page = '';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,570 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\common;
|
||||
|
||||
|
||||
class Config
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 小程序ID
|
||||
*/
|
||||
private string $appid = '';
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 商户号ID
|
||||
*/
|
||||
private string $mch_id = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 设备号
|
||||
*/
|
||||
private string $device_info = 'WEB';
|
||||
|
||||
private string $token;
|
||||
private string $encodingAesKey;
|
||||
|
||||
private string $schema = 'WECHATPAY2-SHA256-RSA2048';
|
||||
|
||||
private string $serial_no = '33F00000E4248E2EB3C61FC8384E1E155ECBB1B2';
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 随机字符串
|
||||
*/
|
||||
private string $nonce_str = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 商品简单描述
|
||||
*/
|
||||
private string $body = '好友默契Pk充值!';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 商户订单号
|
||||
*/
|
||||
private string $out_trade_no = "";
|
||||
|
||||
/**
|
||||
* @var int|float
|
||||
*
|
||||
* 金额
|
||||
*/
|
||||
private float|int $total_fee = 0;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 终端IP
|
||||
*/
|
||||
private string $spbill_create_ip = "";
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 异步回调地址
|
||||
*/
|
||||
private string $notify_url = "";
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 交易类型
|
||||
*/
|
||||
private string $trade_type = 'JSAPI';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 签名方式
|
||||
*/
|
||||
private string $sign_type = 'MD5';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 商户接口地址
|
||||
*/
|
||||
private string $mch_host = 'https://api.mch.weixin.qq.com';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $appsecret = '';
|
||||
|
||||
private string $remote_addr = '127.0.0.1';
|
||||
|
||||
private string $ssl_cert = '';
|
||||
private string $ssl_key = '';
|
||||
private string $ssl_ca = '';
|
||||
private int $port = 443;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $key = '';
|
||||
private string $access_token = '';
|
||||
private string $agent = '';
|
||||
private bool $usrSwoole = false;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSchema(): string
|
||||
{
|
||||
return $this->schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $schema
|
||||
*/
|
||||
public function setSchema(string $schema): void
|
||||
{
|
||||
$this->schema = $schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSerialNo(): string
|
||||
{
|
||||
return $this->serial_no;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $serial_no
|
||||
*/
|
||||
public function setSerialNo(string $serial_no): void
|
||||
{
|
||||
$this->serial_no = $serial_no;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAppid(): string
|
||||
{
|
||||
return $this->appid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $appid
|
||||
*/
|
||||
public function setAppid(string $appid): void
|
||||
{
|
||||
$this->appid = $appid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMchId(): string
|
||||
{
|
||||
return $this->mch_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $mch_id
|
||||
*/
|
||||
public function setMchId(string $mch_id): void
|
||||
{
|
||||
$this->mch_id = $mch_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDeviceInfo(): string
|
||||
{
|
||||
return $this->device_info;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $device_info
|
||||
*/
|
||||
public function setDeviceInfo(string $device_info): void
|
||||
{
|
||||
$this->device_info = $device_info;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getToken(): string
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $token
|
||||
*/
|
||||
public function setToken(string $token): void
|
||||
{
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEncodingAesKey(): string
|
||||
{
|
||||
return $this->encodingAesKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $encodingAesKey
|
||||
*/
|
||||
public function setEncodingAesKey(string $encodingAesKey): void
|
||||
{
|
||||
$this->encodingAesKey = $encodingAesKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getNonceStr(): string
|
||||
{
|
||||
return $this->nonce_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $nonce_str
|
||||
*/
|
||||
public function setNonceStr(string $nonce_str): void
|
||||
{
|
||||
$this->nonce_str = $nonce_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBody(): string
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $body
|
||||
*/
|
||||
public function setBody(string $body): void
|
||||
{
|
||||
$this->body = $body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getOutTradeNo(): string
|
||||
{
|
||||
return $this->out_trade_no;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $out_trade_no
|
||||
*/
|
||||
public function setOutTradeNo(string $out_trade_no): void
|
||||
{
|
||||
$this->out_trade_no = $out_trade_no;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTotalFee(): int|string
|
||||
{
|
||||
return $this->total_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $total_fee
|
||||
*/
|
||||
public function setTotalFee(int|string $total_fee): void
|
||||
{
|
||||
$this->total_fee = $total_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSpbillCreateIp(): string
|
||||
{
|
||||
return $this->spbill_create_ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $spbill_create_ip
|
||||
*/
|
||||
public function setSpbillCreateIp(string $spbill_create_ip): void
|
||||
{
|
||||
$this->spbill_create_ip = $spbill_create_ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getNotifyUrl(): string
|
||||
{
|
||||
return $this->notify_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $notify_url
|
||||
*/
|
||||
public function setNotifyUrl(string $notify_url): void
|
||||
{
|
||||
$this->notify_url = $notify_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTradeType(): string
|
||||
{
|
||||
return $this->trade_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $trade_type
|
||||
*/
|
||||
public function setTradeType(string $trade_type): void
|
||||
{
|
||||
$this->trade_type = $trade_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSignType(): string
|
||||
{
|
||||
return $this->sign_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sign_type
|
||||
*/
|
||||
public function setSignType(string $sign_type): void
|
||||
{
|
||||
$this->sign_type = $sign_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMchHost(): string
|
||||
{
|
||||
return $this->mch_host;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $mch_host
|
||||
*/
|
||||
public function setMchHost(string $mch_host): void
|
||||
{
|
||||
$this->mch_host = $mch_host;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAppsecret(): string
|
||||
{
|
||||
return $this->appsecret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $appsecret
|
||||
*/
|
||||
public function setAppsecret(string $appsecret): void
|
||||
{
|
||||
$this->appsecret = $appsecret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRemoteAddr(): string
|
||||
{
|
||||
return $this->remote_addr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $remote_addr
|
||||
*/
|
||||
public function setRemoteAddr(string $remote_addr): void
|
||||
{
|
||||
$this->remote_addr = $remote_addr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSslCert(): string
|
||||
{
|
||||
return $this->ssl_cert;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $ssl_cert
|
||||
*/
|
||||
public function setSslCert(string $ssl_cert): void
|
||||
{
|
||||
$this->ssl_cert = $ssl_cert;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSslKey(): string
|
||||
{
|
||||
return $this->ssl_key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $ssl_key
|
||||
*/
|
||||
public function setSslKey(string $ssl_key): void
|
||||
{
|
||||
$this->ssl_key = $ssl_key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSslCa(): string
|
||||
{
|
||||
return $this->ssl_ca;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $ssl_ca
|
||||
*/
|
||||
public function setSslCa(string $ssl_ca): void
|
||||
{
|
||||
$this->ssl_ca = $ssl_ca;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPort(): string
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $port
|
||||
*/
|
||||
public function setPort(string $port): void
|
||||
{
|
||||
$this->port = $port;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getKey(): string
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*/
|
||||
public function setKey(string $key): void
|
||||
{
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAccessToken(): string
|
||||
{
|
||||
return $this->access_token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $access_token
|
||||
*/
|
||||
public function setAccessToken(string $access_token): void
|
||||
{
|
||||
$this->access_token = $access_token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAgent(): string
|
||||
{
|
||||
return $this->agent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $agent
|
||||
*/
|
||||
public function setAgent(string $agent): void
|
||||
{
|
||||
$this->agent = $agent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isUsrSwoole(): bool
|
||||
{
|
||||
return $this->usrSwoole;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $usrSwoole
|
||||
*/
|
||||
public function setUsrSwoole(bool $usrSwoole): void
|
||||
{
|
||||
$this->usrSwoole = $usrSwoole;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $configs
|
||||
*/
|
||||
public function __construct(array $configs)
|
||||
{
|
||||
$this->setConfigs($configs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $configs
|
||||
* @return $this
|
||||
*/
|
||||
protected function setConfigs(array $configs): static
|
||||
{
|
||||
if (empty($configs)) {
|
||||
return $this;
|
||||
}
|
||||
foreach ($configs as $key => $val) {
|
||||
if (empty($val) || is_array($val)) {
|
||||
continue;
|
||||
}
|
||||
if (!property_exists($this, $key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->$key = $val;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/26 0026
|
||||
* Time: 10:23
|
||||
*/
|
||||
|
||||
namespace wchat\common;
|
||||
|
||||
use wchat\common\Config;
|
||||
|
||||
abstract class Multiprogramming implements Progaram
|
||||
{
|
||||
|
||||
/** @var Config */
|
||||
protected Config $config;
|
||||
|
||||
protected static ?Multiprogramming $instance = null;
|
||||
|
||||
protected int $errorCode = 0;
|
||||
protected string $errorMsg = '';
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param int $code
|
||||
* @return Result
|
||||
*/
|
||||
protected function sendError($message, int $code = 500): Result
|
||||
{
|
||||
return new Result(code: $code, message: $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $code
|
||||
*/
|
||||
public function setErrorCode($code)
|
||||
{
|
||||
$this->errorCode = $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
*/
|
||||
public function setErrorMessage($message)
|
||||
{
|
||||
$this->errorMsg = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getErrorCode(): int
|
||||
{
|
||||
return $this->errorCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getErrorMessage(): string
|
||||
{
|
||||
return $this->errorMsg;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \wchat\common\Config $config
|
||||
* @return void
|
||||
*/
|
||||
public function setConfig(Config $config): void
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return \wchat\common\Config
|
||||
*/
|
||||
public function getConfig(): \wchat\common\Config
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
* @return array|bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function checkSign($result): array|bool
|
||||
{
|
||||
$data = Help::toArray($result);
|
||||
|
||||
if (!isset($data['sign'])) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$sign = $data['sign'];
|
||||
|
||||
unset($data['sign']);
|
||||
|
||||
$key = $this->config->getKey();
|
||||
$sign_type = $this->config->getSignType();
|
||||
|
||||
$_sign = Help::sign($data, $key, $sign_type);
|
||||
if ($sign != $_sign) {
|
||||
return FALSE;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
<?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']);
|
||||
$client->get('sns/jscode2session', $param);
|
||||
$client->close();
|
||||
|
||||
return Result::init($client);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
<?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\HttpClient;
|
||||
use wchat\common\Result;
|
||||
use wchat\common\Help;
|
||||
|
||||
class Recharge extends SmallProgram
|
||||
{
|
||||
private $money = 0;
|
||||
|
||||
private $orderNo;
|
||||
|
||||
private $data = [];
|
||||
|
||||
private $spill_create_ip = '';
|
||||
|
||||
private $uniformer = 'https://qpay.qq.com/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());
|
||||
$client->post($this->uniformer);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
@@ -1,482 +0,0 @@
|
||||
<?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 $nonce_str = '';
|
||||
private string $sign = '';
|
||||
private string $mch_billno = '';
|
||||
private string $mch_id = '';
|
||||
private string $mch_name = '';
|
||||
private string $qqappid = '';
|
||||
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 $notify_url = '';
|
||||
private string $not_send_msg = '';
|
||||
private string $min_value = '';
|
||||
private string $max_value = '';
|
||||
private string $key = '';
|
||||
private string $signType = '';
|
||||
|
||||
|
||||
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 getNonceStr(): string
|
||||
{
|
||||
return $this->nonce_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $nonce_str
|
||||
*/
|
||||
public function setNonceStr(string $nonce_str): void
|
||||
{
|
||||
$this->nonce_str = $nonce_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 getMchId(): string
|
||||
{
|
||||
return $this->mch_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $mch_id
|
||||
*/
|
||||
public function setMchId(string $mch_id): void
|
||||
{
|
||||
$this->mch_id = $mch_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 getQqappid(): string
|
||||
{
|
||||
return $this->qqappid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $qqappid
|
||||
*/
|
||||
public function setQqappid(string $qqappid): void
|
||||
{
|
||||
$this->qqappid = $qqappid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 getNotifyUrl(): string
|
||||
{
|
||||
return $this->notify_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $notify_url
|
||||
*/
|
||||
public function setNotifyUrl(string $notify_url): void
|
||||
{
|
||||
$this->notify_url = $notify_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 string
|
||||
*/
|
||||
public function getKey(): string
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*/
|
||||
public function setKey(string $key): void
|
||||
{
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSignType(): string
|
||||
{
|
||||
return $this->signType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $signType
|
||||
*/
|
||||
public function setSignType(string $signType): void
|
||||
{
|
||||
$this->signType = $signType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSendUrl(): string
|
||||
{
|
||||
return $this->sendUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sendUrl
|
||||
*/
|
||||
public function setSendUrl(string $sendUrl): void
|
||||
{
|
||||
$this->sendUrl = $sendUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSearchUrl(): string
|
||||
{
|
||||
return $this->searchUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $searchUrl
|
||||
*/
|
||||
public function setSearchUrl(string $searchUrl): void
|
||||
{
|
||||
$this->searchUrl = $searchUrl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* 构建请求参数
|
||||
*/
|
||||
private function generate(): array
|
||||
{
|
||||
$requestParam = [];
|
||||
$requestParam['charset'] = $this->getCharset();
|
||||
$requestParam['nonce_str'] = $this->getNonceStr();
|
||||
$requestParam['mch_billno'] = $this->getMchBillno();
|
||||
$requestParam['mch_id'] = $this->getMchId();
|
||||
$requestParam['mch_name'] = $this->getMchName();
|
||||
$requestParam['qqappid'] = $this->getQqappid();
|
||||
$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->getNotifyUrl();
|
||||
|
||||
$requestParam['sign'] = $this->builderSign($requestParam);
|
||||
|
||||
return $requestParam;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $requestParam
|
||||
* @return string
|
||||
*/
|
||||
private function builderSign($requestParam): string
|
||||
{
|
||||
return Help::sign($requestParam, $this->getKey(), $this->getSignType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
*/
|
||||
public function redEnvelopes(): Result
|
||||
{
|
||||
$client = new Client('qpay.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($this->sendUrl, $this->generate());
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$json = json_decode($client->getBody(), true);
|
||||
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
||||
return new Result(code: 500, message: $json['return_msg'] ?? $json['retmsg']);
|
||||
}
|
||||
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'] = $this->getNonceStr();
|
||||
$requestParam['mch_id'] = $this->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']);
|
||||
$client->post($this->searchUrl, $requestParam);
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$json = json_decode($client->getBody(), true);
|
||||
if (isset($json['result']) && $json['result'] != 'SUCCESS') {
|
||||
return new Result(code: 500, message: $response['res_info'] ?? '订单查询失败!');
|
||||
} else {
|
||||
return new Result(code: 0, data: $json);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
<?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']);
|
||||
$client->upload($path, $data);
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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']);
|
||||
$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: 'network error.');
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
|
||||
use wchat\common\Multiprogramming;
|
||||
|
||||
class SmallProgram extends Multiprogramming
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?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']);
|
||||
$client->get('/api/getToken', $query);
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,177 +0,0 @@
|
||||
<?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());
|
||||
$client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$json = json_decode($client->getBody(), true);
|
||||
|
||||
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
||||
return new Result(code: 500, message: $json['return_msg'] ?? $json['retmsg']);
|
||||
} else if ($json['retcode'] != 0) {
|
||||
if (empty($json['retmsg']) && isset($this->_errors[$json['retcode']])) {
|
||||
$json['retmsg'] = $this->_errors[$json['retcode']];
|
||||
}
|
||||
return new Result(code: $json['retcode'], message: $json['retmsg']);
|
||||
} else {
|
||||
return new Result(code: 0, data: $json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,158 +0,0 @@
|
||||
<?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());
|
||||
$client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$json = json_decode($client->getBody(), true);
|
||||
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
||||
return new Result(code: 500, data: $json['return_msg'] ?? $json['retmsg']);
|
||||
} else if ($json['result_code'] != 'SUCCESS') {
|
||||
return new Result(code: 500, data: $this->_errors[$json['err_code']] ?? $json['err_code_desc']);
|
||||
} else {
|
||||
return new Result(code: 0, data: $json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,200 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/19 0019
|
||||
* Time: 16:12
|
||||
*/
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Decode;
|
||||
use wchat\common\HttpClient;
|
||||
use wchat\common\Result;
|
||||
|
||||
class Account extends SmallProgram
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $code
|
||||
* @return Result
|
||||
*/
|
||||
public function login($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.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->get('sns/jscode2session', $param);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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 $openid
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPublicUserInfo($openid): Result
|
||||
{
|
||||
$query = [
|
||||
'access_token' => $this->config->getAccessToken(),
|
||||
'openid' => $openid,
|
||||
'lang' => 'zh_CN'
|
||||
];
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->get('cgi-bin/user/info', $query);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$body = json_decode($client->getBody(), true);
|
||||
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||
}
|
||||
return new Result(code: 0, data: $body);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $encryptedData
|
||||
* @param $iv
|
||||
* @param $sessionKey
|
||||
* @param bool $asArray
|
||||
* @return object|array
|
||||
* @throws
|
||||
*
|
||||
* * <li>-41001: encodingAesKey 非法</li>
|
||||
* <li>-41003: aes 解密失败</li>
|
||||
* <li>-41004: 解密后得到的buffer非法</li>
|
||||
* <li>-41005: base64加密失败</li>
|
||||
* <li>-41016: base64解密失败</li>
|
||||
*/
|
||||
public function decode($encryptedData, $iv, $sessionKey, bool $asArray = false): object|array
|
||||
{
|
||||
$decode = new Decode();
|
||||
$decode->setSessionKey($sessionKey);
|
||||
$decode->setEncryptedData($encryptedData);
|
||||
$decode->setAppId($this->config->getAppid());
|
||||
$decode->setIv($iv);
|
||||
|
||||
return $decode->decode($asArray);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $width
|
||||
* @return array|mixed|Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function createwxaqrcode($path, $width): mixed
|
||||
{
|
||||
$url = 'cgi-bin/wxaapp/createwxaqrcode?access_token=';
|
||||
$sendBody['path'] = $path;
|
||||
$sendBody['width'] = $width;
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$body = json_decode($client->getBody(), true);
|
||||
if (!is_null($body)) {
|
||||
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||
} else {
|
||||
return new Result(code: 0, data: $client->getBody());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $width
|
||||
* @param bool $is_hyaline
|
||||
* @param bool $auto_color
|
||||
* @param string $line_color
|
||||
* @return Result
|
||||
*/
|
||||
public function getwxacode($path, $width, bool $is_hyaline = false, bool $auto_color = false, string $line_color = ''): Result
|
||||
{
|
||||
$sendBody['path'] = $path;
|
||||
$sendBody['width'] = $width;
|
||||
$sendBody['auto_color'] = $auto_color;
|
||||
$sendBody['is_hyaline'] = $is_hyaline;
|
||||
if ($auto_color) {
|
||||
$sendBody['line_color'] = $line_color;
|
||||
}
|
||||
|
||||
$url = 'wxa/getwxacode?access_token=' . $this->getConfig()->getAccessToken();
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$body = json_decode($client->getBody(), true);
|
||||
if (!is_null($body)) {
|
||||
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||
} else {
|
||||
return new Result(code: 0, data: $client->getBody());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $width
|
||||
* @param bool $is_hyaline
|
||||
* @param bool $auto_color
|
||||
* @param string $line_color
|
||||
* @return Result
|
||||
*/
|
||||
public function getwxacodeunlimit($path, $width, bool $is_hyaline = false, bool $auto_color = false, string $line_color = ''): Result
|
||||
{
|
||||
$sendBody['path'] = $path;
|
||||
$sendBody['width'] = $width;
|
||||
$sendBody['auto_color'] = $auto_color;
|
||||
$sendBody['is_hyaline'] = $is_hyaline;
|
||||
if ($auto_color) {
|
||||
$sendBody['line_color'] = $line_color;
|
||||
}
|
||||
|
||||
$url = 'wxa/getwxacodeunlimit?access_token=' . $this->getConfig()->getAccessToken();
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$body = json_decode($client->getBody(), true);
|
||||
if (!is_null($body)) {
|
||||
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||
} else {
|
||||
return new Result(code: 0, data: $client->getBody());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,315 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
|
||||
/**
|
||||
* Class Message
|
||||
* @package wchat\wx
|
||||
*/
|
||||
class Message extends SmallProgram
|
||||
{
|
||||
|
||||
private array $msgData = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $openid
|
||||
*/
|
||||
public function setOpenid($openid)
|
||||
{
|
||||
$this->msgData['touser'] = $openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendTextNews($content): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'text';
|
||||
$this->msgData['text'] = ['content' => $content];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendImageNews($media_id): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'image';
|
||||
$this->msgData['image'] = ['media_id' => $media_id];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendVoiceNews($media_id): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'voice';
|
||||
$this->msgData['voice'] = ['media_id' => $media_id];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendMpNewsNews($media_id): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'mpnews';
|
||||
$this->msgData['mpnews'] = ['media_id' => $media_id];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @param $description
|
||||
* @param $url
|
||||
* @param $picurl
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendNewsNews($title, $description, $url, $picurl): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'news';
|
||||
$this->msgData['news'] = [
|
||||
'articles' => [
|
||||
[
|
||||
'title' => $title,
|
||||
'description' => $description,
|
||||
'url' => $url,
|
||||
'picurl' => $picurl
|
||||
]
|
||||
]
|
||||
];
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendCardNews($title): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'wxcard';
|
||||
$this->msgData['wxcard'] = ['card_id' => $title];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @param $thumb_media_id
|
||||
* @param $title
|
||||
* @param $description
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendVideoNews($media_id, $thumb_media_id, $title, $description): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'video';
|
||||
$this->msgData['video'] = [
|
||||
'media_id' => [
|
||||
'media_id' => $media_id,
|
||||
'thumb_media_id' => $thumb_media_id,
|
||||
'title' => $title,
|
||||
'description' => $description
|
||||
]
|
||||
];
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $musicurl
|
||||
* @param $hqmusicurl
|
||||
* @param $thumb_media_id
|
||||
* @param $title
|
||||
* @param $description
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendMusicNews($musicurl, $hqmusicurl, $thumb_media_id, $title, $description): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'music';
|
||||
$this->msgData['music'] = [
|
||||
'title' => $title,
|
||||
'description' => $description,
|
||||
'musicurl' => $musicurl,
|
||||
'hqmusicurl' => $hqmusicurl,
|
||||
'thumb_media_id' => $thumb_media_id
|
||||
];
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $head_content
|
||||
* @param $tail_content
|
||||
* @param array $menus
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendMenuNews($head_content, $tail_content, array $menus = []): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'msgmenu';
|
||||
$this->msgData['msgmenu'] = [
|
||||
'head_content' => $head_content,
|
||||
'tail_content' => $tail_content,
|
||||
];
|
||||
if (empty($menus) || !is_array($menus) || count($menus) < 2) {
|
||||
throw new Exception('菜单选项必须有2个');
|
||||
}
|
||||
foreach ($menus as $val) {
|
||||
$this->addNewsMenu($val['id'], $val['name']);
|
||||
}
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
private int $index = 0;
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $menuName
|
||||
* @return $this
|
||||
*/
|
||||
public function addNewsMenu($id, $menuName): static
|
||||
{
|
||||
$lists['id'] = $id;
|
||||
$lists['content'] = $menuName;
|
||||
$this->msgData['msgmenu']['list'][$this->index] = $lists;
|
||||
++$this->index;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @param $appid
|
||||
* @param $pagepath
|
||||
* @param $thumb_media_id
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendMiniprogrampageNews($title, $appid, $pagepath, $thumb_media_id): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'msgmenu';
|
||||
$this->msgData['miniprogrampage'] = [
|
||||
'title' => $title,
|
||||
'appid' => $appid,
|
||||
'pagepath' => $pagepath,
|
||||
'thumb_media_id' => $thumb_media_id,
|
||||
];
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filePath
|
||||
* @param $type
|
||||
* @param bool $isPermanent
|
||||
* @param string $title
|
||||
* @param string $introduction
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function upload($filePath, $type, bool $isPermanent = false, string $title = '', string $introduction = ''): Result
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
throw new Exception('文件不存在');
|
||||
}
|
||||
|
||||
if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
|
||||
throw new Exception('暂不支持的文件类型');
|
||||
}
|
||||
|
||||
$token = $this->config->getAccessToken();
|
||||
if ($isPermanent) {
|
||||
$url = "/cgi-bin/material/add_material?access_token={$token}&type={$type}";
|
||||
} else {
|
||||
$url = "/cgi-bin/media/upload?access_token={$token}&type={$type}";
|
||||
}
|
||||
|
||||
$mime = mime_content_type($filePath);
|
||||
|
||||
$real_path = new \CURLFile(realpath($filePath));
|
||||
|
||||
$data = ["media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[Content-Type]' => $mime];
|
||||
if ($isPermanent && $mime == 'video/mp3') {
|
||||
$data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction];
|
||||
}
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'multipart/form-data']);
|
||||
$client->post($url, $data);
|
||||
$client->close();
|
||||
|
||||
$this->msgData = [];
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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 array
|
||||
*/
|
||||
public function getContents(): array
|
||||
{
|
||||
return $this->msgData;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
private function sendKefuMsg()
|
||||
{
|
||||
$url = '/cgi-bin/message/custom/send?access_token=' . $this->config->getAccessToken();
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($url, $this->msgData);
|
||||
$client->close();
|
||||
|
||||
$this->msgData = [];
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/8 0008
|
||||
* Time: 9:49
|
||||
*/
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
class PublicTemplate extends SmallProgram
|
||||
{
|
||||
|
||||
private array $keywords = [];
|
||||
private string $templateId = '';
|
||||
private array $first = [];
|
||||
private array $remark = [];
|
||||
private string $openId = '';
|
||||
private string $defaultUrl = 'http://weixin.qq.com/download';
|
||||
private string $sendUrl = '/cgi-bin/message/template/send';
|
||||
private array $miniprogram = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param array $keywords
|
||||
*/
|
||||
public function setKeywords(array $keywords)
|
||||
{
|
||||
$this->keywords = $keywords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $templateId
|
||||
*/
|
||||
public function setTemplateId($templateId)
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $openId
|
||||
*/
|
||||
public function setOpenId($openId)
|
||||
{
|
||||
$this->openId = $openId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $defaultUrl
|
||||
*/
|
||||
public function setDefaultUrl($defaultUrl)
|
||||
{
|
||||
$this->defaultUrl = $defaultUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $context
|
||||
* @param string $color
|
||||
*/
|
||||
public function replaceKeyword($name, $context, string $color = '')
|
||||
{
|
||||
$this->keywords[$name] = ['value' => $context, 'color' => $color];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $context
|
||||
* @param null $color
|
||||
*/
|
||||
public function addKeyword($name, $context, $color = null)
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords[$name] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $context
|
||||
* @param string $color
|
||||
* @return void
|
||||
*/
|
||||
public function setFirst($context, string $color = '#f00'): void
|
||||
{
|
||||
$this->first = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $context
|
||||
* @param string $color
|
||||
* @return void
|
||||
*/
|
||||
public function setRemark($context, string $color = '#000'): void
|
||||
{
|
||||
$this->remark = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $appid
|
||||
* @param string $pagepath
|
||||
* @return void
|
||||
*/
|
||||
public function setMiniprogram($appid, string $pagepath): void
|
||||
{
|
||||
$this->miniprogram = [
|
||||
'appid' => $appid,
|
||||
'pagepath' => $pagepath
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*
|
||||
* 奴隶交易通知
|
||||
*/
|
||||
public function sendTemplate(): Result
|
||||
{
|
||||
$url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken();
|
||||
|
||||
$keywords = $this->keywords;
|
||||
$keywords['first'] = $this->first;
|
||||
$keywords['remark'] = $this->remark;
|
||||
|
||||
$default = [
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"url" => $this->defaultUrl,
|
||||
"data" => $keywords,
|
||||
];
|
||||
if (!empty($this->miniprogram)) {
|
||||
$default['miniprogram'] = $this->miniprogram;
|
||||
}
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($url, $default);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class SecCheck
|
||||
* @package wchat
|
||||
*/
|
||||
class SecCheck extends SmallProgram
|
||||
{
|
||||
|
||||
private string $_url = '/wxa/img_sec_check?access_token=';
|
||||
|
||||
private string $_msgUrl = '/wxa/msg_sec_check?access_token=';
|
||||
|
||||
private string $_mediaCheckAsync = '/wxa/media_check_async?access_token=';
|
||||
|
||||
const MEDIA_VIDEO = 1;
|
||||
const MEDIA_IMAGE = 1;
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return Result
|
||||
*/
|
||||
public function image(string $path = ''): Result
|
||||
{
|
||||
if (!file_exists($path)) {
|
||||
return $this->sendError('文件不存在', 404);
|
||||
}
|
||||
$access_token = $this->config->getAccessToken();
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'multipart/form-data']);
|
||||
$client->upload($this->_url . '?access_token=' . $access_token, [
|
||||
'media' => new \CURLFile($path)
|
||||
]);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$body = json_decode($client->getBody(), true);
|
||||
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||
}
|
||||
return new Result(code: 0, data: $body);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param int $type
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public function mediaAsync(string $url, int $type = SecCheck::MEDIA_IMAGE): Result
|
||||
{
|
||||
if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) {
|
||||
throw new \Exception('暂不支持的文件类型');
|
||||
}
|
||||
$requestUrl = $this->_mediaCheckAsync . $this->config->getAccessToken();
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($requestUrl, ['media_url' => $url, 'media_type' => $type]);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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 $params
|
||||
* @return ContentAsyncCheck|null
|
||||
*/
|
||||
public function readByEvent($params): ?ContentAsyncCheck
|
||||
{
|
||||
return ContentAsyncCheck::instance($params);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
* @return Result
|
||||
*/
|
||||
public function text($content): Result
|
||||
{
|
||||
if (empty($content)) {
|
||||
return $this->sendError('文件不存在', 404);
|
||||
}
|
||||
$requestUrl = $this->_msgUrl . $this->config->getAccessToken();
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($requestUrl, ['content' => $content]);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
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.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->get('cgi-bin/token', $query);
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$body = json_decode($client->getBody(), true);
|
||||
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||
}
|
||||
return new Result(code: 0, data: $body);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V2;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Help;
|
||||
use wchat\common\Result;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV2AppPayment extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
private string $uniformed = '/pay/unifiedorder';
|
||||
|
||||
use WxV2PayTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $money
|
||||
* @param string $orderNo
|
||||
* @param string $spbill_create_ip
|
||||
* @return Result
|
||||
*/
|
||||
public function payment(int $money, string $orderNo, string $spbill_create_ip = '127.0.0.1'): Result
|
||||
{
|
||||
if ($money < 0) {
|
||||
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||
}
|
||||
|
||||
$body = $this->getInitCore($orderNo, $money);
|
||||
$body['trade_type'] = 'APP';
|
||||
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withBody($this->sign($body));
|
||||
$client->post($this->uniformed);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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 $this->sign([
|
||||
'appId' => $this->config->getAppid(),
|
||||
'partnerid' => $this->config->getMchId(),
|
||||
'prepayid' => $prepay_id,
|
||||
'package' => 'Sign=WXPay',
|
||||
'noncestr' => Help::random(32),
|
||||
'timestamp' => time()
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,210 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V2;
|
||||
|
||||
use Kiri\Client;
|
||||
use Phalcon\Cache\Frontend\Json;
|
||||
use wchat\common\Help;
|
||||
use wchat\common\Result;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV2PayJsApi extends SmallProgram
|
||||
{
|
||||
|
||||
private string $uniformed = '/pay/unifiedorder';
|
||||
|
||||
use WxV2PayTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $money
|
||||
* @param string $orderNo
|
||||
* @param string $openId
|
||||
* @param string $spbill_create_ip
|
||||
* @return Result
|
||||
*/
|
||||
public function applet(int $money, string $orderNo, string $openId, string $spbill_create_ip = '127.0.0.1'): Result
|
||||
{
|
||||
if ($money < 0) {
|
||||
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||
}
|
||||
|
||||
$body = $this->getInitCore($orderNo, $money);
|
||||
$body['trade_type'] = 'JSAPI';
|
||||
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||
$body['openid'] = $openId;
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withBody($this->sign($body));
|
||||
$client->post($this->uniformed);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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 int $money
|
||||
* @param string $orderNo
|
||||
* @param string $app_name
|
||||
* @param string $package_name
|
||||
* @param string $spbill_create_ip
|
||||
* @return Result
|
||||
*/
|
||||
public function h5Android(int $money, string $orderNo, string $app_name, string $package_name, string $spbill_create_ip = '127.0.0.1'): Result
|
||||
{
|
||||
if ($money < 0) {
|
||||
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||
}
|
||||
|
||||
$body = $this->getInitCore($orderNo, $money);
|
||||
$body['trade_type'] = 'MWEB';
|
||||
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||
$body['scene_info'] = json_encode([
|
||||
'h5_info' => [
|
||||
'type' => 'Android',
|
||||
'app_name' => $app_name,
|
||||
'package_name' => $package_name
|
||||
]
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withBody($this->sign($body));
|
||||
$client->post($this->uniformed);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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 int $money
|
||||
* @param string $orderNo
|
||||
* @param string $app_name
|
||||
* @param string $bundle_id
|
||||
* @param string $spbill_create_ip
|
||||
* @return Result
|
||||
*/
|
||||
public function h5Ios(int $money, string $orderNo, string $app_name, string $bundle_id, string $spbill_create_ip = '127.0.0.1'): Result
|
||||
{
|
||||
if ($money < 0) {
|
||||
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||
}
|
||||
|
||||
$body = $this->getInitCore($orderNo, $money);
|
||||
$body['trade_type'] = 'MWEB';
|
||||
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||
$body['scene_info'] = json_encode([
|
||||
'h5_info' => [
|
||||
'type' => 'IOS',
|
||||
'app_name' => $app_name,
|
||||
'bundle_id' => $bundle_id
|
||||
]
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withBody($this->sign($body));
|
||||
$client->post($this->uniformed);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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 int $money
|
||||
* @param string $orderNo
|
||||
* @param string $wap_url
|
||||
* @param string $wap_name
|
||||
* @param string $spbill_create_ip
|
||||
* @return Result
|
||||
*/
|
||||
public function h5(int $money, string $orderNo, string $wap_url, string $wap_name, string $spbill_create_ip = '127.0.0.1'): Result
|
||||
{
|
||||
if ($money < 0) {
|
||||
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||
}
|
||||
|
||||
$body = $this->getInitCore($orderNo, $money);
|
||||
$body['trade_type'] = 'MWEB';
|
||||
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||
$body['scene_info'] = json_encode([
|
||||
'h5_info' => [
|
||||
'type' => 'IOS',
|
||||
'wap_url' => $wap_url,
|
||||
'wap_name' => $wap_name
|
||||
]
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withBody($this->sign($body));
|
||||
$client->post($this->uniformed);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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 $this->sign([
|
||||
'signType' => $this->config->getSignType(),
|
||||
'package' => 'prepay_id=' . $prepay_id,
|
||||
'nonceStr' => Help::random(32),
|
||||
'timestamp' => time()
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V2;
|
||||
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use wchat\common\Help;
|
||||
|
||||
trait WxV2PayTait
|
||||
{
|
||||
|
||||
/**
|
||||
* 'appId' => $result['appid'],
|
||||
* 'nonceStr' => $result['nonce_str'],
|
||||
* 'package' => 'prepay_id=' . $result['prepay_id'],
|
||||
* 'signType' => 'MD5',
|
||||
* 'timeStamp' => (string)time(),
|
||||
* @param $prepay_id
|
||||
* @return array
|
||||
*/
|
||||
#[ArrayShape(['appId' => "string", 'nonceStr' => "string", 'package' => "string", 'signType' => "string", 'timeStamp' => "string", 'paySign' => "string"])]
|
||||
public function reception($prepay_id): array
|
||||
{
|
||||
$array = [
|
||||
'appId' => $this->config->getAppid(),
|
||||
'nonceStr' => Help::random(32),
|
||||
'package' => 'prepay_id=' . $prepay_id,
|
||||
'signType' => 'MD5',
|
||||
'timeStamp' => (string)time(),
|
||||
];
|
||||
$key = $this->config->getKey();
|
||||
$sign_type = $this->config->getSignType();
|
||||
$array['paySign'] = Help::sign($array, $key, $sign_type);
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $orderNo
|
||||
* @param float|int $total
|
||||
* @return array
|
||||
*/
|
||||
#[ArrayShape(['appid' => "string", 'mch_id' => "string", 'nonce_str' => "string", 'body' => "string", 'out_trade_no' => "string", 'total_fee' => "float|int", 'spbill_create_ip' => "mixed", 'notify_url' => "string", 'trade_type' => "string"])]
|
||||
protected function getInitCore(string $orderNo, float|int $total): array
|
||||
{
|
||||
return [
|
||||
'appid' => $this->config->getAppid(),
|
||||
'mch_id' => $this->config->getMchId(),
|
||||
'nonce_str' => Help::random(32),
|
||||
'body' => $this->config->getBody(),
|
||||
'out_trade_no' => $orderNo,
|
||||
'total_fee' => $total,
|
||||
'notify_url' => $this->config->getNotifyUrl(),
|
||||
'trade_type' => $this->config->getTradeType(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
protected function sign(array $data): string
|
||||
{
|
||||
$key = $this->config->getKey();
|
||||
$sign_type = $this->config->getSignType();
|
||||
|
||||
$data['sign'] = Help::sign($data, $key, $sign_type);
|
||||
|
||||
return Help::toXml($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $orderNo
|
||||
* @param int|float $money
|
||||
* @param string $openid
|
||||
* @return string
|
||||
*/
|
||||
protected function builder(string $orderNo, int|float $money, string $openid): string
|
||||
{
|
||||
$data = [
|
||||
'appid' => $this->config->getAppid(),
|
||||
'mch_id' => $this->config->getMchId(),
|
||||
'nonce_str' => Help::random(32),
|
||||
'body' => $this->config->getBody(),
|
||||
'out_trade_no' => $orderNo,
|
||||
'total_fee' => $money,
|
||||
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
|
||||
'notify_url' => $this->config->getNotifyUrl(),
|
||||
'trade_type' => $this->config->getTradeType(),
|
||||
'openid' => $openid
|
||||
];
|
||||
|
||||
$key = $this->config->getKey();
|
||||
$sign_type = $this->config->getSignType();
|
||||
|
||||
$data['sign'] = Help::sign($data, $key, $sign_type);
|
||||
|
||||
return Help::toXml($data);
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V2;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Help;
|
||||
use wchat\common\Result;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV2Withdrawal extends SmallProgram
|
||||
{
|
||||
|
||||
private string $transfers = '/mmpaymkttransfers/promotion/transfers';
|
||||
|
||||
/**
|
||||
* @param int|float $money
|
||||
* @param string $openid
|
||||
* @param string $order
|
||||
* @param string $desc
|
||||
* @return Result
|
||||
*/
|
||||
public function payment(int|float $money, string $openid, string $order, string $desc = '零钱提现'): Result
|
||||
{
|
||||
$array = [
|
||||
'nonce_str' => Help::random(32),
|
||||
'partner_trade_no' => $order,
|
||||
'mchid' => $this->config->getMchId(),
|
||||
'mch_appid' => $this->config->getAppid(),
|
||||
'openid' => $openid,
|
||||
'check_name' => 'NO_CHECK',
|
||||
'amount' => $money * 100,
|
||||
'spbill_create_ip' => $this->config->getRemoteAddr(),
|
||||
'desc' => $desc,
|
||||
];
|
||||
|
||||
$key = $this->config->getKey();
|
||||
$sign_type = $this->config->getSignType();
|
||||
$array['sign'] = Help::sign($array, $key, $sign_type);
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withBody($body = Help::toXml($array));
|
||||
$client->post($this->transfers);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
|
||||
$data = Help::toArray($client->getBody());
|
||||
|
||||
$data['body'] = $body;
|
||||
if ($data['return_code'] == 'FAIL') {
|
||||
return new Result(code: $array['return_code'], message: $data['return_msg'], data: $data);
|
||||
} else if ($array['result_code'] != 'SUCCESS') {
|
||||
return new Result(code: $array['err_code'], message: $data['err_code_des'], data: $data);
|
||||
} else {
|
||||
return new Result(code: 0, message: '提现成功', data: $data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
|
||||
class TransferDetail
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
public string $out_detail_no,
|
||||
public int|float $transfer_amount,
|
||||
public string $transfer_remark,
|
||||
public string $openid,
|
||||
public string $user_name
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[ArrayShape(['out_detail_no' => "string", 'transfer_amount' => "float|int", 'transfer_remark' => "string", 'openid' => "string", 'user_name' => "string"])]
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'out_detail_no' => $this->out_detail_no,
|
||||
'transfer_amount' => $this->transfer_amount,
|
||||
'transfer_remark' => $this->transfer_remark,
|
||||
'openid' => $this->openid,
|
||||
'user_name' => $this->user_name,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV3AppPayment extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param $orderNo
|
||||
* @param int $total
|
||||
* @param string|null $openId
|
||||
* @param string $payer_client_ip
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function payment($orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
|
||||
{
|
||||
$body = $this->getInitCore($orderNo, $total);
|
||||
|
||||
$body['scene_info'] = ['payer_client_ip' => $payer_client_ip];
|
||||
|
||||
$sign = $this->signature('POST', '/v3/pay/transactions/components', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, TRUE);
|
||||
$client->withAddedHeader('Authorization', $sign)->withContentType('application/json')
|
||||
->withAgent('application/json')->withBody($json)
|
||||
->post('/v3/pay/transactions/components');
|
||||
$client->close();
|
||||
|
||||
$json = json_decode($client->getBody(), TRUE);
|
||||
if (!isset($json['prepay_id'])) {
|
||||
throw new Exception('微信支付调用失败');
|
||||
}
|
||||
return $this->createResponse($json, $body);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use Kiri\Message\Stream;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV3Payment extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param $orderNo
|
||||
* @param int $total
|
||||
* @param string|null $openId
|
||||
* @param string $payer_client_ip
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function payment($orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
|
||||
{
|
||||
$body = $this->getInitCore($orderNo, $total);
|
||||
$body['payer'] = ['openid' => $openId];
|
||||
$body['scene_info'] = ['payer_client_ip' => $payer_client_ip];
|
||||
|
||||
$sign = $this->signature('POST', '/v3/pay/transactions/jsapi', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, TRUE);
|
||||
$client->withAddedHeader('Authorization', $sign)
|
||||
->withContentType('application/json')
|
||||
->withAddedHeader('User-Agent', 'application/json')
|
||||
->withBody(new Stream($json))
|
||||
->post('/v3/pay/transactions/jsapi');
|
||||
$client->close();
|
||||
|
||||
$json = json_decode($client->getBody(), TRUE);
|
||||
if (!isset($json['prepay_id'])) {
|
||||
throw new Exception('微信支付调用失败');
|
||||
}
|
||||
|
||||
return $this->createResponse($json, $body);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use wchat\common\Help;
|
||||
use function Sodium\crypto_aead_aes256gcm_decrypt;
|
||||
use function Sodium\crypto_aead_aes256gcm_is_available;
|
||||
|
||||
const KEY_LENGTH_BYTE = 32;
|
||||
const AUTH_TAG_LENGTH_BYTE = 16;
|
||||
|
||||
trait WxV3PaymentTait
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $orderNo
|
||||
* @param $total
|
||||
* @return array
|
||||
*/
|
||||
public function getInitCore($orderNo, $total): array
|
||||
{
|
||||
$body['appid'] = $this->getConfig()->getAppid();
|
||||
$body['mchid'] = $this->getConfig()->getMchId();
|
||||
$body['description'] = $this->getConfig()->getBody();
|
||||
$body['out_trade_no'] = $orderNo;
|
||||
$body['notify_url'] = $this->getConfig()->getNotifyUrl();
|
||||
$body['amount'] = ['total' => $total, 'currency' => 'CNY'];
|
||||
return $body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $http_method
|
||||
* @param string $canonical_url
|
||||
* @param string $body
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function signature(string $http_method, string $canonical_url, string $body = ''): string
|
||||
{
|
||||
$message = $http_method . "\n" . $canonical_url . "\n" . ($time = time()) . "\n" . ($rand = md5(random_bytes(32))) . "\n" . $body . "\n";
|
||||
|
||||
$sign = $this->openssl_signature($message);
|
||||
|
||||
return sprintf('%s mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $this->getConfig()->getSchema(),
|
||||
$this->getConfig()->getMchId(), $rand, $time, $this->getConfig()->getSerialNo(), $sign);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $body
|
||||
* @return string
|
||||
*/
|
||||
public function openssl_signature($body): string
|
||||
{
|
||||
$pem = file_get_contents($this->getConfig()->getSslCert());
|
||||
|
||||
$mch_private_key = openssl_get_privatekey($pem);
|
||||
|
||||
openssl_sign($body, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
|
||||
return base64_encode($raw_sign);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $json
|
||||
* @param $body
|
||||
* @return array
|
||||
*/
|
||||
private function createResponse($json, $body): array
|
||||
{
|
||||
$responseArray['appId'] = $body['appid'];
|
||||
$responseArray['timeStamp'] = (string)time();
|
||||
$responseArray['nonceStr'] = Help::random(32);
|
||||
$responseArray['package'] = "prepay_id=" . $json['prepay_id'];
|
||||
|
||||
$responseBody = $responseArray['appId'] . PHP_EOL . $responseArray['timeStamp'] . PHP_EOL . $responseArray['nonceStr'] . PHP_EOL . $responseArray['package'] . PHP_EOL;
|
||||
|
||||
$responseArray['signType'] = 'RSA';
|
||||
$responseArray['signBody'] = $responseBody;
|
||||
$responseArray['paySign'] = $this->openssl_signature($responseBody);
|
||||
$responseArray['prepay_id'] = $json['prepay_id'];
|
||||
|
||||
return $responseArray;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $associatedData
|
||||
* @param $nonceStr
|
||||
* @param $ciphertext
|
||||
* @return bool|string
|
||||
*/
|
||||
public function decryptToString($associatedData, $nonceStr, $ciphertext): bool|string
|
||||
{
|
||||
$ciphertext = \base64_decode($ciphertext);
|
||||
if (strlen($ciphertext) <= AUTH_TAG_LENGTH_BYTE) {
|
||||
return FALSE;
|
||||
}
|
||||
if (function_exists('\sodium\crypto_aead_aes256gcm_is_available') && crypto_aead_aes256gcm_is_available()) {
|
||||
return crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, 'XGwwZbmMXy6sD5w0IrxfaBHLl7b7jCaR');
|
||||
}
|
||||
if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') && crypto_aead_aes256gcm_is_available()) {
|
||||
return crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, 'XGwwZbmMXy6sD5w0IrxfaBHLl7b7jCaR');
|
||||
}
|
||||
if (PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())) {
|
||||
$ctext = substr($ciphertext, 0, -AUTH_TAG_LENGTH_BYTE);
|
||||
$authTag = substr($ciphertext, -AUTH_TAG_LENGTH_BYTE);
|
||||
return \openssl_decrypt($ctext, 'aes-256-gcm', 'XGwwZbmMXy6sD5w0IrxfaBHLl7b7jCaR', \OPENSSL_RAW_DATA, $nonceStr, $authTag, $associatedData);
|
||||
}
|
||||
throw new \RuntimeException('AEAD_AES_256_GCM需要PHP 7.1以上或者安装libsodium-php');
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use Kiri\Client;
|
||||
use Kiri\Message\Stream;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV3Withdrawal extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param $orderNo
|
||||
* @param string $batch_name
|
||||
* @param string $batch_remark
|
||||
* @param TransferDetail[] $details
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function payment($orderNo, string $batch_name, string $batch_remark, array $details): array
|
||||
{
|
||||
$body = $this->create($orderNo, $batch_name, $batch_remark, $details);
|
||||
|
||||
$sign = $this->signature('POST', '/v3/pay/transactions/batches', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, TRUE);
|
||||
$client->withAddedHeader('Authorization', $sign)
|
||||
->withContentType('application/json')
|
||||
->withAddedHeader('User-Agent', 'application/json')
|
||||
->withBody(new Stream($json))
|
||||
->post('/v3/pay/transactions/batches');
|
||||
$client->close();
|
||||
|
||||
$json = json_decode($client->getBody(), TRUE);
|
||||
if (!isset($json['prepay_id'])) {
|
||||
throw new Exception('微信支付调用失败');
|
||||
}
|
||||
|
||||
return $this->createResponse($json, $body);
|
||||
}
|
||||
|
||||
|
||||
#[ArrayShape(['transfer_detail_list' => "array", 'total_amount' => "int", 'total_num' => "int", 'batch_remark' => "string", 'batch_name' => "string", 'out_batch_no' => ""])]
|
||||
private function create($orderNo, string $batch_name, string $batch_remark, array $details): array
|
||||
{
|
||||
$total = 0;
|
||||
$body = ['transfer_detail_list' => []];
|
||||
$body['out_batch_no'] = $orderNo;
|
||||
$body['batch_name'] = $batch_name;
|
||||
$body['batch_remark'] = $batch_remark;
|
||||
$body['total_num'] = count($details);
|
||||
foreach ($details as $detail) {
|
||||
$total += $detail->transfer_amount;
|
||||
$body['transfer_detail_list'][] = $detail->toArray();
|
||||
}
|
||||
$body['total_amount'] = $total;
|
||||
return $body;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Kiri\Di\Container;
|
||||
use ReflectionException;
|
||||
use wchat\common\Config;
|
||||
|
||||
class WxFactory
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param Config $config
|
||||
* @return object|null
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public static function get($class, Config $config): ?object
|
||||
{
|
||||
$container = Container::instance();
|
||||
$object = $container->get($class);
|
||||
$object->setConfig($config);
|
||||
return $object;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/19 0019
|
||||
* Time: 16:12
|
||||
*/
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use wchat\common\Result;
|
||||
|
||||
class Account extends SmallProgram
|
||||
{
|
||||
|
||||
/**
|
||||
* @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';
|
||||
|
||||
return $this->get('api.weixin.qq.com', '/sns/jscode2session', $param);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
* @return Result
|
||||
*/
|
||||
public function AppLogin(string $code): Result
|
||||
{
|
||||
$param['appid'] = $this->payConfig->pay->wx->appId;
|
||||
$param['secret'] = $this->payConfig->pay->wx->appSecret;
|
||||
$param['js_code'] = $code;
|
||||
$param['grant_type'] = 'authorization_code';
|
||||
|
||||
return $this->get('api.weixin.qq.com', '/sns/oauth2/access_token', $param);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $openid
|
||||
* @return Result
|
||||
*/
|
||||
public function getPublicUserInfo(string $openid): Result
|
||||
{
|
||||
$query = [
|
||||
'access_token' => $this->payConfig->getAccessToken(),
|
||||
'openid' => $openid,
|
||||
'lang' => 'zh_CN'
|
||||
];
|
||||
return $this->get('api.weixin.qq.com', '/cgi-bin/user/info', $query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $openid
|
||||
* @return Result
|
||||
*/
|
||||
public function getAppUserInfo(string $openid): Result
|
||||
{
|
||||
$query = [
|
||||
'access_token' => $this->payConfig->getAccessToken(),
|
||||
'openid' => $openid,
|
||||
];
|
||||
return $this->get('api.weixin.qq.com', '/sns/userinfo', $query);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param int $width
|
||||
* @return array|mixed|Result
|
||||
*/
|
||||
public function createwxaqrcode(string $path, int $width): mixed
|
||||
{
|
||||
$url = 'cgi-bin/wxaapp/createwxaqrcode?access_token=';
|
||||
$sendBody['path'] = $path;
|
||||
$sendBody['width'] = $width;
|
||||
|
||||
return $this->get('api.weixin.qq.com', $url . $this->payConfig->getAccessToken(), $sendBody);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param int $width
|
||||
* @param bool $is_hyaline
|
||||
* @param bool $auto_color
|
||||
* @param string $line_color
|
||||
* @return Result
|
||||
*/
|
||||
public function getwxacode(string $path, int $width, bool $is_hyaline = false, bool $auto_color = false, string $line_color = ''): Result
|
||||
{
|
||||
$sendBody['path'] = $path;
|
||||
$sendBody['width'] = $width;
|
||||
$sendBody['auto_color'] = $auto_color;
|
||||
$sendBody['is_hyaline'] = $is_hyaline;
|
||||
$url = 'wxa/getwxacode?access_token=' . $this->payConfig->getAccessToken();
|
||||
if ($auto_color) {
|
||||
$sendBody['line_color'] = $line_color;
|
||||
}
|
||||
return $this->get('api.weixin.qq.com', $url . $this->payConfig->getAccessToken(), $sendBody);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param int $width
|
||||
* @param bool $is_hyaline
|
||||
* @param bool $auto_color
|
||||
* @param string $line_color
|
||||
* @return Result
|
||||
*/
|
||||
public function getwxacodeunlimit(string $path, int $width, bool $is_hyaline = false, bool $auto_color = false, string $line_color = ''): Result
|
||||
{
|
||||
$sendBody['path'] = $path;
|
||||
$sendBody['width'] = $width;
|
||||
$sendBody['auto_color'] = $auto_color;
|
||||
$sendBody['is_hyaline'] = $is_hyaline;
|
||||
$url = 'wxa/getwxacodeunlimit?access_token=' . $this->payConfig->getAccessToken();
|
||||
if ($auto_color) {
|
||||
$sendBody['line_color'] = $line_color;
|
||||
}
|
||||
return $this->get('api.weixin.qq.com', $url . $this->payConfig->getAccessToken(), $sendBody);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -114,11 +114,11 @@ class ContentAsyncCheck
|
||||
return $class->init($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $params
|
||||
* @return $this
|
||||
*/
|
||||
private function init($params): static
|
||||
/**
|
||||
* @param array $params
|
||||
* @return $this
|
||||
*/
|
||||
private function init(array $params): static
|
||||
{
|
||||
foreach ($params as $item => $param) {
|
||||
$this->{'_' . $item} = $param;
|
||||
+277
@@ -0,0 +1,277 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Exception;
|
||||
use wchat\common\Result;
|
||||
|
||||
|
||||
/**
|
||||
* Class Message
|
||||
* @package wchat\wx
|
||||
*/
|
||||
class Message extends SmallProgram
|
||||
{
|
||||
|
||||
private array $msgData = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param string $openid
|
||||
*/
|
||||
public function setOpenid(string $openid): void
|
||||
{
|
||||
$this->msgData['touser'] = $openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @return Result
|
||||
* @throws
|
||||
*/
|
||||
public function sendTextNews(string $content): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'text';
|
||||
$this->msgData['text'] = ['content' => $content];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $media_id
|
||||
* @return Result
|
||||
* @throws
|
||||
*/
|
||||
public function sendImageNews(string $media_id): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'image';
|
||||
$this->msgData['image'] = ['media_id' => $media_id];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $media_id
|
||||
* @return Result
|
||||
* @throws
|
||||
*/
|
||||
public function sendVoiceNews(string $media_id): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'voice';
|
||||
$this->msgData['voice'] = ['media_id' => $media_id];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $media_id
|
||||
* @return Result
|
||||
* @throws
|
||||
*/
|
||||
public function sendMpNewsNews(string $media_id): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'mpnews';
|
||||
$this->msgData['mpnews'] = ['media_id' => $media_id];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @param string $description
|
||||
* @param string $url
|
||||
* @param string $picurl
|
||||
* @return Result
|
||||
* @throws
|
||||
*/
|
||||
public function sendNewsNews(string $title, string $description, string $url, string $picurl): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'news';
|
||||
$this->msgData['news'] = [
|
||||
'articles' => [
|
||||
[
|
||||
'title' => $title,
|
||||
'description' => $description,
|
||||
'url' => $url,
|
||||
'picurl' => $picurl
|
||||
]
|
||||
]
|
||||
];
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return Result
|
||||
* @throws
|
||||
*/
|
||||
public function sendCardNews(string $title): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'wxcard';
|
||||
$this->msgData['wxcard'] = ['card_id' => $title];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $media_id
|
||||
* @param string $thumb_media_id
|
||||
* @param string $title
|
||||
* @param string $description
|
||||
* @return Result
|
||||
* @throws
|
||||
*/
|
||||
public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'video';
|
||||
$this->msgData['video'] = [
|
||||
'media_id' => [
|
||||
'media_id' => $media_id,
|
||||
'thumb_media_id' => $thumb_media_id,
|
||||
'title' => $title,
|
||||
'description' => $description
|
||||
]
|
||||
];
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $musicurl
|
||||
* @param string $hqmusicurl
|
||||
* @param string $thumb_media_id
|
||||
* @param string $title
|
||||
* @param string $description
|
||||
* @return Result
|
||||
* @throws
|
||||
*/
|
||||
public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'music';
|
||||
$this->msgData['music'] = [
|
||||
'title' => $title,
|
||||
'description' => $description,
|
||||
'musicurl' => $musicurl,
|
||||
'hqmusicurl' => $hqmusicurl,
|
||||
'thumb_media_id' => $thumb_media_id
|
||||
];
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $head_content
|
||||
* @param string $tail_content
|
||||
* @param array $menus
|
||||
* @return Result
|
||||
* @throws
|
||||
*/
|
||||
public function sendMenuNews(string $head_content, string $tail_content, array $menus = []): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'msgmenu';
|
||||
$this->msgData['msgmenu'] = [
|
||||
'head_content' => $head_content,
|
||||
'tail_content' => $tail_content,
|
||||
];
|
||||
if (empty($menus) || !is_array($menus) || count($menus) < 2) {
|
||||
throw new Exception('菜单选项必须有2个');
|
||||
}
|
||||
foreach ($menus as $val) {
|
||||
$this->addNewsMenu($val['id'], $val['name']);
|
||||
}
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
private int $index = 0;
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string $menuName
|
||||
* @return $this
|
||||
*/
|
||||
public function addNewsMenu(string $id, string $menuName): static
|
||||
{
|
||||
$lists['id'] = $id;
|
||||
$lists['content'] = $menuName;
|
||||
$this->msgData['msgmenu']['list'][$this->index] = $lists;
|
||||
++$this->index;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @param string $appid
|
||||
* @param string $pagepath
|
||||
* @param string $thumb_media_id
|
||||
* @return Result
|
||||
* @throws
|
||||
*/
|
||||
public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'msgmenu';
|
||||
$this->msgData['miniprogrampage'] = [
|
||||
'title' => $title,
|
||||
'appid' => $appid,
|
||||
'pagepath' => $pagepath,
|
||||
'thumb_media_id' => $thumb_media_id,
|
||||
];
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filePath
|
||||
* @param string $type
|
||||
* @param bool $isPermanent
|
||||
* @param string $title
|
||||
* @param string $introduction
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public function uploadFile(string $filePath, string $type, bool $isPermanent = false, string $title = '', string $introduction = ''): Result
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
throw new Exception('文件不存在');
|
||||
}
|
||||
if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
|
||||
throw new Exception('暂不支持的文件类型');
|
||||
}
|
||||
$token = $this->payConfig->getAccessToken();
|
||||
if ($isPermanent) {
|
||||
$url = "/cgi-bin/material/add_material?access_token={$token}&type={$type}";
|
||||
} else {
|
||||
$url = "/cgi-bin/media/upload?access_token={$token}&type={$type}";
|
||||
}
|
||||
$mime = mime_content_type($filePath);
|
||||
$real_path = new \CURLFile(realpath($filePath));
|
||||
$data = ["media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[Content-Type]' => $mime];
|
||||
if ($isPermanent && $mime == 'video/mp3') {
|
||||
$data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction];
|
||||
}
|
||||
return $this->post('api.weixin.qq.com', $url, $data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getContents(): array
|
||||
{
|
||||
return $this->msgData;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws
|
||||
*/
|
||||
private function sendKefuMsg(): Result
|
||||
{
|
||||
$url = '/cgi-bin/message/custom/send?access_token=' . $this->payConfig->getAccessToken();
|
||||
return $this->post('api.weixin.qq.com', $url, $this->msgData);
|
||||
}
|
||||
}
|
||||
@@ -71,12 +71,12 @@ class Notify extends SmallProgram
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param $value
|
||||
* @return void
|
||||
*/
|
||||
public function __set(string $name, $value): void
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function __set(string $name, mixed $value): void
|
||||
{
|
||||
if (property_exists($this, $name)) {
|
||||
$this->$name = $value;
|
||||
@@ -93,8 +93,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->pay->wx->secret;
|
||||
$nowSign = Help::sign($params, $privateKey, $signType);
|
||||
if ($sign === $nowSign) {
|
||||
return true;
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/8 0008
|
||||
* Time: 9:49
|
||||
*/
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use wchat\common\Result;
|
||||
|
||||
class PublicTemplate extends SmallProgram
|
||||
{
|
||||
|
||||
private array $keywords = [];
|
||||
private string $templateId = '';
|
||||
private array $first = [];
|
||||
private array $remark = [];
|
||||
private string $openId = '';
|
||||
private string $defaultUrl = 'http://weixin.qq.com/download';
|
||||
private string $sendUrl = '/cgi-bin/message/template/send';
|
||||
private array $miniprogram = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param array $keywords
|
||||
*/
|
||||
public function setKeywords(array $keywords): void
|
||||
{
|
||||
$this->keywords = $keywords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $templateId
|
||||
*/
|
||||
public function setTemplateId(string $templateId): void
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $openId
|
||||
*/
|
||||
public function setOpenId(string $openId): void
|
||||
{
|
||||
$this->openId = $openId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $defaultUrl
|
||||
*/
|
||||
public function setDefaultUrl(string $defaultUrl): void
|
||||
{
|
||||
$this->defaultUrl = $defaultUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $context
|
||||
* @param string $color
|
||||
*/
|
||||
public function replaceKeyword(string $name, string $context, string $color = ''): void
|
||||
{
|
||||
$this->keywords[$name] = ['value' => $context, 'color' => $color];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $context
|
||||
* @param null $color
|
||||
*/
|
||||
public function addKeyword(string $name, string $context, $color = null): void
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords[$name] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $context
|
||||
* @param string $color
|
||||
* @return void
|
||||
*/
|
||||
public function setFirst(string $context, string $color = '#f00'): void
|
||||
{
|
||||
$this->first = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $context
|
||||
* @param string $color
|
||||
* @return void
|
||||
*/
|
||||
public function setRemark(string $context, string $color = '#000'): void
|
||||
{
|
||||
$this->remark = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $appid
|
||||
* @param string $pagepath
|
||||
* @return void
|
||||
*/
|
||||
public function setMiniprogram(string $appid, string $pagepath): void
|
||||
{
|
||||
$this->miniprogram = [
|
||||
'appid' => $appid,
|
||||
'pagepath' => $pagepath
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws
|
||||
*
|
||||
* 奴隶交易通知
|
||||
*/
|
||||
public function sendTemplate(): Result
|
||||
{
|
||||
$url = $this->sendUrl . '?access_token=' . $this->payConfig->getAccessToken();
|
||||
$keywords = $this->keywords;
|
||||
$keywords['first'] = $this->first;
|
||||
$keywords['remark'] = $this->remark;
|
||||
$default = [
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"url" => $this->defaultUrl,
|
||||
"data" => $keywords,
|
||||
];
|
||||
if (!empty($this->miniprogram)) {
|
||||
$default['miniprogram'] = $this->miniprogram;
|
||||
}
|
||||
return $this->post('api.weixin.qq.com', $url, $default);
|
||||
}
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
use wchat\common\Result;
|
||||
|
||||
//if (PHP_VERSION > '8.2') {
|
||||
// enum Scene
|
||||
// {
|
||||
//
|
||||
// case SCENE_INFO;
|
||||
// case SCENE_REPLY;
|
||||
// case SCENE_DISCAZ;
|
||||
// case SCENE_SHEJIAO;
|
||||
//
|
||||
//
|
||||
// public function getValue(): int
|
||||
// {
|
||||
// return match ($this) {
|
||||
// self::SCENE_INFO => 1,
|
||||
// self::SCENE_REPLY => 2,
|
||||
// self::SCENE_DISCAZ => 3,
|
||||
// self::SCENE_SHEJIAO => 4,
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
/**
|
||||
* Class SecCheck
|
||||
* @package wchat
|
||||
*/
|
||||
class SecCheck extends SmallProgram
|
||||
{
|
||||
|
||||
private string $_url = '/wxa/img_sec_check?access_token=';
|
||||
private string $_msgUrl = '/wxa/msg_sec_check?access_token=';
|
||||
private string $_mediaCheckAsync = '/wxa/media_check_async?access_token=';
|
||||
const int MEDIA_VIDEO = 1;
|
||||
const int MEDIA_IMAGE = 1;
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return Result
|
||||
*/
|
||||
public function image(string $path = ''): Result
|
||||
{
|
||||
if (!file_exists($path)) {
|
||||
return $this->sendError('文件不存在', 404);
|
||||
}
|
||||
$access_token = $this->payConfig->getAccessToken();
|
||||
|
||||
return $this->upload('api.weixin.qq.com', $this->_url . '?access_token=' . $access_token, [
|
||||
'media' => new \CURLFile($path)
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param int $type
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public function mediaAsync(string $url, int $type = SecCheck::MEDIA_IMAGE): Result
|
||||
{
|
||||
if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) {
|
||||
throw new \Exception('暂不支持的文件类型');
|
||||
}
|
||||
$requestUrl = $this->_mediaCheckAsync . $this->payConfig->getAccessToken();
|
||||
|
||||
return $this->post('api.weixin.qq.com', $requestUrl, ['media_url' => $url, 'media_type' => $type]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return ContentAsyncCheck|null
|
||||
*/
|
||||
public function readByEvent(array $params): ?ContentAsyncCheck
|
||||
{
|
||||
return ContentAsyncCheck::instance($params);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @param int $scene
|
||||
* @param string $openId
|
||||
* @return Result
|
||||
*/
|
||||
public function text(string $content, int $scene, string $openId): Result
|
||||
{
|
||||
if (empty($content)) {
|
||||
return $this->sendError('文件不存在', 404);
|
||||
}
|
||||
$requestUrl = $this->_msgUrl . $this->payConfig->getAccessToken();
|
||||
|
||||
return $this->post('api.weixin.qq.com', $requestUrl, json_encode([
|
||||
'content' => $content,
|
||||
'version' => 2,
|
||||
'scene' => $scene,
|
||||
'openid' => $openId
|
||||
], JSON_UNESCAPED_UNICODE), 'application/json');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,6 @@
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class Subject
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
use wchat\common\Result;
|
||||
|
||||
class Token extends SmallProgram
|
||||
{
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
*/
|
||||
public function token(): Result
|
||||
{
|
||||
$query = ['grant_type' => 'client_credential'];
|
||||
if ($this->payConfig->typeIsApp()) {
|
||||
$query['appid'] = $this->payConfig->pay->wx->appId;
|
||||
$query['secret'] = $this->payConfig->pay->wx->appSecret;
|
||||
} else {
|
||||
$query['appid'] = $this->payConfig->appId;
|
||||
$query['secret'] = $this->payConfig->appSecret;
|
||||
}
|
||||
return $this->get('api.weixin.qq.com', 'cgi-bin/token', $query);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3\Notify;
|
||||
|
||||
class GoodsDetail
|
||||
{
|
||||
public string $goods_remark = "商品备注信息";
|
||||
public int $quantity = 1;
|
||||
public int $discount_amount = 1;
|
||||
public string $goods_id = "M1006";
|
||||
public int $unit_price = 100;
|
||||
|
||||
/**
|
||||
* @param array $value
|
||||
*/
|
||||
public function __construct(readonly public array $value)
|
||||
{
|
||||
$this->goods_remark = $this->value['goods_remark'];
|
||||
$this->quantity = $this->value['quantity'];
|
||||
$this->discount_amount = $this->value['discount_amount'];
|
||||
$this->goods_id = $this->value['goods_id'];
|
||||
$this->unit_price = $this->value['unit_price'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3\Notify;
|
||||
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
|
||||
class NotifyModel
|
||||
{
|
||||
|
||||
//公众号支付
|
||||
const string PAY_TYPE_JSAPI = 'JSAPI';
|
||||
|
||||
//扫码支付
|
||||
const string PAY_TYPE_NATIVE = 'NATIVE';
|
||||
|
||||
//App支付
|
||||
const string PAY_TYPE_App = 'App';
|
||||
|
||||
//付款码支付
|
||||
const string PAY_TYPE_MICROPAY = 'MICROPAY';
|
||||
|
||||
//H5支付
|
||||
const string PAY_TYPE_MWEB = 'MWEB';
|
||||
|
||||
//刷脸支付
|
||||
const string PAY_TYPE_FACEPAY = 'FACEPAY';
|
||||
|
||||
|
||||
// 支付成功
|
||||
const string PAY_RESULT_SUCCESS = 'SUCCESS';
|
||||
|
||||
// 转入退款
|
||||
const string PAY_RESULT_REFUND = 'REFUND';
|
||||
|
||||
// 未支付
|
||||
const string PAY_RESULT_NOTPAY = 'NOTPAY';
|
||||
|
||||
// 已关闭
|
||||
const string PAY_RESULT_CLOSED = 'CLOSED';
|
||||
|
||||
// 已撤销(付款码支付)
|
||||
const string PAY_RESULT_REVOKED = 'REVOKED';
|
||||
|
||||
// 用户支付中(付款码支付)
|
||||
const string PAY_RESULT_USERPAYING = 'USERPAYING';
|
||||
|
||||
// 支付失败(其他原因,如银行返回失败)
|
||||
const string PAY_RESULT_PAYERROR = 'PAYERROR';
|
||||
|
||||
|
||||
public string $appid;
|
||||
public string $mchid;
|
||||
public string $out_trade_no;
|
||||
public string $transaction_id;
|
||||
public string $trade_type;
|
||||
public string $trade_state;
|
||||
public string $trade_state_desc;
|
||||
public string $bank_type;
|
||||
public string $attach;
|
||||
public string $success_time;
|
||||
|
||||
/**
|
||||
* @var array|string[]
|
||||
*/
|
||||
#[ArrayShape(['openid' => 'string'])]
|
||||
public array $payer = ['openid' => ''];
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
#[ArrayShape(['payer_total' => 'int', 'total' => 'int', 'currency' => 'string', 'payer_currency' => 'string'])]
|
||||
public array $amount = [
|
||||
"payer_total" => 100,
|
||||
"total" => 100,
|
||||
"currency" => "CNY",
|
||||
"payer_currency" => "CNY"
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* @var array|string[]
|
||||
*/
|
||||
#[ArrayShape(['device_id' => 'string'])]
|
||||
public array $scene_info = [
|
||||
'device_id' => ''
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array<PromotionDetail>
|
||||
*/
|
||||
public array $promotion_detail = [];
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3\Notify;
|
||||
|
||||
class PromotionDetail
|
||||
{
|
||||
|
||||
public int $amount = 100;
|
||||
public int $wechatpay_contribute = 0;
|
||||
public string $coupon_id = "109519";
|
||||
public string $scope = "GLOBAL";
|
||||
public int $merchant_contribute = 0;
|
||||
public string $name = "单品惠-6";
|
||||
public int $other_contribute = 0;
|
||||
public string $currency = "CNY";
|
||||
public string $stock_id = "931386";
|
||||
|
||||
/**
|
||||
* @var array<GoodsDetail>
|
||||
*/
|
||||
public array $goods_detail = [];
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class TransferBatches extends SmallProgram
|
||||
{
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $transfer_scene_id
|
||||
* @param array $transfer_scene_report_infos
|
||||
* @param TransferDetail $detail
|
||||
* @return array
|
||||
*/
|
||||
#[ArrayShape([])]
|
||||
public function transfer(string $transfer_scene_id ,array $transfer_scene_report_infos ,TransferDetail $detail): array
|
||||
{
|
||||
$payConfig = $this->getPayConfig();
|
||||
$body = [];
|
||||
if ($payConfig->typeIsApp()) {
|
||||
$body['appid'] = $payConfig->pay->wx->appId;
|
||||
} else {
|
||||
$body['appid'] = $payConfig->appId;
|
||||
}
|
||||
$body['out_batch_no'] = $detail->out_detail_no;
|
||||
$body["batch_name"] = $payConfig->getBody();
|
||||
$body["body"] = $payConfig->getBody();
|
||||
$body["batch_remark"] = $payConfig->getBody();
|
||||
$body["total_amount"] = $detail->transfer_amount;
|
||||
$body["total_num"] = 1;
|
||||
$body["transfer_scene_id"] = $transfer_scene_id;
|
||||
$body["transfer_scene_report_infos"] = $transfer_scene_report_infos;
|
||||
$body["transfer_detail_list"] = [$detail->toArray()];
|
||||
|
||||
$sign = $this->signature('POST' ,'/v3/transfer/batches' ,$json = json_encode($body ,JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$client = $this->createClient($sign ,$json);
|
||||
$client->post('/v3/transfer/batches');
|
||||
$client->close();
|
||||
|
||||
$data = json_decode($client->getBody() ,TRUE);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
return ['code' => $client->getStatusCode() ,'message' => $client->getBody()];
|
||||
} else {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
|
||||
class TransferDetail
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param string $out_detail_no
|
||||
* @param int|float $transfer_amount
|
||||
* @param string $transfer_remark
|
||||
* @param string $openid
|
||||
* @param string $user_name
|
||||
*/
|
||||
public function __construct(
|
||||
public string $out_detail_no,
|
||||
public int|float $transfer_amount,
|
||||
public string $transfer_remark,
|
||||
public string $openid,
|
||||
public string $user_name = ''
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[ArrayShape(['out_detail_no' => "string", 'transfer_amount' => "float|int", 'transfer_remark' => "string", 'openid' => "string", 'user_name' => "string"])]
|
||||
public function toArray(): array
|
||||
{
|
||||
if (empty($this->user_name)) {
|
||||
return [
|
||||
'out_detail_no' => $this->out_detail_no,
|
||||
'transfer_amount' => $this->transfer_amount,
|
||||
'transfer_remark' => $this->transfer_remark,
|
||||
'openid' => $this->openid,
|
||||
];
|
||||
}
|
||||
return [
|
||||
'out_detail_no' => $this->out_detail_no,
|
||||
'transfer_amount' => $this->transfer_amount,
|
||||
'transfer_remark' => $this->transfer_remark,
|
||||
'openid' => $this->openid,
|
||||
'user_name' => $this->user_name,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV3AppPayment extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $orderNo
|
||||
* @param int $total
|
||||
* @param string|null $openId
|
||||
* @param string $payer_client_ip
|
||||
* @return array
|
||||
* @throws
|
||||
*/
|
||||
public function payment(string $orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
|
||||
{
|
||||
$body = $this->getInitCore($orderNo, $total);
|
||||
|
||||
$body['scene_info'] = ['payer_client_ip' => $payer_client_ip];
|
||||
|
||||
$sign = $this->signature('POST', '/v3/pay/transactions/components', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$client = $this->createClient($sign, $json);
|
||||
$client->post('/v3/pay/transactions/components');
|
||||
$client->close();
|
||||
|
||||
$json = json_decode($client->getBody(), TRUE);
|
||||
if (!isset($json['prepay_id'])) {
|
||||
throw new Exception('微信支付调用失败');
|
||||
}
|
||||
return $this->createResponse($json, $body);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -3,8 +3,6 @@
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use Kiri\Message\Stream;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
@@ -15,25 +13,23 @@ class WxV3NativePayment extends SmallProgram
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param $orderNo
|
||||
* @param int $total
|
||||
* @param string|null $openId
|
||||
* @param string $payer_client_ip
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
/**
|
||||
* @param string $orderNo
|
||||
* @param int $total
|
||||
* @param string|null $openId
|
||||
* @param string $payer_client_ip
|
||||
* @return array
|
||||
* @throws
|
||||
*/
|
||||
#[ArrayShape(['code_url' => "string"])]
|
||||
public function payment($orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
|
||||
public function payment(string $orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
|
||||
{
|
||||
$body = $this->getInitCore($orderNo, $total);
|
||||
|
||||
$sign = $this->signature('POST', '/v3/pay/transactions/native', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, TRUE);
|
||||
$client->withAddedHeader('Authorization', $sign)->withContentType('application/json')
|
||||
->withAgent('application/json')->withBody(new Stream($json))
|
||||
->post('/v3/pay/transactions/native');
|
||||
$client = $this->createClient($sign, $json);
|
||||
$client->post('/v3/pay/transactions/native');
|
||||
$client->close();
|
||||
|
||||
$json = json_decode($client->getBody(), TRUE);
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV3Payment extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $orderNo
|
||||
* @param int $total
|
||||
* @param string|null $openId
|
||||
* @param string $payer_client_ip
|
||||
* @return array
|
||||
* @throws
|
||||
*/
|
||||
public function payment(string $orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
|
||||
{
|
||||
$body = $this->getInitCore($orderNo, $total);
|
||||
$body['payer'] = ['openid' => $openId];
|
||||
$body['scene_info'] = ['payer_client_ip' => $payer_client_ip];
|
||||
|
||||
$sign = $this->signature('POST', '/v3/pay/transactions/jsapi', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$client = $this->createClient($sign, $json);
|
||||
$client->post('/v3/pay/transactions/jsapi');
|
||||
$client->close();
|
||||
|
||||
$json = json_decode($client->getBody(), TRUE);
|
||||
if (!isset($json['prepay_id'])) {
|
||||
throw new Exception('微信支付调用失败: ' . $client->getBody());
|
||||
}
|
||||
|
||||
return $this->createResponse($json, $body);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use OpenSSLAsymmetricKey;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use wchat\wx\SmallProgram;
|
||||
use wchat\wx\V3\Notify\GoodsDetail;
|
||||
use wchat\wx\V3\Notify\NotifyModel;
|
||||
use wchat\wx\V3\Notify\PromotionDetail;
|
||||
|
||||
const KEY_TYPE_PUBLIC = 'public';
|
||||
const KEY_TYPE_PRIVATE = 'private';
|
||||
|
||||
class WxV3PaymentNotify extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string $create_time
|
||||
* @param string $resource_type
|
||||
* @param string $event_type
|
||||
* @param string $summary
|
||||
* @param array $resource
|
||||
*/
|
||||
public function __construct(
|
||||
public string $id = "EV-2018022511223320873",
|
||||
public string $create_time = "2015-05-20T13:29:35+08:00",
|
||||
public string $resource_type = "encrypt-resource",
|
||||
public string $event_type = "TRANSACTION.SUCCESS",
|
||||
public string $summary = "支付成功",
|
||||
public array $resource = []
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @var NotifyModel
|
||||
*/
|
||||
public NotifyModel $notifyModel;
|
||||
|
||||
|
||||
/**
|
||||
* @param RequestInterface $request
|
||||
* @return bool
|
||||
* @throws
|
||||
*/
|
||||
public function verify(RequestInterface $request): bool
|
||||
{
|
||||
$platformPublicKeyInstance = $this->rsaFrom($this->payConfig->pay->wx->mchKey, KEY_TYPE_PUBLIC);
|
||||
$inWechatpaySignature = $request->getHeaderLine('wechatpay-signature'); // 请根据实际情况获取
|
||||
$inWechatpayTimestamp = $request->getHeaderLine('wechatpay-timestamp'); // 请根据实际情况获取
|
||||
$inWechatpayNonce = $request->getHeaderLine('wechatpay-nonce'); // 请根据实际情况获取
|
||||
$inBody = $request->getBody()->getContents(); // 请根据实际情况获取,例如: file_get_contents('php://input');
|
||||
$timeOffsetStatus = 300 >= abs(time() - (int)$inWechatpayTimestamp);
|
||||
$verifiedStatus = $this->notifyVerify(
|
||||
$this->lineFeed($inWechatpayTimestamp, $inWechatpayNonce, $inBody),
|
||||
$inWechatpaySignature,
|
||||
$platformPublicKeyInstance);
|
||||
|
||||
$this->decode($this->resource['ciphertext'], $this->resource['nonce'], $this->resource['associated_data']);
|
||||
|
||||
if (!$timeOffsetStatus || !$verifiedStatus) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ...$pieces
|
||||
* @return string
|
||||
*/
|
||||
protected function lineFeed(...$pieces): string
|
||||
{
|
||||
return implode("\n", array_merge($pieces, ['']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|bool
|
||||
*/
|
||||
protected function body(): string|bool
|
||||
{
|
||||
return json_encode(['id' => $this->id, 'create_time' => $this->create_time, 'resource_type' => $this->resource_type, 'event_type' => $this->event_type, 'summary' => $this->summary, 'resource' => $this->resource]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param string $signature
|
||||
* @param OpenSSLAsymmetricKey $publicKey
|
||||
* @return bool
|
||||
*/
|
||||
protected function notifyVerify(string $message, string $signature, OpenSSLAsymmetricKey $publicKey): bool
|
||||
{
|
||||
if (($result = openssl_verify($message, base64_decode($signature), $publicKey, OPENSSL_ALGO_SHA256)) === false) {
|
||||
throw new \UnexpectedValueException('Verified the input $message failed, please checking your $publicKey whether or nor correct.');
|
||||
}
|
||||
return $result === 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $thing
|
||||
* @param string $type
|
||||
* @return OpenSSLAsymmetricKey
|
||||
*/
|
||||
protected function rsaFrom(string $thing, string $type = KEY_TYPE_PRIVATE): OpenSSLAsymmetricKey
|
||||
{
|
||||
$pkey = (($isPublic = $type === KEY_TYPE_PUBLIC) ? openssl_pkey_get_public('file://' . $thing) : openssl_pkey_get_private('file://' . $thing));
|
||||
if (false === $pkey) {
|
||||
throw new \UnexpectedValueException(sprintf('Cannot load %s from(%s), please take care about the $thing input.', $isPublic ? 'publicKey' : 'privateKey', gettype($thing)));
|
||||
}
|
||||
return $pkey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $ciphertext
|
||||
* @param string $nonce
|
||||
* @param string $associated_data
|
||||
* @return bool
|
||||
*/
|
||||
public function decode(string $ciphertext, string $nonce, string $associated_data): bool
|
||||
{
|
||||
$data = $this->decrypt($ciphertext, $this->payConfig->pay->wx->secret, $nonce, $associated_data);
|
||||
$this->notifyModel = new NotifyModel();
|
||||
$this->notifyModel->amount = $data['amount'];
|
||||
$this->notifyModel->payer = $data['payer'];
|
||||
$this->notifyModel->scene_info = $data['payer'];
|
||||
$this->notifyModel->appid = $data['appid'];
|
||||
$this->notifyModel->mchid = $data['mchid'];
|
||||
$this->notifyModel->out_trade_no = $data['out_trade_no'];
|
||||
$this->notifyModel->transaction_id = $data['transaction_id'];
|
||||
$this->notifyModel->trade_type = $data['trade_type'];
|
||||
$this->notifyModel->trade_state = $data['trade_state'];
|
||||
$this->notifyModel->trade_state_desc = $data['trade_state_desc'];
|
||||
$this->notifyModel->bank_type = $data['bank_type'];
|
||||
$this->notifyModel->attach = $data['attach'];
|
||||
$this->notifyModel->success_time = $data['success_time'];
|
||||
$this->notifyModel->promotion_detail = [];
|
||||
if (!isset($data['promotion_detail'])) {
|
||||
return true;
|
||||
}
|
||||
foreach ($data['promotion_detail'] as $datum) {
|
||||
$detail = new PromotionDetail();
|
||||
$detail->amount = $datum['amount'];
|
||||
$detail->wechatpay_contribute = $datum['wechatpay_contribute'];
|
||||
$detail->coupon_id = $datum['coupon_id'];
|
||||
$detail->scope = $datum['scope'];
|
||||
$detail->merchant_contribute = $datum['merchant_contribute'];
|
||||
$detail->name = $datum['name'];
|
||||
$detail->other_contribute = $datum['other_contribute'];
|
||||
$detail->currency = $datum['currency'];
|
||||
$detail->stock_id = $datum['stock_id'];
|
||||
$detail->goods_detail = [];
|
||||
if (isset($data['goods_detail'])) {
|
||||
foreach ($datum['goods_detail'] as $value) {
|
||||
$detail->goods_detail[] = new GoodsDetail($value);
|
||||
}
|
||||
}
|
||||
$this->notifyModel->promotion_detail[] = $detail;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Help;
|
||||
|
||||
|
||||
/**
|
||||
* Bytes Length of the AES block
|
||||
*/
|
||||
const BLOCK_SIZE = 16;
|
||||
|
||||
/**
|
||||
* The `aes-256-gcm` algorithm string
|
||||
*/
|
||||
const ALGO_AES_256_GCM = 'aes-256-gcm';
|
||||
|
||||
trait WxV3PaymentTait
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $orderNo
|
||||
* @param int $total
|
||||
* @return array
|
||||
*/
|
||||
public function getInitCore(string $orderNo, int $total): array
|
||||
{
|
||||
$payConfig = $this->getPayConfig();
|
||||
if ($payConfig->typeIsApp()) {
|
||||
$body['appid'] = $payConfig->pay->wx->appId;
|
||||
} else {
|
||||
$body['appid'] = $payConfig->appId;
|
||||
}
|
||||
$body['mchid'] = $payConfig->pay->wx->mchId;
|
||||
$body['description'] = $payConfig->getBody();
|
||||
$body['out_trade_no'] = $orderNo;
|
||||
$body['notify_url'] = $payConfig->getNotifyUrl();
|
||||
$body['amount'] = ['total' => $total, 'currency' => $payConfig->getCurrency()];
|
||||
return $body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $sign
|
||||
* @param string $json
|
||||
* @return Client
|
||||
*/
|
||||
public function createClient(string $sign, string $json): Client
|
||||
{
|
||||
$client = new Client('api.mch.weixin.qq.com', 80, TRUE);
|
||||
$client->withAddedHeader('Authorization', $sign)
|
||||
->withContentType('application/json')->withAddedHeader('User-Agent', 'application/json')
|
||||
->withAddedHeader("Accept", "*/*");
|
||||
if (!empty($json)) {
|
||||
$client->withBody($json);
|
||||
}
|
||||
$proxyHost = $this->getPayConfig()->getProxyHost();
|
||||
$proxyPort = $this->getPayConfig()->getProxyPort();
|
||||
if (!empty($proxyHost) && $proxyPort > 0) {
|
||||
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
|
||||
}
|
||||
return $client;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $orderNo
|
||||
* @return array
|
||||
* @throws
|
||||
*/
|
||||
public function searchByOutTradeNo(string $orderNo): array
|
||||
{
|
||||
return $this->search('/v3/pay/transactions/out-trade-no/' . $orderNo);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $orderNo
|
||||
* @return array
|
||||
* @throws
|
||||
*/
|
||||
public function searchByTransactionId(string $orderNo): array
|
||||
{
|
||||
return $this->search('/v3/pay/transactions/id/' . $orderNo);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $parseUrl
|
||||
* @return array
|
||||
* @throws
|
||||
*/
|
||||
private function search(string $parseUrl): array
|
||||
{
|
||||
$config = $this->getPayConfig();
|
||||
|
||||
$sign = $this->signature('GET', $parseUrl . '?mchid=' . $config->pay->wx->mchId);
|
||||
$client = $this->createClient($sign, '');
|
||||
$client->withAddedHeader('Accept', 'application/json');
|
||||
$client->get($parseUrl, ['mchid' => $config->pay->wx->mchId]);
|
||||
$client->close();
|
||||
|
||||
return json_decode($client->getBody(), TRUE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $http_method
|
||||
* @param string $canonical_url
|
||||
* @param string $body
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
public function signature(string $http_method, string $canonical_url, string $body = ''): string
|
||||
{
|
||||
$payConfig = $this->getPayConfig();
|
||||
|
||||
$rand = md5(random_bytes(32));
|
||||
$time = time();
|
||||
|
||||
$message = sprintf("%s\n%s\n%d\n%s\n%s\n", $http_method, $canonical_url, $time, $rand, $body);
|
||||
|
||||
$sign = $this->openssl_signature($message);
|
||||
|
||||
return sprintf('%s mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $payConfig->pay->wx->schema,
|
||||
$payConfig->pay->wx->mchId, $rand, $time, $payConfig->pay->wx->SerialNumber, $sign);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $body
|
||||
* @return string
|
||||
*/
|
||||
public function openssl_signature(string $body): string
|
||||
{
|
||||
$payConfig = $this->getPayConfig();
|
||||
|
||||
$mch_private_key = openssl_get_privatekey(file_get_contents($payConfig->pay->wx->mchCert));
|
||||
|
||||
openssl_sign($body, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
|
||||
return base64_encode($raw_sign);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $json
|
||||
* @param array $body
|
||||
* @return array
|
||||
*/
|
||||
private function createResponse(array $json, array $body): array
|
||||
{
|
||||
$responseArray['appId'] = $body['appid'];
|
||||
$responseArray['timeStamp'] = (string)time();
|
||||
$responseArray['nonceStr'] = Help::random(32);
|
||||
$responseArray['package'] = "prepay_id=" . $json['prepay_id'];
|
||||
|
||||
$responseBody = $responseArray['appId'] . PHP_EOL . $responseArray['timeStamp'] . PHP_EOL . $responseArray['nonceStr'] . PHP_EOL . $responseArray['package'] . PHP_EOL;
|
||||
|
||||
$responseArray['signType'] = 'RSA';
|
||||
$responseArray['signBody'] = $responseBody;
|
||||
$responseArray['paySign'] = $this->openssl_signature($responseBody);
|
||||
$responseArray['prepay_id'] = $json['prepay_id'];
|
||||
|
||||
return $responseArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $ciphertext
|
||||
* @param string $v3Key
|
||||
* @param string $iv
|
||||
* @param string $aad
|
||||
* @return array
|
||||
*/
|
||||
public function decrypt(string $ciphertext, string $v3Key, string $iv = '', string $aad = ''): array
|
||||
{
|
||||
$ciphertext = base64_decode($ciphertext);
|
||||
$authTag = substr($ciphertext, $tailLength = 0 - BLOCK_SIZE);
|
||||
$tagLength = strlen($authTag);
|
||||
|
||||
/* Manually checking the length of the tag, because the `openssl_decrypt` was mentioned there, it's the caller's responsibility. */
|
||||
if ($tagLength > BLOCK_SIZE || ($tagLength < 12 && $tagLength !== 8 && $tagLength !== 4)) {
|
||||
throw new \RuntimeException('The inputs `$ciphertext` incomplete, the bytes length must be one of 16, 15, 14, 13, 12, 8 or 4.');
|
||||
}
|
||||
$plaintext = openssl_decrypt(substr($ciphertext, 0, $tailLength), ALGO_AES_256_GCM, $v3Key, OPENSSL_RAW_DATA, $iv, $authTag, $aad);
|
||||
if (false === $plaintext) {
|
||||
throw new \UnexpectedValueException('Decrypting the input $ciphertext failed, please checking your $key and $iv whether or nor correct.');
|
||||
}
|
||||
return json_decode($plaintext, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use Kiri\Client;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV3Withdrawal extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $orderNo
|
||||
* @param string $batch_name
|
||||
* @param string $batch_remark
|
||||
* @param TransferDetail[] $details
|
||||
* @return array
|
||||
* @throws
|
||||
*/
|
||||
public function payment(string $orderNo, string $batch_name, string $batch_remark, array $details): array
|
||||
{
|
||||
$body = $this->create($orderNo, $batch_name, $batch_remark, $details);
|
||||
|
||||
$sign = $this->signature('POST', '/v3/pay/transactions/batches', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$client = $this->createClient($sign, $json);
|
||||
$client->post('/v3/pay/transactions/batches');
|
||||
$client->close();
|
||||
|
||||
$json = json_decode($client->getBody(), TRUE);
|
||||
if (!isset($json['prepay_id'])) {
|
||||
throw new Exception('微信支付调用失败');
|
||||
}
|
||||
|
||||
return $this->createResponse($json, $body);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $orderNo
|
||||
* @param string $batch_name
|
||||
* @param string $batch_remark
|
||||
* @param array $details
|
||||
* @return array[]
|
||||
*/
|
||||
#[ArrayShape(['transfer_detail_list' => "array", 'total_amount' => "int", 'total_num' => "int", 'batch_remark' => "string", 'batch_name' => "string", 'out_batch_no' => ""])]
|
||||
private function create(string $orderNo, string $batch_name, string $batch_remark, array $details): array
|
||||
{
|
||||
$total = 0;
|
||||
$body = ['transfer_detail_list' => []];
|
||||
$body['out_batch_no'] = $orderNo;
|
||||
$body['batch_name'] = $batch_name;
|
||||
$body['batch_remark'] = $batch_remark;
|
||||
$body['total_num'] = count($details);
|
||||
foreach ($details as $detail) {
|
||||
$total += $detail->transfer_amount;
|
||||
$body['transfer_detail_list'][] = $detail->toArray();
|
||||
}
|
||||
$body['total_amount'] = $total;
|
||||
return $body;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Kiri\Di\Container;
|
||||
use wchat\common\AppConfig;
|
||||
|
||||
class WxFactory
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param AppConfig $config
|
||||
* @return object|null
|
||||
* @throws
|
||||
*/
|
||||
public static function get(string $class, AppConfig $config): ?object
|
||||
{
|
||||
$container = Container::instance();
|
||||
$object = $container->get($class);
|
||||
$object->setPayConfig($config);
|
||||
return $object;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user