fix: 会员充值链路加固(体验资格、积分分红、czjilu审计、余额抵扣)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -879,6 +879,7 @@ class DsqrgmdhView(APIView):
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def post(self, request):
|
||||
from jituan.services.member_recharge import MemberRechargeError
|
||||
try:
|
||||
leixing = request.data.get('leixing')
|
||||
shenfen_id = request.data.get('shenfen_id')
|
||||
@@ -969,6 +970,7 @@ class DsqrgmdhView(APIView):
|
||||
return Response({'code': 400, 'msg': f'{identity["name"]}余额不足,需要{required}元,当前{balance}元'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
try:
|
||||
fulfill_error = None
|
||||
with transaction.atomic():
|
||||
locked_profile = identity['model'].objects.select_for_update().get(id=profile.id)
|
||||
current_balance = getattr(locked_profile, identity['balance_field'])
|
||||
@@ -995,7 +997,8 @@ class DsqrgmdhView(APIView):
|
||||
shuoming = f"{identity['name']}抵扣 {action_desc},抵扣金额{required}元"
|
||||
cz_leixing = leixing
|
||||
cz_club_id = club_id
|
||||
cz_zhuangtai = 1
|
||||
from jituan.services.member_recharge import MEMBER_PENDING_STATUS, MEMBER_FAILED_STATUS, MEMBER_PAID_STATUS
|
||||
cz_zhuangtai = MEMBER_PENDING_STATUS
|
||||
if leixing == 4:
|
||||
from jituan.services.club_penalty import penalty_cz_club_id
|
||||
from products.czjilu_types import (
|
||||
@@ -1031,35 +1034,52 @@ class DsqrgmdhView(APIView):
|
||||
|
||||
czjilu = Czjilu.query.create(**czjilu_kwargs)
|
||||
|
||||
response_data = {}
|
||||
response_data = {'dingdan_id': dingdan_id}
|
||||
fulfill_error = None
|
||||
|
||||
if leixing == 1:
|
||||
from jituan.services.member_recharge import fulfill_member_balance_purchase
|
||||
huiyuan_record, is_first_buy = fulfill_member_balance_purchase(
|
||||
czjilu, huiyuan, club_id=club_id,
|
||||
from jituan.services.member_recharge import (
|
||||
fulfill_member_balance_purchase,
|
||||
validate_member_purchase,
|
||||
)
|
||||
response_data['huiyuan'] = {
|
||||
'huiyuanid': huiyuan_id,
|
||||
'huiyuanming': huiyuan.jieshao,
|
||||
'daoqi': huiyuan_record.daoqi_time.strftime('%Y-%m-%d %H:%M:%S') if hasattr(huiyuan_record, 'daoqi_time') else '',
|
||||
}
|
||||
if is_first_buy:
|
||||
try:
|
||||
dashou = UserDashou.query.get(user=user)
|
||||
dashou.jifen += 10
|
||||
dashou.save(update_fields=['jifen'])
|
||||
response_data['jifen'] = dashou.jifen
|
||||
except UserDashou.DoesNotExist:
|
||||
pass
|
||||
try:
|
||||
ok, err_msg, _, _ = validate_member_purchase(
|
||||
yonghuid, huiyuan_id, club_id, is_trial=False,
|
||||
)
|
||||
if not ok:
|
||||
raise MemberRechargeError(err_msg or '会员不可购买', 'purchase_not_allowed')
|
||||
huiyuan_record, new_jifen = fulfill_member_balance_purchase(
|
||||
czjilu, huiyuan, club_id=club_id,
|
||||
)
|
||||
response_data['huiyuan'] = {
|
||||
'huiyuanid': huiyuan_id,
|
||||
'huiyuanming': huiyuan.jieshao,
|
||||
'daoqi': huiyuan_record.daoqi_time.strftime('%Y-%m-%d %H:%M:%S') if hasattr(huiyuan_record, 'daoqi_time') else '',
|
||||
}
|
||||
response_data['jifen'] = new_jifen
|
||||
except MemberRechargeError as exc:
|
||||
setattr(locked_profile, identity['balance_field'], current_balance)
|
||||
locked_profile.save(update_fields=[identity['balance_field']])
|
||||
czjilu.zhuangtai = MEMBER_FAILED_STATUS
|
||||
czjilu.shuoming = f"{shuoming} | 履约失败:{str(exc)[:40]}"[:100]
|
||||
czjilu.save(update_fields=['zhuangtai', 'shuoming'])
|
||||
fulfill_error = exc
|
||||
|
||||
elif leixing == 2:
|
||||
try:
|
||||
dashou = UserDashou.objects.select_for_update().get(user=user)
|
||||
dashou.yajin += yajin_jine
|
||||
dashou.save(update_fields=['yajin'])
|
||||
czjilu.zhuangtai = MEMBER_PAID_STATUS
|
||||
czjilu.save(update_fields=['zhuangtai'])
|
||||
response_data['yajin'] = dashou.yajin
|
||||
except UserDashou.DoesNotExist:
|
||||
return Response({'code': 500, 'msg': '打手信息不存在,无法充值押金'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
setattr(locked_profile, identity['balance_field'], current_balance)
|
||||
locked_profile.save(update_fields=[identity['balance_field']])
|
||||
czjilu.zhuangtai = MEMBER_FAILED_STATUS
|
||||
czjilu.shuoming = f"{shuoming} | 履约失败:打手不存在"[:100]
|
||||
czjilu.save(update_fields=['zhuangtai', 'shuoming'])
|
||||
fulfill_error = MemberRechargeError('打手信息不存在,无法充值押金', 'dashou_not_found')
|
||||
|
||||
elif leixing == 4:
|
||||
if fadan_obj.Status not in [1, 3]:
|
||||
@@ -1082,14 +1102,29 @@ class DsqrgmdhView(APIView):
|
||||
new_jifen = dashou.jifen + INTEGRAL_INCREMENT
|
||||
dashou.jifen = min(new_jifen, MAX_INTEGRAL)
|
||||
dashou.save(update_fields=['jifen'])
|
||||
czjilu.zhuangtai = MEMBER_PAID_STATUS
|
||||
czjilu.save(update_fields=['zhuangtai'])
|
||||
response_data['jifen'] = dashou.jifen
|
||||
except UserDashou.DoesNotExist:
|
||||
return Response({'code': 500, 'msg': '打手信息不存在,无法补充积分'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
setattr(locked_profile, identity['balance_field'], current_balance)
|
||||
locked_profile.save(update_fields=[identity['balance_field']])
|
||||
czjilu.zhuangtai = MEMBER_FAILED_STATUS
|
||||
czjilu.shuoming = f"{shuoming} | 履约失败:打手不存在"[:100]
|
||||
czjilu.save(update_fields=['zhuangtai', 'shuoming'])
|
||||
fulfill_error = MemberRechargeError('打手信息不存在,无法补充积分', 'dashou_not_found')
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"事务处理异常: {str(e)}", exc_info=True)
|
||||
return Response({'code': 500, 'msg': f'支付处理失败: {str(e)}'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
if fulfill_error:
|
||||
logger.warning('余额抵扣履约失败 dingdan=%s err=%s', dingdan_id, fulfill_error)
|
||||
return Response({
|
||||
'code': 400,
|
||||
'msg': str(fulfill_error),
|
||||
'data': response_data,
|
||||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
final_profile = identity['model'].query.get(user=user)
|
||||
response_data['balance'] = str(getattr(final_profile, identity['balance_field']))
|
||||
|
||||
|
||||
@@ -168,8 +168,15 @@ class HuiyuanGoumai(APIView):
|
||||
try:
|
||||
pay_openid, _ = get_payment_openid(request)
|
||||
if not pay_openid:
|
||||
chongzhi_jilu.delete()
|
||||
return Response({'code': 400, 'message': '用户openid不存在'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
from jituan.services.member_recharge import MEMBER_CANCELLED_STATUS
|
||||
chongzhi_jilu.zhuangtai = MEMBER_CANCELLED_STATUS
|
||||
chongzhi_jilu.shuoming = f"{shuoming} | 下单失败:openid不存在"[:100]
|
||||
chongzhi_jilu.save(update_fields=['zhuangtai', 'shuoming'])
|
||||
return Response({
|
||||
'code': 400,
|
||||
'message': '用户openid不存在',
|
||||
'dingdanid': dingdanid,
|
||||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
pay_params = self.generate_wechat_pay_params(
|
||||
dingdanid=dingdanid,
|
||||
jine=jine,
|
||||
@@ -177,8 +184,10 @@ class HuiyuanGoumai(APIView):
|
||||
pay_type='huiyuan'
|
||||
)
|
||||
except Exception as e:
|
||||
# 支付参数生成失败,删除订单记录
|
||||
chongzhi_jilu.delete()
|
||||
from jituan.services.member_recharge import MEMBER_CANCELLED_STATUS
|
||||
chongzhi_jilu.zhuangtai = MEMBER_CANCELLED_STATUS
|
||||
chongzhi_jilu.shuoming = f"{shuoming} | 下单失败:{str(e)[:40]}"[:100]
|
||||
chongzhi_jilu.save(update_fields=['zhuangtai', 'shuoming'])
|
||||
logger.error(f"生成会员支付参数失败: {str(e)}")
|
||||
return Response({
|
||||
'code': 500,
|
||||
|
||||
@@ -290,26 +290,7 @@ class DashouHuiyuanList(APIView):
|
||||
)
|
||||
} if _huiyuan_ids else {}
|
||||
|
||||
# 批量预取 2:当前用户已购 Huiyuangoumai,按 huiyuan_id 索引
|
||||
_goumai_map = {
|
||||
gm.huiyuan_id: gm
|
||||
for gm in Huiyuangoumai.query.filter(
|
||||
yonghu_id=yonghuid,
|
||||
huiyuan_id__in=_huiyuan_ids,
|
||||
)
|
||||
} if _huiyuan_ids else {}
|
||||
|
||||
# 批量预取 3:当前用户在本俱乐部已支付正式会员的 huiyuan_id 集合
|
||||
_formal_paid_set = set(
|
||||
Czjilu.query.filter(
|
||||
yonghuid=yonghuid,
|
||||
huiyuan_id__in=_huiyuan_ids,
|
||||
leixing=1,
|
||||
zhuangtai=3,
|
||||
club_id=club_id,
|
||||
is_trial=False,
|
||||
).values_list('huiyuan_id', flat=True)
|
||||
) if _huiyuan_ids else set()
|
||||
from jituan.services.member_recharge import can_purchase_trial
|
||||
|
||||
def _local_sellable(cp_row, is_trial):
|
||||
"""单行 ClubHuiyuanPrice 计算 sellable/price/days(避免重复查询)。"""
|
||||
@@ -330,17 +311,9 @@ class DashouHuiyuanList(APIView):
|
||||
return True, price, days
|
||||
|
||||
def _local_can_trial(huiyuan_id):
|
||||
"""基于已预取的数据判断体验会员是否可购。"""
|
||||
cp_row = _club_price_map.get(huiyuan_id)
|
||||
trial_sellable, _, _ = _local_sellable(cp_row, is_trial=True)
|
||||
if not trial_sellable:
|
||||
return False
|
||||
goumai = _goumai_map.get(huiyuan_id)
|
||||
if goumai and goumai.has_used_trial:
|
||||
return False
|
||||
if huiyuan_id in _formal_paid_set:
|
||||
return False
|
||||
return True
|
||||
"""与下单/履约共用 can_purchase_trial。"""
|
||||
ok, _ = can_purchase_trial(yonghuid, huiyuan_id, club_id)
|
||||
return ok
|
||||
|
||||
huiyuan_list = []
|
||||
for huiyuan in huiyuan_queryset:
|
||||
|
||||
Reference in New Issue
Block a user