|
|
@@ -8,6 +8,7 @@ use biz\shop\classes\ShopCapitalClass;
|
|
|
use biz\stat\classes\StatOutClass;
|
|
|
use biz\stat\classes\StatRefundClass;
|
|
|
use bizHd\base\services\BaseService;
|
|
|
+use bizHd\custom\classes\CustomClass;
|
|
|
use bizHd\goods\classes\GoodsClass;
|
|
|
use bizHd\merchant\classes\ShopClass;
|
|
|
use bizHd\order\classes\OrderClass;
|
|
|
@@ -16,6 +17,7 @@ use bizHd\order\classes\OrderItemClass;
|
|
|
use bizHd\refund\classes\HdRefundClass;
|
|
|
use bizHd\refund\classes\HdRefundGoodsClass;
|
|
|
use bizHd\refund\classes\HdRefundItemClass;
|
|
|
+use bizGhs\shop\classes\ShopMoneyClass;
|
|
|
use bizHd\wx\classes\WxOpenClass;
|
|
|
use common\components\dict;
|
|
|
use common\components\orderSn;
|
|
|
@@ -39,13 +41,13 @@ class HdRefundService extends BaseService
|
|
|
$order->refundPrice = $refundPrice;
|
|
|
$currentTkPrice = bcadd($order->tkPrice, $refundPrice, 2);
|
|
|
if ($currentTkPrice > $order->mainPay) {
|
|
|
- util::fail('退款金额超过支付金额');
|
|
|
+ util::fail('可退款金额不足');
|
|
|
}
|
|
|
$order->tkPrice = $currentTkPrice;
|
|
|
$order->refund = OrderClass::REFUND_YES;
|
|
|
$currentActPrice = bcsub($order->actPrice, $refundPrice, 2);
|
|
|
if ($currentActPrice < 0) {
|
|
|
- util::fail('退款金额有问题!');
|
|
|
+ util::fail('可退款金额不足!');
|
|
|
}
|
|
|
$order->actPrice = $currentActPrice;
|
|
|
$order->realPrice = $currentActPrice;
|
|
|
@@ -193,6 +195,7 @@ class HdRefundService extends BaseService
|
|
|
|
|
|
$payWay = $order->payWay;
|
|
|
$shopId = $order->shopId;
|
|
|
+ $onlinePay = $order->onlinePay ?? 1;
|
|
|
$main = MainClass::getLockById($mainId);
|
|
|
$shop = ShopClass::getLockById($shopId);
|
|
|
if (empty($main)) {
|
|
|
@@ -229,54 +232,80 @@ class HdRefundService extends BaseService
|
|
|
//退款统计
|
|
|
StatRefundClass::replace($main, $shop, $payWay, $refundPrice);
|
|
|
|
|
|
- //退款流程
|
|
|
- if ($payWay == dict::getDict('payWay', 'wxPay')) {
|
|
|
+ if ($onlinePay == dict::getDict('onlinePay', 'yes')) {
|
|
|
+ //退款流程
|
|
|
+ if ($payWay == dict::getDict('payWay', 'wxPay')) {
|
|
|
|
|
|
- //门店余额减少
|
|
|
- \biz\shop\classes\ShopClass::hdSaleRefundReduceBalance($main, $shop, $refund, $refundPrice);
|
|
|
+ //门店余额减少
|
|
|
+ \biz\shop\classes\ShopClass::hdSaleRefundReduceBalance($main, $shop, $refund, $refundPrice);
|
|
|
|
|
|
- //微信收款的原路退回
|
|
|
- ini_set('date.timezone', 'Asia/Shanghai');
|
|
|
- $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
|
|
|
- require_once($wx . '/lib/WxPay.Api.php');
|
|
|
- require_once($wx . '/example/WxPay.Config.php');
|
|
|
+ //微信收款的原路退回
|
|
|
+ ini_set('date.timezone', 'Asia/Shanghai');
|
|
|
+ $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
|
|
|
+ require_once($wx . '/lib/WxPay.Api.php');
|
|
|
+ require_once($wx . '/example/WxPay.Config.php');
|
|
|
|
|
|
- $totalFee = $order->mainPay * 100;
|
|
|
- $refundFee = $refundPrice * 100;
|
|
|
+ $totalFee = $order->mainPay * 100;
|
|
|
+ $refundFee = $refundPrice * 100;
|
|
|
|
|
|
- $merchantExt = WxOpenClass::getMallWxInfo();
|
|
|
- $mid = $merchantExt['wxPayMerchantId'] ?? '';
|
|
|
- $appId = $merchantExt['miniAppId'] ?? '';
|
|
|
- $key = $merchantExt['wxPayKey'] ?? '';
|
|
|
+ $merchantExt = WxOpenClass::getMallWxInfo();
|
|
|
+ $mid = $merchantExt['wxPayMerchantId'] ?? '';
|
|
|
+ $appId = $merchantExt['miniAppId'] ?? '';
|
|
|
+ $key = $merchantExt['wxPayKey'] ?? '';
|
|
|
|
|
|
- $input = new \WxPayRefund();
|
|
|
- $input->SetTransaction_id($transaction_id);
|
|
|
- $input->SetTotal_fee($totalFee);
|
|
|
- $input->SetRefund_fee($refundFee);
|
|
|
- Yii::info('totalFee:' . $totalFee . ' refundFee:' . $refundFee);
|
|
|
- $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-refund-callback/');
|
|
|
+ $input = new \WxPayRefund();
|
|
|
+ $input->SetTransaction_id($transaction_id);
|
|
|
+ $input->SetTotal_fee($totalFee);
|
|
|
+ $input->SetRefund_fee($refundFee);
|
|
|
+ Yii::info('totalFee:' . $totalFee . ' refundFee:' . $refundFee);
|
|
|
+ $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-refund-callback/');
|
|
|
|
|
|
- $config = new \WxPayConfig();
|
|
|
- $config->SetAppId($appId);
|
|
|
- $config->SetMerchantId($mid);
|
|
|
- $config->SetKey($key);
|
|
|
+ $config = new \WxPayConfig();
|
|
|
+ $config->SetAppId($appId);
|
|
|
+ $config->SetMerchantId($mid);
|
|
|
+ $config->SetKey($key);
|
|
|
|
|
|
- $input->SetOut_refund_no($refundSn);
|
|
|
- $input->SetOp_user_id($mid);
|
|
|
- $return = \WxPayApi::refund($config, $input);
|
|
|
- if (isset($return['return_code']) == false || $return['return_code'] != 'SUCCESS' || isset($return['result_code']) == false || $return['result_code'] != 'SUCCESS') {
|
|
|
- $str = '退款失败:';
|
|
|
- foreach ($return as $rKey => $text) {
|
|
|
- $str .= $rKey . ':' . $text . " | ";
|
|
|
+ $input->SetOut_refund_no($refundSn);
|
|
|
+ $input->SetOp_user_id($mid);
|
|
|
+ $return = \WxPayApi::refund($config, $input);
|
|
|
+ if (isset($return['return_code']) == false || $return['return_code'] != 'SUCCESS' || isset($return['result_code']) == false || $return['result_code'] != 'SUCCESS') {
|
|
|
+ $str = '退款失败:';
|
|
|
+ foreach ($return as $rKey => $text) {
|
|
|
+ $str .= $rKey . ':' . $text . " | ";
|
|
|
+ }
|
|
|
+ Yii::info($str);
|
|
|
+ util::fail('退款失败');
|
|
|
}
|
|
|
- Yii::info($str);
|
|
|
- util::fail('退款失败');
|
|
|
+ $wxRefundId = $return['refund_id'] ?? '';
|
|
|
+ $refund->thirdRefundNo = $wxRefundId;
|
|
|
+ $refund->save();
|
|
|
+ } else {
|
|
|
+ util::fail('此支付方式暂不支持退款');
|
|
|
}
|
|
|
- $wxRefundId = $return['refund_id'] ?? '';
|
|
|
- $refund->thirdRefundNo = $wxRefundId;
|
|
|
- $refund->save();
|
|
|
} else {
|
|
|
- util::fail('暂时只支持微信支付订单退款');
|
|
|
+ if ($payWay == dict::getDict('payWay', 'debtPay')) {
|
|
|
+
|
|
|
+ $settleId = $order->settleId;
|
|
|
+ if (!empty($settleId)) {
|
|
|
+ util::fail('订单已结账,无法再发起退款');
|
|
|
+ }
|
|
|
+
|
|
|
+ //客户欠款金额减少
|
|
|
+ $customId = $order->customId ?? 0;
|
|
|
+ $custom = CustomClass::getLockById($customId);
|
|
|
+ if (empty($custom)) {
|
|
|
+ util::fail('没有找到客户');
|
|
|
+ }
|
|
|
+ CustomClass::refundDebtAmountReduce($custom, $refundPrice, $refund);
|
|
|
+ } elseif ($payWay == dict::getDict('payWay', 'cash')) {
|
|
|
+ $moneyData = [
|
|
|
+ 'shopId' => $shopId,
|
|
|
+ 'amount' => $refundPrice,
|
|
|
+ 'orderSn' => $order->orderSn,
|
|
|
+ 'mainId' => $mainId,
|
|
|
+ ];
|
|
|
+ ShopMoneyClass::hdRefundSkOutAmount($moneyData, $main);
|
|
|
+ }
|
|
|
}
|
|
|
return $refund;
|
|
|
}
|