1.提交缺失的东西

This commit is contained in:
2025-05-16 17:34:53 +08:00
parent a5688bfbee
commit 24a9b88f8a
2 changed files with 363 additions and 0 deletions

View File

@ -0,0 +1,257 @@
<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\api\controller;
use app\api\model\Order;
use app\common\model\Order as CommonOrder;
use app\common\model\Client_;
use app\common\service\AliPayServer;
use app\common\service\ConfigServer;
use app\common\service\WeChatPayServer;
use app\common\service\WeChatServer;
use app\common\logic\PaymentLogic;
use app\common\model\Pay;
use think\facade\Db;
/**
* 支付逻辑
* Class Payment
* @package app\api\controller
*/
class PaymentController extends BaseApiController
{
public array $notNeedLogin = ['aliNotify', 'notifyMnp', 'notifyOa', 'notifyApp'];
/**
* Notes: 预支付
* @author 段誉(2021/2/23 14:33)
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function prepay()
{
$post = $this->request->post();
if(!isset($post['from']) || !isset($post['order_id']) || !isset($post['pay_way'])) {
return $this->fail('参数缺失');
}
switch ($post['from']) {
case 'order':
$order = Order::find($post['order_id']);
if ($order['order_status'] == CommonOrder::STATUS_CLOSE || $order['del'] == 1) {
return $this->fail('订单已关闭');
}
break;
case 'recharge':
$order = Db::name('recharge_order')->where(['id' => $post['order_id']])->find();
break;
}
//找不到订单
if (empty($order)) {
return $this->fail('订单不存在');
}
// 变更支付方式
$order['pay_way'] = $post['pay_way'];
//已支付
if ($order['pay_status'] == pay\Pay::ISPAID) {
return $this->success('支付成功', ['order_id' => $order['id']], 10000);
}
$result = PaymentLogic::pay($post['from'], $order, $this->client);
if (false === $result) {
return $this->fail(PaymentLogic::getError(), ['order_id' => $order['id']], PaymentLogic::getReturnCode());
}
if (PaymentLogic::getReturnCode() != 0) {
return $this->success('', $result, PaymentLogic::getReturnCode());
}
return $this->success('', $result);
}
/**
* Notes: pc端预支付 NATIVE
* @author 段誉(2021/3/18 16:03)
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function pcPrepay()
{
$post = $this->request->post();
$order = Order::get($post['order_id']);
$order['pay_way'] = $post['pay_way'];
$return_msg = ['order_id' => $order['id'], 'order_amount' => $order['order_amount']];
//找不到订单
if (empty($order)) {
$this->_error('订单不存在');
}
if ($order['order_status'] == CommonOrder::STATUS_CLOSE || $order['del'] == 1) {
$this->_error('订单已关闭');
}
//已支付
if ($order['pay_status'] == Pay::ISPAID) {
$this->_success('支付成功', $return_msg, 10001);
}
$result = PaymentLogic::pcPay($order, $post['order_source']);
if (false === $result) {
$this->_error(PaymentLogic::getError(), $return_msg, PaymentLogic::getReturnCode());
}
if ($order['pay_way'] == Pay::BALANCE_PAY) {
$this->_success('支付成功', $return_msg, PaymentLogic::getReturnCode());
}
$return_msg['data'] = $result;
if (PaymentLogic::getReturnCode() != 0) {
$this->_success('支付成功', $return_msg, PaymentLogic::getReturnCode());
}
$this->_success('支付成功', $return_msg);
}
/**
* Notes: 小程序回调
* @author 段誉(2021/2/23 14:34)
*/
public function notifyMnp()
{
$config = WeChatServer::getPayConfig(Client_::mnp);
return WeChatPayServer::notify($config);
}
/**
* Notes: 公众号回调
* @author 段誉(2021/2/23 14:34)
*/
public function notifyOa()
{
$config = WeChatServer::getPayConfig(Client_::oa);
return WeChatPayServer::notify($config);
}
/**
* Notes: APP回调
* @author 段誉(2021/2/23 14:34)
*/
public function notifyApp()
{
$config = WeChatServer::getPayConfig(Client_::ios);
return WeChatPayServer::notify($config);
}
/**
* Notes: 支付宝回调
* @author 段誉(2021/3/23 11:37)
*/
public function aliNotify()
{
$data = $this->request->post();
$result = (new AliPayServer())->verifyNotify($data);
if (true === $result) {
echo 'success';
} else {
echo 'fail';
}
}
/**
* Notes:
* @author 段誉(2021/3/23 11:36)
* @return \think\Model[]
*/
public function payway()
{
$params = $this->request->get();
if(!isset($params['from']) || !isset($params['order_id'])) {
return $this->_error('参数缺失');
}
if($params['from'] == 'order') {
$order = Db::name('order')->where('id', $params['order_id'])->find();
}else if($params['from'] == 'recharge') {
$order = Db::name('recharge_order')->where('id', $params['order_id'])->find();
}
$payModel = new Pay();
$pay = $payModel->where(['status' => 1])->order('sort')->hidden(['config'])->select()->toArray();
foreach ($pay as $k => &$item) {
if ($item['code'] == 'wechat') {
$item['extra'] = '微信快捷支付';
$item['pay_way'] = Pay::WECHAT_PAY;
}
if ($item['code'] == 'balance') {
$user_money = Db::name('user')->where(['id' => $this->user_id])->value('user_money');
$item['extra'] = '可用余额:'.$user_money;
$item['pay_way'] = Pay::BALANCE_PAY;
}
if ($item['code'] == 'alipay') {
$item['extra'] = '';
$item['pay_way'] = Pay::ALI_PAY;
}
if (in_array($this->client, [Client_::mnp, Client_::oa]) && $item['code'] == 'alipay') {
unset($pay[$k]);
}
if($params['from'] == 'recharge' && $item['code'] == 'balance') {
unset($pay[$k]);
}
}
// 订单自动取消时间
$cancelTime = ConfigServer::get('trading', 'order_cancel');
if(empty($cancelTime)) {
// 前端检测为0时不显示倒计时
$cancelTime = 0;
}else{
// 订单创建时间 + 后台设置自动关闭未付款订单时长
$cancelTime = $order['create_time'] + intval($cancelTime) * 60;
}
$data = [
'pay' => array_values($pay),
'order_amount' => $order['order_amount'],
'cancel_time' => $cancelTime,
];
$this->_success('', $data);
}
}

