From 51b380824ae74a171f902a714e6cc94ff470507a Mon Sep 17 00:00:00 2001 From: huangyw <1207046171@qq.com> Date: Thu, 14 Nov 2024 21:38:56 +0800 Subject: [PATCH] =?UTF-8?q?release-=20=E8=8B=B9=E6=9E=9C=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E5=92=8C=E5=BE=AE=E4=BF=A1=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/enums/MembershipEnums.java | 9 + .../user/controller/ApplePayController.java | 25 +- .../user/controller/MembershipController.java | 16 ++ .../controller/WeChatApiV3PayController.java | 78 +++-- .../com/luoo/user/dao/MembershipOrderDao.java | 8 + .../java/com/luoo/user/dao/VipSkuDao.java | 8 + .../com/luoo/user/pojo/MembershipOrder.java | 71 +++++ .../main/java/com/luoo/user/pojo/VipSku.java | 43 +++ .../luoo/user/service/MemberShipService.java | 271 ++++++++++++++++++ .../user/service/MembershipCodeService.java | 2 +- .../com/luoo/user/vo/membership/VipSkuVO.java | 36 +++ 11 files changed, 506 insertions(+), 61 deletions(-) create mode 100644 luoo_user/src/main/java/com/luoo/user/dao/MembershipOrderDao.java create mode 100644 luoo_user/src/main/java/com/luoo/user/dao/VipSkuDao.java create mode 100644 luoo_user/src/main/java/com/luoo/user/pojo/MembershipOrder.java create mode 100644 luoo_user/src/main/java/com/luoo/user/pojo/VipSku.java create mode 100644 luoo_user/src/main/java/com/luoo/user/service/MemberShipService.java create mode 100644 luoo_user/src/main/java/com/luoo/user/vo/membership/VipSkuVO.java diff --git a/luoo_common/src/main/java/enums/MembershipEnums.java b/luoo_common/src/main/java/enums/MembershipEnums.java index f7aadc5..7ca751a 100644 --- a/luoo_common/src/main/java/enums/MembershipEnums.java +++ b/luoo_common/src/main/java/enums/MembershipEnums.java @@ -18,6 +18,15 @@ public enum MembershipEnums { DISCARD(4, "已废弃"), + WX_PAY(1, "微信支付"), + ALI_PAY(2, "支付宝支付"), + MEMBERSHIP_CODE(3, "会员码兑换"), + APPLE_PAY(4, "苹果支付"), + + WAIT_PAY(0, "待支付"), + PAY_SUCCESS(1, "支付成功"), + PAY_FAIL(2, "支付失败"), + diff --git a/luoo_user/src/main/java/com/luoo/user/controller/ApplePayController.java b/luoo_user/src/main/java/com/luoo/user/controller/ApplePayController.java index ef22d12..680d33e 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/ApplePayController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/ApplePayController.java @@ -5,6 +5,7 @@ import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.luoo.user.pojo.AppleOrder; import com.luoo.user.pojo.AppleResponse; +import com.luoo.user.service.MemberShipService; import com.luoo.user.util.IosVerifyUtil; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -62,6 +63,9 @@ public class ApplePayController { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } }; + @Autowired + private MemberShipService memberShipService; + /** * 接收自己APP客户端发过来的购买凭证 * @@ -153,26 +157,7 @@ public class ApplePayController { String quantity = appleOrder.getQuantity(); // 获取用户购买的产品Id, String product_id = appleOrder.getProduct_id(); - switch (product_id) { - case "cn.luoo.IndieMusic.annual": { - // 年度会员 逻辑从这开始 - System.out.println("appleIAP 年度会员 逻辑从这开始,userid___" +userId); - - //这里拿到userId,在我们自己的订单逻辑里插一条订单记录,给用户开通相应的会员 - userId=""; - break; - } - case "cn.luoo.IndieMusic.monthly": { - // 月度会员逻辑从这开始 - System.out.println("appleIAP 月度会员 逻辑从这开始,userid___"+userId);; - break; - } - case "cn.luoo.IndieMusic.lifetime": { - // 终身会员逻辑从这开始 - System.out.println("appleIAP 终身会员 逻辑从这开始,userid___"+userId); - break; - } - } + memberShipService.applePayMembershipOrder(product_id, userId); log.info("{}发送奖励成功喽!",userId); } } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/MembershipController.java b/luoo_user/src/main/java/com/luoo/user/controller/MembershipController.java index 684b7a8..3b2c5e9 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/MembershipController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/MembershipController.java @@ -5,6 +5,7 @@ import api.PageResult; import api.Result; import com.luoo.user.dto.memberShip.MembershipCodeSearchDto; import com.luoo.user.pojo.MembershipCode; +import com.luoo.user.service.MemberShipService; import com.luoo.user.service.MembershipCodeService; import controller.BaseController; import io.swagger.annotations.*; @@ -37,6 +38,9 @@ public class MembershipController extends BaseController { @Autowired private MembershipCodeService membershipCodeService; + @Autowired + private MemberShipService memberShipService; + @ApiOperation(value = "1.生成会员码", notes = "仅限admin权限用户调用") @GetMapping("/code/generate") @GlobalInterceptor(checkAdminLogin = true) @@ -119,4 +123,16 @@ public class MembershipController extends BaseController { return Result.success(); } + @ApiOperation(value = "6.会员购买", notes = "仅限app用户调用-获取微信预支付id") + @PostMapping("/prepay") + @GlobalInterceptor(checkAppUserLogin = true) + @ApiImplicitParams(value = { + @ApiImplicitParam(name = "skuId", value = "会员skuId", required = true, dataType = "String", paramType = "query") + }) + public Result prepay( + @RequestHeader(value = "Authorization", required = true) String authorization, + @NotNull String skuId) { + return Result.success(memberShipService.prepay(skuId)); + } + } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/WeChatApiV3PayController.java b/luoo_user/src/main/java/com/luoo/user/controller/WeChatApiV3PayController.java index 1691f70..d07a9be 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/WeChatApiV3PayController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/WeChatApiV3PayController.java @@ -6,6 +6,7 @@ import com.ijpay.core.kit.WxPayKit; import com.luoo.user.config.WxPayConfig; import com.luoo.user.config.WxPayNotificationConfig; import com.luoo.user.config.WxPayPublicKeyConfig; +import com.luoo.user.service.MemberShipService; import com.luoo.user.service.WxPayApiV3Service; import com.wechat.pay.java.core.Config; import com.wechat.pay.java.core.RSAAutoCertificateConfig; @@ -41,7 +42,6 @@ public class WeChatApiV3PayController extends BaseController { // public static AppService service; - @Autowired private WxPayNotificationConfig wxPayNotificationConfig; @@ -52,36 +52,39 @@ public class WeChatApiV3PayController extends BaseController { @Autowired private WxPayApiV3Service wxPayApiV3Service; - // 下单接口 - @GetMapping("/appPay") - public Result appPay() { - - - try { - - PrepayResponse response = wxPayApiV3Service.prepay(); - - - Map map = WxPayKit.appCreateSign(wxPayConfig.getAppId(), wxPayConfig.getMchId(), response.getPrepayId(), wxPayConfig.getPrivateKeyPath()); - return Result.success(map); - } catch (HttpException e) { // 发送HTTP请求失败 - // 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义 - e.getHttpRequest(); - System.out.println( e.getHttpRequest()); - } catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500 - // 调用e.getResponseBody()获取返回体打印日志或上报监控,更多方法见ServiceException定义 - e.getResponseBody(); - System.out.println(e.getResponseBody()); - } catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败 - // 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义 - e.getMessage(); - System.out.println(e.getMessage()); - } catch (Exception e) { - throw new RuntimeException(e); - } + @Autowired + private MemberShipService memberShipService; - return Result.success(""); - } + // 下单接口 +// @GetMapping("/appPay") +// public Result appPay() { +// +// +// try { +// +// PrepayResponse response = wxPayApiV3Service.prepay(); +// +// +// Map map = WxPayKit.appCreateSign(wxPayConfig.getAppId(), wxPayConfig.getMchId(), response.getPrepayId(), wxPayConfig.getPrivateKeyPath()); +// return Result.success(map); +// } catch (HttpException e) { // 发送HTTP请求失败 +// // 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义 +// e.getHttpRequest(); +// System.out.println(e.getHttpRequest()); +// } catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500 +// // 调用e.getResponseBody()获取返回体打印日志或上报监控,更多方法见ServiceException定义 +// e.getResponseBody(); +// System.out.println(e.getResponseBody()); +// } catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败 +// // 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义 +// e.getMessage(); +// System.out.println(e.getMessage()); +// } catch (Exception e) { +// throw new RuntimeException(e); +// } +// +// return Result.success(""); +// } @PostMapping("/payNotify") @@ -90,27 +93,22 @@ public class WeChatApiV3PayController extends BaseController { String wechatpayNonce = request.getHeader("Wechatpay-Nonce"); String wechatPaySerial = request.getHeader("Wechatpay-Serial"); String wechatSignature = request.getHeader("Wechatpay-Signature"); -// request. + // request. System.out.println("body:_____" + requestBody); // 构造 RequestParam - - RequestParam requestParam = new RequestParam.Builder().serialNumber(wechatPaySerial).nonce(wechatpayNonce).signature(wechatSignature).timestamp(wechatTimestamp).body(requestBody).build(); + // 如果已经初始化了 RSAAutoCertificateConfig,可直接使用 + // 没有的话,则构造一个 -// 如果已经初始化了 RSAAutoCertificateConfig,可直接使用 -// 没有的话,则构造一个 - - - - -// 初始化 NotificationParser + // 初始化 NotificationParser NotificationParser parser = new NotificationParser(wxPayNotificationConfig.config()); try { // 以支付通知回调为例,验签、解密并转换成 Transaction Transaction transaction = parser.parse(requestParam, Transaction.class); System.out.println("transaction:_____" + transaction.toString()); + memberShipService.updateOrderStatus(transaction); } catch (ValidationException e) { // 签名验证失败,返回 401 UNAUTHORIZED 状态码 // logger.error("sign verification failed", e); diff --git a/luoo_user/src/main/java/com/luoo/user/dao/MembershipOrderDao.java b/luoo_user/src/main/java/com/luoo/user/dao/MembershipOrderDao.java new file mode 100644 index 0000000..d0b0b07 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dao/MembershipOrderDao.java @@ -0,0 +1,8 @@ +package com.luoo.user.dao; + +import com.luoo.user.pojo.MembershipOrder; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +public interface MembershipOrderDao extends JpaRepository, JpaSpecificationExecutor { +} diff --git a/luoo_user/src/main/java/com/luoo/user/dao/VipSkuDao.java b/luoo_user/src/main/java/com/luoo/user/dao/VipSkuDao.java new file mode 100644 index 0000000..92c3b4a --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dao/VipSkuDao.java @@ -0,0 +1,8 @@ +package com.luoo.user.dao; + +import com.luoo.user.pojo.VipSku; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +public interface VipSkuDao extends JpaRepository, JpaSpecificationExecutor { +} diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/MembershipOrder.java b/luoo_user/src/main/java/com/luoo/user/pojo/MembershipOrder.java new file mode 100644 index 0000000..5c28864 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/pojo/MembershipOrder.java @@ -0,0 +1,71 @@ +package com.luoo.user.pojo; + +import com.luoo.user.config.JPABasePojo; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.hibernate.annotations.DynamicUpdate; +import org.hibernate.annotations.Where; + +import javax.persistence.Entity; +import javax.persistence.Table; +import java.time.LocalDate; + +/** + * @program: luoo_parent + * @description: 会员开通订单 + * @author: yawei.huang + * @create: 2024-11-14 20:48 + **/ +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Entity +@DynamicUpdate +@Where(clause = "del_flag = 0") +@Table(name = "tb_membership_order") +public class MembershipOrder extends JPABasePojo { + + @ApiModelProperty(value = "用户id") + private String userId; + + @ApiModelProperty(value = "商品id") + private String skuId; + + @ApiModelProperty(value = "会员码id") + private String membershipCodeId; + + @ApiModelProperty(value = "会员码") + private String membershipCode; + + @ApiModelProperty(value = "支付方式 1-微信支付 2-支付宝支付 3-会员码兑换") + private Integer payType; + + @ApiModelProperty(value = "订单号") + private String orderCode; + + @ApiModelProperty(value = "天数") + private Integer duration; + + @ApiModelProperty(value = "是否永久会员 1-是 2-否") + private Integer life; + + @ApiModelProperty(value = "价格,单位(分)") + private Integer amount; + + @ApiModelProperty(value = "昵称") + private String nickName; + + @ApiModelProperty(value = "开始时间") + private LocalDate vipStartDate; + + @ApiModelProperty(value = "结束时间") + private LocalDate vipStopDate; + + @ApiModelProperty(value = "支付状态 0-待支付 1-支付成功 2-支付失败") + private Integer status; + +} diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/VipSku.java b/luoo_user/src/main/java/com/luoo/user/pojo/VipSku.java new file mode 100644 index 0000000..7f63d13 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/pojo/VipSku.java @@ -0,0 +1,43 @@ +package com.luoo.user.pojo; + +import com.luoo.user.config.JPABasePojo; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.hibernate.annotations.DynamicUpdate; +import org.hibernate.annotations.Where; + +import javax.persistence.Entity; +import javax.persistence.Table; +import java.math.BigDecimal; + +/** + * @program: luoo_parent + * @description: vip_sku + * @author: yawei.huang + * @create: 2024-11-14 19:59 + **/ +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Entity +@DynamicUpdate +@Where(clause = "del_flag = 0") +@Table(name = "tb_vip_sku") +public class VipSku extends JPABasePojo { + + @ApiModelProperty(value = "描述") + private String description; + + @ApiModelProperty(value = "天数") + private Integer days; + + @ApiModelProperty(value = "是否永久会员 1-是 2-否") + private Integer lifeFlag; + + @ApiModelProperty(value = "价格,单位(分)") + private Integer amount; +} diff --git a/luoo_user/src/main/java/com/luoo/user/service/MemberShipService.java b/luoo_user/src/main/java/com/luoo/user/service/MemberShipService.java new file mode 100644 index 0000000..456e5e5 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/service/MemberShipService.java @@ -0,0 +1,271 @@ +package com.luoo.user.service; + +import api.Result; +import com.ijpay.core.kit.WxPayKit; +import com.luoo.user.config.WxPayConfig; +import com.luoo.user.config.WxPayPublicKeyConfig; +import com.luoo.user.dao.MembershipOrderDao; +import com.luoo.user.dao.UserInfoDao; +import com.luoo.user.dao.VipSkuDao; +import com.luoo.user.pojo.MembershipOrder; +import com.luoo.user.pojo.QMembershipOrder; +import com.luoo.user.pojo.UserInfo; +import com.luoo.user.pojo.VipSku; +import com.luoo.user.vo.membership.VipSkuVO; +import com.querydsl.jpa.impl.JPAQueryFactory; +import com.wechat.pay.java.service.payments.app.AppService; +import com.wechat.pay.java.service.payments.app.model.Amount; +import com.wechat.pay.java.service.payments.app.model.PrepayRequest; +import com.wechat.pay.java.service.payments.app.model.PrepayResponse; +import com.wechat.pay.java.service.payments.model.Transaction; +import constants.TaskPointIdConstants; +import dto.UserLoginDto; +import enums.MembershipEnums; +import enums.PointEnums; +import enums.UserVipStatusEnum; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import util.IdWorker; +import util.JwtUtil; + +import javax.annotation.Resource; +import java.time.LocalDate; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +/** + * @program: luoo_parent + * @description: 会员业务 + * @author: yawei.huang + * @create: 2024-11-14 20:10 + **/ +@Service +@Slf4j +public class MemberShipService { + + private final VipSkuDao vipSkuDao; + + private final WxPayConfig wxPayConfig; + + private final IdWorker idWorker; + + private final WxPayPublicKeyConfig wxPayPublicKeyConfig; + + public static AppService wxPayAppService; + + private final MembershipOrderDao membershipOrderDao; + + private final JwtUtil jwtUtil; + + private final JPAQueryFactory jpaQueryFactory; + + private final UserInfoDao userInfoDao; + + private final UserPointLogService userPointLogService; + private final UserInfoService userInfoService; + + public MemberShipService(VipSkuDao vipSkuDao, WxPayConfig wxPayConfig, IdWorker idWorker, WxPayPublicKeyConfig wxPayPublicKeyConfig, MembershipOrderDao membershipOrderDao, JwtUtil jwtUtil, JPAQueryFactory jpaQueryFactory, UserInfoDao userInfoDao, UserPointLogService userPointLogService, UserInfoService userInfoService) { + this.vipSkuDao = vipSkuDao; + this.wxPayConfig = wxPayConfig; + this.idWorker = idWorker; + this.wxPayPublicKeyConfig = wxPayPublicKeyConfig; + this.membershipOrderDao = membershipOrderDao; + this.jwtUtil = jwtUtil; + this.jpaQueryFactory = jpaQueryFactory; + this.userInfoDao = userInfoDao; + this.userPointLogService = userPointLogService; + this.userInfoService = userInfoService; + } + + /** + * 获取会员商品SKU + * + * @return + */ + public List getVipSkuList() { + List all = vipSkuDao.findAll(); + return all.stream().map(item -> VipSkuVO.builder() + .id(item.getId()) + .description(item.getDescription()) + .days(item.getDays()) + .lifeFlag(item.getLifeFlag()) + .amount(item.getAmount()) + .build()).collect(Collectors.toList()); + } + + /** + * 前端发起支付请求,获得预支付id,同时创建订单 + * + * @param skuId 会员商品id + * @return 拉起微信支付的参数 + */ + @Transactional(rollbackFor = Exception.class) + public Object prepay(String skuId) { + VipSku one = vipSkuDao.getOne(skuId); + + PrepayRequest request = new PrepayRequest(); + // 初始化服务 + wxPayAppService = new AppService.Builder().config(wxPayPublicKeyConfig.config()).build(); + // 内部订单号 + String outTradeNo = idWorker.nextId() + ""; + UserLoginDto user = jwtUtil.getUser(); + + // 创建订单 + MembershipOrder membershipOrder = MembershipOrder.builder() + .orderCode(outTradeNo) + .skuId(skuId) + .payType(MembershipEnums.WX_PAY.getCode()) + .amount(one.getAmount()) + .duration(one.getDays()) + .life(one.getLifeFlag()) + .userId(user.getUserId()) + .nickName(user.getNickName()) + .status(MembershipEnums.WAIT_PAY.getCode()) + .build(); + membershipOrder.setId(String.valueOf(idWorker.nextId())); + membershipOrderDao.save(membershipOrder); + + // 调用request.setXxx(val)设置所需参数,具体参数可见Request定义 + Amount amount = new Amount(); + amount.setTotal(one.getAmount()); + request.setAmount(amount); + request.setAppid(wxPayConfig.getAppId()); + request.setMchid(wxPayConfig.getMchId()); + request.setDescription(one.getDescription()); + request.setNotifyUrl(wxPayConfig.getNotifyUrl()); + + request.setOutTradeNo(outTradeNo); + // 调用接口 + PrepayResponse response = wxPayAppService.prepay(request); + Map map = null; + try { + map = WxPayKit.appCreateSign(wxPayConfig.getAppId(), wxPayConfig.getMchId(), response.getPrepayId(), wxPayConfig.getPrivateKeyPath()); + } catch (Exception e) { + throw new RuntimeException(e); + } + return map; + } + + @Transactional(rollbackFor = Exception.class) + public void updateOrderStatus(Transaction transaction) { + String outTradeNo = transaction.getOutTradeNo(); + QMembershipOrder qMembershipOrder = QMembershipOrder.membershipOrder; + MembershipOrder one = jpaQueryFactory.selectFrom(qMembershipOrder) + .where(qMembershipOrder.orderCode.eq(outTradeNo)) + .fetchOne(); + + if ("SUCCESS".equals(transaction.getTradeState().toString())) { + one.setStatus(MembershipEnums.PAY_SUCCESS.getCode()); + + dealUserVipCalc(one); + + } else { + one.setStatus(MembershipEnums.PAY_FAIL.getCode()); + } + membershipOrderDao.save(one); + } + + /** + * 处理用户vip状态 + * + * @param one 会员订单表 + */ + private void dealUserVipCalc(MembershipOrder one) { + // 处理用户表 + UserInfo userInfo = userInfoDao.findById(one.getUserId()).get(); + + if (Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.INITIAL.getCode())) { + // 首次开通会员 + userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.BUY_MEMBER, one.getUserId()); + } + + // 永久会员码 + if (Objects.equals(one.getLife(), PointEnums.MEMBERSHIP_LIFE.getCode())) { + userInfo.setVipStatus(UserVipStatusEnum.LIFE.getCode()); + } else { + // 非永久会员码 + // 计算会员到期时间 + LocalDate vipExpireTime; + if (Objects.equals(UserVipStatusEnum.ACTIVE.getCode(), userInfo.getVipStatus())) { + // 如果是当前正是会员,以到期时间为基准,再增加membershipCode的duration天 + vipExpireTime = (userInfo.getVipExpireTime() == null ? LocalDate.now() : userInfo.getVipExpireTime()).plusDays(one.getDuration()); + } else { + // 如果是当前不是会员,从今天开始,增加membershipCode的duration天 + userInfo.setVipStatus(UserVipStatusEnum.INITIAL.getCode()); + vipExpireTime = LocalDate.now().plusDays(one.getDuration()); + } + userInfo.setVipExpireTime(vipExpireTime); + // 无论如何当即设置为会员生效状态 + userInfo.setVipStatus(UserVipStatusEnum.ACTIVE.getCode()); + } + userInfoDao.save(userInfo); + } + + /** + * 苹果内购 + * + * @param type 会员种类 + * @param userId 用户id + */ + @Transactional(rollbackFor = Exception.class) + public void applePayMembershipOrder(String type, String userId) { + // 内部订单号 + String outTradeNo = idWorker.nextId() + ""; + UserInfo userInfo = userInfoService.findById(userId); + // 创建订单 + // 苹果支付只有成功 + MembershipOrder membershipOrder = MembershipOrder.builder() + .orderCode(outTradeNo) + .payType(MembershipEnums.APPLE_PAY.getCode()) + .userId(userInfo.getId()) + .nickName(userInfo.getNickName()) + .status(MembershipEnums.PAY_SUCCESS.getCode()) + .build(); + + String skuId = ""; + switch (type) { + case "cn.luoo.IndieMusic.annual": { + // 年度会员 逻辑从这开始 + System.out.println("appleIAP 年度会员 逻辑从这开始,userid___" + userId); + // todo 这边会员种类是写死的,最好苹果支付有个字段能对应,否则每次都要改代码后发布 + skuId = "2"; + + //这里拿到userId,在我们自己的订单逻辑里插一条订单记录,给用户开通相应的会员 + userId = ""; + break; + } + case "cn.luoo.IndieMusic.monthly": { + // 月度会员逻辑从这开始 + System.out.println("appleIAP 月度会员 逻辑从这开始,userid___" + userId); + ; + skuId = "1"; + break; + } + case "cn.luoo.IndieMusic.lifetime": { + // 终身会员逻辑从这开始 + System.out.println("appleIAP 终身会员 逻辑从这开始,userid___" + userId); + skuId = "3"; + break; + } + } + + if (StringUtils.isNotBlank(skuId)) { + membershipOrder.setId(String.valueOf(idWorker.nextId())); + membershipOrder.setSkuId(skuId); + VipSku vipSku = vipSkuDao.getOne(skuId); + membershipOrder.setAmount(vipSku.getAmount()); + membershipOrder.setLife(vipSku.getLifeFlag()); + membershipOrder.setDuration(vipSku.getDays()); + + dealUserVipCalc(membershipOrder); + membershipOrderDao.save(membershipOrder); + } + + + } +} diff --git a/luoo_user/src/main/java/com/luoo/user/service/MembershipCodeService.java b/luoo_user/src/main/java/com/luoo/user/service/MembershipCodeService.java index af11b1d..b002ce2 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/MembershipCodeService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/MembershipCodeService.java @@ -164,7 +164,7 @@ public class MembershipCodeService { // 非永久会员码 // 计算会员到期时间 LocalDate vipExpireTime; - if (!Objects.equals(UserVipStatusEnum.INITIAL.getCode(), userInfo.getVipStatus())) { + if (Objects.equals(UserVipStatusEnum.ACTIVE.getCode(), userInfo.getVipStatus())) { vipExpireTime = (userInfo.getVipExpireTime() == null ? LocalDate.now() : userInfo.getVipExpireTime()).plusDays(membershipCode.getDuration()); } else { // 从今天开始,增加membershipCode的duration天 diff --git a/luoo_user/src/main/java/com/luoo/user/vo/membership/VipSkuVO.java b/luoo_user/src/main/java/com/luoo/user/vo/membership/VipSkuVO.java new file mode 100644 index 0000000..8d7bb67 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/vo/membership/VipSkuVO.java @@ -0,0 +1,36 @@ +package com.luoo.user.vo.membership; + +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.math.BigDecimal; + +/** + * @program: luoo_parent + * @description: 会员SKU + * @author: yawei.huang + * @create: 2024-11-14 20:20 + **/ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class VipSkuVO implements Serializable { + private String id; + + @ApiModelProperty(value = "描述") + private String description; + + @ApiModelProperty(value = "天数") + private Integer days; + + @ApiModelProperty(value = "是否永久会员 1-是 2-否") + private Integer lifeFlag; + + @ApiModelProperty(value = "价格,单位(分)") + private Integer amount; +}