This commit is contained in:
xl
2023-11-14 00:45:54 +08:00
parent e721fe4f36
commit 0d8ddfef54
12 changed files with 763 additions and 1300 deletions
+53
View File
@@ -33,6 +33,9 @@ class AppConfig
private string $body = '';
private string $currency = 'CNY';
private string $remoteIp = '';
private string $accessToken = '';
private string $tradeType = 'JSAPI';
private string $signType = 'MD5';
/**
* @var int
@@ -165,4 +168,54 @@ class AppConfig
{
$this->remoteIp = $remoteIp;
}
/**
* @return string
*/
public function getAccessToken(): string
{
return $this->accessToken;
}
/**
* @param string $accessToken
*/
public function setAccessToken(string $accessToken): void
{
$this->accessToken = $accessToken;
}
/**
* @return string
*/
public function getTradeType(): string
{
return $this->tradeType;
}
/**
* @param string $tradeType
*/
public function setTradeType(string $tradeType): void
{
$this->tradeType = $tradeType;
}
/**
* @return string
*/
public function getSignType(): string
{
return $this->signType;
}
/**
* @param string $signType
*/
public function setSignType(string $signType): void
{
$this->signType = $signType;
}
}
+70
View File
@@ -8,6 +8,8 @@
namespace wchat\common;
use Kiri\Client;
abstract class Multiprogramming implements Progaram
{
@@ -130,4 +132,72 @@ abstract class Multiprogramming implements Progaram
return $data;
}
/**
* @param string $requestUrl
* @param mixed $body
* @return Result
*/
protected function post(string $requestUrl, mixed $body): Result
{
return $this->request('post', $requestUrl, $body);
}
/**
* @param string $requestUrl
* @param mixed $body
* @return Result
*/
protected function get(string $requestUrl, mixed $body): Result
{
return $this->request('get', $requestUrl, $body);
}
/**
* @param string $requestUrl
* @param mixed $body
* @return Result
*/
protected function upload(string $requestUrl, mixed $body): Result
{
return $this->request('upload', $requestUrl, $body);
}
/**
* @param string $method
* @param string $requestUrl
* @param $body
* @return Result
*/
private function request(string $method, string $requestUrl, $body): Result
{
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
if ($method == 'post') {
$client->post($requestUrl, $body);
} else if ($method == 'upload') {
$client->upload($requestUrl, $body);
} else {
$client->get($requestUrl, $body);
}
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$body = json_decode($client->getBody(), true);
if (!is_null($body)) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $client->getBody());
}
}
}
+36 -86
View File
@@ -9,9 +9,7 @@
namespace wchat\wx;
use Exception;
use Kiri\Client;
use wchat\common\Decode;
use wchat\common\HttpClient;
use wchat\common\Result;
class Account extends SmallProgram
@@ -23,28 +21,26 @@ class Account extends SmallProgram
*/
public function login($code): Result
{
$param['appid'] = $this->config->getAppid();
$param['secret'] = $this->config->getAppsecret();
$param['appid'] = $this->payConfig->appId;
$param['secret'] = $this->payConfig->appSecret;
$param['js_code'] = $code;
$param['grant_type'] = 'authorization_code';
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
if (!empty($this->getConfig()->getProxyHost()) && $this->getConfig()->getProxyPort() > 0) {
$client->withProxyHost("192.168.0.57")->withProxyPort(12286);
return $this->get('/sns/jscode2session', $param);
}
$client->get('sns/jscode2session', $param);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $body);
}
/**
* @param $code
* @return Result
*/
public function AppLogin($code): Result
{
$param['appid'] = $this->payConfig->pay->wx->appId;
$param['secret'] = $this->payConfig->pay->wx->appSecret;
$param['js_code'] = $code;
$param['grant_type'] = 'authorization_code';
return $this->get('/sns/oauth2/access_token', $param);
}
/**
@@ -55,27 +51,25 @@ class Account extends SmallProgram
public function getPublicUserInfo($openid): Result
{
$query = [
'access_token' => $this->config->getAccessToken(),
'access_token' => $this->payConfig->getAccessToken(),
'openid' => $openid,
'lang' => 'zh_CN'
];
return $this->get('/cgi-bin/user/info', $query);
}
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
if (!empty($this->getConfig()->getProxyHost()) && $this->getConfig()->getProxyPort() > 0) {
$client->withProxyHost("192.168.0.57")->withProxyPort(12286);
}
$client->get('cgi-bin/user/info', $query);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
}
return new Result(code: 0, data: $body);
/**
* @param $openid
* @return Result
* @throws Exception
*/
public function getAppUserInfo($openid): Result
{
$query = [
'access_token' => $this->payConfig->getAccessToken(),
'openid' => $openid,
];
return $this->get('/sns/userinfo', $query);
}
@@ -98,7 +92,7 @@ class Account extends SmallProgram
$decode = new Decode();
$decode->setSessionKey($sessionKey);
$decode->setEncryptedData($encryptedData);
$decode->setAppId($this->config->getAppid());
$decode->setAppId($this->payConfig->getAppid());
$decode->setIv($iv);
return $decode->decode($asArray);
@@ -116,25 +110,8 @@ class Account extends SmallProgram
$url = 'cgi-bin/wxaapp/createwxaqrcode?access_token=';
$sendBody['path'] = $path;
$sendBody['width'] = $width;
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$body = json_decode($client->getBody(), true);
if (!is_null($body)) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $client->getBody());
}
return $this->get($url . $this->payConfig->getAccessToken(), $sendBody);
}
@@ -152,25 +129,12 @@ class Account extends SmallProgram
$sendBody['width'] = $width;
$sendBody['auto_color'] = $auto_color;
$sendBody['is_hyaline'] = $is_hyaline;
$url = 'wxa/getwxacode?access_token=' . $this->getConfig()->getAccessToken();
if ($auto_color) {
$sendBody['line_color'] = $line_color;
}
return $this->get($url . $this->payConfig->getAccessToken(), $sendBody);
$url = 'wxa/getwxacode?access_token=' . $this->getConfig()->getAccessToken();
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$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());
}
}
@@ -188,25 +152,11 @@ class Account extends SmallProgram
$sendBody['width'] = $width;
$sendBody['auto_color'] = $auto_color;
$sendBody['is_hyaline'] = $is_hyaline;
$url = 'wxa/getwxacodeunlimit?access_token=' . $this->getConfig()->getAccessToken();
if ($auto_color) {
$sendBody['line_color'] = $line_color;
}
$url = 'wxa/getwxacodeunlimit?access_token=' . $this->getConfig()->getAccessToken();
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$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());
}
return $this->get($url . $this->payConfig->getAccessToken(), $sendBody);
}
}
+4 -51
View File
@@ -238,48 +238,22 @@ class Message extends SmallProgram
if (!file_exists($filePath)) {
throw new Exception('文件不存在');
}
if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
throw new Exception('暂不支持的文件类型');
}
$token = $this->config->getAccessToken();
$token = $this->payConfig->getAccessToken();
if ($isPermanent) {
$url = "/cgi-bin/material/add_material?access_token={$token}&type={$type}";
} else {
$url = "/cgi-bin/media/upload?access_token={$token}&type={$type}";
}
$mime = mime_content_type($filePath);
$real_path = new \CURLFile(realpath($filePath));
$data = ["media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[Content-Type]' => $mime];
if ($isPermanent && $mime == 'video/mp3') {
$data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction];
}
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'multipart/form-data']);
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->post($url, $data);
$client->close();
$this->msgData = [];
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $body);
}
return $this->post($url, $data);
}
@@ -298,28 +272,7 @@ class Message extends SmallProgram
*/
private function sendKefuMsg()
{
$url = '/cgi-bin/message/custom/send?access_token=' . $this->config->getAccessToken();
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->post($url, $this->msgData);
$client->close();
$this->msgData = [];
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $body);
}
$url = '/cgi-bin/message/custom/send?access_token=' . $this->payConfig->getAccessToken();
return $this->post($url, $this->msgData);
}
}
+2 -2
View File
@@ -93,8 +93,8 @@ class Notify extends SmallProgram
$sign = $params['sign'];
unset($params['sign']);
$signType = $this->config->getSignType();
$privateKey = $this->config->getKey();
$signType = $this->payConfig->getSignType();
$privateKey = $this->payConfig->getKey();
$nowSign = Help::sign($params, $privateKey, $signType);
if ($sign === $nowSign) {
return true;
+2 -23
View File
@@ -130,12 +130,10 @@ class PublicTemplate extends SmallProgram
*/
public function sendTemplate(): Result
{
$url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken();
$url = $this->sendUrl . '?access_token=' . $this->payConfig->getAccessToken();
$keywords = $this->keywords;
$keywords['first'] = $this->first;
$keywords['remark'] = $this->remark;
$default = [
"touser" => $this->openId,
"template_id" => $this->templateId,
@@ -145,25 +143,6 @@ class PublicTemplate extends SmallProgram
if (!empty($this->miniprogram)) {
$default['miniprogram'] = $this->miniprogram;
}
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->post($url, $default);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $body);
}
return $this->post($url, $default);
}
}
+6 -61
View File
@@ -15,11 +15,8 @@ class SecCheck extends SmallProgram
{
private string $_url = '/wxa/img_sec_check?access_token=';
private string $_msgUrl = '/wxa/msg_sec_check?access_token=';
private string $_mediaCheckAsync = '/wxa/media_check_async?access_token=';
const MEDIA_VIDEO = 1;
const MEDIA_IMAGE = 1;
@@ -32,28 +29,11 @@ class SecCheck extends SmallProgram
if (!file_exists($path)) {
return $this->sendError('文件不存在', 404);
}
$access_token = $this->config->getAccessToken();
$access_token = $this->payConfig->getAccessToken();
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'multipart/form-data']);
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->upload($this->_url . '?access_token=' . $access_token, [
return $this->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: $client->getBody());
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
}
return new Result(code: 0, data: $body);
}
@@ -68,26 +48,9 @@ class SecCheck extends SmallProgram
if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) {
throw new \Exception('暂不支持的文件类型');
}
$requestUrl = $this->_mediaCheckAsync . $this->config->getAccessToken();
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->post($requestUrl, ['media_url' => $url, 'media_type' => $type]);
$client->close();
$requestUrl = $this->_mediaCheckAsync . $this->payConfig->getAccessToken();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $body);
}
return $this->post($requestUrl, ['media_url' => $url, 'media_type' => $type]);
}
@@ -110,27 +73,9 @@ class SecCheck extends SmallProgram
if (empty($content)) {
return $this->sendError('文件不存在', 404);
}
$requestUrl = $this->_msgUrl . $this->config->getAccessToken();
$requestUrl = $this->_msgUrl . $this->payConfig->getAccessToken();
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->post($requestUrl, ['content' => $content]);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $body);
}
return $this->post($requestUrl, ['content' => $content]);
}
}
+8 -21
View File
@@ -15,28 +15,15 @@ class Token extends SmallProgram
*/
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']);
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
$query = ['grant_type' => 'client_credential'];
if ($this->payConfig->typeIsApp()) {
$query['appid'] = $this->payConfig->pay->wx->appId;
$query['secret'] = $this->payConfig->pay->wx->appSecret;
} else {
$query['appid'] = $this->payConfig->appId;
$query['secret'] = $this->payConfig->appSecret;
}
$client->get('cgi-bin/token', $query);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
}
return new Result(code: 0, data: $body);
return $this->get('cgi-bin/token', $query);
}
-77
View File
@@ -1,77 +0,0 @@
<?php
namespace wchat\wx\V2;
use Kiri\Client;
use wchat\common\Help;
use wchat\common\Result;
use wchat\wx\SmallProgram;
class WxV2AppPayment extends SmallProgram
{
private string $uniformed = '/pay/unifiedorder';
use WxV2PayTait;
/**
* @param int $money
* @param string $orderNo
* @param string $spbill_create_ip
* @return Result
*/
public function payment(int $money, string $orderNo, string $spbill_create_ip = '127.0.0.1'): Result
{
if ($money < 0) {
return new Result(code: 400, message: '充值金额不能小于0.');
}
$body = $this->getInitCore($orderNo, $money);
$body['trade_type'] = 'APP';
$body['spbill_create_ip'] = $spbill_create_ip;
$client = new Client('api.mch.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$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: $client->getBody());
}
$data = Help::toArray($client->getBody());
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
return new Result(code: 503, message: $data['return_msg']);
}
if ($data['result_code'] != 'SUCCESS') {
return new Result(code: 504, message: $data['err_code_des']);
}
return new Result(code: 0, data: $this->reception($data['prepayid']));
}
/**
* @param string $prepay_id
* @return string
*/
public function reception(string $prepay_id): string
{
return $this->sign([
'appId' => $this->config->getAppid(),
'partnerid' => $this->config->getMchId(),
'prepayid' => $prepay_id,
'package' => 'Sign=WXPay',
'noncestr' => Help::random(32),
'timestamp' => time()
]);
}
}
-230
View File
@@ -1,230 +0,0 @@
<?php
namespace wchat\wx\V2;
use Kiri\Client;
use Phalcon\Cache\Frontend\Json;
use wchat\common\Help;
use wchat\common\Result;
use wchat\wx\SmallProgram;
class WxV2PayJsApi extends SmallProgram
{
private string $uniformed = '/pay/unifiedorder';
use WxV2PayTait;
/**
* @param int $money
* @param string $orderNo
* @param string $openId
* @param string $spbill_create_ip
* @return Result
*/
public function applet(int $money, string $orderNo, string $openId, string $spbill_create_ip = '127.0.0.1'): Result
{
if ($money < 0) {
return new Result(code: 400, message: '充值金额不能小于0.');
}
$body = $this->getInitCore($orderNo, $money);
$body['trade_type'] = 'JSAPI';
$body['spbill_create_ip'] = $spbill_create_ip;
$body['openid'] = $openId;
$client = new Client('api.mch.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->withBody($this->sign($body));
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->post($this->uniformed);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$data = Help::toArray($client->getBody());
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
return new Result(code: 503, message: $data['return_msg']);
}
if ($data['result_code'] != 'SUCCESS') {
return new Result(code: 504, message: $data['err_code_des']);
}
return new Result(code: 0, data: $this->reception($data['prepayid']));
}
/**
* @param 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));
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->post($this->uniformed);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$data = Help::toArray($client->getBody());
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
return new Result(code: 503, message: $data['return_msg']);
}
if ($data['result_code'] != 'SUCCESS') {
return new Result(code: 504, message: $data['err_code_des']);
}
return new Result(code: 0, data: $this->reception($data['prepayid']));
}
/**
* @param 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));
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->post($this->uniformed);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$data = Help::toArray($client->getBody());
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
return new Result(code: 503, message: $data['return_msg']);
}
if ($data['result_code'] != 'SUCCESS') {
return new Result(code: 504, message: $data['err_code_des']);
}
return new Result(code: 0, data: $this->reception($data['prepayid']));
}
/**
* @param 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));
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->post($this->uniformed);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$data = Help::toArray($client->getBody());
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
return new Result(code: 503, message: $data['return_msg']);
}
if ($data['result_code'] != 'SUCCESS') {
return new Result(code: 504, message: $data['err_code_des']);
}
return new Result(code: 0, data: $this->reception($data['prepayid']));
}
/**
* @param string $prepay_id
* @return string
*/
public function reception(string $prepay_id): string
{
return $this->sign([
'signType' => $this->config->getSignType(),
'package' => 'prepay_id=' . $prepay_id,
'nonceStr' => Help::random(32),
'timestamp' => time()
]);
}
}
-100
View File
@@ -1,100 +0,0 @@
<?php
namespace wchat\wx\V2;
use JetBrains\PhpStorm\ArrayShape;
use wchat\common\Help;
trait WxV2PayTait
{
/**
* 'appId' => $result['appid'],
* 'nonceStr' => $result['nonce_str'],
* 'package' => 'prepay_id=' . $result['prepay_id'],
* 'signType' => 'MD5',
* 'timeStamp' => (string)time(),
* @param $prepay_id
* @return array
*/
#[ArrayShape(['appId' => "string", 'nonceStr' => "string", 'package' => "string", 'signType' => "string", 'timeStamp' => "string", 'paySign' => "string"])]
public function reception($prepay_id): array
{
$array = [
'appId' => $this->config->getAppid(),
'nonceStr' => Help::random(32),
'package' => 'prepay_id=' . $prepay_id,
'signType' => 'MD5',
'timeStamp' => (string)time(),
];
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$array['paySign'] = Help::sign($array, $key, $sign_type);
return $array;
}
/**
* @param string $orderNo
* @param float|int $total
* @return array
*/
#[ArrayShape(['appid' => "string", 'mch_id' => "string", 'nonce_str' => "string", 'body' => "string", 'out_trade_no' => "string", 'total_fee' => "float|int", 'spbill_create_ip' => "mixed", 'notify_url' => "string", 'trade_type' => "string"])]
protected function getInitCore(string $orderNo, float|int $total): array
{
return [
'appid' => $this->config->getAppid(),
'mch_id' => $this->config->getMchId(),
'nonce_str' => Help::random(32),
'body' => $this->config->getBody(),
'out_trade_no' => $orderNo,
'total_fee' => $total,
'notify_url' => $this->config->getNotifyUrl(),
'trade_type' => $this->config->getTradeType(),
];
}
/**
* @param array $data
* @return string
*/
protected function sign(array $data): string
{
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$data['sign'] = Help::sign($data, $key, $sign_type);
return Help::toXml($data);
}
/**
* @param string $orderNo
* @param int|float $money
* @param string $openid
* @return string
*/
protected function builder(string $orderNo, int|float $money, string $openid): string
{
$data = [
'appid' => $this->config->getAppid(),
'mch_id' => $this->config->getMchId(),
'nonce_str' => Help::random(32),
'body' => $this->config->getBody(),
'out_trade_no' => $orderNo,
'total_fee' => $money,
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
'notify_url' => $this->config->getNotifyUrl(),
'trade_type' => $this->config->getTradeType(),
'openid' => $openid
];
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$data['sign'] = Help::sign($data, $key, $sign_type);
return Help::toXml($data);
}
}
-67
View File
@@ -1,67 +0,0 @@
<?php
namespace wchat\wx\V2;
use Kiri\Client;
use wchat\common\Help;
use wchat\common\Result;
use wchat\wx\SmallProgram;
class WxV2Withdrawal extends SmallProgram
{
private string $transfers = '/mmpaymkttransfers/promotion/transfers';
/**
* @param int|float $money
* @param string $openid
* @param string $order
* @param string $desc
* @return Result
*/
public function payment(int|float $money, string $openid, string $order, string $desc = '零钱提现'): Result
{
$array = [
'nonce_str' => Help::random(32),
'partner_trade_no' => $order,
'mchid' => $this->config->getMchId(),
'mch_appid' => $this->config->getAppid(),
'openid' => $openid,
'check_name' => 'NO_CHECK',
'amount' => $money * 100,
'spbill_create_ip' => $this->config->getRemoteAddr(),
'desc' => $desc,
];
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$array['sign'] = Help::sign($array, $key, $sign_type);
$client = new Client('api.mch.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->withBody($body = Help::toXml($array));
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->post($this->transfers);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$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);
}
}
}