add clear
This commit is contained in:
+84
-84
@@ -8,6 +8,8 @@
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Decode;
|
||||
use wchat\common\HttpClient;
|
||||
use wchat\common\Result;
|
||||
@@ -15,48 +17,39 @@ use wchat\common\Result;
|
||||
class Account extends SmallProgram
|
||||
{
|
||||
|
||||
private $wxaqr = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=';
|
||||
private $getwxacode = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=';
|
||||
private $getwxacodeunlimit = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=';
|
||||
|
||||
private $savePath = __DIR__ . '/../../Users/';
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*/
|
||||
public function setSavePath($path)
|
||||
{
|
||||
$this->savePath = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $code
|
||||
* @return Result
|
||||
*/
|
||||
public function login($code)
|
||||
public function login($code): Result
|
||||
{
|
||||
$param['appid'] = $this->config->getAppid();
|
||||
$param['secret'] = $this->config->getAppsecret();
|
||||
$param['js_code'] = $code;
|
||||
$param['grant_type'] = 'authorization_code';
|
||||
|
||||
$this->request->setMethod(HttpClient::GET);
|
||||
$this->request->addHeader('Content-Type', 'application/json');
|
||||
$this->request->setHost('api.weixin.qq.com');
|
||||
$this->request->addHeader('Host', 'api.weixin.qq.com');
|
||||
$this->request->setErrorField('errcode');
|
||||
$this->request->setErrorMsgField('errmsg');
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->get('sns/jscode2session', $param);
|
||||
$client->close();
|
||||
|
||||
return $this->request->get('sns/jscode2session', $param);
|
||||
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 array|mixed|Result
|
||||
* @throws \Exception
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPublicUserInfo($openid)
|
||||
public function getPublicUserInfo($openid): Result
|
||||
{
|
||||
$query = [
|
||||
'access_token' => $this->getAccessToken(),
|
||||
@@ -64,16 +57,19 @@ class Account extends SmallProgram
|
||||
'lang' => 'zh_CN'
|
||||
];
|
||||
|
||||
$client = HttpClient::NewRequest();
|
||||
$client->setAgent($this->config->getAgent());
|
||||
$client->setUseSwoole($this->config->isUsrSwoole());
|
||||
$client->setMethod(HttpClient::GET);
|
||||
$client->setIsSSL(true);
|
||||
$client->setHost('api.weixin.qq.com');
|
||||
$client->addHeader('Host', 'api.weixin.qq.com');
|
||||
$client->setErrorField('errcode');
|
||||
$client->setErrorMsgField('errmsg');
|
||||
return $client->get('cgi-bin/user/info', $query);
|
||||
$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);
|
||||
}
|
||||
|
||||
|
||||
@@ -81,7 +77,7 @@ class Account extends SmallProgram
|
||||
* @param $encryptedData
|
||||
* @param $iv
|
||||
* @param $sessionKey
|
||||
* @param $asArray
|
||||
* @param bool $asArray
|
||||
* @return object|array
|
||||
* @throws
|
||||
*
|
||||
@@ -91,7 +87,7 @@ class Account extends SmallProgram
|
||||
* <li>-41005: base64加密失败</li>
|
||||
* <li>-41016: base64解密失败</li>
|
||||
*/
|
||||
public function decode($encryptedData, $iv, $sessionKey, $asArray = false)
|
||||
public function decode($encryptedData, $iv, $sessionKey, bool $asArray = false): object|array
|
||||
{
|
||||
$decode = new Decode();
|
||||
$decode->setSessionKey($sessionKey);
|
||||
@@ -107,21 +103,26 @@ class Account extends SmallProgram
|
||||
* @param $path
|
||||
* @param $width
|
||||
* @return array|mixed|Result
|
||||
* @throws \Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public function createwxaqrcode($path, $width)
|
||||
public function createwxaqrcode($path, $width): mixed
|
||||
{
|
||||
$url = $this->wxaqr . $this->getAccessToken();
|
||||
|
||||
$url = 'cgi-bin/wxaapp/createwxaqrcode?access_token=';
|
||||
$sendBody['path'] = $path;
|
||||
$sendBody['width'] = $width;
|
||||
|
||||
$this->request->setMethod(HttpClient::POST);
|
||||
$this->request->setCallback([$this, 'saveByPath']);
|
||||
$this->request->setErrorField('errcode');
|
||||
$this->request->setErrorMsgField('errmsg');
|
||||
|
||||
return $this->request->post($url, $sendBody);
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -130,11 +131,10 @@ class Account extends SmallProgram
|
||||
* @param $width
|
||||
* @param bool $is_hyaline
|
||||
* @param bool $auto_color
|
||||
* @param $line_color
|
||||
* @return array|mixed|Result
|
||||
* @throws \Exception
|
||||
* @param string $line_color
|
||||
* @return Result
|
||||
*/
|
||||
public function getwxacode($path, $width, $is_hyaline = false, $auto_color = false, $line_color = '')
|
||||
public function getwxacode($path, $width, bool $is_hyaline = false, bool $auto_color = false, string $line_color = ''): Result
|
||||
{
|
||||
$sendBody['path'] = $path;
|
||||
$sendBody['width'] = $width;
|
||||
@@ -144,13 +144,21 @@ class Account extends SmallProgram
|
||||
$sendBody['line_color'] = $line_color;
|
||||
}
|
||||
|
||||
$url = $this->getwxacode . $this->getAccessToken();
|
||||
$url = 'wxa/getwxacode?access_token=' . $this->getConfig()->getAccessToken();
|
||||
|
||||
$this->request->setMethod(HttpClient::POST);
|
||||
$this->request->setCallback([$this, 'saveByPath']);
|
||||
$this->request->setErrorField('errcode');
|
||||
$this->request->setErrorMsgField('errmsg');
|
||||
return $this->request->post($url, $sendBody);
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -159,11 +167,10 @@ class Account extends SmallProgram
|
||||
* @param $width
|
||||
* @param bool $is_hyaline
|
||||
* @param bool $auto_color
|
||||
* @param $line_color
|
||||
* @return array|mixed|Result
|
||||
* @throws \Exception
|
||||
* @param string $line_color
|
||||
* @return Result
|
||||
*/
|
||||
public function getwxacodeunlimit($path, $width, $is_hyaline = false, $auto_color = false, $line_color = '')
|
||||
public function getwxacodeunlimit($path, $width, bool $is_hyaline = false, bool $auto_color = false, string $line_color = ''): Result
|
||||
{
|
||||
$sendBody['path'] = $path;
|
||||
$sendBody['width'] = $width;
|
||||
@@ -173,28 +180,21 @@ class Account extends SmallProgram
|
||||
$sendBody['line_color'] = $line_color;
|
||||
}
|
||||
|
||||
$url = $this->getwxacodeunlimit . $this->getAccessToken();
|
||||
$url = 'wxa/getwxacodeunlimit?access_token=' . $this->getConfig()->getAccessToken();
|
||||
|
||||
$this->request->setMethod(HttpClient::POST);
|
||||
$this->request->setCallback([$this, 'saveByPath']);
|
||||
$this->request->setErrorField('errcode');
|
||||
$this->request->setErrorMsgField('errmsg');
|
||||
return $this->request->post($url, $sendBody);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $body
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function saveByPath($body)
|
||||
{
|
||||
if (!is_null($json = json_decode($body))) {
|
||||
throw new \Exception($json['errmsg'], $json['errcode']);
|
||||
$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());
|
||||
}
|
||||
|
||||
$push = md5_file($body) . '.png';
|
||||
file_put_contents($this->savePath . $push, $this->savePath);
|
||||
return $this->savePath . $push;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,21 +10,21 @@ namespace wchat\wx;
|
||||
*/
|
||||
class ContentAsyncCheck
|
||||
{
|
||||
private $_ToUserName = '';
|
||||
private $_FromUserName = '';
|
||||
private $_CreateTime = '';
|
||||
private $_MsgType = '';
|
||||
private $_Event = '';
|
||||
private $_isrisky = '';
|
||||
private $_extra_info_json = '';
|
||||
private $_appid = '';
|
||||
private $_trace_id = '';
|
||||
private $_status_code = '';
|
||||
private mixed $_ToUserName = '';
|
||||
private mixed $_FromUserName = '';
|
||||
private mixed $_CreateTime = '';
|
||||
private mixed $_MsgType = '';
|
||||
private mixed $_Event = '';
|
||||
private mixed $_isrisky = '';
|
||||
private mixed $_extra_info_json = '';
|
||||
private mixed $_appid = '';
|
||||
private mixed $_trace_id = '';
|
||||
private mixed $_status_code = '';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getToUserName()
|
||||
public function getToUserName(): mixed
|
||||
{
|
||||
return $this->_ToUserName;
|
||||
}
|
||||
@@ -32,7 +32,7 @@ class ContentAsyncCheck
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFromUserName()
|
||||
public function getFromUserName(): mixed
|
||||
{
|
||||
return $this->_FromUserName;
|
||||
}
|
||||
@@ -40,7 +40,7 @@ class ContentAsyncCheck
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCreateTime()
|
||||
public function getCreateTime(): mixed
|
||||
{
|
||||
return $this->_CreateTime;
|
||||
}
|
||||
@@ -48,7 +48,7 @@ class ContentAsyncCheck
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMsgType()
|
||||
public function getMsgType(): mixed
|
||||
{
|
||||
return $this->_MsgType;
|
||||
}
|
||||
@@ -56,7 +56,7 @@ class ContentAsyncCheck
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEvent()
|
||||
public function getEvent(): mixed
|
||||
{
|
||||
return $this->_Event;
|
||||
}
|
||||
@@ -64,7 +64,7 @@ class ContentAsyncCheck
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getIsrisky()
|
||||
public function getIsrisky(): mixed
|
||||
{
|
||||
return $this->_isrisky;
|
||||
}
|
||||
@@ -72,7 +72,7 @@ class ContentAsyncCheck
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getExtraInfoJson()
|
||||
public function getExtraInfoJson(): mixed
|
||||
{
|
||||
return $this->_extra_info_json;
|
||||
}
|
||||
@@ -80,7 +80,7 @@ class ContentAsyncCheck
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAppid()
|
||||
public function getAppid(): mixed
|
||||
{
|
||||
return $this->_appid;
|
||||
}
|
||||
@@ -88,7 +88,7 @@ class ContentAsyncCheck
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTraceId()
|
||||
public function getTraceId(): mixed
|
||||
{
|
||||
return $this->_trace_id;
|
||||
}
|
||||
@@ -96,16 +96,16 @@ class ContentAsyncCheck
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getStatusCode()
|
||||
public function getStatusCode(): mixed
|
||||
{
|
||||
return $this->_status_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return ContentAsyncCheck|null
|
||||
* @return ContentAsyncCheck
|
||||
*/
|
||||
public static function instance(array $params)
|
||||
public static function instance(array $params): static
|
||||
{
|
||||
static $class = null;
|
||||
if ($class === null) {
|
||||
@@ -118,7 +118,7 @@ class ContentAsyncCheck
|
||||
* @param $params
|
||||
* @return $this
|
||||
*/
|
||||
private function init($params)
|
||||
private function init($params): static
|
||||
{
|
||||
foreach ($params as $item => $param) {
|
||||
$this->{'_' . $item} = $param;
|
||||
@@ -129,7 +129,7 @@ class ContentAsyncCheck
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isRisky()
|
||||
public function isRisky(): bool
|
||||
{
|
||||
return intval($this->_isrisky) === 0;
|
||||
}
|
||||
@@ -137,7 +137,7 @@ class ContentAsyncCheck
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isSuccess()
|
||||
public function isSuccess(): bool
|
||||
{
|
||||
return $this->_status_code === 0;
|
||||
}
|
||||
|
||||
+257
-243
@@ -4,7 +4,7 @@
|
||||
namespace wchat\wx;
|
||||
|
||||
use Exception;
|
||||
use wchat\common\HttpClient;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
|
||||
@@ -15,287 +15,301 @@ use wchat\common\Result;
|
||||
class Message extends SmallProgram
|
||||
{
|
||||
|
||||
private array $msgData = [];
|
||||
private array $msgData = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $openid
|
||||
*/
|
||||
public function setOpenid($openid)
|
||||
{
|
||||
$this->msgData['touser'] = $openid;
|
||||
}
|
||||
/**
|
||||
* @param $openid
|
||||
*/
|
||||
public function setOpenid($openid)
|
||||
{
|
||||
$this->msgData['touser'] = $openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendTextNews($content)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'text';
|
||||
$this->msgData['text'] = ['content' => $content];
|
||||
/**
|
||||
* @param $content
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendTextNews($content): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'text';
|
||||
$this->msgData['text'] = ['content' => $content];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendImageNews($media_id)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'image';
|
||||
$this->msgData['image'] = ['media_id' => $media_id];
|
||||
/**
|
||||
* @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();
|
||||
}
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendVoiceNews($media_id)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'voice';
|
||||
$this->msgData['voice'] = ['media_id' => $media_id];
|
||||
/**
|
||||
* @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();
|
||||
}
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendMpNewsNews($media_id)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'mpnews';
|
||||
$this->msgData['mpnews'] = ['media_id' => $media_id];
|
||||
/**
|
||||
* @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();
|
||||
}
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @param $description
|
||||
* @param $url
|
||||
* @param $picurl
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendNewsNews($title, $description, $url, $picurl)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'news';
|
||||
$this->msgData['news'] = ['articles' => [[
|
||||
'title' => $title,
|
||||
'description' => $description,
|
||||
'url' => $url,
|
||||
'picurl' => $picurl
|
||||
]]];
|
||||
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)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'wxcard';
|
||||
$this->msgData['wxcard'] = ['card_id' => $title];
|
||||
/**
|
||||
* @param $title
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendCardNews($title): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'wxcard';
|
||||
$this->msgData['wxcard'] = ['card_id' => $title];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
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)
|
||||
{
|
||||
$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 $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)
|
||||
{
|
||||
$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 $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 = [])
|
||||
{
|
||||
$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 $key => $val) {
|
||||
$this->addNewsMenu($val['id'], $val['name']);
|
||||
}
|
||||
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;
|
||||
private int $index = 0;
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $menuName
|
||||
* @return $this
|
||||
*/
|
||||
public function addNewsMenu($id, $menuName)
|
||||
{
|
||||
$lists['id'] = $id;
|
||||
$lists['content'] = $menuName;
|
||||
$this->msgData['msgmenu']['list'][$this->index] = $lists;
|
||||
++$this->index;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'msgmenu';
|
||||
$this->msgData['miniprogrampage'] = [
|
||||
'title' => $title,
|
||||
'appid' => $appid,
|
||||
'pagepath' => $pagepath,
|
||||
'thumb_media_id' => $thumb_media_id,
|
||||
];
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
/**
|
||||
* @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 $title
|
||||
* @param $introduction
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function upload($filePath, $type, $isPermanent = false, $title = '', $introduction = '')
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
throw new Exception('文件不存在');
|
||||
}
|
||||
/**
|
||||
* @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('暂不支持的文件类型');
|
||||
}
|
||||
if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
|
||||
throw new Exception('暂不支持的文件类型');
|
||||
}
|
||||
|
||||
$token = $this->getAccessToken();
|
||||
if ($isPermanent) {
|
||||
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type={$type}";
|
||||
} else {
|
||||
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}";
|
||||
}
|
||||
$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);
|
||||
$mime = mime_content_type($filePath);
|
||||
|
||||
$real_path = new \CURLFile(realpath($filePath));
|
||||
$real_path = new \CURLFile(realpath($filePath));
|
||||
|
||||
$data = array("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];
|
||||
}
|
||||
$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];
|
||||
}
|
||||
|
||||
$this->request->setMethod(HttpClient::POST);
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'multipart/form-data']);
|
||||
$client->post($url, $data);
|
||||
$client->close();
|
||||
|
||||
/** @var Result $body */
|
||||
$data = $this->request->post($url, $data);
|
||||
if (!$data->isResultsOK()) {
|
||||
throw new Exception($data->getMessage());
|
||||
}
|
||||
$this->msgData = [];
|
||||
|
||||
return $data->getData();
|
||||
}
|
||||
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()
|
||||
{
|
||||
return $this->msgData;
|
||||
}
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getContents(): array
|
||||
{
|
||||
return $this->msgData;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
private function sendKefuMsg()
|
||||
{
|
||||
$data = json_encode($this->msgData, JSON_UNESCAPED_UNICODE);
|
||||
/**
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
private function sendKefuMsg()
|
||||
{
|
||||
$url = '/cgi-bin/message/custom/send?access_token=' . $this->config->getAccessToken();
|
||||
|
||||
$url = '/cgi-bin/message/custom/send?access_token=' . $this->getAccessToken();
|
||||
$this->request->setMethod(HttpClient::POST);
|
||||
$this->request->setErrorField('errcode');
|
||||
$this->request->setErrorMsgField('errmsg');
|
||||
$this->request->setHost('api.weixin.qq.com');
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($url, $this->msgData);
|
||||
$client->close();
|
||||
|
||||
$this->request->addHeader('Content-Type', 'application/json');
|
||||
$this->msgData = [];
|
||||
|
||||
/** @var Result $body */
|
||||
$body = $this->request->post($url, $data);
|
||||
|
||||
if (!$body->isResultsOK()) {
|
||||
throw new Exception($body->getMessage());
|
||||
}
|
||||
return $body;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+158
-203
@@ -3,43 +3,44 @@
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use wchat\common\Help;
|
||||
|
||||
class Notify extends SmallProgram
|
||||
{
|
||||
public $appid = '';
|
||||
public $mch_id = '';
|
||||
public $device_info = '';
|
||||
public $nonce_str = '';
|
||||
public $sign = '';
|
||||
public $sign_type = '';
|
||||
public $result_code = '';
|
||||
public $err_code = '';
|
||||
public $err_code_des = '';
|
||||
public $openid = '';
|
||||
public $is_subscribe = '';
|
||||
public $trade_type = '';
|
||||
public $bank_type = '';
|
||||
public $total_fee = '';
|
||||
public $settlement_total_fee = '';
|
||||
public $fee_type = '';
|
||||
public $cash_fee = '';
|
||||
public $cash_fee_type = '';
|
||||
public $coupon_fee = '';
|
||||
public $coupon_count = '';
|
||||
public $coupon_type_n = '';
|
||||
public $coupon_id_n = '';
|
||||
public $coupon_fee_n = '';
|
||||
public $transaction_id = '';
|
||||
public $out_trade_no = '';
|
||||
public $attach = '';
|
||||
public $time_end = '';
|
||||
public $return_code = '';
|
||||
public mixed $appid = '';
|
||||
public mixed $mch_id = '';
|
||||
public mixed $device_info = '';
|
||||
public mixed $nonce_str = '';
|
||||
public mixed $sign = '';
|
||||
public mixed $sign_type = '';
|
||||
public mixed $result_code = '';
|
||||
public mixed $err_code = '';
|
||||
public mixed $err_code_des = '';
|
||||
public mixed $openid = '';
|
||||
public mixed $is_subscribe = '';
|
||||
public mixed $trade_type = '';
|
||||
public mixed $bank_type = '';
|
||||
public mixed $total_fee = '';
|
||||
public mixed $settlement_total_fee = '';
|
||||
public mixed $fee_type = '';
|
||||
public mixed $cash_fee = '';
|
||||
public mixed $cash_fee_type = '';
|
||||
public mixed $coupon_fee = '';
|
||||
public mixed $coupon_count = '';
|
||||
public mixed $coupon_type_n = '';
|
||||
public mixed $coupon_id_n = '';
|
||||
public mixed $coupon_fee_n = '';
|
||||
public mixed $transaction_id = '';
|
||||
public mixed $out_trade_no = '';
|
||||
public mixed $attach = '';
|
||||
public mixed $time_end = '';
|
||||
public mixed $return_code = '';
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* 判断是否完成支付
|
||||
*/
|
||||
public function isSuccess()
|
||||
public function isSuccess(): bool
|
||||
{
|
||||
if ($this->getReturnCode() != "SUCCESS") {
|
||||
return false;
|
||||
@@ -56,7 +57,7 @@ class Notify extends SmallProgram
|
||||
* @param array $params
|
||||
* @return $this
|
||||
*/
|
||||
public function setPayNotifyData(array $params)
|
||||
public function setPayNotifyData(array $params): static
|
||||
{
|
||||
if (!$this->validation($params)) {
|
||||
$this->setResultCode('FAIL');
|
||||
@@ -64,20 +65,30 @@ class Notify extends SmallProgram
|
||||
unset($params['result_code'], $params['err_code_des']);
|
||||
}
|
||||
foreach ($params as $key => $val) {
|
||||
if (!property_exists($this, $key)) {
|
||||
continue;
|
||||
}
|
||||
$this->$key = $val;
|
||||
$this->__set($key, $val);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param $value
|
||||
* @return void
|
||||
*/
|
||||
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)
|
||||
public function validation(array $params): bool
|
||||
{
|
||||
$sign = $params['sign'];
|
||||
unset($params['sign']);
|
||||
@@ -92,507 +103,451 @@ class Notify extends SmallProgram
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getAppid()
|
||||
public function getAppid(): mixed
|
||||
{
|
||||
return $this->appid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $appid
|
||||
* @return Notify
|
||||
* @param mixed|string $appid
|
||||
*/
|
||||
public function setAppid($appid)
|
||||
public function setAppid(mixed $appid): void
|
||||
{
|
||||
$this->appid = $appid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getMchId()
|
||||
public function getMchId(): mixed
|
||||
{
|
||||
return $this->mch_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $mch_id
|
||||
* @return Notify
|
||||
* @param mixed|string $mch_id
|
||||
*/
|
||||
public function setMchId($mch_id)
|
||||
public function setMchId(mixed $mch_id): void
|
||||
{
|
||||
$this->mch_id = $mch_id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getDeviceInfo()
|
||||
public function getDeviceInfo(): mixed
|
||||
{
|
||||
return $this->device_info;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $device_info
|
||||
* @return Notify
|
||||
* @param mixed|string $device_info
|
||||
*/
|
||||
public function setDeviceInfo($device_info)
|
||||
public function setDeviceInfo(mixed $device_info): void
|
||||
{
|
||||
$this->device_info = $device_info;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getNonceStr()
|
||||
public function getNonceStr(): mixed
|
||||
{
|
||||
return $this->nonce_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $nonce_str
|
||||
* @return Notify
|
||||
* @param mixed|string $nonce_str
|
||||
*/
|
||||
public function setNonceStr($nonce_str)
|
||||
public function setNonceStr(mixed $nonce_str): void
|
||||
{
|
||||
$this->nonce_str = $nonce_str;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getSign()
|
||||
public function getSign(): mixed
|
||||
{
|
||||
return $this->sign;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $sign
|
||||
* @return Notify
|
||||
* @param mixed|string $sign
|
||||
*/
|
||||
public function setSign($sign)
|
||||
public function setSign(mixed $sign): void
|
||||
{
|
||||
$this->sign = $sign;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getSignType()
|
||||
public function getSignType(): mixed
|
||||
{
|
||||
return $this->sign_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $sign_type
|
||||
* @return Notify
|
||||
* @param mixed|string $sign_type
|
||||
*/
|
||||
public function setSignType($sign_type)
|
||||
public function setSignType(mixed $sign_type): void
|
||||
{
|
||||
$this->sign_type = $sign_type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getResultCode()
|
||||
public function getResultCode(): mixed
|
||||
{
|
||||
return $this->result_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $result_code
|
||||
* @return Notify
|
||||
* @param mixed|string $result_code
|
||||
*/
|
||||
public function setResultCode($result_code)
|
||||
public function setResultCode(mixed $result_code): void
|
||||
{
|
||||
$this->result_code = $result_code;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getErrCode()
|
||||
public function getErrCode(): mixed
|
||||
{
|
||||
return $this->err_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $err_code
|
||||
* @return Notify
|
||||
* @param mixed|string $err_code
|
||||
*/
|
||||
public function setErrCode($err_code)
|
||||
public function setErrCode(mixed $err_code): void
|
||||
{
|
||||
$this->err_code = $err_code;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getErrCodeDes()
|
||||
public function getErrCodeDes(): mixed
|
||||
{
|
||||
return $this->err_code_des;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $err_code_des
|
||||
* @return Notify
|
||||
* @param mixed|string $err_code_des
|
||||
*/
|
||||
public function setErrCodeDes($err_code_des)
|
||||
public function setErrCodeDes(mixed $err_code_des): void
|
||||
{
|
||||
$this->err_code_des = $err_code_des;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getOpenid()
|
||||
public function getOpenid(): mixed
|
||||
{
|
||||
return $this->openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $openid
|
||||
* @return Notify
|
||||
* @param mixed|string $openid
|
||||
*/
|
||||
public function setOpenid($openid)
|
||||
public function setOpenid(mixed $openid): void
|
||||
{
|
||||
$this->openid = $openid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getIsSubscribe()
|
||||
public function getIsSubscribe(): mixed
|
||||
{
|
||||
return $this->is_subscribe;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $is_subscribe
|
||||
* @return Notify
|
||||
* @param mixed|string $is_subscribe
|
||||
*/
|
||||
public function setIsSubscribe($is_subscribe)
|
||||
public function setIsSubscribe(mixed $is_subscribe): void
|
||||
{
|
||||
$this->is_subscribe = $is_subscribe;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getTradeType()
|
||||
public function getTradeType(): mixed
|
||||
{
|
||||
return $this->trade_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $trade_type
|
||||
* @return Notify
|
||||
* @param mixed|string $trade_type
|
||||
*/
|
||||
public function setTradeType($trade_type)
|
||||
public function setTradeType(mixed $trade_type): void
|
||||
{
|
||||
$this->trade_type = $trade_type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getBankType()
|
||||
public function getBankType(): mixed
|
||||
{
|
||||
return $this->bank_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $bank_type
|
||||
* @return Notify
|
||||
* @param mixed|string $bank_type
|
||||
*/
|
||||
public function setBankType($bank_type)
|
||||
public function setBankType(mixed $bank_type): void
|
||||
{
|
||||
$this->bank_type = $bank_type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getTotalFee()
|
||||
public function getTotalFee(): mixed
|
||||
{
|
||||
return $this->total_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $total_fee
|
||||
* @return Notify
|
||||
* @param mixed|string $total_fee
|
||||
*/
|
||||
public function setTotalFee($total_fee)
|
||||
public function setTotalFee(mixed $total_fee): void
|
||||
{
|
||||
$this->total_fee = $total_fee;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getSettlementTotalFee()
|
||||
public function getSettlementTotalFee(): mixed
|
||||
{
|
||||
return $this->settlement_total_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $settlement_total_fee
|
||||
* @return Notify
|
||||
* @param mixed|string $settlement_total_fee
|
||||
*/
|
||||
public function setSettlementTotalFee($settlement_total_fee)
|
||||
public function setSettlementTotalFee(mixed $settlement_total_fee): void
|
||||
{
|
||||
$this->settlement_total_fee = $settlement_total_fee;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getFeeType()
|
||||
public function getFeeType(): mixed
|
||||
{
|
||||
return $this->fee_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $fee_type
|
||||
* @return Notify
|
||||
* @param mixed|string $fee_type
|
||||
*/
|
||||
public function setFeeType($fee_type)
|
||||
public function setFeeType(mixed $fee_type): void
|
||||
{
|
||||
$this->fee_type = $fee_type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCashFee()
|
||||
public function getCashFee(): mixed
|
||||
{
|
||||
return $this->cash_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $cash_fee
|
||||
* @return Notify
|
||||
* @param mixed|string $cash_fee
|
||||
*/
|
||||
public function setCashFee($cash_fee)
|
||||
public function setCashFee(mixed $cash_fee): void
|
||||
{
|
||||
$this->cash_fee = $cash_fee;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCashFeeType()
|
||||
public function getCashFeeType(): mixed
|
||||
{
|
||||
return $this->cash_fee_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $cash_fee_type
|
||||
* @return Notify
|
||||
* @param mixed|string $cash_fee_type
|
||||
*/
|
||||
public function setCashFeeType($cash_fee_type)
|
||||
public function setCashFeeType(mixed $cash_fee_type): void
|
||||
{
|
||||
$this->cash_fee_type = $cash_fee_type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCouponFee()
|
||||
public function getCouponFee(): mixed
|
||||
{
|
||||
return $this->coupon_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $coupon_fee
|
||||
* @return Notify
|
||||
* @param mixed|string $coupon_fee
|
||||
*/
|
||||
public function setCouponFee($coupon_fee)
|
||||
public function setCouponFee(mixed $coupon_fee): void
|
||||
{
|
||||
$this->coupon_fee = $coupon_fee;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCouponCount()
|
||||
public function getCouponCount(): mixed
|
||||
{
|
||||
return $this->coupon_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $coupon_count
|
||||
* @return Notify
|
||||
* @param mixed|string $coupon_count
|
||||
*/
|
||||
public function setCouponCount($coupon_count)
|
||||
public function setCouponCount(mixed $coupon_count): void
|
||||
{
|
||||
$this->coupon_count = $coupon_count;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCouponTypeN()
|
||||
public function getCouponTypeN(): mixed
|
||||
{
|
||||
return $this->coupon_type_n;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $coupon_type_n
|
||||
* @return Notify
|
||||
* @param mixed|string $coupon_type_n
|
||||
*/
|
||||
public function setCouponTypeN($coupon_type_n)
|
||||
public function setCouponTypeN(mixed $coupon_type_n): void
|
||||
{
|
||||
$this->coupon_type_n = $coupon_type_n;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCouponIdN()
|
||||
public function getCouponIdN(): mixed
|
||||
{
|
||||
return $this->coupon_id_n;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $coupon_id_n
|
||||
* @return Notify
|
||||
* @param mixed|string $coupon_id_n
|
||||
*/
|
||||
public function setCouponIdN($coupon_id_n)
|
||||
public function setCouponIdN(mixed $coupon_id_n): void
|
||||
{
|
||||
$this->coupon_id_n = $coupon_id_n;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCouponFeeN()
|
||||
public function getCouponFeeN(): mixed
|
||||
{
|
||||
return $this->coupon_fee_n;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $coupon_fee_n
|
||||
* @return Notify
|
||||
* @param mixed|string $coupon_fee_n
|
||||
*/
|
||||
public function setCouponFeeN($coupon_fee_n)
|
||||
public function setCouponFeeN(mixed $coupon_fee_n): void
|
||||
{
|
||||
$this->coupon_fee_n = $coupon_fee_n;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getTransactionId()
|
||||
public function getTransactionId(): mixed
|
||||
{
|
||||
return $this->transaction_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $transaction_id
|
||||
* @return Notify
|
||||
* @param mixed|string $transaction_id
|
||||
*/
|
||||
public function setTransactionId($transaction_id)
|
||||
public function setTransactionId(mixed $transaction_id): void
|
||||
{
|
||||
$this->transaction_id = $transaction_id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getOutTradeNo()
|
||||
public function getOutTradeNo(): mixed
|
||||
{
|
||||
return $this->out_trade_no;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $out_trade_no
|
||||
* @return Notify
|
||||
* @param mixed|string $out_trade_no
|
||||
*/
|
||||
public function setOutTradeNo($out_trade_no)
|
||||
public function setOutTradeNo(mixed $out_trade_no): void
|
||||
{
|
||||
$this->out_trade_no = $out_trade_no;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getAttach()
|
||||
public function getAttach(): mixed
|
||||
{
|
||||
return $this->attach;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $attach
|
||||
* @return Notify
|
||||
* @param mixed|string $attach
|
||||
*/
|
||||
public function setAttach($attach)
|
||||
public function setAttach(mixed $attach): void
|
||||
{
|
||||
$this->attach = $attach;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getTimeEnd()
|
||||
public function getTimeEnd(): mixed
|
||||
{
|
||||
return $this->time_end;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $time_end
|
||||
* @return Notify
|
||||
* @param mixed|string $time_end
|
||||
*/
|
||||
public function setTimeEnd($time_end)
|
||||
public function setTimeEnd(mixed $time_end): void
|
||||
{
|
||||
$this->time_end = $time_end;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getReturnCode()
|
||||
public function getReturnCode(): mixed
|
||||
{
|
||||
return $this->return_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $return_code
|
||||
* @return Notify
|
||||
* @param mixed|string $return_code
|
||||
*/
|
||||
public function setReturnCode($return_code)
|
||||
public function setReturnCode(mixed $return_code): void
|
||||
{
|
||||
$this->return_code = $return_code;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+39
-32
@@ -8,19 +8,20 @@
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
class PublicTemplate extends SmallProgram
|
||||
{
|
||||
|
||||
private $keywords = [];
|
||||
private $templateId = '';
|
||||
private $first = '';
|
||||
private $remark = '';
|
||||
private $openId = '';
|
||||
private $defaultUrl = 'http://weixin.qq.com/download';
|
||||
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send';
|
||||
private $miniprogram = [];
|
||||
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 = [];
|
||||
|
||||
|
||||
/**
|
||||
@@ -58,9 +59,9 @@ class PublicTemplate extends SmallProgram
|
||||
/**
|
||||
* @param $name
|
||||
* @param $context
|
||||
* @param $color
|
||||
* @param string $color
|
||||
*/
|
||||
public function replaceKeyword($name, $context, $color = '')
|
||||
public function replaceKeyword($name, $context, string $color = '')
|
||||
{
|
||||
$this->keywords[$name] = ['value' => $context, 'color' => $color];
|
||||
}
|
||||
@@ -84,9 +85,10 @@ class PublicTemplate extends SmallProgram
|
||||
|
||||
/**
|
||||
* @param $context
|
||||
* @param $color
|
||||
* @param string $color
|
||||
* @return void
|
||||
*/
|
||||
public function setFirst($context, $color = '#f00')
|
||||
public function setFirst($context, string $color = '#f00'): void
|
||||
{
|
||||
$this->first = [
|
||||
'value' => $context,
|
||||
@@ -96,9 +98,10 @@ class PublicTemplate extends SmallProgram
|
||||
|
||||
/**
|
||||
* @param $context
|
||||
* @param $color
|
||||
* @param string $color
|
||||
* @return void
|
||||
*/
|
||||
public function setRemark($context, $color = '#000')
|
||||
public function setRemark($context, string $color = '#000'): void
|
||||
{
|
||||
$this->remark = [
|
||||
'value' => $context,
|
||||
@@ -108,12 +111,13 @@ class PublicTemplate extends SmallProgram
|
||||
|
||||
/**
|
||||
* @param $appid
|
||||
* @param $pagepath
|
||||
* @param string $pagepath
|
||||
* @return void
|
||||
*/
|
||||
public function setMiniprogram($appid, $pagepath)
|
||||
public function setMiniprogram($appid, string $pagepath): void
|
||||
{
|
||||
$this->miniprogram = [
|
||||
'appid' => $appid,
|
||||
'appid' => $appid,
|
||||
'pagepath' => $pagepath
|
||||
];
|
||||
}
|
||||
@@ -124,34 +128,37 @@ class PublicTemplate extends SmallProgram
|
||||
*
|
||||
* 奴隶交易通知
|
||||
*/
|
||||
public function sendTemplate()
|
||||
public function sendTemplate(): Result
|
||||
{
|
||||
$url = $this->sendUrl . '?access_token=' . $this->getAccessToken();
|
||||
$url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken();
|
||||
|
||||
$keywords = $this->keywords;
|
||||
$keywords['first'] = $this->first;
|
||||
$keywords['remark'] = $this->remark;
|
||||
|
||||
$default = [
|
||||
"touser" => $this->openId,
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"url" => $this->defaultUrl,
|
||||
"data" => $keywords,
|
||||
"url" => $this->defaultUrl,
|
||||
"data" => $keywords,
|
||||
];
|
||||
|
||||
if (!empty($this->miniprogram)) {
|
||||
$default['miniprogram'] = $this->miniprogram;
|
||||
}
|
||||
|
||||
$params = json_encode($default, JSON_UNESCAPED_UNICODE);
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($url, $default);
|
||||
$client->close();
|
||||
|
||||
$this->request->setIsSSL(true);
|
||||
$this->request->addHeader('Content-Type', 'application/json');
|
||||
$this->request->setErrorField('errcode');
|
||||
$this->request->setErrorMsgField('errmsg');
|
||||
$result = $this->request->post($url, $params);
|
||||
$result->append('postBody', $default);
|
||||
|
||||
return $result;
|
||||
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,188 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/26 0026
|
||||
* Time: 10:22
|
||||
*/
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use wchat\common\HttpClient;
|
||||
use wchat\common\Result;
|
||||
use wchat\common\Help;
|
||||
|
||||
/**
|
||||
* Class Recharge
|
||||
* @package wchat\wx
|
||||
*/
|
||||
class Recharge extends SmallProgram
|
||||
{
|
||||
private $money = 0;
|
||||
|
||||
private $orderNo;
|
||||
|
||||
private $data = [];
|
||||
|
||||
private $transfers = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
|
||||
private $uniformed = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
|
||||
|
||||
/**
|
||||
* @param int $money
|
||||
* @param $orderNo
|
||||
* @param $openId
|
||||
* @return array|mixed|Result
|
||||
* @throws
|
||||
*/
|
||||
public function recharge(int $money, $orderNo, $openId = '')
|
||||
{
|
||||
if ($money < 0) {
|
||||
return new Result(['code' => 500, 'message' => '充值金额不能小于0.']);
|
||||
}
|
||||
$this->money = $money;
|
||||
$this->orderNo = $orderNo;
|
||||
$this->data['openid'] = $openId;
|
||||
|
||||
$config = $this->config;
|
||||
$this->request->setCallback(function ($result, $body) use ($config) {
|
||||
$data = Help::toArray($result);
|
||||
if (isset($data['sign'])) {
|
||||
$sign = $data['sign'];
|
||||
unset($data['sign']);
|
||||
}
|
||||
$return = [];
|
||||
$_sign = Help::sign($data, $config->getKey(), $config->getSignType());
|
||||
if (!isset($sign) || $sign != $_sign) {
|
||||
$return['code'] = -1;
|
||||
$return['message'] = $data['return_msg'] ?? '返回数据签名验证失败';
|
||||
} else {
|
||||
$return['code'] = 0;
|
||||
$return['data'] = $data;
|
||||
$return['data']['postBody'] = $body;
|
||||
if ($data['return_code'] == 'FAIL') {
|
||||
$return['code'] = -1;
|
||||
$return['message'] = $data['return_msg'];
|
||||
}
|
||||
}
|
||||
return new Result($return);
|
||||
});
|
||||
|
||||
return $this->send($this->uniformed, $this->builder());
|
||||
}
|
||||
|
||||
/**
|
||||
* 'appId' => $result['appid'],
|
||||
* 'nonceStr' => $result['nonce_str'],
|
||||
* 'package' => 'prepay_id=' . $result['prepay_id'],
|
||||
* 'signType' => 'MD5',
|
||||
* 'timeStamp' => (string)time(),
|
||||
* @param $prepay_id
|
||||
* @return array
|
||||
*/
|
||||
public function reception($prepay_id)
|
||||
{
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function builder()
|
||||
{
|
||||
$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' => $_SERVER['REMOTE_ADDR'],
|
||||
'notify_url' => $this->config->getNotifyUrl(),
|
||||
'trade_type' => $this->config->getTradeType(),
|
||||
'openid' => $this->data['openid']
|
||||
];
|
||||
|
||||
$key = $this->config->getKey();
|
||||
$sign_type = $this->config->getSignType();
|
||||
|
||||
$data['sign'] = Help::sign($data, $key, $sign_type);
|
||||
// $data['sign_type'] = $sign_type;
|
||||
|
||||
return Help::toXml($data);
|
||||
}
|
||||
|
||||
public function getSignData()
|
||||
{
|
||||
$data = $this->data;
|
||||
$data['key'] = $this->config->getKey();
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $money
|
||||
* @param $openid
|
||||
* @param $order
|
||||
* @param $desc
|
||||
* @return Result
|
||||
* @throws
|
||||
*
|
||||
* 提现
|
||||
*/
|
||||
public function cashWithdrawal($money, $openid, $order, $desc = '零钱提现')
|
||||
{
|
||||
$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);
|
||||
|
||||
$this->request->setCallback(function ($data) {
|
||||
$array = Help::toArray($data);
|
||||
if ($array['return_code'] != 'SUCCESS') {
|
||||
$data = ['code' => $array['return_code'], 'message' => $array['return_msg']];
|
||||
} else if ($array['result_code'] != 'SUCCESS') {
|
||||
$data = ['code' => $array['err_code'], 'message' => $array['err_code_des']];
|
||||
} else {
|
||||
$data = ['code' => 0, 'message' => '支付成功', 'data' => $array];
|
||||
}
|
||||
return new Result($data);
|
||||
});
|
||||
return $this->send($this->transfers, Help::toXml($array));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param $data
|
||||
* @return array|mixed|Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function send($url, $data)
|
||||
{
|
||||
$this->request->setIsSSL(true);
|
||||
$this->request->setMethod(HttpClient::POST);
|
||||
$this->request->addHeader('Content-Type', 'text/xml');
|
||||
$this->request->setErrorField('errcode');
|
||||
$this->request->setErrorMsgField('errmsg');
|
||||
return $this->request->send($url, $data);
|
||||
}
|
||||
}
|
||||
+56
-37
@@ -4,6 +4,7 @@
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
@@ -13,59 +14,70 @@ use wchat\common\Result;
|
||||
class SecCheck extends SmallProgram
|
||||
{
|
||||
|
||||
private $_url = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=';
|
||||
private string $_url = '/wxa/img_sec_check?access_token=';
|
||||
|
||||
private $_msgUrl = '/wxa/msg_sec_check?access_token=';
|
||||
private string $_msgUrl = '/wxa/msg_sec_check?access_token=';
|
||||
|
||||
private $_mediaCheckAsync = '/wxa/media_check_async?access_token=';
|
||||
private string $_mediaCheckAsync = '/wxa/media_check_async?access_token=';
|
||||
|
||||
const MEDIA_VIDEO = 1;
|
||||
const MEDIA_IMAGE = 1;
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @return array|Result|mixed
|
||||
* @param string $path
|
||||
* @return Result
|
||||
*/
|
||||
public function image($path = '')
|
||||
public function image(string $path = ''): Result
|
||||
{
|
||||
if (!file_exists($path)) {
|
||||
return $this->sendError('文件不存在', 404);
|
||||
}
|
||||
$this->request->setUseSwoole(false);
|
||||
$this->request->setIsSSL(true);
|
||||
$this->request->setHeader('Content-Type', 'multipart/form-data');
|
||||
$this->request->setErrorField('errcode');
|
||||
$this->request->setErrorMsgField('errmsg');
|
||||
return $this->request->upload($this->_url . $this->config->getAccessToken(), ['media' => new \CURLFile($path)]);
|
||||
$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 $url
|
||||
* @param string $url
|
||||
* @param int $type
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public function mediaAsync($url, $type = SecCheck::MEDIA_IMAGE)
|
||||
public function mediaAsync(string $url, int $type = SecCheck::MEDIA_IMAGE): Result
|
||||
{
|
||||
if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) {
|
||||
throw new \Exception('暂不支持的文件类型');
|
||||
}
|
||||
$this->request->setIsSSL(true);
|
||||
$this->request->setHost('api.weixin.qq.com');
|
||||
$this->request->setHeader('Content-Type', 'application/json');
|
||||
$this->request->setErrorField('errcode');
|
||||
$this->request->setErrorMsgField('errmsg');
|
||||
|
||||
$requestUrl = $this->_mediaCheckAsync . $this->config->getAccessToken();
|
||||
$response = $this->request->post($requestUrl, [
|
||||
'media_url' => $url,
|
||||
'media_type' => $type
|
||||
]);
|
||||
if (!$response->isResultsOK()) {
|
||||
throw new \Exception($response->getMessage());
|
||||
$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);
|
||||
}
|
||||
return $response->getData('trace_id');
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +85,7 @@ class SecCheck extends SmallProgram
|
||||
* @param $params
|
||||
* @return ContentAsyncCheck|null
|
||||
*/
|
||||
public function readByEvent($params)
|
||||
public function readByEvent($params): ?ContentAsyncCheck
|
||||
{
|
||||
return ContentAsyncCheck::instance($params);
|
||||
}
|
||||
@@ -81,22 +93,29 @@ class SecCheck extends SmallProgram
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
* @return array|Result|mixed
|
||||
* @return Result
|
||||
*/
|
||||
public function text($content)
|
||||
public function text($content): Result
|
||||
{
|
||||
if (empty($content)) {
|
||||
return $this->sendError('文件不存在', 404);
|
||||
}
|
||||
$this->request->setIsSSL(true);
|
||||
$this->request->setHost('api.weixin.qq.com');
|
||||
$this->request->setHeader('Content-Type', 'application/json');
|
||||
$this->request->setErrorField('errcode');
|
||||
$this->request->setErrorMsgField('errmsg');
|
||||
$requestUrl = $this->_msgUrl . $this->config->getAccessToken();
|
||||
|
||||
$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();
|
||||
|
||||
return $this->request->post($requestUrl, ['content' => $content]);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,41 +4,8 @@
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
use wchat\common\Miniprogarampage;
|
||||
use wchat\common\Multiprogramming;
|
||||
|
||||
class SmallProgram extends Miniprogarampage
|
||||
class SmallProgram extends Multiprogramming
|
||||
{
|
||||
|
||||
protected static $instance;
|
||||
|
||||
private $url = 'https://api.weixin.qq.com/cgi-bin/token';
|
||||
|
||||
|
||||
/**
|
||||
* @param bool $get_token
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function generateAccess_token($get_token = false)
|
||||
{
|
||||
if (!empty($this->config->getToken())) {
|
||||
return $this->config->getToken();
|
||||
}
|
||||
$query = [
|
||||
'grant_type' => 'client_credential',
|
||||
'appid' => $this->config->getAppid(),
|
||||
'secret' => $this->config->getAppsecret()
|
||||
];
|
||||
$this->request->setErrorField('errcode');
|
||||
$this->request->setErrorMsgField('errmsg');
|
||||
$param = $this->request->get($this->url, $query);
|
||||
if (!$param->isResultsOK()) {
|
||||
throw new \Exception($param->getMessage());
|
||||
}
|
||||
if ($get_token) {
|
||||
return $param->getData('access_token');
|
||||
}
|
||||
return $param->getData();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
-3
@@ -13,13 +13,21 @@ use wchat\common\Result;
|
||||
class Subject extends \wchat\base\Subject
|
||||
{
|
||||
|
||||
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl()
|
||||
public function getUrl(): string
|
||||
{
|
||||
return $this->sendUrl;
|
||||
return 'cgi-bin/message/subscribe/send';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHost(): string
|
||||
{
|
||||
return 'api.weixin.qq.com';
|
||||
}
|
||||
}
|
||||
|
||||
+15
-3
@@ -14,11 +14,23 @@ namespace wchat\wx;
|
||||
*/
|
||||
class Template extends \wchat\base\Template
|
||||
{
|
||||
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send';
|
||||
|
||||
public function getUrl()
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl(): string
|
||||
{
|
||||
return $this->sendUrl;
|
||||
return '/cgi-bin/message/wxopen/template/send';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHost(): string
|
||||
{
|
||||
return 'api.weixin.qq.com';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+26
-2
@@ -4,11 +4,35 @@
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
use wchat\base\Access_Token;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
class Token extends SmallProgram implements Access_Token
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
<?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()
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
<?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()
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?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,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use Kiri\Message\Stream;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV3NativePayment extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param $orderNo
|
||||
* @param int $total
|
||||
* @param string|null $openId
|
||||
* @param string $payer_client_ip
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
#[ArrayShape(['code_url' => "string"])]
|
||||
public function payment($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->close();
|
||||
|
||||
$json = json_decode($client->getBody(), TRUE);
|
||||
if (!isset($json['code_url'])) {
|
||||
throw new Exception('微信支付调用失败');
|
||||
}
|
||||
return ['code_url' => $json['code_url']];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?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');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Kiri\Di\Container;
|
||||
use wchat\common\Config;
|
||||
|
||||
class WxFactory
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param Config $config
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get($class, Config $config)
|
||||
{
|
||||
$container = Container::getInstance();
|
||||
$object = $container->get($class);
|
||||
$object->setConfig($config);
|
||||
return $object;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user