Stripe 添加一次订阅服务(默认关闭自动续订功能)

This commit is contained in:
2025-02-17 17:14:30 +08:00
parent d2d5eebe12
commit 37ff60fcfb
12 changed files with 207 additions and 68 deletions

View File

@@ -1,5 +1,6 @@
package com.ai.da.common.utils; package com.ai.da.common.utils;
import com.ai.da.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.text.ParseException; import java.text.ParseException;
@@ -96,7 +97,7 @@ public class DateUtil {
} }
public static String changeTimeStampFormat(LocalDateTime localDate){ public static String changeTimeStampFormat(LocalDateTime localDate){
return localDate.format(DateTimeFormatter.ofPattern("MMM. dd, yyyy, EEEE", Locale.US)); return localDate.format(DateTimeFormatter.ofPattern(CommonConstant.TIME_FORMAT_MMM_dd_yyyy_EEEE, Locale.US));
} }
} }

View File

@@ -841,7 +841,7 @@ public class SendEmailUtil {
try { try {
String merchantEmail = "kimwong@code-create.com.hk"; String merchantEmail = "kimwong@code-create.com.hk";
String developer = "xupei3360@163.com"; String developer = "xupei3360@163.com";
String[] receiverEmail = {merchantEmail, developer}; String[] receiverEmail = {/*merchantEmail, */developer};
Credential cred = new Credential(SECRET_ID, SECRET_KEy); Credential cred = new Credential(SECRET_ID, SECRET_KEy);
// 实例化一个http选项可选的没有特殊需求可以跳过 // 实例化一个http选项可选的没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile(); HttpProfile httpProfile = new HttpProfile();
@@ -1008,7 +1008,7 @@ public class SendEmailUtil {
req.setFromEmailAddress(SEND_ADDRESS); req.setFromEmailAddress(SEND_ADDRESS);
String merchantEmail = "kimwong@code-create.com.hk"; String merchantEmail = "kimwong@code-create.com.hk";
String developerEmail = "xupei@code-create.com.hk"; String developerEmail = "xupei@code-create.com.hk";
req.setDestination(new String[]{merchantEmail, developerEmail}); req.setDestination(new String[]{/*merchantEmail,*/ developerEmail});
Template template = new Template(); Template template = new Template();
req.setSubject("New Credit Purchase Order"); req.setSubject("New Credit Purchase Order");
template.setTemplateID(CREDITS_PURCHASE_MERCHANT); template.setTemplateID(CREDITS_PURCHASE_MERCHANT);

View File

@@ -23,6 +23,8 @@ public class OrderInfo extends BaseEntity{
private String note; private String note;
private byte autoRenewal;
private String paymentType;//支付方式 private String paymentType;//支付方式
// 可用于标记用户订单是否首次订阅 // 可用于标记用户订单是否首次订阅

View File

@@ -42,6 +42,8 @@ public class SubscriptionEmailParamsDTO {
// 订阅开始时间 // 订阅开始时间
private String startDate; private String startDate;
private String endDate;
// 下一个支付日期 // 下一个支付日期
private String nextPayDate; private String nextPayDate;

View File

@@ -15,7 +15,8 @@ public interface OrderInfoService extends IService<OrderInfo> {
OrderInfo createOrderByProductId(Integer productId, String paymentType, HttpServletRequest request); OrderInfo createOrderByProductId(Integer productId, String paymentType, HttpServletRequest request);
OrderInfo createOrderByProductId(Integer amount, String paymentType, ProductEnum product, HttpServletRequest request); OrderInfo createOrderByProductId(Integer amount, String paymentType, ProductEnum product,
HttpServletRequest request, byte autoRenewal);
void saveCodeUrl(String orderNo, String codeUrl); void saveCodeUrl(String orderNo, String codeUrl);

View File

@@ -78,7 +78,7 @@ public class AffiliateServiceImpl extends ServiceImpl<AffiliateMapper, Affiliate
// 邮件通知审批者 // 邮件通知审批者
String merchantEmail = "kimwong@code-create.com.hk"; String merchantEmail = "kimwong@code-create.com.hk";
String developer = "xupei3360@163.com"; String developer = "xupei3360@163.com";
String[] receiverEmail = {merchantEmail, developer}; String[] receiverEmail = {/*merchantEmail,*/ developer};
SendEmailUtil.affiliateEmailReminder(receiverEmail, new AffiliateEmailParamsDTO(userHolder.getUsername(), promotionMethod), "new"); SendEmailUtil.affiliateEmailReminder(receiverEmail, new AffiliateEmailParamsDTO(userHolder.getUsername(), promotionMethod), "new");
}else { }else {
throw new BusinessException("You have registered an Affiliate", ResultEnum.PROMPT.getCode()); throw new BusinessException("You have registered an Affiliate", ResultEnum.PROMPT.getCode());
@@ -315,7 +315,7 @@ public class AffiliateServiceImpl extends ServiceImpl<AffiliateMapper, Affiliate
String merchantEmail = "kimwong@code-create.com.hk"; String merchantEmail = "kimwong@code-create.com.hk";
String developer = "xupei3360@163.com"; String developer = "xupei3360@163.com";
String[] receiverEmail = {merchantEmail, developer}; String[] receiverEmail = {/*merchantEmail,*/ developer};
// 邮件通知 // 邮件通知
SendEmailUtil.affiliateEmailReminder(receiverEmail, affiliateEmailParamsDTO, "summary"); SendEmailUtil.affiliateEmailReminder(receiverEmail, affiliateEmailParamsDTO, "summary");
} }

View File

@@ -89,7 +89,8 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
} }
} }
public OrderInfo createOrderByProductId(Integer amount, String paymentType, ProductEnum product, HttpServletRequest request) { public OrderInfo createOrderByProductId(Integer amount, String paymentType, ProductEnum product,
HttpServletRequest request, byte autoRenewal) {
//获取商品信息 //获取商品信息
// Product product = productMapper.selectById(amount); // Product product = productMapper.selectById(amount);

View File

@@ -236,14 +236,16 @@ public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, Payme
if (Objects.isNull(paymentInfo)){ if (Objects.isNull(paymentInfo)){
String orderNo; String orderNo;
try { try {
if (invoice.getBillingReason().equals("manual")){ String chargeId = invoice.getCharge();
// 手动创建的发票针对one-time支付 orderNo = Charge.retrieve(chargeId).getDescription().replace("AiDA - ", "");
orderNo = invoice.getLines().getData().get(0).getPrice().getMetadata().get("orderId"); // if (invoice.getBillingReason().equals("manual")){
}else { // // 手动创建的发票针对one-time支付
String subscriptionId = invoice.getSubscription(); //// orderNo = invoice.getLines().getData().get(0).getPrice().getMetadata().get("orderId");
// 从subscription中获取orderNo // }else {
orderNo = Subscription.retrieve(subscriptionId).getDescription().replace("AiDA - ", ""); // String subscriptionId = invoice.getSubscription();
} // // 从subscription中获取orderNo
// orderNo = Subscription.retrieve(subscriptionId).getDescription().replace("AiDA - ", "");
// }
} catch (StripeException e) { } catch (StripeException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@@ -39,6 +39,7 @@ import java.math.RoundingMode;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -95,9 +96,9 @@ public class StripeServiceImpl implements StripeService {
case "Year": case "Year":
productEnum = ProductEnum.AnnualSubscription; productEnum = ProductEnum.AnnualSubscription;
break; break;
/*case "Day": case "Day":
productEnum = ProductEnum.DailySubscription; productEnum = ProductEnum.DailySubscription;
break;*/ break;
default: default:
throw new BusinessException("unknown subscription type"); throw new BusinessException("unknown subscription type");
} }
@@ -106,14 +107,17 @@ public class StripeServiceImpl implements StripeService {
throw new BusinessException("unknown product type"); throw new BusinessException("unknown product type");
} }
log.info("生成订单"); log.info("生成订单");
OrderInfo orderInfo = orderInfoService.createOrderByProductId(productPurchaseDTO.getQuantity(),
PayTypeEnum.STRIPE.getType(), productEnum, request);
String payType; String payType;
byte autoRenewal;
if (productPurchaseDTO.getAutoRenewal()){ if (productPurchaseDTO.getAutoRenewal()){
payType = "recurring"; payType = "recurring";
autoRenewal = 1;
}else { }else {
payType = "one_time"; payType = "one_time";
autoRenewal = 0;
} }
OrderInfo orderInfo = orderInfoService.createOrderByProductId(productPurchaseDTO.getQuantity(),
PayTypeEnum.STRIPE.getType(), productEnum, request, autoRenewal);
try { try {
Long id = UserContext.getUserHolder().getId(); Long id = UserContext.getUserHolder().getId();
@@ -139,7 +143,10 @@ public class StripeServiceImpl implements StripeService {
// one-time 手动创建发票;订阅会自动创建invoice // one-time 手动创建发票;订阅会自动创建invoice
sessionBuilder.setInvoiceCreation(SessionCreateParams.InvoiceCreation.builder().setEnabled(Boolean.TRUE).build()); sessionBuilder.setInvoiceCreation(SessionCreateParams.InvoiceCreation.builder().setEnabled(Boolean.TRUE).build());
} }
// developer test
// sessionBuilder.setPaymentMethodConfiguration("pmc_1QIKyq02n1TEydyNKVEYvhW7"); // sessionBuilder.setPaymentMethodConfiguration("pmc_1QIKyq02n1TEydyNKVEYvhW7");
// kim test
sessionBuilder.setPaymentMethodConfiguration("pmc_1LywTWH7nPZ8bkrN6FvdCUWG");
// sessionBuilder.addPaymentMethodType(SessionCreateParams.PaymentMethodType.ALIPAY); // sessionBuilder.addPaymentMethodType(SessionCreateParams.PaymentMethodType.ALIPAY);
// sessionBuilder.addPaymentMethodType(SessionCreateParams.PaymentMethodType.CARD); // sessionBuilder.addPaymentMethodType(SessionCreateParams.PaymentMethodType.CARD);
sessionBuilder.setCustomer(customerId); sessionBuilder.setCustomer(customerId);
@@ -276,17 +283,17 @@ public class StripeServiceImpl implements StripeService {
return Boolean.FALSE; return Boolean.FALSE;
} }
log.info("stripe验签成功"); log.info("stripe验签成功");
Boolean response = Boolean.TRUE; boolean response = Boolean.TRUE;
log.info("回调事件 {}", event.getType()); log.info("回调事件 {}", event.getType());
if (stripeObject instanceof Session){ if (stripeObject instanceof Session){
Session session = (Session) stripeObject; Session session = (Session) stripeObject;
if (event.getType().equals("checkout.session.completed")) { if (event.getType().equals("checkout.session.completed")) {
processOrder(session); response = processOrder(session);
}else if (event.getType().equals("checkout.session.expired")){ }else if (event.getType().equals("checkout.session.expired")){
String orderNo = session.getMetadata().get("orderId"); String orderNo = session.getMetadata().get("orderId");
// 会话过期 未支付 且之后没有支付成功的订单 // 会话过期 未支付 且之后没有支付成功的订单
processExpiredOrder(orderNo); response = processExpiredOrder(orderNo);
} }
} else if (stripeObject instanceof Subscription){ } else if (stripeObject instanceof Subscription){
Subscription subscription = (Subscription) stripeObject; Subscription subscription = (Subscription) stripeObject;
@@ -299,7 +306,7 @@ public class StripeServiceImpl implements StripeService {
SubscriptionInfo subscriptionInfo = updateSubscription(subscription); SubscriptionInfo subscriptionInfo = updateSubscription(subscription);
log.info("订阅更新"); log.info("订阅更新");
if (subscription.getStatus().equals("active")){ if (subscription.getStatus().equals("active")){
response = sendEmail(subscription.getId(), null); response = sendEmail(subscription.getId(), null, null);
} }
// 续订支付失败,邮件通知用户 // 续订支付失败,邮件通知用户
if (subscription.getStatus().equals("past_due")){ if (subscription.getStatus().equals("past_due")){
@@ -312,9 +319,14 @@ public class StripeServiceImpl implements StripeService {
log.info("用户 {} 取消连续订阅 {}", subscriptionInfo.getAccountId(), subscription.getId()); log.info("用户 {} 取消连续订阅 {}", subscriptionInfo.getAccountId(), subscription.getId());
if (subscriptionInfo.getCancelNotified() == (byte)0){ if (subscriptionInfo.getCancelNotified() == (byte)0){
log.info("取消订阅 邮件通知商家"); log.info("取消订阅 邮件通知商家");
response = sendEmail(subscription.getId(), "cancel"); response = sendEmail(subscription.getId(), "cancel", null);
subscriptionInfo.setCancelNotified((byte)1); if (response){
subscriptionInfoMapper.updateById(subscriptionInfo); subscriptionInfo.setCancelNotified((byte)1);
subscriptionInfoMapper.updateById(subscriptionInfo);
// 更新订单信息
OrderInfo orderInfo = orderInfoService.getOrderByOrderNo(subscriptionInfo.getOrderNo());
orderInfo.setAutoRenewal((byte)0);
}
} }
}/* else if (event.getType().equals("customer.subscription.paused")){ }/* else if (event.getType().equals("customer.subscription.paused")){
@@ -334,10 +346,20 @@ public class StripeServiceImpl implements StripeService {
// 更新t_order_info中的total_fee,记录该订单的累计付款金额 // 更新t_order_info中的total_fee,记录该订单的累计付款金额
orderInfoService.updateTotalFeeByOrderNo(paymentInfo.getOrderNo()); orderInfoService.updateTotalFeeByOrderNo(paymentInfo.getOrderNo());
// 邮件通知商家和用户 // 邮件通知商家和用户
if (invoice.getBillingReason().equals("subscription_create")){ String billingReason = invoice.getBillingReason();
response = sendEmail(invoice.getSubscription(), "new"); switch (billingReason) {
} else if (invoice.getBillingReason().equals("subscription_cycle")){ case "subscription_create":
response = sendEmail(invoice.getSubscription(), "renewal"); response = sendEmail(invoice.getSubscription(), "new", null);
break;
case "subscription_cycle":
response = sendEmail(invoice.getSubscription(), "renewal", null);
break;
case "manual":
boolean b = invoice.getLines().getData().get(0).getDescription().endsWith("Subscription");
if (b) {
response = sendEmail(invoice.getSubscription(), "new", null);
}
break;
} }
} }
} else if (event.getType().equals("invoice.payment_failed")) { } else if (event.getType().equals("invoice.payment_failed")) {
@@ -386,28 +408,30 @@ public class StripeServiceImpl implements StripeService {
return response; return response;
} }
public void processOrder(Session session) { public boolean processOrder(Session session) {
String orderId = session.getMetadata().get("orderId"); Stripe.apiKey = privateKey;
String orderNo = session.getMetadata().get("orderId");
float totalAmount = new BigDecimal(session.getAmountTotal()).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).floatValue(); float totalAmount = new BigDecimal(session.getAmountTotal()).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).floatValue();
boolean resp = true;
try { try {
//处理重复通知 //处理重复通知
//接口调用的幂等性:无论接口被调用多少次,以下业务执行一次 //接口调用的幂等性:无论接口被调用多少次,以下业务执行一次
// String orderStatus = orderInfoService.getOrderStatus(orderNo); // String orderStatus = orderInfoService.getOrderStatus(orderNo);
OrderInfo orderByOrderNo = orderInfoService.getOrderByOrderNo(orderId); OrderInfo orderByOrderNo = orderInfoService.getOrderByOrderNo(orderNo);
String orderStatus = orderByOrderNo.getOrderStatus(); String orderStatus = orderByOrderNo.getOrderStatus();
// 当订单状态处于未支付或超时已关闭时,更新订单状态,其他状态均不更新订单状态 // 当订单状态处于未支付或超时已关闭时,更新订单状态,其他状态均不更新订单状态
if (!OrderStatusEnum.NOT_PAY.getType().equals(orderStatus) && !OrderStatusEnum.TIMEOUT_CLOSED.getType().equals(orderStatus)) { if (!OrderStatusEnum.NOT_PAY.getType().equals(orderStatus) && !OrderStatusEnum.TIMEOUT_CLOSED.getType().equals(orderStatus)) {
log.info("订单状态 : {}", orderStatus); log.info("订单状态 : {}", orderStatus);
}else { }else {
//更新订单状态 //更新订单状态
orderInfoService.updateStatusByOrderNo(orderId, OrderStatusEnum.SUCCESS); orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.SUCCESS);
log.info("Stripe 订单:{} 状态更新成功", orderId); log.info("Stripe 订单:{} 状态更新成功", orderNo);
} }
if (orderByOrderNo.getTitle().startsWith("积分购买")){ if (orderByOrderNo.getTitle().startsWith("积分购买")){
// 查询当前订单的积分是否已添加 // 查询当前订单的积分是否已添加
CreditsDetail creditsDetail = creditsService.queryDetailByTaskId(orderId); CreditsDetail creditsDetail = creditsService.queryDetailByTaskId(orderNo);
if (Objects.isNull(creditsDetail)){ if (Objects.isNull(creditsDetail)){
float quantity = totalAmount / ProductEnum.CreditsProduct.getPrice(); float quantity = totalAmount / ProductEnum.CreditsProduct.getPrice();
// 更新积分 // 更新积分
@@ -416,21 +440,36 @@ public class StripeServiceImpl implements StripeService {
creditsService.insertToCreditsDetail(orderByOrderNo.getAccountId(), creditsService.insertToCreditsDetail(orderByOrderNo.getAccountId(),
CreditsEventsEnum.BUY_CREDITS.getName() + "--Stripe", CreditsEventsEnum.BUY_CREDITS.getName() + "--Stripe",
String.valueOf((Long.parseLong(CreditsEventsEnum.BUY_CREDITS.getValue()) * quantity)), String.valueOf((Long.parseLong(CreditsEventsEnum.BUY_CREDITS.getValue()) * quantity)),
"positive", orderId); "positive", orderNo);
log.info("用户:{} 积分信息更新成功", orderByOrderNo.getAccountId()); log.info("用户:{} 积分信息更新成功", orderByOrderNo.getAccountId());
} }
}else if (orderByOrderNo.getTitle().endsWith("Subscription") && orderByOrderNo.getAutoRenewal() == (byte)0){
String invoiceId = session.getInvoice();
Invoice invoice = Invoice.retrieve(invoiceId);
InvoiceLineItem invoiceLineItem = invoice.getLines().getData().get(0);
String description = invoiceLineItem.getDescription();
Long amount = invoiceLineItem.getAmount();
boolean b = createSubscriptionAndUpdateAccount(orderNo, orderByOrderNo.getAccountId(), description, amount);
// 邮件通知用户
if (b){
resp = sendEmail(null, "new", orderNo);
}
log.info("单次订阅订单:{} 处理完成", orderNo);
} }
} catch (Exception e) { } catch (Exception e) {
log.info(e.getMessage()); log.info(e.getMessage());
resp = false;
} }
return resp;
} }
private void processExpiredOrder(String orderNo) { private boolean processExpiredOrder(String orderNo) {
// 支付失败 通知商家的条件 1、会话过期 2、支付失败 3、这个用户在这个支付失败后再无支付成功的订阅 // 支付失败 通知商家的条件 1、会话过期 2、支付失败 3、这个用户在这个支付失败后再无支付成功的订阅
// 1、获取当前订单的支付状态 // 1、获取当前订单的支付状态
// String orderNo = session.getMetadata().get("orderId"); // String orderNo = session.getMetadata().get("orderId");
OrderInfo orderByOrderNo = orderInfoService.getOrderByOrderNo(orderNo); OrderInfo orderByOrderNo = orderInfoService.getOrderByOrderNo(orderNo);
// 2、确认订单状态为支付失败 // 2、确认订单状态为支付失败
boolean resp = true;
if (orderByOrderNo.getOrderStatus().equals(OrderStatusEnum.FAILURE.getType())) { if (orderByOrderNo.getOrderStatus().equals(OrderStatusEnum.FAILURE.getType())) {
// 3、判断失败订单之后再无成功的订单 // 3、判断失败订单之后再无成功的订单
QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>(); QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
@@ -448,13 +487,14 @@ public class StripeServiceImpl implements StripeService {
if (Objects.isNull(subscriptionInfo) if (Objects.isNull(subscriptionInfo)
|| subscriptionInfo.getStatus().equals("incomplete") || subscriptionInfo.getStatus().equals("incomplete")
|| subscriptionInfo.getStatus().equals("incomplete_expired")) { || subscriptionInfo.getStatus().equals("incomplete_expired")) {
sendEmail(orderNo); resp = sendEmail(orderNo);
}else { }else {
// todo 续订失败 应该不会走这里 // todo 续订失败 应该不会走这里
sendEmail(subscriptionInfo.getSubscriptionId(), "fail_renewal"); resp = sendEmail(subscriptionInfo.getSubscriptionId(), "fail_renewal", null);
} }
} }
} }
return resp;
} }
@@ -497,6 +537,60 @@ public class StripeServiceImpl implements StripeService {
return subscriptionInfo; return subscriptionInfo;
} }
/**
* 非自动续订订阅
* Stripe不会自动创建Subscription,所以没有subscription相关的回调无法触发订阅相关的处理代码
*/
public boolean createSubscriptionAndUpdateAccount(String orderNo, Long accountId, String description, Long amount){
QueryWrapper<SubscriptionInfo> qw = new QueryWrapper<>();
qw.eq("order_no", orderNo);
SubscriptionInfo subscriptionInfo = subscriptionInfoMapper.selectOne(qw);
if (Objects.isNull(subscriptionInfo)) {
String interval;
// 获取当前时间戳(秒级)
long currentPeriodStart = Instant.now().getEpochSecond();;
long currentPeriodEnd;
// InvoiceLineItem invoiceLineItem = invoice.getLines().getData().get(0);
if (description.equals(ProductEnum.DailySubscription.getName())
&& amount.equals(ProductEnum.DailySubscription.getPrice() * 100)){
interval = "day";
// 获取一天后的时间戳(秒级)
currentPeriodEnd = Instant.now().plus(1, ChronoUnit.DAYS).getEpochSecond();
}else if (description.equals(ProductEnum.MonthlySubscription.getName())
&& amount.equals(ProductEnum.MonthlySubscription.getPrice() * 100)){
interval = "month";
// 获取一天后的时间戳(秒级)
currentPeriodEnd = Instant.now().plus(1, ChronoUnit.MONTHS).getEpochSecond();
}else if (description.equals(ProductEnum.AnnualSubscription.getName())
&& amount.equals(ProductEnum.AnnualSubscription.getPrice() * 100)){
interval = "year";
// 获取一天后的时间戳(秒级)
currentPeriodEnd = Instant.now().plus(1, ChronoUnit.YEARS).getEpochSecond();
}else {
log.error("未知订阅类型");
return false;
}
subscriptionInfo = new SubscriptionInfo();
subscriptionInfo.setAccountId(accountId);
subscriptionInfo.setOrderNo(orderNo);
subscriptionInfo.setType(interval);
subscriptionInfo.setStatus("canceled");
subscriptionInfo.setCurrentPeriodStart(currentPeriodStart);
subscriptionInfo.setCurrentPeriodEnd(currentPeriodEnd);
subscriptionInfo.setCreateTime(LocalDateTime.now());
subscriptionInfoMapper.insertIgnore(subscriptionInfo);
log.info("创建订阅更新账号信息");
// 更新账号到期时间
boolean b = accountService.updateAccountValidity(subscriptionInfo.getAccountId(), subscriptionInfo.getCurrentPeriodEnd());
// 更新账号身份和积分
if (b) accountService.updateUserRoleAndCredits(subscriptionInfo.getAccountId(), interval);
return true;
}
return true;
}
public SubscriptionInfo getSubscriptionInfoBySubId(String subId){ public SubscriptionInfo getSubscriptionInfoBySubId(String subId){
QueryWrapper<SubscriptionInfo> qw = new QueryWrapper<>(); QueryWrapper<SubscriptionInfo> qw = new QueryWrapper<>();
qw.eq("subscription_id", subId); qw.eq("subscription_id", subId);
@@ -704,7 +798,8 @@ public class StripeServiceImpl implements StripeService {
try { try {
customerId = getCustomer(username, userEmail); customerId = getCustomer(username, userEmail);
SubscriptionCollection list = Subscription.list(SubscriptionListParams.builder() SubscriptionCollection list = Subscription.list(SubscriptionListParams.builder()
.setCustomer(customerId).build()); .setCustomer(customerId).setLimit(20L)
.build());
return list.getData(); return list.getData();
} catch (StripeException e) { } catch (StripeException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@@ -832,28 +927,42 @@ public class StripeServiceImpl implements StripeService {
// return null; // return null;
} }
public boolean sendEmail(String subscriptionId, String type) { public boolean sendEmail(String subscriptionId, String type, String orderNo) {
SubscriptionEmailParamsDTO emailParamsDTO = new SubscriptionEmailParamsDTO();
QueryWrapper<SubscriptionInfo> qwSI = new QueryWrapper<>();
qwSI.eq("subscription_id", subscriptionId);
List<SubscriptionInfo> subscriptionInfoList = subscriptionInfoMapper.selectList(qwSI);
SubscriptionInfo subscriptionInfo; SubscriptionInfo subscriptionInfo;
if (subscriptionInfoList.isEmpty()){ QueryWrapper<SubscriptionInfo> qwSI = new QueryWrapper<>();
return false; if (!StringUtil.isNullOrEmpty(subscriptionId)) {
}else { qwSI.eq("subscription_id", subscriptionId);
List<SubscriptionInfo> activeSubscriptions = subscriptionInfoList.stream() List<SubscriptionInfo> subscriptionInfoList = subscriptionInfoMapper.selectList(qwSI);
.filter(subscription -> "active".equals(subscription.getStatus()))
.collect(Collectors.toList()); if (subscriptionInfoList.isEmpty()){
if (activeSubscriptions.isEmpty()){ log.info("不发送邮件原因【subscriptionInfoList 为空】");
return false; return false;
}else {
List<SubscriptionInfo> activeSubscriptions = subscriptionInfoList.stream()
.filter(subscription -> "active".equals(subscription.getStatus()))
.collect(Collectors.toList());
if (!StringUtil.isNullOrEmpty(type) && type.equals("cancel")){
subscriptionInfo = subscriptionInfoList.get(0);
}else if (activeSubscriptions.isEmpty()){
log.info("不发送邮件,原因:【当前邮件类型:{}, 但是状态为active的subscriptionInfo为空】", type);
return false;
}else {
subscriptionInfo = activeSubscriptions.get(0);
}
} }
subscriptionInfo = activeSubscriptions.get(0); }else if (!StringUtil.isNullOrEmpty(orderNo)) {
qwSI.eq("order_no", orderNo);
subscriptionInfo = subscriptionInfoMapper.selectOne(qwSI);
}else {
log.info("不发送邮件原因【入参中的subscriptionIdorderNo均为空】");
return false;
} }
QueryWrapper<PaymentInfo> qwPI = new QueryWrapper<>(); QueryWrapper<PaymentInfo> qwPI = new QueryWrapper<>();
qwPI.eq("order_no", subscriptionInfo.getOrderNo()).orderByDesc("id"); qwPI.eq("order_no", subscriptionInfo.getOrderNo()).orderByDesc("id");
List<PaymentInfo> paymentInfos = paymentInfoMapper.selectList(qwPI); List<PaymentInfo> paymentInfos = paymentInfoMapper.selectList(qwPI);
if (paymentInfos.isEmpty()) { if (paymentInfos.isEmpty()) {
log.info("不发送邮件原因【根据order_no:{},查询到的paymentInfos为空】", orderNo);
return false; return false;
} }
PaymentInfo paymentInfo = paymentInfos.get(0); PaymentInfo paymentInfo = paymentInfos.get(0);
@@ -864,6 +973,7 @@ public class StripeServiceImpl implements StripeService {
} }
if (!type.equals("reminder") && !type.equals("cancel") && paymentInfo.getNotified() == 1){ if (!type.equals("reminder") && !type.equals("cancel") && paymentInfo.getNotified() == 1){
// 已经邮件通知过,直接返回 // 已经邮件通知过,直接返回
log.info("不发送邮件原因【type为{}order_no为{},且已经已经进行邮件通知】", type, orderNo);
return true; return true;
} }
@@ -872,6 +982,7 @@ public class StripeServiceImpl implements StripeService {
String language = account.getLanguage(); String language = account.getLanguage();
OrderInfo orderByOrderNo = orderInfoService.getOrderByOrderNo(subscriptionInfo.getOrderNo()); OrderInfo orderByOrderNo = orderInfoService.getOrderByOrderNo(subscriptionInfo.getOrderNo());
SubscriptionEmailParamsDTO emailParamsDTO = new SubscriptionEmailParamsDTO();
emailParamsDTO.setUsername(userName); emailParamsDTO.setUsername(userName);
emailParamsDTO.setOrderId(paymentInfo.getId().toString()); emailParamsDTO.setOrderId(paymentInfo.getId().toString());
emailParamsDTO.setOrderRef("\"" + orderListLink + paymentInfo.getId().toString() + "\""); emailParamsDTO.setOrderRef("\"" + orderListLink + paymentInfo.getId().toString() + "\"");
@@ -880,7 +991,7 @@ public class StripeServiceImpl implements StripeService {
emailParamsDTO.setTotalFee(paymentInfo.getPayerTotal().toString()); emailParamsDTO.setTotalFee(paymentInfo.getPayerTotal().toString());
emailParamsDTO.setLastOrderDate(DateUtil.changeTimeStampFormat(subscriptionInfo.getCurrentPeriodStart(), "seconds", CommonConstant.TIME_FORMAT_MMM_dd_yyyy)); emailParamsDTO.setLastOrderDate(DateUtil.changeTimeStampFormat(subscriptionInfo.getCurrentPeriodStart(), "seconds", CommonConstant.TIME_FORMAT_MMM_dd_yyyy));
emailParamsDTO.setEndOfPrepaidTerm(DateUtil.changeTimeStampFormat(subscriptionInfo.getCurrentPeriodEnd(), "seconds", CommonConstant.TIME_FORMAT_MMM_dd_yyyy)); emailParamsDTO.setEndOfPrepaidTerm(DateUtil.changeTimeStampFormat(subscriptionInfo.getCurrentPeriodEnd(), "seconds", CommonConstant.TIME_FORMAT_MMM_dd_yyyy));
setSubscriptionParams(paymentInfo, subscriptionInfo, orderByOrderNo, emailParamsDTO); setSubscriptionParams(paymentInfo, subscriptionInfo, orderByOrderNo, emailParamsDTO, language);
SendEmailUtil.subscriptionEmailReminder(type, emailParamsDTO, language, account.getUserEmail()); SendEmailUtil.subscriptionEmailReminder(type, emailParamsDTO, language, account.getUserEmail());
@@ -906,6 +1017,7 @@ public class StripeServiceImpl implements StripeService {
qwPI.eq("order_no", orderNo); qwPI.eq("order_no", orderNo);
List<PaymentInfo> paymentInfos = paymentInfoMapper.selectList(qwPI); List<PaymentInfo> paymentInfos = paymentInfoMapper.selectList(qwPI);
if (paymentInfos.isEmpty()) { if (paymentInfos.isEmpty()) {
log.info("不发送邮件原因【根据order_no:{},查询到的paymentInfos为空】", orderNo);
return false; return false;
} }
PaymentInfo paymentInfo = paymentInfos.get(0); PaymentInfo paymentInfo = paymentInfos.get(0);
@@ -976,7 +1088,7 @@ public class StripeServiceImpl implements StripeService {
emailParamsDTO.setCreateDate(String.valueOf(paymentInfo.getCreateTime()).replace("T", " ")); emailParamsDTO.setCreateDate(String.valueOf(paymentInfo.getCreateTime()).replace("T", " "));
emailParamsDTO.setQuantity(String.valueOf(1)); emailParamsDTO.setQuantity(String.valueOf(1));
emailParamsDTO.setTotalFee(paymentInfo.getPayerTotal().toString()); emailParamsDTO.setTotalFee(paymentInfo.getPayerTotal().toString());
setSubscriptionParams(paymentInfo, subscriptionInfo, orderByOrderNo, emailParamsDTO); setSubscriptionParams(paymentInfo, subscriptionInfo, orderByOrderNo, emailParamsDTO, language);
// 4、发邮件 // 4、发邮件
SendEmailUtil.subscriptionEmailReminder("fail_renewal", emailParamsDTO, language, account.getUserEmail()); SendEmailUtil.subscriptionEmailReminder("fail_renewal", emailParamsDTO, language, account.getUserEmail());
@@ -989,14 +1101,28 @@ public class StripeServiceImpl implements StripeService {
return true; return true;
} }
private void setSubscriptionParams(PaymentInfo paymentInfo, SubscriptionInfo subscriptionInfo, OrderInfo orderByOrderNo, SubscriptionEmailParamsDTO emailParamsDTO) { private void setSubscriptionParams(PaymentInfo paymentInfo, SubscriptionInfo subscriptionInfo, OrderInfo orderByOrderNo,
SubscriptionEmailParamsDTO emailParamsDTO, String language) {
emailParamsDTO.setPaymentMethod(paymentInfo.getPaymentMethod()); emailParamsDTO.setPaymentMethod(paymentInfo.getPaymentMethod());
emailParamsDTO.setLast4(paymentInfo.getLast4()); emailParamsDTO.setLast4(paymentInfo.getLast4());
emailParamsDTO.setSubscriptionId(subscriptionInfo.getId().toString()); emailParamsDTO.setSubscriptionId(subscriptionInfo.getId().toString());
emailParamsDTO.setFailMessage(orderByOrderNo.getNote()); emailParamsDTO.setFailMessage(orderByOrderNo.getNote());
emailParamsDTO.setSubscriptionType(subscriptionInfo.getType()); emailParamsDTO.setSubscriptionType(subscriptionInfo.getType());
emailParamsDTO.setStartDate(DateUtil.changeTimeStampFormat(orderByOrderNo.getCreateTime())); emailParamsDTO.setStartDate(DateUtil.changeTimeStampFormat(orderByOrderNo.getCreateTime()));
emailParamsDTO.setNextPayDate(DateUtil.changeTimeStampFormat(subscriptionInfo.getCurrentPeriodEnd(), "seconds", CommonConstant.TIME_FORMAT_MMM_dd_yyyy)); if (subscriptionInfo.getStatus().equals("active")){
if (language.equals("ENGLISH")){
emailParamsDTO.setEndDate("When cancelled");
}else {
emailParamsDTO.setEndDate("手动取消订阅时");
}
}else {
emailParamsDTO.setEndDate(DateUtil.changeTimeStampFormat(subscriptionInfo.getCurrentPeriodEnd(), "seconds", CommonConstant.TIME_FORMAT_MMM_dd_yyyy_EEEE));
}
if (StringUtil.isNullOrEmpty(subscriptionInfo.getNextPayDate())){
emailParamsDTO.setNextPayDate("N/A");
} else {
emailParamsDTO.setNextPayDate(DateUtil.changeTimeStampFormat(subscriptionInfo.getCurrentPeriodEnd(), "seconds", CommonConstant.TIME_FORMAT_MMM_dd_yyyy));
}
emailParamsDTO.setRenewalTime(DateUtil.changeTimeStampFormat(subscriptionInfo.getCurrentPeriodEnd(), "seconds", CommonConstant.TIME_FORMAT_MMM_dd_yyyy)); emailParamsDTO.setRenewalTime(DateUtil.changeTimeStampFormat(subscriptionInfo.getCurrentPeriodEnd(), "seconds", CommonConstant.TIME_FORMAT_MMM_dd_yyyy));
} }
@@ -1016,7 +1142,7 @@ public class StripeServiceImpl implements StripeService {
List<SubscriptionInfo> subscriptionInfos = subscriptionInfoMapper.selectList(qw); List<SubscriptionInfo> subscriptionInfos = subscriptionInfoMapper.selectList(qw);
for (SubscriptionInfo subscriptionInfo : subscriptionInfos) { for (SubscriptionInfo subscriptionInfo : subscriptionInfos) {
boolean b = sendEmail(subscriptionInfo.getSubscriptionId(), "reminder"); boolean b = sendEmail(subscriptionInfo.getSubscriptionId(), "reminder", null);
if (b) log.info("提前7天向用户 {} 发送续订通知邮件", subscriptionInfo.getAccountId()); if (b) log.info("提前7天向用户 {} 发送续订通知邮件", subscriptionInfo.getAccountId());
} }
} }
@@ -1040,7 +1166,7 @@ public class StripeServiceImpl implements StripeService {
public String createSubscriptionTemp(String name, String email){ public String createSubscriptionTemp(String name, String email){
Stripe.apiKey = privateKey; Stripe.apiKey = privateKey;
try { try {
OrderInfo orderInfo = orderInfoService.createOrderByProductId(1, PayTypeEnum.STRIPE.getType(), ProductEnum.DailySubscription, null); OrderInfo orderInfo = orderInfoService.createOrderByProductId(1, PayTypeEnum.STRIPE.getType(), ProductEnum.DailySubscription, null, (byte)0);
// String customerId = getCustomer(name, email); // String customerId = getCustomer(name, email);
String paymentMethodCode = "pm_card_mastercard"; String paymentMethodCode = "pm_card_mastercard";

View File

@@ -16,7 +16,8 @@ spring.security.jwtSecret=JWTSECRET
spring.security.jwtTokenHeader=Authorization spring.security.jwtTokenHeader=Authorization
spring.security.jwtTokenPrefix=Bearer- spring.security.jwtTokenPrefix=Bearer-
## 24Сʱ ## 24Сʱ
spring.security.jwtExpiration=8640000000 #spring.security.jwtExpiration=8640000000
spring.security.jwtExpiration=604800000
#spring security权限设置 认证了token还要认证权限 不然报错Full authentication is required to access this resource #spring security权限设置 认证了token还要认证权限 不然报错Full authentication is required to access this resource
spring.security.ignorePaths=/,/favicon.ico,/doc.html,/webjars/**,/swagger-resources,/v2/api-docs,\ spring.security.ignorePaths=/,/favicon.ico,/doc.html,/webjars/**,/swagger-resources,/v2/api-docs,\
/api/account/**,/api/element/**,/api/python/**,/api/design/**,/api/history/**,/api/library/**,/api/third/party/**,/api/generate/**,/api/workspace/**,/api/classification/**,\ /api/account/**,/api/element/**,/api/python/**,/api/design/**,/api/history/**,/api/library/**,/api/third/party/**,/api/generate/**,/api/workspace/**,/api/classification/**,\

View File

@@ -169,9 +169,9 @@
type, content, notified, payment_method, last4, hosted_invoice_url, type, content, notified, payment_method, last4, hosted_invoice_url,
country, city, ip_address, create_time) country, city, ip_address, create_time)
VALUES (#{paymentInfo.orderNo}, #{paymentInfo.transactionId}, #{paymentInfo.paymentType}, VALUES (#{paymentInfo.orderNo}, #{paymentInfo.transactionId}, #{paymentInfo.paymentType},
#{paymentInfo.tradeState}, #{paymentInfo.payerTotal}, #{paymentInfo.content},#{paymentInfo.type}, #{paymentInfo.tradeState}, #{paymentInfo.payerTotal},#{paymentInfo.type}, #{paymentInfo.content},
#{paymentInfo.notified},#{paymentInfo.paymentMethod}, #{paymentInfo.last4},#{paymentInfo.hostedInvoiceUrl}, #{paymentInfo.notified},#{paymentInfo.paymentMethod}, #{paymentInfo.last4},#{paymentInfo.hostedInvoiceUrl},
#{paymentInfo.ipAddress},#{paymentInfo.country},#{paymentInfo.city},#{paymentInfo.createTime}); #{paymentInfo.country},#{paymentInfo.city},#{paymentInfo.ipAddress},#{paymentInfo.createTime});
</insert> </insert>
</mapper> </mapper>

View File

@@ -28,16 +28,19 @@ paypal.webhook_id=1D107312EX592781K
# developer # developer
#stripe.private-key=sk_test_51P4ZZL02n1TEydyN8qQHjOA9imsFU7Oxs2HMHGy2urHnnQgSHnZuu5vVP6pKhEACwUpsKNyrbZpdcg5TJWJLRHcY008dEO1fn2 #stripe.private-key=sk_test_51P4ZZL02n1TEydyN8qQHjOA9imsFU7Oxs2HMHGy2urHnnQgSHnZuu5vVP6pKhEACwUpsKNyrbZpdcg5TJWJLRHcY008dEO1fn2
#stripe.webhook-sign-secret=whsec_e0dBiJngx6qqgJj6yPyJ2A9ouh1Cjv5w ##stripe.webhook-sign-secret=whsec_e0dBiJngx6qqgJj6yPyJ2A9ouh1Cjv5w
#stripe.webhook-sign-secret=whsec_TJcMSnAkh4uktrNY1M6Iy8XaVze4Rzqm #stripe.webhook-sign-secret=whsec_TJcMSnAkh4uktrNY1M6Iy8XaVze4Rzqm
# kim - test # kim - test
#stripe.private-key=sk_test_51LwPrxH7nPZ8bkrNj67TFD7sxucaTANs1lf0KGSu1QSJfxYXcnigq2wTaZyZzST7y0fMbhhvaJZ4LjjFhr95M83a00eXrmOTL0 stripe.private-key=sk_test_51LwPrxH7nPZ8bkrNj67TFD7sxucaTANs1lf0KGSu1QSJfxYXcnigq2wTaZyZzST7y0fMbhhvaJZ4LjjFhr95M83a00eXrmOTL0
# prod 端点
#stripe.webhook-sign-secret=whsec_GoyVEAaBtuGD5Rt55z83JnPnLDAZTN3u #stripe.webhook-sign-secret=whsec_GoyVEAaBtuGD5Rt55z83JnPnLDAZTN3u
# local 端点
stripe.webhook-sign-secret=whsec_NvwM3hDQiN5GXclYOYekE9IKHLjmROF8
# kim - live # kim - live
stripe.private-key=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 #stripe.private-key=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
# 正式支付环境下生产分支端点 ## 正式支付环境下生产分支端点
stripe.webhook-sign-secret=whsec_hhGDgdelQRHSg4LmChtQe41crj41eb11 #stripe.webhook-sign-secret=whsec_hhGDgdelQRHSg4LmChtQe41crj41eb11
# 正式支付环境下测试分支端点 # 正式支付环境下测试分支端点
#stripe.webhook-sign-secret=whsec_cFUtjUOo8wnrIKZmt4GNvt7ZY1bOfrYr #stripe.webhook-sign-secret=whsec_cFUtjUOo8wnrIKZmt4GNvt7ZY1bOfrYr