初始化项目
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\wechat;
|
||||
|
||||
/**
|
||||
* 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,60 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2024 https://www..com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 科技 <admin@.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\common\library\wechat;
|
||||
|
||||
use app\common\library\helper;
|
||||
use cores\exception\BaseException;
|
||||
|
||||
/**
|
||||
* 小程序二维码
|
||||
* Class Qrcode
|
||||
* @package app\common\library\wechat
|
||||
*/
|
||||
class Qrcode extends WxBase
|
||||
{
|
||||
/**
|
||||
* 获取小程序码
|
||||
* API文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
|
||||
* @param string $scene 场景值 (例如: uid:10001)
|
||||
* @param string|null $page 页面地址
|
||||
* @param int $width 二维码宽度
|
||||
* @return mixed
|
||||
* @throws BaseException
|
||||
*/
|
||||
public function getQrcode(string $scene, string $page = null, int $width = 430)
|
||||
{
|
||||
// 微信接口url
|
||||
$accessToken = $this->getAccessToken();
|
||||
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
|
||||
// 构建请求
|
||||
$data = compact('scene', 'width');
|
||||
!is_null($page) && $data['page'] = $page;
|
||||
// 返回结果
|
||||
$result = $this->post($url, helper::jsonEncode($data));
|
||||
// 记录日志
|
||||
log_record([
|
||||
'name' => '获取小程序码',
|
||||
'params' => $data,
|
||||
'result' => !strpos($result, 'errcode') ? 'true' : $result
|
||||
]);
|
||||
if (!strpos($result, 'errcode')) {
|
||||
return $result;
|
||||
}
|
||||
$data = helper::jsonDecode($result);
|
||||
$error = '小程序码获取失败 ' . $data['errmsg'];
|
||||
if ($data['errcode'] == 41030) {
|
||||
$error = '小程序页面不存在,请先发布上线后再生成';
|
||||
}
|
||||
throwError($error);
|
||||
}
|
||||
}
|
||||
@@ -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\wechat;
|
||||
|
||||
class WXBizDataCrypt
|
||||
{
|
||||
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\wechat;
|
||||
|
||||
use think\facade\Cache;
|
||||
use cores\traits\ErrorTrait;
|
||||
use app\common\library\helper;
|
||||
use app\common\exception\BaseException;
|
||||
|
||||
/**
|
||||
* 微信api基类
|
||||
* Class wechat
|
||||
* @package app\library
|
||||
*/
|
||||
class WxBase
|
||||
{
|
||||
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,408 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\library\wechat;
|
||||
|
||||
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 cores\exception\BaseException;
|
||||
|
||||
/**
|
||||
* 微信支付
|
||||
* Class WxPay
|
||||
* @package app\common\library\wechat
|
||||
*/
|
||||
class WxPay extends WxBase
|
||||
{
|
||||
// 微信支付配置
|
||||
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 = [
|
||||
'appid' => $this->appId,
|
||||
'attach' => helper::jsonEncode(['order_type' => $orderType]),
|
||||
'body' => $orderNo,
|
||||
'mch_id' => $this->config['mchid'],
|
||||
'nonce_str' => $nonceStr,
|
||||
'notify_url' => base_url() . 'notice.php', // 异步通知地址
|
||||
'openid' => $openid,
|
||||
'out_trade_no' => $orderNo,
|
||||
'spbill_create_ip' => \request()->ip(),
|
||||
'total_fee' => (int)helper::bcmul($totalFee, 100), // 价格:单位分
|
||||
'trade_type' => 'JSAPI',
|
||||
];
|
||||
// 生成签名
|
||||
$params['sign'] = $this->makeSign($params);
|
||||
// 请求API
|
||||
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
|
||||
$result = $this->post($url, $this->toXml($params));
|
||||
$prepay = $this->fromXml($result);
|
||||
// 请求失败
|
||||
if ($prepay['return_code'] === 'FAIL') {
|
||||
$errMsg = "微信支付api:{$prepay['return_msg']}";
|
||||
throwError($errMsg, null, ['errorCode' => 'WECHAT_PAY', 'isCreated' => true]);
|
||||
}
|
||||
if ($prepay['result_code'] === 'FAIL') {
|
||||
$errMsg = "微信支付api:{$prepay['err_code_des']}";
|
||||
throwError($errMsg, null, ['errorCode' => 'WECHAT_PAY', 'isCreated' => true]);
|
||||
}
|
||||
// 生成 nonce_str 供前端使用
|
||||
$paySign = $this->makePaySign($params['nonce_str'], $prepay['prepay_id'], $time);
|
||||
return [
|
||||
'prepay_id' => $prepay['prepay_id'],
|
||||
'nonceStr' => $nonceStr,
|
||||
'timeStamp' => (string)$time,
|
||||
'paySign' => $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['out_trade_no'], $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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发货
|
||||
* API文档地址:https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html
|
||||
* @param string $transaction_id 微信支付交易id
|
||||
* @param string $desc 商品描述
|
||||
* @param string $openid 用户openid
|
||||
* @return mixed
|
||||
* @throws BaseException
|
||||
*/
|
||||
public function uploadShipping(string $transaction_id, string $desc, string $openid)
|
||||
{
|
||||
// 微信接口url
|
||||
$accessToken = $this->getAccessToken();
|
||||
$url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token={$accessToken}";
|
||||
$dt = new \DateTime('now', new \DateTimeZone('Asia/Shanghai'));
|
||||
$upload_time = $dt->format('Y-m-d\TH:i:s.vP');
|
||||
// 构建请求
|
||||
$data = [
|
||||
'order_key' => [
|
||||
'order_number_type' => 2,
|
||||
'transaction_id' => $transaction_id,
|
||||
],
|
||||
'logistics_type' => 3,
|
||||
'delivery_mode' => 1,
|
||||
'shipping_list' => [
|
||||
[
|
||||
'item_desc' => $desc,
|
||||
]
|
||||
],
|
||||
'upload_time' => $upload_time,
|
||||
'payer' => [
|
||||
'openid' => $openid
|
||||
]
|
||||
];
|
||||
// 返回结果
|
||||
$result = $this->post($url, helper::jsonEncode($data));
|
||||
// 记录日志
|
||||
log_record([
|
||||
'name' => '微信支付自动发货',
|
||||
'params' => $data,
|
||||
'result' => !strpos($result, 'errcode') ? 'true' : $result
|
||||
]);
|
||||
$data = helper::jsonDecode($result);
|
||||
if ($data['errcode'] == 0) {
|
||||
return $result;
|
||||
}
|
||||
$error = '微信支付自动发货 ' . $data['errmsg'];
|
||||
/*if ($data['errcode'] == 41030) {
|
||||
$error = '小程序页面不存在,请先发布上线后再生成';
|
||||
}*/
|
||||
// throwError($error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?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\wechat;
|
||||
|
||||
use app\common\library\helper;
|
||||
|
||||
/**
|
||||
* 微信小程序用户管理类
|
||||
* Class WxUser
|
||||
* @package app\common\library\wechat
|
||||
*/
|
||||
class WxUser extends WxBase
|
||||
{
|
||||
/**
|
||||
* 获取session_key
|
||||
* @param string $code
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function jscode2session(string $code)
|
||||
{
|
||||
/**
|
||||
* code 换取 session_key
|
||||
* 这是一个 HTTPS 接口,开发者服务器使用登录凭证 code 获取 session_key 和 openid。
|
||||
* 其中 session_key 是对用户数据进行加密签名的密钥。为了自身应用安全,session_key 不应该在网络上传输。
|
||||
*/
|
||||
$url = 'https://api.weixin.qq.com/sns/jscode2session';
|
||||
$result = helper::jsonDecode($this->get($url, [
|
||||
'appid' => $this->appId,
|
||||
'secret' => $this->appSecret,
|
||||
'grant_type' => 'authorization_code',
|
||||
'js_code' => $code
|
||||
]));
|
||||
if (isset($result['errcode'])) {
|
||||
$this->error = $result['errmsg'];
|
||||
return false;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
Reference in New Issue
Block a user