初始化项目
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\common\library\leshua;
|
||||
|
||||
/**
|
||||
* error code 说明.
|
||||
* <ul>
|
||||
* <li>-41001: encodingAesKey 非法</li>
|
||||
* <li>-41003: aes 解密失败</li>
|
||||
* <li>-41004: 解密后得到的buffer非法</li>
|
||||
* <li>-41005: base64加密失败</li>
|
||||
* <li>-41016: base64解密失败</li>
|
||||
* </ul>
|
||||
*/
|
||||
class ErrorCode
|
||||
{
|
||||
public static $OK = 0;
|
||||
public static $IllegalAesKey = -41001;
|
||||
public static $IllegalIv = -41002;
|
||||
public static $IllegalBuffer = -41003;
|
||||
public static $DecodeBase64Error = -41004;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\common\library\leshua;
|
||||
|
||||
class LSBizDataCrypt
|
||||
{
|
||||
private $appid;
|
||||
private $sessionKey;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param string $appid string 小程序的appid
|
||||
* @param string $sessionKey string 用户在小程序登录后获取的会话密钥
|
||||
*/
|
||||
public function __construct(string $appid, string $sessionKey)
|
||||
{
|
||||
$this->sessionKey = $sessionKey;
|
||||
$this->appid = $appid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检验数据的真实性,并且获取解密后的明文.
|
||||
* @param string $encryptedData 加密的用户数据
|
||||
* @param string $iv 与用户数据一同返回的初始向量
|
||||
* @param mixed $content 解密后的原文
|
||||
* @return int 成功0,失败返回对应的错误码
|
||||
*/
|
||||
public function decryptData(string $encryptedData, string $iv, &$content): int
|
||||
{
|
||||
if (strlen($this->sessionKey) != 24) {
|
||||
return ErrorCode::$IllegalAesKey;
|
||||
}
|
||||
if (strlen($iv) != 24) {
|
||||
return ErrorCode::$IllegalIv;
|
||||
}
|
||||
$aesKey = base64_decode($this->sessionKey);
|
||||
$aesIV = base64_decode($iv);
|
||||
$aesCipher = base64_decode($encryptedData);
|
||||
$result = openssl_decrypt($aesCipher, 'AES-128-CBC', $aesKey, 1, $aesIV);
|
||||
|
||||
if (empty($result)) {
|
||||
return ErrorCode::$IllegalBuffer;
|
||||
}
|
||||
$resultArr = json_decode($result, true);
|
||||
if (empty($resultArr)) {
|
||||
return ErrorCode::$IllegalBuffer;
|
||||
}
|
||||
if ($resultArr['watermark']['appid'] != $this->appid) {
|
||||
return ErrorCode::$IllegalBuffer;
|
||||
}
|
||||
$content = $resultArr;
|
||||
return ErrorCode::$OK;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\common\library\leshua;
|
||||
|
||||
use think\facade\Cache;
|
||||
use cores\traits\ErrorTrait;
|
||||
use app\common\library\helper;
|
||||
use app\common\exception\BaseException;
|
||||
|
||||
/**
|
||||
* 乐刷api基类
|
||||
* Class leshua
|
||||
* @package app\library
|
||||
*/
|
||||
class LsBase
|
||||
{
|
||||
use ErrorTrait;
|
||||
|
||||
protected $appId;
|
||||
protected $appSecret;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* WxBase constructor.
|
||||
* @param $appId
|
||||
* @param $appSecret
|
||||
*/
|
||||
public function __construct($appId = null, $appSecret = null)
|
||||
{
|
||||
$this->setConfig($appId, $appSecret);
|
||||
}
|
||||
|
||||
protected function setConfig($appId = null, $appSecret = null)
|
||||
{
|
||||
!empty($appId) && $this->appId = $appId;
|
||||
!empty($appSecret) && $this->appSecret = $appSecret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取access_token
|
||||
* @return mixed
|
||||
* @throws \cores\exception\BaseException
|
||||
*/
|
||||
protected function getAccessToken()
|
||||
{
|
||||
$cacheKey = $this->appId . '@access_token';
|
||||
if (!Cache::get($cacheKey)) {
|
||||
// 请求API获取 access_token
|
||||
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}";
|
||||
$result = $this->get($url);
|
||||
$response = $this->jsonDecode($result);
|
||||
if (array_key_exists('errcode', $response)) {
|
||||
throwError("access_token获取失败,错误信息:{$result}");
|
||||
}
|
||||
// 记录日志
|
||||
log_record([
|
||||
'name' => '获取access_token',
|
||||
'url' => $url,
|
||||
'appId' => $this->appId,
|
||||
'result' => $result
|
||||
]);
|
||||
// 写入缓存
|
||||
Cache::set($cacheKey, $response['access_token'], 6000);
|
||||
}
|
||||
return Cache::get($cacheKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟GET请求 HTTPS的页面
|
||||
* @param string $url 请求地址
|
||||
* @param array $data
|
||||
* @return string $result
|
||||
* @throws \cores\exception\BaseException
|
||||
*/
|
||||
protected function get(string $url, array $data = [])
|
||||
{
|
||||
// 处理query参数
|
||||
if (!empty($data)) {
|
||||
$url = $url . '?' . http_build_query($data);
|
||||
}
|
||||
$curl = curl_init();
|
||||
curl_setopt($curl, CURLOPT_URL, $url);
|
||||
curl_setopt($curl, CURLOPT_HEADER, 0);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
|
||||
$result = curl_exec($curl);
|
||||
if ($result === false) {
|
||||
throwError(curl_error($curl));
|
||||
}
|
||||
curl_close($curl);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟POST请求
|
||||
* @param string $url 请求地址
|
||||
* @param mixed $data 请求数据
|
||||
* @param false $useCert 是否引入微信支付证书
|
||||
* @param array $sslCert 证书路径
|
||||
* @return mixed|bool|string
|
||||
* @throws \cores\exception\BaseException
|
||||
*/
|
||||
protected function post(string $url, $data = [], bool $useCert = false, array $sslCert = [])
|
||||
{
|
||||
$header = [
|
||||
'Content-type: application/json;'
|
||||
];
|
||||
$curl = curl_init();
|
||||
curl_setopt($curl, CURLOPT_URL, $url);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
|
||||
curl_setopt($curl, CURLOPT_HEADER, false);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($curl, CURLOPT_POST, TRUE);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
|
||||
if ($useCert == true) {
|
||||
// 设置证书:cert 与 key 分别属于两个.pem文件
|
||||
curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
|
||||
curl_setopt($curl, CURLOPT_SSLCERT, $sslCert['certPem']);
|
||||
curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
|
||||
curl_setopt($curl, CURLOPT_SSLKEY, $sslCert['keyPem']);
|
||||
}
|
||||
$result = curl_exec($curl);
|
||||
if ($result === false) {
|
||||
throwError(curl_error($curl));
|
||||
}
|
||||
curl_close($curl);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟POST请求 [第二种方式, 用于兼容微信api]
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
* @throws \cores\exception\BaseException
|
||||
*/
|
||||
protected function post2($url, array $data = [])
|
||||
{
|
||||
$header = [
|
||||
'Content-Type: application/x-www-form-urlencoded'
|
||||
];
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
|
||||
$result = curl_exec($ch);
|
||||
if ($result === false) {
|
||||
throwError(curl_error($ch));
|
||||
}
|
||||
curl_close($ch);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数组转json
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
protected function jsonEncode($data): string
|
||||
{
|
||||
return helper::jsonEncode($data, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* json转数组
|
||||
* @param $json
|
||||
* @return mixed
|
||||
*/
|
||||
protected function jsonDecode($json)
|
||||
{
|
||||
return helper::jsonDecode($json);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,360 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\library\leshua;
|
||||
|
||||
use app\common\model\wxapp\Setting as WxappSettingModel;
|
||||
use app\common\enum\OrderType as OrderTypeEnum;
|
||||
use app\common\enum\order\PayType as OrderPayTypeEnum;
|
||||
use app\common\library\helper;
|
||||
use app\common\exception\BaseException;
|
||||
|
||||
/**
|
||||
* 乐刷支付
|
||||
* Class LsPay
|
||||
* @package app\common\library\leshua
|
||||
*/
|
||||
class LsPay extends LsBase
|
||||
{
|
||||
// 乐刷支付配置
|
||||
private $config;
|
||||
|
||||
// 当前商城ID
|
||||
private $storeId;
|
||||
|
||||
// 订单模型
|
||||
private $modelClass = [
|
||||
OrderTypeEnum::ORDER => \app\api\service\order\PaySuccess::class,
|
||||
OrderTypeEnum::RECHARGE => \app\api\service\recharge\PaySuccess::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param array $config
|
||||
* @param int|null $storeId
|
||||
*/
|
||||
public function __construct(array $config = [], ?int $storeId = null)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->config = $config;
|
||||
$this->storeId = $storeId;
|
||||
if (!empty($this->config)) {
|
||||
$this->setConfig($this->config['app_id'], $this->config['app_secret']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一下单API
|
||||
* @param $orderNo
|
||||
* @param $openid
|
||||
* @param $totalFee
|
||||
* @param int $orderType 订单类型
|
||||
* @return array
|
||||
* @throws \cores\exception\BaseException
|
||||
*/
|
||||
public function unifiedorder($orderNo, $openid, $totalFee, int $orderType = OrderTypeEnum::ORDER): array
|
||||
{
|
||||
// 当前时间
|
||||
$time = time();
|
||||
// 生成随机字符串
|
||||
$nonceStr = md5($time . $openid);
|
||||
// API参数
|
||||
$params = [
|
||||
'service' => 'get_tdcode',
|
||||
'pay_way' => 'WXZF',
|
||||
'merchant_id' => $this->config['mchid'],
|
||||
'third_order_id' => $orderNo,
|
||||
'amount' => (int)helper::bcmul($totalFee, 100), // 价格:单位分
|
||||
'jspay_flag' => 3,
|
||||
'appid' => $this->appId,
|
||||
'sub_openid' => $openid,
|
||||
'notify_url' => base_url() . 'leshua.php', // 异步通知地址
|
||||
'body' => $orderNo,
|
||||
'attach' => helper::jsonEncode(['order_type' => $orderType]),
|
||||
'nonce_str' => $nonceStr,
|
||||
//'spbill_create_ip' => \request()->ip(),
|
||||
'order_expiration' => 60,
|
||||
];
|
||||
// 生成签名
|
||||
$params['sign'] = $this->makeSign($params);
|
||||
// 请求API
|
||||
$url = 'https://paygate.leshuazf.com/cgi-bin/lepos_pay_gateway.cgi';
|
||||
$result = $this->post($url, $params);
|
||||
$prepay = $this->fromXml($result);
|
||||
// 请求失败
|
||||
if ($prepay['resp_code'] != '0') {
|
||||
$errMsg = "乐刷支付api:{$prepay['resp_msg']}";
|
||||
throwError($errMsg, null, ['errorCode' => 'LESHUA_PAY', 'isCreated' => true]);
|
||||
}
|
||||
if ($prepay['result_code'] != '0') {
|
||||
$errMsg = "乐刷支付api:{$prepay['error_msg']}";
|
||||
throwError($errMsg, null, ['errorCode' => 'LESHUA_PAY', 'isCreated' => true]);
|
||||
}
|
||||
$jspayInfo = helper::jsonDecode($prepay['jspay_info']);
|
||||
$prepayId = explode('=', $jspayInfo['package'])[1];
|
||||
// 生成 nonce_str 供前端使用
|
||||
// $paySign = $this->makePaySign($params['nonce_str'], $prepayId, $time);
|
||||
return [
|
||||
'prepay_id' => $prepayId,
|
||||
'nonceStr' => $nonceStr,
|
||||
'timeStamp' => (string)$time,
|
||||
'paySign' => $jspayInfo['paySign']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付成功异步通知
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function notify()
|
||||
{
|
||||
if (!$xml = file_get_contents('php://input')) {
|
||||
$this->returnCode(false, 'Not found DATA');
|
||||
}
|
||||
// 将服务器返回的XML数据转化为数组
|
||||
$data = $this->fromXml($xml);
|
||||
// 记录日志
|
||||
log_record($xml);
|
||||
log_record($data);
|
||||
// 实例化订单模型
|
||||
$model = $this->getOrderModel($data['third_order_id'], $data['attach']);
|
||||
// 订单信息
|
||||
$order = $model->getOrderInfo();
|
||||
empty($order) && $this->returnCode(false, '订单不存在');
|
||||
// 小程序配置信息
|
||||
$wxConfig = WxappSettingModel::getWxappConfig($order['store_id']);
|
||||
// 设置支付秘钥
|
||||
$this->config['apikey'] = $wxConfig['apikey'];
|
||||
// 保存微信服务器返回的签名sign
|
||||
$dataSign = $data['sign'];
|
||||
// sign不参与签名算法
|
||||
unset($data['sign']);
|
||||
// 生成签名
|
||||
$sign = $this->makeSign($data);
|
||||
// 判断签名是否正确 判断支付状态
|
||||
if (
|
||||
($sign !== $dataSign)
|
||||
|| ($data['return_code'] !== 'SUCCESS')
|
||||
|| ($data['result_code'] !== 'SUCCESS')
|
||||
) {
|
||||
$this->returnCode(false, '签名失败');
|
||||
}
|
||||
// 订单支付成功业务处理
|
||||
$status = $model->onPaySuccess(OrderPayTypeEnum::WECHAT, $data);
|
||||
if ($status == false) {
|
||||
$this->returnCode(false, $model->getError());
|
||||
}
|
||||
// 返回状态
|
||||
$this->returnCode(true, 'OK');
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请退款API
|
||||
* @param string $transactionId 微信支付交易流水号
|
||||
* @param string $totalFee 订单总金额
|
||||
* @param string $refundFee 退款金额
|
||||
* @return bool
|
||||
* @throws BaseException
|
||||
* @throws \cores\exception\BaseException
|
||||
*/
|
||||
public function refund(string $transactionId, string $totalFee, string $refundFee): bool
|
||||
{
|
||||
// 当前时间
|
||||
$time = time();
|
||||
// 生成随机字符串
|
||||
$nonceStr = md5($time . $transactionId . $totalFee . $refundFee);
|
||||
// API参数
|
||||
$params = [
|
||||
'appid' => $this->appId,
|
||||
'mch_id' => $this->config['mchid'],
|
||||
'nonce_str' => $nonceStr,
|
||||
'transaction_id' => $transactionId,
|
||||
'out_refund_no' => $time,
|
||||
'total_fee' => (int)helper::bcmul($totalFee,100),
|
||||
'refund_fee' => (int)helper::bcmul($refundFee,100),
|
||||
];
|
||||
// 生成签名
|
||||
$params['sign'] = $this->makeSign($params);
|
||||
// 请求API
|
||||
$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
|
||||
$result = $this->post($url, $this->toXml($params), true, $this->getCertPem());
|
||||
// 请求失败
|
||||
if (empty($result)) {
|
||||
throwError('微信退款api请求失败');
|
||||
}
|
||||
// 格式化返回结果
|
||||
$prepay = $this->fromXml($result);
|
||||
// 记录日志
|
||||
log_record(['name' => '微信退款API', [
|
||||
'params' => $params,
|
||||
'result' => $result,
|
||||
'prepay' => $prepay
|
||||
]]);
|
||||
// 请求失败
|
||||
if ($prepay['return_code'] === 'FAIL') {
|
||||
throwError("return_msg: {$prepay['return_msg']}");
|
||||
}
|
||||
if ($prepay['result_code'] === 'FAIL') {
|
||||
throwError("err_code_des: {$prepay['err_code_des']}");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取cert证书文件
|
||||
* @return string[]
|
||||
* @throws \cores\exception\BaseException
|
||||
*/
|
||||
private function getCertPem(): array
|
||||
{
|
||||
if (empty($this->config['cert_pem']) || empty($this->config['key_pem'])) {
|
||||
throwError('请先到后台小程序设置填写微信支付证书文件');
|
||||
}
|
||||
// cert目录
|
||||
$filePath = __DIR__ . "/cert/{$this->storeId}/";
|
||||
return [
|
||||
'certPem' => $filePath . 'cert.pem',
|
||||
'keyPem' => $filePath . 'key.pem'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 实例化订单模型 (根据attach判断)
|
||||
* @param $orderNo
|
||||
* @param null $attach
|
||||
* @return mixed
|
||||
*/
|
||||
private function getOrderModel($orderNo, $attach = null)
|
||||
{
|
||||
$attach = helper::jsonDecode($attach);
|
||||
// 判断订单类型返回对应的订单模型
|
||||
$model = $this->modelClass[$attach['order_type']];
|
||||
return new $model($orderNo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回状态给微信服务器
|
||||
* @param boolean $returnCode
|
||||
* @param string|null $msg
|
||||
*/
|
||||
private function returnCode(bool $returnCode = true, string $msg = null)
|
||||
{
|
||||
// 返回状态
|
||||
$return = [
|
||||
'return_code' => $returnCode ? 'SUCCESS' : 'FAIL',
|
||||
'return_msg' => $msg ?: 'OK',
|
||||
];
|
||||
// 记录日志
|
||||
log_record([
|
||||
'name' => '返回微信支付状态',
|
||||
'data' => $return
|
||||
]);
|
||||
die($this->toXml($return));
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成paySign
|
||||
* @param string $nonceStr
|
||||
* @param string $prepayId
|
||||
* @param int $timeStamp
|
||||
* @return string
|
||||
*/
|
||||
private function makePaySign(string $nonceStr, string $prepayId, int $timeStamp): string
|
||||
{
|
||||
$data = [
|
||||
'appId' => $this->appId,
|
||||
'nonceStr' => $nonceStr,
|
||||
'package' => 'prepay_id=' . $prepayId,
|
||||
'signType' => 'MD5',
|
||||
'timeStamp' => $timeStamp,
|
||||
];
|
||||
// 签名步骤一:按字典序排序参数
|
||||
ksort($data);
|
||||
$string = $this->toUrlParams($data);
|
||||
// 签名步骤二:在string后加入KEY
|
||||
$string = $string . '&key=' . $this->config['apikey'];
|
||||
// 签名步骤三:MD5加密
|
||||
$string = md5($string);
|
||||
// 签名步骤四:所有字符转为大写
|
||||
return strtoupper($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将xml转为array
|
||||
* @param string $xml
|
||||
* @return mixed
|
||||
*/
|
||||
private function fromXml(string $xml)
|
||||
{
|
||||
// 禁止引用外部xml实体
|
||||
libxml_disable_entity_loader(true);
|
||||
return helper::jsonDecode(helper::jsonEncode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成签名
|
||||
* @param array $values
|
||||
* @return string 本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
|
||||
*/
|
||||
private function makeSign(array $values): string
|
||||
{
|
||||
//签名步骤一:按字典序排序参数
|
||||
ksort($values);
|
||||
$string = $this->toUrlParams($values);
|
||||
//签名步骤二:在string后加入KEY
|
||||
$string = $string . '&key=' . $this->config['apikey'];
|
||||
//签名步骤三:MD5加密
|
||||
$string = md5($string);
|
||||
//签名步骤四:所有字符转为大写
|
||||
return strtoupper($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化参数格式化成url参数
|
||||
* @param array $values
|
||||
* @return string
|
||||
*/
|
||||
private function toUrlParams(array $values): string
|
||||
{
|
||||
$buff = '';
|
||||
foreach ($values as $k => $v) {
|
||||
if ($k != 'sign' && $v != '' && !is_array($v)) {
|
||||
$buff .= $k . '=' . $v . '&';
|
||||
}
|
||||
}
|
||||
return trim($buff, '&');
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出xml字符
|
||||
* @param array $values
|
||||
* @return bool|string
|
||||
*/
|
||||
private function toXml(array $values)
|
||||
{
|
||||
if (!is_array($values)
|
||||
|| count($values) <= 0
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
$xml = "<xml>";
|
||||
foreach ($values as $key => $val) {
|
||||
if (is_numeric($val)) {
|
||||
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
|
||||
} else {
|
||||
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
|
||||
}
|
||||
}
|
||||
$xml .= "</xml>";
|
||||
return $xml;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user