View File

@ -0,0 +1,106 @@
<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\common\model\pay;
use app\common\service\UrlServer;
use think\Model;
class Pay extends Model
{
protected $name = 'dev_pay';
const UNPAID = 0;//待支付
const ISPAID = 1;//已支付
const REFUNDED = 2;//已退款
const REFUSED_REFUND = 3;//拒绝退款
//支付方式
const WECHAT_PAY = 1;//微信支付
const ALI_PAY = 2;//支付宝支付
const BALANCE_PAY = 3;//余额支付
const OFFLINE_PAY = 4;//线下支付
//支付状态
public static function getPayStatus($type)
{
$data = [
self::UNPAID => '待支付',
self::ISPAID => '已支付',
self::REFUNDED => '已退款',
self::REFUSED_REFUND => '拒绝退款',
];
if ($type === true) {
return $data;
}
return $data[$type] ?? '未知';
}
//支付方式
public static function getPayWay($type)
{
$data = [
self::WECHAT_PAY => '微信支付',
self::ALI_PAY => '支付宝支付',
self::BALANCE_PAY => '余额支付',
// self::OFFLINE_PAY => '线下支付',
];
if ($type === true) {
return $data;
}
return $data[$type] ?? '其他';
}
//图片路径
public function getIconAttr($value, $data)
{
return UrlServer::getFileUrl($value);
}
//支付设置状态
public function getStatusTextAttr($value, $data)
{
if ($data['status'] == 1){
return '启用';
}
return '关闭';
}
//各种支付的配置
public function getConfigAttr($value, $data)
{
if ($value){
return json_decode($value, true);
}
return $value;
}
}