Files
anmo/server/app/common/logic/RefundLogic.php
2025-08-19 14:16:51 +08:00

235 lines
8.1 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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\logic;
use app\common\enum\accountLog\AccountLogEnum;
use app\common\enum\OrderEnum;
use app\common\enum\OrderRefundEnum;
use app\common\enum\PayEnum;
use app\common\model\accountLog\AccountLog;
use app\common\model\order\Order;
use app\common\model\order\OrderRefund;
use app\common\model\order\OrderRefundLog;
use app\common\model\user\User;
use app\common\service\AliPayService;
use app\common\service\WeChatConfigService;
use app\common\service\WeChatPayService;
class RefundLogic extends BaseLogic
{
protected $refund;
protected $refund_log;
/**
* @notes 退款
* @param $order
* @param $refund_amount
* @param $type
* @param $operator_id
* @return bool
* @throws \Exception
* @author ljj
* @date 2022/2/15 5:05 下午
*/
public function refund($order, $refund_amount,$refund_car,$type,$orderType,$operator_id)
{
if ($refund_amount+$refund_car <= 0) {
return false;
}
//生成退款记录
$this->log($order,$refund_amount,$refund_car,$type,$orderType,$operator_id);
$totalRefundAmount = round($refund_amount + $refund_car,2);
//原路退款
switch ($order['pay_way']) {
//微信退款
case PayEnum::WECHAT_PAY:
$this->wechatRefund($order,$totalRefundAmount);
break;
//支付宝退款
case PayEnum::ALI_PAY:
$this->alipayRefund($order,$totalRefundAmount);
break;
case PayEnum::BALANCE_PAY:
$this->balanceRefund($order,$totalRefundAmount,$orderType);
break;
}
return true;
}
/**
* @notes 退款记录
* @param $order
* @param $refund_amount
* @param $type
* @param $operator_id
* @author ljj
* @date 2022/2/15 3:49 下午
*/
public function log($order,$refund_amount,$refund_car,$type,$orderType,$operator_id)
{
if(1 == $orderType){
$orderId = $order['id'];
$subOrderId = 0;
}else{
$orderId = $order['order_id'];
$subOrderId = $order['id'] ;
}
$refund = OrderRefund::create([
'sn' => generate_sn(new OrderRefund(), 'sn'),
'order_id' => $orderId,
'sub_order_id' => $subOrderId,
'user_id' => $order['user_id'],
'order_amount' => $order['order_amount'],
'refund_amount' => $refund_amount,
'refund_car_amount' => $refund_car,
'order_terminal' => $order['order_terminal'],
'transaction_id' => $order['transaction_id'],
'type' => $type,
'order_type'=> $orderType
]);
//退款日志
$refund_log = OrderRefundLog::create([
'sn' => generate_sn(new OrderRefundLog(), 'sn'),
'refund_id' => $refund->id,
'type' => $type,
'operator_id' => $operator_id,
]);
$this->refund = $refund;
$this->refund_log = $refund_log;
}
/**
* @notes 微信退款
* @param $order
* @param $refund_amount
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @author ljj
* @date 2022/2/15 5:06 下午
*/
public function wechatRefund($order,$refund_amount)
{
//微信配置信息
$wechatConfig = WeChatConfigService::getWechatConfigByTerminal($order['order_terminal']);
if (!file_exists($wechatConfig['cert_path']) || !file_exists($wechatConfig['key_path'])) {
throw new \Exception('微信证书不存在,请联系管理员!');
}
//发起退款
$result = (new WeChatPayService($order['order_terminal']))->refund([
'transaction_id' => $order['transaction_id'],
'refund_sn' => $this->refund_log->sn,
'total_fee' => $order['order_amount'] * 100,//订单金额,单位为分
'refund_fee' => intval($refund_amount * 100),//退款金额
]);
if ($result['return_code'] == 'FAIL' || $result['result_code'] == 'FAIL') {
if ($result['err_code'] == 'SYSTEMERROR' || $result['err_code'] == 'BIZERR_NEED_RETRY') {
return true;
}
//更新退款日志记录
OrderRefundLog::update([
'wechat_refund_id' => $result['refund_id'] ?? 0,
'refund_status' => OrderRefundEnum::STATUS_FAIL,
'refund_msg' => json_encode($result, JSON_UNESCAPED_UNICODE),
], ['id'=>$this->refund_log->id]);
//更新订单退款状态
OrderRefund::update([
'refund_status' => OrderRefundEnum::STATUS_FAIL,
], ['id'=>$this->refund->id]);
}
return true;
}
/**
* @notes 支付宝退款
* @param $order
* @param $refund_amount
* @return bool
* @throws \Exception
* @author ljj
* @date 2024/3/21 4:55 下午
*/
public function alipayRefund($order,$refund_amount)
{
//原路退回到支付宝的情况
$result = (new AliPayService())->refund($order['sn'], $refund_amount, $this->refund_log->sn);
$result = (array)$result;
//更新退款日志记录
OrderRefundLog::update([
'refund_status' => ($result['code'] == '10000' && $result['msg'] == 'Success' && $result['fundChange'] == 'Y') ? 1 : 2,
'refund_msg' => json_encode($result, JSON_UNESCAPED_UNICODE),
], ['id'=>$this->refund_log->id]);
//更新订单退款状态
OrderRefund::update([
'refund_status' => ($result['code'] == '10000' && $result['msg'] == 'Success' && $result['fundChange'] == 'Y') ? 1 : 2,
], ['id'=>$this->refund->id]);
return true;
}
public function balanceRefund($order,$refund_amount,$orderType)
{
$user = User::where(['id'=>$order['user_id']])->findOrEmpty();
$user->user_money = round($user->user_money + $refund_amount,2);
$user->save();
$mainOrder = $order;
if(1 != $orderType){
$mainOrder = Order::where(['id'=>$order['order_id']])->findOrEmpty();
}
$changeType = AccountLogEnum::CANCEL_ORDER_ADD_MONEY;
if(OrderEnum::ORDER_STATUS_SERVER_FINISH == $mainOrder['order_status'] && OrderEnum::ORDER_STATUS_SERVER_FINISH == $mainOrder['order_status']){
$changeType = AccountLogEnum::REFUND_ORDER_ADD_MONEY;
}
//
AccountLogLogic::add(
$user->id,
AccountLogEnum::MONEY,
$changeType,
AccountLogEnum::INC,
$refund_amount,
$order['sn']);
//更新退款日志记录
OrderRefundLog::update([
'refund_status' => 1 ,
'refund_msg' => '',
], ['id'=>$this->refund_log->id]);
//更新订单退款状态
OrderRefund::update([
'refund_status' => 1,
], ['id'=>$this->refund->id]);
return true;
}
}