|
@@ -2,6 +2,8 @@
|
|
|
|
|
|
namespace app\admin\service\order;
|
|
|
|
|
|
+use app\model\CouponDetail;
|
|
|
+use app\model\CouponGoods;
|
|
|
use app\model\GoodsRunning;
|
|
|
use app\model\GoodsSku;
|
|
|
use app\model\Member;
|
|
@@ -225,16 +227,17 @@ class OrderService
|
|
|
$config['notify_url'] = getenv('NOTIFY_DOMAIN_ADMIN') . '/notify/orderPay/wxpay';
|
|
|
Pay::wechat($config)->pos($payData);
|
|
|
$result = self::findWxpay($params['orderGroupId'], 0);
|
|
|
- }catch (GatewayException $g){
|
|
|
+ } catch (GatewayException $g) {
|
|
|
$result = self::findWxpay($params['orderGroupId'], 0);
|
|
|
} catch (\Exception $e) {
|
|
|
$log->error("WXPAY", ['msg' => $e->getMessage()]);
|
|
|
$result = self::findWxpay($params['orderGroupId'], 0);
|
|
|
// throw new BusinessException("支付失败");
|
|
|
}
|
|
|
- try{
|
|
|
+ try {
|
|
|
$log->info("WXPAY_RETURN", json_decode(json_encode($result), true));
|
|
|
- }catch (\Exception $e){}
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ }
|
|
|
|
|
|
|
|
|
} else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
|
|
@@ -246,19 +249,20 @@ class OrderService
|
|
|
];
|
|
|
try {
|
|
|
$config = config('payment.alipay');
|
|
|
- $config['notify_url'] = getenv('NOTIFY_DOMAIN_ADMIN').'/notify/orderPay/alipay';
|
|
|
+ $config['notify_url'] = getenv('NOTIFY_DOMAIN_ADMIN') . '/notify/orderPay/alipay';
|
|
|
Pay::alipay($config)->pos($payData);
|
|
|
$result = self::findAlipay($params['orderGroupId'], 0);
|
|
|
- } catch (GatewayException $g){
|
|
|
+ } catch (GatewayException $g) {
|
|
|
$result = self::findAlipay($params['orderGroupId'], 0);
|
|
|
} catch (\Exception $e) {
|
|
|
$log->error("ALIPAY", ['msg' => $e->getMessage()]);
|
|
|
throw new BusinessException("支付失败");
|
|
|
}
|
|
|
|
|
|
- try{
|
|
|
+ try {
|
|
|
$log->info("ALIPAY_RETURN", json_decode(json_encode($result), true));
|
|
|
- }catch (\Exception $e){}
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ }
|
|
|
} else {
|
|
|
throw new BusinessException("付款码无效");
|
|
|
}
|
|
@@ -286,16 +290,15 @@ class OrderService
|
|
|
}
|
|
|
|
|
|
if (!empty($result['return_code']) && $result['return_code'] == 'SUCCESS' && !empty($result['result_code']) && $result['result_code'] == 'SUCCESS' && !empty($result['trade_state']) && $result['trade_state'] == 'SUCCESS') {
|
|
|
- SupportLog::channel('pay')->info("FIND_WXPAY_SUCCESS", ['nbr' => $nbr,'order_id'=>$orderId]);
|
|
|
+ SupportLog::channel('pay')->info("FIND_WXPAY_SUCCESS", ['nbr' => $nbr, 'order_id' => $orderId]);
|
|
|
return $result;
|
|
|
} else {
|
|
|
- if ($nbr > 2) {
|
|
|
- SupportLog::channel('pay')->error("FIND_WXPAY", ['msg' => '订单查询失败','order_id'=>$orderId]);
|
|
|
- return ['msg'=>'订单查询失败'];
|
|
|
+ if ($nbr > 1) {
|
|
|
+ SupportLog::channel('pay')->error("FIND_WXPAY", ['msg' => '订单查询失败', 'order_id' => $orderId]);
|
|
|
+ return ['msg' => '订单查询失败'];
|
|
|
}
|
|
|
- dump('nbr-'.$nbr);
|
|
|
- sleep(5);
|
|
|
- SupportLog::channel('pay')->error("FIND_WXPAY", ['nbr' => $nbr,'order_id'=>$orderId]);
|
|
|
+ sleep(3);
|
|
|
+ SupportLog::channel('pay')->error("FIND_WXPAY", ['nbr' => $nbr, 'order_id' => $orderId]);
|
|
|
return self::findWxpay($orderId, $nbr + 1);
|
|
|
}
|
|
|
}
|
|
@@ -320,15 +323,15 @@ class OrderService
|
|
|
}
|
|
|
|
|
|
if (!empty($result['code']) && $result['code'] == '10000' && !empty($result['trade_status']) && $result['trade_status'] == 'TRADE_SUCCESS') {
|
|
|
- SupportLog::channel('pay')->info("FIND_ALIPAY_SUCCESS", ['nbr' => $nbr,'order_id'=>$orderId]);
|
|
|
+ SupportLog::channel('pay')->info("FIND_ALIPAY_SUCCESS", ['nbr' => $nbr, 'order_id' => $orderId]);
|
|
|
return $result;
|
|
|
} else {
|
|
|
- if ($nbr > 2) {
|
|
|
- SupportLog::channel('pay')->error("FIND_ALIPAY", ['msg' => '订单查询失败','order_id'=>$orderId]);
|
|
|
- return ['msg'=>'订单查询失败'];
|
|
|
+ if ($nbr > 1) {
|
|
|
+ SupportLog::channel('pay')->error("FIND_ALIPAY", ['msg' => '订单查询失败', 'order_id' => $orderId]);
|
|
|
+ return ['msg' => '订单查询失败'];
|
|
|
}
|
|
|
- sleep(5);
|
|
|
- SupportLog::channel('pay')->error("FIND_ALIPAY", ['nbr' => $nbr,'order_id'=>$orderId]);
|
|
|
+ sleep(3);
|
|
|
+ SupportLog::channel('pay')->error("FIND_ALIPAY", ['nbr' => $nbr, 'order_id' => $orderId]);
|
|
|
return self::findAlipay($orderId, $nbr + 1);
|
|
|
}
|
|
|
}
|
|
@@ -340,7 +343,7 @@ class OrderService
|
|
|
{
|
|
|
foreach ($params['goodsContentList'] as $goods) {
|
|
|
// 减库存,规格和总库存
|
|
|
- if (!isset($params['submit_goods_classify']) || $params['submit_goods_classify'] != 'MEALS') {
|
|
|
+ if (!isset($params['submit_goods_classify']) || !in_array($params['submit_goods_classify'], ['MEALS', 'PACKAGE'])) {
|
|
|
$goodsSku = GoodsSku::where('goods_sku_id', $goods['sku_id'])->first();
|
|
|
$skuStorageJson = json_decode($goodsSku->goods_sku_storage_json, true);
|
|
|
if (isset($skuStorageJson['storage']) && !empty($skuStorageJson['storage'])) {
|
|
@@ -412,9 +415,410 @@ class OrderService
|
|
|
$payDetail->save();
|
|
|
}
|
|
|
|
|
|
+ public static function getPayWayByPrepayId($prepayId)
|
|
|
+ {
|
|
|
+ $payWay = '';
|
|
|
+ $categoryArray = explode('-', $prepayId);
|
|
|
+ if (isset($categoryArray[1])) {
|
|
|
+ $payWay = $categoryArray[1];
|
|
|
+ } else if (in_array($categoryArray[0], ['WXPAY', 'ALIPAY', 'OFFLINE', 'OFFLINE_ALIPAY', 'OFFLINE_WXPAY', 'MONEY'])) {
|
|
|
+ $payWay = $categoryArray[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $payWay;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Desc 支付使用优惠券
|
|
|
+ * @Author Gorden
|
|
|
+ * @Date 2024/8/28 14:59
|
|
|
+ *
|
|
|
+ * @param $memberId
|
|
|
+ * @param $goods
|
|
|
+ * @param $coupon
|
|
|
+ * @param $payAmount
|
|
|
+ * @return array|mixed
|
|
|
+ */
|
|
|
+ public static function payUseCoupon($type, $settlementNow, $memberId, $goods, $coupon, $payAmount)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ foreach ($coupon as $item) {
|
|
|
+ if (!in_array(substr($item, 0, 2), ['CU', 'CO'])) {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $amountBalance = [
|
|
|
+ 'pay_amount' => $payAmount,
|
|
|
+ 'welfare_balance' => 0,
|
|
|
+ 'cut_balance' => 0,
|
|
|
+ ];
|
|
|
+ foreach ($goods as $good) {
|
|
|
+ $result = OrderService::useCoupon($type, $settlementNow, $memberId, $goods, $good, $coupon, $amountBalance);
|
|
|
+ $amountBalance = [
|
|
|
+ 'pay_amount' => $result['pay_amount'],
|
|
|
+ 'welfare_balance' => $result['welfare_balance'],
|
|
|
+ 'cut_balance' => $result['cut_balance'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ $couponDetailsIds = Redis::sMembers("ORDER:USE:COUPON:" . $memberId);
|
|
|
+ Redis::del("ORDER:USE:COUPON:" . $memberId);
|
|
|
+ $useCouponJson = Redis::get("ORDER:USE:COUPON:DISCOUNT:" . $memberId);
|
|
|
+ Redis::del("ORDER:USE:COUPON:DISCOUNT:" . $memberId);
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'pay_amount' => $amountBalance['pay_amount'],
|
|
|
+ 'detail_ids' => $couponDetailsIds,
|
|
|
+ 'use_coupon_json' => $useCouponJson ?? []
|
|
|
+ ];
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ dump($e->getTrace());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function useCoupon($type, $settlementNow, $memberId, $goods, $good, $coupon, $amountBalance)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $cacheKey = "ORDER:USE:COUPON:" . $memberId;
|
|
|
+ $cacheDiscountKey = "ORDER:USE:COUPON:DISCOUNT:" . $memberId;
|
|
|
+ $payAmount = $amountBalance['pay_amount'];
|
|
|
+ $welfareBalance = $amountBalance['welfare_balance'];
|
|
|
+ $cutBalance = $amountBalance['cut_balance'];
|
|
|
+ $goodsId = $good['goods_id'];
|
|
|
+ $money = $good['goods_sales_price'] * $good['nbr'];
|
|
|
+
|
|
|
+ $discountData = Redis::get($cacheDiscountKey);
|
|
|
+ if (empty($discountData)) {
|
|
|
+ $discountData = [];
|
|
|
+ } else {
|
|
|
+ $discountData = json_decode($discountData, true);
|
|
|
+ }
|
|
|
+ foreach ($coupon as $couponId) {
|
|
|
+ $couponDetail = CouponDetail::leftJoin('coupon_goods', 'coupon_goods.join_goods_coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
|
|
|
+ ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
|
|
|
+ ->select('coupon_detail.coupon_detail_id', 'coupon_goods.coupon_goods_id', 'coupon_id', 'coupon_classify', 'coupon_value', 'coupon_minimum_limit', 'coupon_category')
|
|
|
+ ->where('coupon_goods.join_coupon_goods_id', $goodsId)
|
|
|
+ ->where('coupon_goods.join_coupon_goods_sku_id', $good['sku_id'])
|
|
|
+ ->where('join_goods_coupon_id', $couponId)
|
|
|
+ ->where('coupon_detail.join_coupon_detail_member_id', $memberId);
|
|
|
+ if ($settlementNow == 'Y' && $type == 'pay') {
|
|
|
+ $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']);
|
|
|
+ } else {
|
|
|
+ $couponDetail = $couponDetail->where('coupon_detail.coupon_detail_status', 'ACTIVED');
|
|
|
+ }
|
|
|
+ $couponDetail = $couponDetail->orderBy('coupon_detail_id', 'DESC')
|
|
|
+ ->first();
|
|
|
+ if (!$couponDetail) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if ($settlementNow == 'Y') {
|
|
|
+ $updateData = [
|
|
|
+ 'coupon_detail_status' => 'USED',
|
|
|
+ 'coupon_detail_used_datetime' => date('Y-m-d H:i:s')
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ $updateData = [
|
|
|
+ 'coupon_detail_status' => 'WAITING',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ // 计算优惠券包含的优惠商品的件数和总价
|
|
|
+ $countAndAmount = self::countAndAmount($goods, $couponId);
|
|
|
+ // 如果是计件
|
|
|
+ if ($couponDetail->coupon_category == 'PIECE' && $countAndAmount['count'] < $couponDetail->coupon_minimum_limit) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (in_array($couponDetail->coupon_classify, ['立减券', '满减券'])) {
|
|
|
+ if (Redis::sIsMember($cacheKey, $couponId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Redis::sAdd($cacheKey, $couponId);
|
|
|
+ if ($couponDetail->coupon_category == 'PIECE' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
|
|
|
+ $payAmount = $payAmount - $couponDetail->coupon_value;
|
|
|
+ // json记录
|
|
|
+ $discountData[$couponId] = [
|
|
|
+ 'coupon_id' => $couponId,
|
|
|
+ 'coupon_value' => $couponDetail->coupon_value,
|
|
|
+ 'coupon_classify' => $couponDetail->coupon_classify,
|
|
|
+ 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
|
|
|
+ ];
|
|
|
+ Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
|
|
|
+
|
|
|
+ CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
|
|
|
+ }
|
|
|
+ } elseif ($couponDetail->coupon_classify == '折扣券') {
|
|
|
+ if (Redis::sIsMember($cacheKey, $couponId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Redis::sAdd($cacheKey, $couponId);
|
|
|
+ if ($couponDetail->coupon_category == 'PIECE' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
|
|
|
+ $zhekouAmount = round($countAndAmount['amount'] * (100 - $couponDetail->coupon_value) / 100, 2);
|
|
|
+ $payAmount = $payAmount - $zhekouAmount;
|
|
|
+ // json记录
|
|
|
+ $discountData[$couponId] = [
|
|
|
+ 'coupon_id' => $couponId,
|
|
|
+ 'coupon_value' => $zhekouAmount,
|
|
|
+ 'coupon_classify' => $couponDetail->coupon_classify,
|
|
|
+ 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
|
|
|
+ ];
|
|
|
+ Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
|
|
|
+ CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
|
|
|
+ }
|
|
|
+ } elseif (in_array($couponDetail->coupon_classify, ['抵用券', '赠品券'])) {
|
|
|
+ if (Redis::sIsMember($cacheKey, $couponId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Redis::sAdd($cacheKey, $couponId);
|
|
|
+ CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
|
|
|
+ if ($good['nbr'] > 1) {
|
|
|
+ $diyongAmount = $good['goods_sales_price'];
|
|
|
+ $payAmount = $payAmount - $diyongAmount;
|
|
|
+ } elseif (ceil($good['nbr']) == 1) {
|
|
|
+ $diyongAmount = round($good['goods_sales_price'] * $good['nbr'], 2);
|
|
|
+ $payAmount = $payAmount - $diyongAmount;
|
|
|
+ }
|
|
|
+ // json记录
|
|
|
+ $discountData[$couponId] = [
|
|
|
+ 'coupon_id' => $couponId,
|
|
|
+ 'coupon_value' => $diyongAmount,
|
|
|
+ 'coupon_classify' => $couponDetail->coupon_classify,
|
|
|
+ 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
|
|
|
+ ];
|
|
|
+ Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
|
|
|
+ } elseif ($couponDetail->coupon_classify == '福利券') {
|
|
|
+ if (Redis::sIsMember($cacheKey, $couponId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Redis::sAdd($cacheKey, $couponId);
|
|
|
+ CouponDetail::where('coupon_detail_id', $couponDetail->coupon_detail_id)->update($updateData);
|
|
|
+ $fuliAmount = 0;
|
|
|
+ if (!empty($couponDetail->coupon_value)) {
|
|
|
+ $fuliAmount = $couponDetail->coupon_value;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($fuliAmount >= $countAndAmount['amount']) {
|
|
|
+ $preferentialAmount = $countAndAmount['amount'];
|
|
|
+ } else {
|
|
|
+ $preferentialAmount = $fuliAmount;
|
|
|
+ }
|
|
|
+ $payAmount = $payAmount - $preferentialAmount;
|
|
|
+
|
|
|
+ // json记录
|
|
|
+ $discountData[$couponId] = [
|
|
|
+ 'coupon_id' => $couponId,
|
|
|
+ 'coupon_value' => $preferentialAmount,
|
|
|
+ 'coupon_classify' => $couponDetail->coupon_classify,
|
|
|
+ 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
|
|
|
+ ];
|
|
|
+ Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
|
|
|
+ } elseif (in_array($couponDetail->coupon_classify, ['年卡', '季卡', '月卡'])) {
|
|
|
+ if (Redis::sIsMember($cacheKey, $couponId) || Redis::sIsMember($cacheKey, $goodsId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Redis::sAdd($cacheKey, $couponId);
|
|
|
+ Redis::sAdd($cacheKey, $goodsId);
|
|
|
+ $kaAmount = 0;
|
|
|
+ if (!empty($discountData[$couponId]['coupon_value'])) {
|
|
|
+ $kaAmount = $discountData[$couponId]['coupon_value'];
|
|
|
+ }
|
|
|
+ if ($good['nbr'] > 1) {
|
|
|
+ $payAmount = $payAmount - $good['goods_sales_price'];
|
|
|
+ $kaAmount = $good['goods_sales_price'];
|
|
|
+ } else {
|
|
|
+ $payAmount = $payAmount - $good['goods_sales_price'] * $good['nbr'];
|
|
|
+ $kaAmount = $good['goods_sales_price'] * $good['nbr'];
|
|
|
+ }
|
|
|
+
|
|
|
+ // json记录
|
|
|
+ $discountData[$couponId] = [
|
|
|
+ 'coupon_id' => $couponId,
|
|
|
+ 'coupon_value' => $kaAmount,
|
|
|
+ 'coupon_classify' => $couponDetail->coupon_classify,
|
|
|
+ 'coupon_detail_id' => [$couponDetail->coupon_detail_id]
|
|
|
+ ];
|
|
|
+ Redis::set($cacheDiscountKey, json_encode($discountData, JSON_UNESCAPED_UNICODE));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($payAmount < 0) {
|
|
|
+ $payAmount = 0;
|
|
|
+ }
|
|
|
+ $amountBalance = [
|
|
|
+ 'pay_amount' => round($payAmount, 2),
|
|
|
+ 'welfare_balance' => 0,
|
|
|
+ 'cut_balance' => 0
|
|
|
+ ];
|
|
|
+ return $amountBalance;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Redis::del("ORDER:USE:COUPON:" . $memberId);
|
|
|
+ Redis::del("ORDER:USE:COUPON:DISCOUNT:" . $memberId);
|
|
|
+ dump($e->getTrace());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Desc 选择优惠券,计算
|
|
|
+ * @Author Gorden
|
|
|
+ * @Date 2024/8/28 14:18
|
|
|
+ *
|
|
|
+ * @param $memberId
|
|
|
+ * @param $goods
|
|
|
+ * @param $good
|
|
|
+ * @param $coupon
|
|
|
+ * @param $amountBalance
|
|
|
+ * @return array|void
|
|
|
+ */
|
|
|
+ public static function chooseCoupon($settlementNow, $memberId, $goods, $good, $coupon, $amountBalance)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $cacheKey = "ORDER:USE:COUPON:" . $memberId;
|
|
|
+ $payAmount = $amountBalance['pay_amount'];
|
|
|
+ $welfareBalance = $amountBalance['welfare_balance'];
|
|
|
+ $cutBalance = $amountBalance['cut_balance'];
|
|
|
+ $goodsId = $good['goods_id'];
|
|
|
+ $money = $good['goods_sales_price'] * $good['nbr'];
|
|
|
+ foreach ($coupon as $couponId) {
|
|
|
+ $couponDetail = CouponDetail::leftJoin('coupon_goods', 'coupon_goods.join_goods_coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
|
|
|
+ ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
|
|
|
+ ->select('coupon_detail.coupon_detail_id', 'coupon_goods.coupon_goods_id', 'coupon_id', 'coupon_classify', 'coupon_value', 'coupon_minimum_limit', 'coupon_category')
|
|
|
+ ->where('coupon_goods.join_coupon_goods_id', $goodsId)
|
|
|
+ ->where('coupon_goods.join_coupon_goods_sku_id', $good['sku_id'])
|
|
|
+ ->where('join_goods_coupon_id', $couponId)
|
|
|
+ ->where('coupon_detail.join_coupon_detail_member_id', $memberId);
|
|
|
+ if ($settlementNow == 'Y') {
|
|
|
+ $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']);
|
|
|
+ } else {
|
|
|
+ $couponDetail = $couponDetail->where('coupon_detail.coupon_detail_status', 'ACTIVED');
|
|
|
+ }
|
|
|
+ $couponDetail = $couponDetail->orderBy('coupon_detail_id', 'DESC')
|
|
|
+ ->first();
|
|
|
+ if (!$couponDetail) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 计算优惠券包含的优惠商品的件数和总价
|
|
|
+ $countAndAmount = self::countAndAmount($goods, $couponId);
|
|
|
+ // 如果是计件
|
|
|
+ if ($couponDetail->coupon_category == 'PIECE' && $countAndAmount['count'] < $couponDetail->coupon_minimum_limit) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (in_array($couponDetail->coupon_classify, ['立减券', '满减券'])) {
|
|
|
+ if (Redis::sIsMember($cacheKey, $couponId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Redis::sAdd($cacheKey, $couponId);
|
|
|
+ if ($couponDetail->coupon_category == 'PIECE' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
|
|
|
+ $payAmount = $payAmount - $couponDetail->coupon_value;
|
|
|
+ }
|
|
|
+ } elseif ($couponDetail->coupon_classify == '折扣券') {
|
|
|
+ if (Redis::sIsMember($cacheKey, $couponId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Redis::sAdd($cacheKey, $couponId);
|
|
|
+ if ($couponDetail->coupon_category == 'PIECE' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
|
|
|
+ $zhekouAmount = round($countAndAmount['amount'] * (100 - $couponDetail->coupon_value) / 100, 2);
|
|
|
+ $payAmount = $payAmount - $zhekouAmount;
|
|
|
+ }
|
|
|
+ } elseif (in_array($couponDetail->coupon_classify, ['抵用券', '赠品券'])) {
|
|
|
+ if (Redis::sIsMember($cacheKey, $couponId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Redis::sAdd($cacheKey, $couponId);
|
|
|
+ if ($good['nbr'] > 1) {
|
|
|
+ $diyongAmount = $good['goods_sales_price'];
|
|
|
+ $payAmount = $payAmount - $diyongAmount;
|
|
|
+ } elseif (ceil($good['nbr']) == 1) {
|
|
|
+ $diyongAmount = round($good['goods_sales_price'] * $good['nbr'], 2);
|
|
|
+ $payAmount = $payAmount - $diyongAmount;
|
|
|
+ }
|
|
|
+ } elseif ($couponDetail->coupon_classify == '福利券') {
|
|
|
+ if (Redis::sIsMember($cacheKey, $couponId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Redis::sAdd($cacheKey, $couponId);
|
|
|
+ $fuliAmount = 0;
|
|
|
+ if (!empty($couponDetail->coupon_value)) {
|
|
|
+ $fuliAmount = $couponDetail->coupon_value;
|
|
|
+ }
|
|
|
+ if ($fuliAmount >= $countAndAmount['amount']) {
|
|
|
+ $preferentialAmount = $countAndAmount['amount'];
|
|
|
+ } else {
|
|
|
+ $preferentialAmount = $fuliAmount;
|
|
|
+ }
|
|
|
+ $payAmount = $payAmount - $preferentialAmount;
|
|
|
+ } elseif (in_array($couponDetail->coupon_classify, ['年卡', '季卡', '月卡'])) {
|
|
|
+ if (Redis::sIsMember($cacheKey, $couponId) || Redis::sIsMember($cacheKey, $goodsId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Redis::sAdd($cacheKey, $couponId);
|
|
|
+ Redis::sAdd($cacheKey, $goodsId);
|
|
|
+ if ($good['nbr'] > 1) {
|
|
|
+ $payAmount = $payAmount - $good['goods_sales_price'];
|
|
|
+ } else {
|
|
|
+ $payAmount = $payAmount - $good['goods_sales_price'] * $good['nbr'];
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($payAmount < 0) {
|
|
|
+ $payAmount = 0;
|
|
|
+ }
|
|
|
+ $amountBalance = [
|
|
|
+ 'pay_amount' => round($payAmount, 2),
|
|
|
+ 'welfare_balance' => $welfareBalance,
|
|
|
+ 'cut_balance' => $cutBalance
|
|
|
+ ];
|
|
|
+ return $amountBalance;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Redis::del("ORDER:USE:COUPON:" . $memberId);
|
|
|
+ Redis::del("ORDER:USE:COUPON:DISCOUNT:" . $memberId);
|
|
|
+ dump($e->getTrace());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function countAndAmount($goods, $couponId)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $goodsIds = array_column($goods, 'goods_id');
|
|
|
+ $couponGoods = CouponGoods::whereIn('join_coupon_goods_id', $goodsIds)
|
|
|
+ ->where('join_goods_coupon_id', $couponId)
|
|
|
+ ->select('join_coupon_goods_id', 'join_coupon_goods_sku_id')
|
|
|
+ ->get()
|
|
|
+ ->toArray();
|
|
|
+ $count = 0;
|
|
|
+ $amount = 0;
|
|
|
+ foreach ($couponGoods as $couponGood) {
|
|
|
+ foreach ($goods as $good) {
|
|
|
+ if ($good['goods_id'] == $couponGood['join_coupon_goods_id'] && $good['sku_id'] == $couponGood['join_coupon_goods_sku_id']) {
|
|
|
+ $count += $good['nbr'];
|
|
|
+ $amount += $good['goods_sales_price'] * $good['nbr'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return compact('count', 'amount');
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ dump($e->getTrace());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static $couponClassify = [
|
|
|
+ 'wipe' => '抹零',
|
|
|
+ 'custom' => '自定义优惠金额'
|
|
|
+ ];
|
|
|
|
|
|
- public static $couponClassify=[
|
|
|
- 'wipe'=>'抹零',
|
|
|
- 'custom'=>'自定义优惠金额'
|
|
|
+ public static $payWay = [
|
|
|
+ 'WXPAY' => '微信支付',
|
|
|
+ 'ALIPAY' => '支付宝',
|
|
|
+ 'CASH' => '账户余额',
|
|
|
+ 'CARD' => '储值卡',
|
|
|
+ 'WELFARE' => '福利账户',
|
|
|
+ 'MONEY' => '现金',
|
|
|
+ 'OFFLINE' => '线下支付',
|
|
|
+ 'OFFLINE_WXPAY' => '线下支付-微信',
|
|
|
+ 'OFFLINE_ALIPAY' => '线下支付-支付宝',
|
|
|
+ 'QRCODE' => '付款码',
|
|
|
+ 'NONE' => '付零',
|
|
|
+ 'VIP' => 'VIP账户'
|
|
|
];
|
|
|
}
|