add clear
This commit is contained in:
+16
-9
@@ -5,23 +5,30 @@
|
||||
* Date: 2018/4/19 0019
|
||||
* Time: 16:12
|
||||
*/
|
||||
|
||||
namespace wchat;
|
||||
class Account extends Base
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $code
|
||||
* @return Result
|
||||
*/
|
||||
public function login($code)
|
||||
{
|
||||
return WxClient::get('sns/jscode2session', [
|
||||
'appid' => $this->appid,
|
||||
'secret' => $this->appsecret,
|
||||
'js_code' => $code,
|
||||
'grant_type' => 'authorization_code'
|
||||
], null, ['Content-Type' => 'text/xml']);
|
||||
$param = [
|
||||
'sns/jscode2session',
|
||||
[
|
||||
'appid' => $this->appid,
|
||||
'secret' => $this->appsecret,
|
||||
'js_code' => $code,
|
||||
'grant_type' => 'authorization_code'
|
||||
],
|
||||
null,
|
||||
['Content-Type' => 'text/xml']
|
||||
];
|
||||
return WxClient::get(...$param);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
-113
@@ -180,65 +180,6 @@ abstract class Base
|
||||
return $this->nonce_str = implode($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public static function toXml(array $data)
|
||||
{
|
||||
$xml = "<xml>";
|
||||
foreach ($data as $key => $val) {
|
||||
if (is_numeric($val)) {
|
||||
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
|
||||
} else {
|
||||
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
|
||||
}
|
||||
}
|
||||
$xml .= "</xml>";
|
||||
return $xml;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function sign(array $array)
|
||||
{
|
||||
ksort($array, SORT_STRING);
|
||||
$string = '';
|
||||
foreach ($array as $key => $val) {
|
||||
if (empty($string)) {
|
||||
$string = $key . '=' . $val;
|
||||
} else {
|
||||
$string .= '&' . $key . '=' . $val;
|
||||
}
|
||||
}
|
||||
$string .= '&key=' . $this->key;
|
||||
|
||||
// var_dump($string);
|
||||
|
||||
if ($this->sign_type == 'MD5') {
|
||||
return strtoupper(md5($string));
|
||||
} else {
|
||||
return hash('sha256', $string);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @return mixed
|
||||
*/
|
||||
public function toArray($xml)
|
||||
{
|
||||
if (!is_null($json = json_decode($xml, TRUE))) {
|
||||
return $json;
|
||||
}
|
||||
if (is_array($json)) {
|
||||
return $json;
|
||||
}
|
||||
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||
return json_decode(json_encode($data), TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|mixed|string
|
||||
@@ -317,58 +258,4 @@ abstract class Base
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
public static $OK = 0;
|
||||
public static $IllegalAesKey = -41001;
|
||||
public static $IllegalIv = -41002;
|
||||
public static $IllegalBuffer = -41003;
|
||||
public static $DecodeBase64Error = -41004;
|
||||
|
||||
public static function d($code)
|
||||
{
|
||||
$messages = [
|
||||
static::$OK => '',
|
||||
static::$IllegalAesKey => '',
|
||||
static::$IllegalIv => '',
|
||||
static::$IllegalBuffer => '',
|
||||
static::$DecodeBase64Error => '',
|
||||
];
|
||||
return $messages[$code] ?? static::$DecodeBase64Error;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $encryptedData
|
||||
* @param $iv
|
||||
* @param $sessionKey
|
||||
* @param $data
|
||||
* @param null $appId
|
||||
* @return int
|
||||
*/
|
||||
public static function decode($encryptedData, $iv, $sessionKey, &$data, $appId = null)
|
||||
{
|
||||
if (strlen($sessionKey) != 24) {
|
||||
return self::$IllegalAesKey;
|
||||
}
|
||||
|
||||
flush();
|
||||
$aesKey = base64_decode($sessionKey);
|
||||
if (strlen($iv) != 24) {
|
||||
return self::$IllegalIv;
|
||||
}
|
||||
|
||||
$aesIV = base64_decode($iv);
|
||||
$aesCipher = base64_decode($encryptedData);
|
||||
|
||||
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, OPENSSL_RAW_DATA, $aesIV);
|
||||
if ($result === false) {
|
||||
return self::$IllegalBuffer;
|
||||
}
|
||||
|
||||
$dataObj = json_decode($result);
|
||||
if ($dataObj->watermark->appid != $appId) {
|
||||
return self::$IllegalBuffer;
|
||||
}
|
||||
$data = $dataObj;
|
||||
return self::$OK;
|
||||
}
|
||||
}
|
||||
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat;
|
||||
|
||||
|
||||
class Help extends Base
|
||||
{
|
||||
public static $OK = 0;
|
||||
public static $IllegalAesKey = -41001;
|
||||
public static $IllegalIv = -41002;
|
||||
public static $IllegalBuffer = -41003;
|
||||
public static $DecodeBase64Error = -41004;
|
||||
|
||||
public static function d($code)
|
||||
{
|
||||
$messages = [
|
||||
static::$OK => '',
|
||||
static::$IllegalAesKey => '',
|
||||
static::$IllegalIv => '',
|
||||
static::$IllegalBuffer => '',
|
||||
static::$DecodeBase64Error => '',
|
||||
];
|
||||
return $messages[$code] ?? static::$DecodeBase64Error;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $encryptedData
|
||||
* @param $iv
|
||||
* @param $sessionKey
|
||||
* @param $data
|
||||
* @param null $appId
|
||||
* @return int
|
||||
*/
|
||||
public static function decode($encryptedData, $iv, $sessionKey, &$data, $appId = null)
|
||||
{
|
||||
if (strlen($sessionKey) != 24) {
|
||||
return self::$IllegalAesKey;
|
||||
}
|
||||
|
||||
flush();
|
||||
$aesKey = base64_decode($sessionKey);
|
||||
if (strlen($iv) != 24) {
|
||||
return self::$IllegalIv;
|
||||
}
|
||||
|
||||
$aesIV = base64_decode($iv);
|
||||
$aesCipher = base64_decode($encryptedData);
|
||||
|
||||
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, OPENSSL_RAW_DATA, $aesIV);
|
||||
if ($result === false) {
|
||||
return self::$IllegalBuffer;
|
||||
}
|
||||
|
||||
$dataObj = json_decode($result);
|
||||
if ($dataObj->watermark->appid != $appId) {
|
||||
return self::$IllegalBuffer;
|
||||
}
|
||||
$data = $dataObj;
|
||||
return self::$OK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public static function toXml(array $data)
|
||||
{
|
||||
$xml = "<xml>";
|
||||
foreach ($data as $key => $val) {
|
||||
if (is_numeric($val)) {
|
||||
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
|
||||
} else {
|
||||
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
|
||||
}
|
||||
}
|
||||
$xml .= "</xml>";
|
||||
return $xml;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @return mixed
|
||||
*/
|
||||
public static function toArray($xml)
|
||||
{
|
||||
if (!is_null($json = json_decode($xml, TRUE))) {
|
||||
return $json;
|
||||
}
|
||||
if (is_array($json)) {
|
||||
return $json;
|
||||
}
|
||||
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||
return json_decode(json_encode($data), TRUE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @param $key
|
||||
* @param $type
|
||||
* @return string
|
||||
*/
|
||||
public static function sign(array $array, $key, $type)
|
||||
{
|
||||
ksort($array, SORT_STRING);
|
||||
$string = '';
|
||||
foreach ($array as $key => $val) {
|
||||
if (empty($string)) {
|
||||
$string = $key . '=' . $val;
|
||||
} else {
|
||||
$string .= '&' . $key . '=' . $val;
|
||||
}
|
||||
}
|
||||
$string .= '&key=' . $key;
|
||||
|
||||
if ($type == 'MD5') {
|
||||
return strtoupper(md5($string));
|
||||
} else {
|
||||
return hash('sha256', $string);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+128
-105
@@ -11,124 +11,147 @@ namespace wchat;
|
||||
class Recharge extends Base
|
||||
{
|
||||
|
||||
/** @var Recharge */
|
||||
private static $recharge;
|
||||
/** @var Recharge */
|
||||
private static $recharge;
|
||||
|
||||
private $money = 0;
|
||||
private $money = 0;
|
||||
|
||||
private $orderNo;
|
||||
private $orderNo;
|
||||
|
||||
private $data = [];
|
||||
private $data = [];
|
||||
|
||||
/**
|
||||
* @param int $money
|
||||
* @param string $orderNo
|
||||
* @return bool|Result
|
||||
*/
|
||||
public function payment(int $money, string $orderNo, $openId = NULL)
|
||||
{
|
||||
$_this = $this;
|
||||
if ($money < 0) {
|
||||
return new Result(['code' => 500, 'message' => '充值金额不能小于0.']);
|
||||
}
|
||||
$this->money = $money;
|
||||
$this->orderNo = $orderNo;
|
||||
$this->data['openid'] = $openId;
|
||||
return WxClient::post($this->createPayUrl(), $this->builder(),
|
||||
function ($result, $body) use ($_this) {
|
||||
$data = $_this->toArray($result);
|
||||
if (isset($data['sign'])) {
|
||||
$sign = $data['sign'];
|
||||
unset($data['sign']);
|
||||
$_sign = $_this->sign($data);
|
||||
}
|
||||
$return = [];
|
||||
if (!isset($sign) || $sign != $_sign) {
|
||||
$return['code'] = -1;
|
||||
$return['message'] = $data['return_msg'] ?? '返回数据签名验证失败';
|
||||
} else {
|
||||
if ($data['return_code'] == 'FAIL') {
|
||||
$return['code'] = -1;
|
||||
$return['message'] = $data['return_msg'];
|
||||
} else {
|
||||
$return['code'] = 0;
|
||||
$return['data'] = $data;
|
||||
$return['data']['postBody'] = $body;
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}, ['Content-Type' => 'text/xml']
|
||||
);
|
||||
}
|
||||
/**
|
||||
* @param int $money
|
||||
* @param string $orderNo
|
||||
* @return bool|Result
|
||||
*/
|
||||
public function payment(int $money, string $orderNo, $openId = NULL)
|
||||
{
|
||||
if ($money < 0) {
|
||||
return new Result(['code' => 500, 'message' => '充值金额不能小于0.']);
|
||||
}
|
||||
$this->money = $money;
|
||||
$this->orderNo = $orderNo;
|
||||
$this->data['openid'] = $openId;
|
||||
|
||||
$params = [
|
||||
$this->mch_host . '/pay/unifiedorder',
|
||||
$this->builder(),
|
||||
[$this, 'payCallback'],
|
||||
['Content-Type' => 'text/xml']
|
||||
];
|
||||
|
||||
return WxClient::post(...$params);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function builder()
|
||||
{
|
||||
$data = [
|
||||
'appid' => $this->appid,
|
||||
'mch_id' => $this->mch_id,
|
||||
'nonce_str' => $this->random(32),
|
||||
'body' => $this->body,
|
||||
'out_trade_no' => $this->orderNo,
|
||||
'total_fee' => $this->money,
|
||||
'sign_type' => $this->sign_type,
|
||||
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
|
||||
'notify_url' => $this->notify_url,
|
||||
'trade_type' => $this->trade_type,
|
||||
];
|
||||
/**
|
||||
* @param $result
|
||||
* @param $body
|
||||
* @return array
|
||||
*/
|
||||
public function payCallback($result, $body)
|
||||
{
|
||||
$data = Help::toArray($result);
|
||||
if (isset($data['sign'])) {
|
||||
$sign = $data['sign'];
|
||||
unset($data['sign']);
|
||||
$_sign = Help::sign($data, $this->key, $this->sign_type);
|
||||
}
|
||||
$return = [];
|
||||
if (!isset($sign) || $sign != $_sign) {
|
||||
$return['code'] = -1;
|
||||
$return['message'] = $data['return_msg'] ?? '返回数据签名验证失败';
|
||||
} else {
|
||||
if ($data['return_code'] == 'FAIL') {
|
||||
$return['code'] = -1;
|
||||
$return['message'] = $data['return_msg'];
|
||||
} else {
|
||||
$return['code'] = 0;
|
||||
$return['data'] = $data;
|
||||
$return['data']['postBody'] = $body;
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
$data = array_merge($data, $this->data);
|
||||
|
||||
$data['sign'] = $this->sign($data);
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function builder()
|
||||
{
|
||||
$data = [
|
||||
'appid' => $this->appid,
|
||||
'mch_id' => $this->mch_id,
|
||||
'nonce_str' => $this->random(32),
|
||||
'body' => $this->body,
|
||||
'out_trade_no' => $this->orderNo,
|
||||
'total_fee' => $this->money,
|
||||
'sign_type' => $this->sign_type,
|
||||
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
|
||||
'notify_url' => $this->notify_url,
|
||||
'trade_type' => $this->trade_type,
|
||||
];
|
||||
|
||||
return static::toXml($data);
|
||||
}
|
||||
$data = array_merge($data, $this->data);
|
||||
|
||||
private function createPayUrl()
|
||||
{
|
||||
return $this->mch_host . '/pay/unifiedorder';
|
||||
}
|
||||
$data['sign'] = Help::sign($data, $this->key, $this->sign_type);
|
||||
|
||||
/**
|
||||
* @param $money
|
||||
* @param $openid
|
||||
* @param $order
|
||||
* @param $REMOTE_ADDR
|
||||
* @return Result
|
||||
* @throws
|
||||
*
|
||||
* 提现
|
||||
*/
|
||||
public function tx($money, $openid, $order, $REMOTE_ADDR, $desc = NULL)
|
||||
{
|
||||
$array = [
|
||||
'nonce_str' => $this->random(32),
|
||||
'partner_trade_no' => $order,
|
||||
'mchid' => $this->mch_id,
|
||||
'mch_appid' => $this->appid,
|
||||
'openid' => $openid,
|
||||
'check_name' => 'NO_CHECK',
|
||||
'amount' => $money * 100,
|
||||
'spbill_create_ip' => $REMOTE_ADDR,
|
||||
'desc' => $desc ?? '有大佬给你发红包啦.',
|
||||
];
|
||||
return Help::toXml($data);
|
||||
}
|
||||
|
||||
$transfers = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
|
||||
/**
|
||||
* @param $money
|
||||
* @param $openid
|
||||
* @param $order
|
||||
* @param $REMOTE_ADDR
|
||||
* @return Result
|
||||
* @throws
|
||||
*
|
||||
* 提现
|
||||
*/
|
||||
public function tx($money, $openid, $order, $REMOTE_ADDR, $desc = NULL)
|
||||
{
|
||||
$array = [
|
||||
'nonce_str' => $this->random(32),
|
||||
'partner_trade_no' => $order,
|
||||
'mchid' => $this->mch_id,
|
||||
'mch_appid' => $this->appid,
|
||||
'openid' => $openid,
|
||||
'check_name' => 'NO_CHECK',
|
||||
'amount' => $money * 100,
|
||||
'spbill_create_ip' => $REMOTE_ADDR,
|
||||
'desc' => $desc ?? '有大佬给你发红包啦 . ',
|
||||
];
|
||||
|
||||
$array['sign'] = $this->sign($array);
|
||||
$array['sign'] = Help::sign($array, $this->key, $this->sign_type);
|
||||
$prams = [
|
||||
$this->mch_host . '/mmpaymkttransfers/promotion/transfers',
|
||||
Help::toXml($array),
|
||||
[$this, 'txCallback'],
|
||||
NULL,
|
||||
[$this->ssl_cert, $this->ssl_key]
|
||||
];
|
||||
|
||||
return WxClient::post($transfers, static::toXml($array), function ($data) {
|
||||
$array = $this->toArray($data);
|
||||
if ($array['result_code'] != 'SUCCESS') {
|
||||
$data = ['code' => $array['err_code'], 'message' => $array['err_code_des']];
|
||||
} else {
|
||||
$data = ['code' => 0, 'message' => '支付成功'];
|
||||
}
|
||||
return new Result($data);
|
||||
}, NULL, [$this->ssl_cert, $this->ssl_key]);
|
||||
}
|
||||
return WxClient::post(...$prams);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return Result
|
||||
* 提现回调
|
||||
*/
|
||||
public function txCallback($data)
|
||||
{
|
||||
$array = Help::toArray($data);
|
||||
if ($array['result_code'] != 'SUCCESS') {
|
||||
$data = ['code' => $array['err_code'], 'message' => $array['err_code_des']];
|
||||
} else {
|
||||
$data = ['code' => 0, 'message' => '支付成功'];
|
||||
}
|
||||
return new Result($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+128
-16
@@ -11,20 +11,132 @@ namespace wchat;
|
||||
class Template extends Base
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $openId
|
||||
* @param $message
|
||||
* @param $form_id
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*
|
||||
* 奴隶交易通知
|
||||
*/
|
||||
public function sendTemplate(string $access,array $postBody)
|
||||
{
|
||||
$url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access;
|
||||
$postBody = json_encode($postBody);
|
||||
return WxClient::post($url, $postBody, NULL, ['content-type' => 'application/json'])
|
||||
->append('postBody', $postBody);
|
||||
}
|
||||
private $keywords = [];
|
||||
private $templateId = '';
|
||||
private $formId = '';
|
||||
private $openId = '';
|
||||
private $defaultUrl = '';
|
||||
private $page = 'pages/index/index';
|
||||
private $emphasis_keyword = '';
|
||||
|
||||
/** @var Template $instance */
|
||||
private static $instance = null;
|
||||
|
||||
/**
|
||||
* @return Template
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (static::$instance === null) {
|
||||
static::$instance = new Template();
|
||||
}
|
||||
return static::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $keywords
|
||||
*/
|
||||
public function setKeywords(array $keywords)
|
||||
{
|
||||
$this->keywords = $keywords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $templateId
|
||||
*/
|
||||
public function setTemplateId(string $templateId)
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $formId
|
||||
*/
|
||||
public function setFormId(string $formId)
|
||||
{
|
||||
$this->formId = $formId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $openId
|
||||
*/
|
||||
public function setOpenId(string $openId)
|
||||
{
|
||||
$this->openId = $openId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $defaultUrl
|
||||
*/
|
||||
public function setDefaultUrl(string $defaultUrl)
|
||||
{
|
||||
$this->defaultUrl = $defaultUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $page
|
||||
*/
|
||||
public function setPage(string $page)
|
||||
{
|
||||
$this->page = $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $emphasis_keyword
|
||||
*/
|
||||
public function setEmphasisKeyword(string $emphasis_keyword)
|
||||
{
|
||||
$this->emphasis_keyword = $emphasis_keyword;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $index
|
||||
* @param $context
|
||||
*/
|
||||
public function replaceKeyword($index, $context)
|
||||
{
|
||||
$this->keywords['keyword' . $index] = [
|
||||
'value' => $context
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $index
|
||||
* @param $context
|
||||
*/
|
||||
public function addKeyword($context)
|
||||
{
|
||||
$this->keywords['keyword' . count($this->keywords)] = [
|
||||
'value' => $context
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $access
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*
|
||||
* 奴隶交易通知
|
||||
*/
|
||||
public function sendTemplate(string $access)
|
||||
{
|
||||
$url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access;
|
||||
|
||||
$params = [
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"page" => $this->page,
|
||||
"form_id" => $this->formId,
|
||||
"data" => $this->keywords,
|
||||
];
|
||||
|
||||
if (!empty($this->emphasis_keyword)) {
|
||||
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
||||
}
|
||||
|
||||
$header = ['content-type' => 'application/json'];
|
||||
return WxClient::post($url, $params, NULL, $header)
|
||||
->append('postBody', $params);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user