From b01ee9129b02545bf122110564b19399e3a33cab Mon Sep 17 00:00:00 2001 From: xupei Date: Fri, 27 Dec 2024 10:31:27 +0800 Subject: [PATCH 1/4] =?UTF-8?q?develop=20=E7=8E=AF=E5=A2=83webhook=5Fsecre?= =?UTF-8?q?t=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/payment.properties | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/resources/payment.properties b/src/main/resources/payment.properties index 43c4e4b7..b4fad5ad 100644 --- a/src/main/resources/payment.properties +++ b/src/main/resources/payment.properties @@ -28,7 +28,8 @@ paypal.webhook_id=1D107312EX592781K # developer stripe.private-key=sk_test_51P4ZZL02n1TEydyN8qQHjOA9imsFU7Oxs2HMHGy2urHnnQgSHnZuu5vVP6pKhEACwUpsKNyrbZpdcg5TJWJLRHcY008dEO1fn2 -stripe.webhook-sign-secret=whsec_e0dBiJngx6qqgJj6yPyJ2A9ouh1Cjv5w +#stripe.webhook-sign-secret=whsec_e0dBiJngx6qqgJj6yPyJ2A9ouh1Cjv5w +stripe.webhook-sign-secret=whsec_TJcMSnAkh4uktrNY1M6Iy8XaVze4Rzqm # kim - test #stripe.private-key=sk_test_51LwPrxH7nPZ8bkrNj67TFD7sxucaTANs1lf0KGSu1QSJfxYXcnigq2wTaZyZzST7y0fMbhhvaJZ4LjjFhr95M83a00eXrmOTL0 From f45bd7acc404bf700b7331c95c89bd4fcdd4439d Mon Sep 17 00:00:00 2001 From: xupei Date: Fri, 27 Dec 2024 10:46:52 +0800 Subject: [PATCH 2/4] =?UTF-8?q?develop=20=E7=8E=AF=E5=A2=83webhook=5Fsecre?= =?UTF-8?q?t=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/payment.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/payment.properties b/src/main/resources/payment.properties index b4fad5ad..2415fd97 100644 --- a/src/main/resources/payment.properties +++ b/src/main/resources/payment.properties @@ -28,8 +28,8 @@ paypal.webhook_id=1D107312EX592781K # developer stripe.private-key=sk_test_51P4ZZL02n1TEydyN8qQHjOA9imsFU7Oxs2HMHGy2urHnnQgSHnZuu5vVP6pKhEACwUpsKNyrbZpdcg5TJWJLRHcY008dEO1fn2 -#stripe.webhook-sign-secret=whsec_e0dBiJngx6qqgJj6yPyJ2A9ouh1Cjv5w -stripe.webhook-sign-secret=whsec_TJcMSnAkh4uktrNY1M6Iy8XaVze4Rzqm +stripe.webhook-sign-secret=whsec_e0dBiJngx6qqgJj6yPyJ2A9ouh1Cjv5w +#stripe.webhook-sign-secret=whsec_TJcMSnAkh4uktrNY1M6Iy8XaVze4Rzqm # kim - test #stripe.private-key=sk_test_51LwPrxH7nPZ8bkrNj67TFD7sxucaTANs1lf0KGSu1QSJfxYXcnigq2wTaZyZzST7y0fMbhhvaJZ4LjjFhr95M83a00eXrmOTL0 From ca4d75c63fc1a02c2bb49d62a6aa2385b9d04f5e Mon Sep 17 00:00:00 2001 From: xupei Date: Fri, 27 Dec 2024 14:29:07 +0800 Subject: [PATCH 3/4] =?UTF-8?q?BUGFIX:=E7=94=A8=E6=88=B7=E8=AE=A2=E9=98=85?= =?UTF-8?q?=E5=90=8E=E6=9B=B4=E6=96=B0=E7=94=A8=E6=88=B7=E8=BA=AB=E4=BB=BD?= =?UTF-8?q?=E5=92=8C=E7=A7=AF=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ai/da/service/AccountService.java | 4 +++ .../da/service/impl/AccountServiceImpl.java | 28 +++++++++++++++++++ .../ai/da/service/impl/StripeServiceImpl.java | 20 ++++++------- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/ai/da/service/AccountService.java b/src/main/java/com/ai/da/service/AccountService.java index 07944fcd..bff69738 100644 --- a/src/main/java/com/ai/da/service/AccountService.java +++ b/src/main/java/com/ai/da/service/AccountService.java @@ -218,4 +218,8 @@ public interface AccountService extends IService { Boolean unbindWeChat(); Boolean unbindGoogle(); + + void updateAccountValidity(Long accountId, Long currentPeriodEnd); + + void updateUserRoleAndCredits(Long accountId, String type); } diff --git a/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java b/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java index 96d66d72..83b82a8e 100644 --- a/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java @@ -2591,4 +2591,32 @@ public class AccountServiceImpl extends ServiceImpl impl accountMapper.updateById(account); return Boolean.TRUE; } + + public void updateAccountValidity(Long accountId, Long currentPeriodEnd){ + // 不管当前用户的账号是否到期,都根据付款信息重置账号到期时间 + Account account = accountMapper.selectById(accountId); + account.setValidEndTime(currentPeriodEnd * 1000); + + accountMapper.updateById(account); + } + + public void updateUserRoleAndCredits(Long accountId, String type){ + Account account = accountMapper.selectById(accountId); + switch (type) { + case "month": + account.setSystemUser(2); + account.setCredits(BigDecimal.valueOf(Long.parseLong(CreditsEventsEnum.INIT_MONTHLY.getValue()))); + break; + case "year": + account.setSystemUser(1); + account.setCredits(BigDecimal.valueOf(Long.parseLong(CreditsEventsEnum.INIT_YEARLY.getValue()))); + break; + case "day": + account.setSystemUser(3); + account.setCredits(BigDecimal.valueOf(Long.parseLong(CreditsEventsEnum.INIT_WEEKLY.getValue()))); + break; + } + + accountMapper.updateById(account); + } } diff --git a/src/main/java/com/ai/da/service/impl/StripeServiceImpl.java b/src/main/java/com/ai/da/service/impl/StripeServiceImpl.java index d3b2df08..0fc00b32 100644 --- a/src/main/java/com/ai/da/service/impl/StripeServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/StripeServiceImpl.java @@ -58,6 +58,8 @@ public class StripeServiceImpl implements StripeService { private CreditsService creditsService; @Resource private RefundInfoService refundInfoService; + @Resource + private AccountService accountService; @Resource private AccountMapper accountMapper; @@ -368,6 +370,7 @@ public class StripeServiceImpl implements StripeService { orderInfoService.updateById(orderInfo); } } + log.info("回调事件 {} 处理完成", event.getType()); return response; } @@ -466,7 +469,10 @@ public class StripeServiceImpl implements StripeService { subscriptionInfoMapper.insert(subscriptionInfo); // 更新账号到期时间 - updateAccountValidity(subscriptionInfo.getAccountId(), subscriptionInfo.getCurrentPeriodEnd()); + accountService.updateAccountValidity(subscriptionInfo.getAccountId(), subscriptionInfo.getCurrentPeriodEnd()); + + // 更新账号身份和积分 + accountService.updateUserRoleAndCredits(subscriptionInfo.getAccountId(), interval); } return subscriptionInfo; } @@ -519,7 +525,9 @@ public class StripeServiceImpl implements StripeService { subscriptionInfo.setCurrentPeriodEnd(subscription.getCurrentPeriodEnd()); subscriptionInfo.setNextPayDate(DateUtil.changeTimeStampFormat(subscription.getCurrentPeriodEnd(), "seconds", CommonConstant.TIME_FORMAT_MMM_dd_yyyy_EEEE)); // 更新账号到期时间 - updateAccountValidity(subscriptionInfo.getAccountId(), subscriptionInfo.getCurrentPeriodEnd()); + accountService.updateAccountValidity(subscriptionInfo.getAccountId(), subscriptionInfo.getCurrentPeriodEnd()); + // 更新账号身份和积分 + accountService.updateUserRoleAndCredits(subscriptionInfo.getAccountId(), subscriptionInfo.getType()); log.info("更新 {} 账号到期时间为:{}", subscriptionInfo.getAccountId(), DateUtil.changeTimeStampFormat(subscriptionInfo.getCurrentPeriodEnd(), "seconds", CommonConstant.TIME_FORMAT_MMM_dd_yyyy_EEEE)); flag = true; } @@ -530,14 +538,6 @@ public class StripeServiceImpl implements StripeService { return subscriptionInfo; } - private void updateAccountValidity(Long accountId, Long currentPeriodEnd){ - // 不管当前用户的账号是否到期,都根据付款信息重置账号到期时间 - com.ai.da.mapper.primary.entity.Account account = accountMapper.selectById(accountId); - account.setValidEndTime(currentPeriodEnd * 1000); - - accountMapper.updateById(account); - } - // 取消连续订阅 将订阅从pause状态转为cancel状态(使用定时器,定期检索DB中,过期且不续订的订阅) public void cancelSubscription(String subscriptionId, String cancelReason) { Stripe.apiKey = privateKey; From 1157b41730f229cac5c79a25bb3430f050754fc5 Mon Sep 17 00:00:00 2001 From: xupei Date: Tue, 31 Dec 2024 11:38:12 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E7=A7=AF=E5=88=86=E8=B4=AD=E4=B9=B0=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=82=AE=E4=BB=B6=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ai/da/common/utils/SendEmailUtil.java | 40 +++++++++++++++++++ .../da/service/impl/AlipayHKServiceImpl.java | 7 ++++ 2 files changed, 47 insertions(+) diff --git a/src/main/java/com/ai/da/common/utils/SendEmailUtil.java b/src/main/java/com/ai/da/common/utils/SendEmailUtil.java index 048f65af..712cffc8 100644 --- a/src/main/java/com/ai/da/common/utils/SendEmailUtil.java +++ b/src/main/java/com/ai/da/common/utils/SendEmailUtil.java @@ -985,4 +985,44 @@ public class SendEmailUtil { } } + private final static Long CREDITS_PURCHASE_MERCHANT = 133275L; + + public static void creditsPurchaseReminder(String username, String quantity, String amount) { + try { + Credential cred = new Credential(SECRET_ID, SECRET_KEy); + // 实例化一个http选项,可选的,没有特殊需求可以跳过 + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint("ses.tencentcloudapi.com"); + // 实例化一个client选项,可选的,没有特殊需求可以跳过 + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + // 实例化要请求产品的client对象,clientProfile是可选的 + SesClient client = new SesClient(cred, "ap-hongkong", clientProfile); + // 实例化一个请求对象,每个接口都会对应一个request对象 + SendEmailRequest req = new SendEmailRequest(); + req.setFromEmailAddress(SEND_ADDRESS); + String merchantEmail = "kimwong@code-create.com.hk"; + String developerEmail = "xupei@code-create.com.hk"; + req.setDestination(new String[]{merchantEmail, developerEmail}); + Template template = new Template(); + req.setSubject("New Credit Purchase Order"); + template.setTemplateID(CREDITS_PURCHASE_MERCHANT); + JSONObject jsonObject = new JSONObject(); + // 设置试用订单相关数据 + jsonObject.put("userName", username); + jsonObject.put("quantity", quantity); + jsonObject.put("totalFee", amount); + + template.setTemplateData(JSON.toJSONString(jsonObject)); + req.setTemplate(template); + + // 返回的resp是一个SendEmailResponse的实例,与请求对象对应 + SendEmailResponse resp = client.SendEmail(req); + log.info("短信发送结果res###{}", SendEmailResponse.toJsonString(resp)); + } catch (TencentCloudSDKException e) { + log.info("邮件发送失败###{}", e.toString()); + throw new BusinessException("failed.to.send.mail"); + } + } + } diff --git a/src/main/java/com/ai/da/service/impl/AlipayHKServiceImpl.java b/src/main/java/com/ai/da/service/impl/AlipayHKServiceImpl.java index 417a03bd..910129ef 100644 --- a/src/main/java/com/ai/da/service/impl/AlipayHKServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/AlipayHKServiceImpl.java @@ -6,6 +6,8 @@ import com.ai.da.common.enums.OrderStatusEnum; import com.ai.da.common.enums.PayTypeEnum; import com.ai.da.common.utils.AlipayHKEncryptionUtil; import com.ai.da.common.utils.AlipayHKRequestUtil; +import com.ai.da.common.utils.SendEmailUtil; +import com.ai.da.mapper.primary.AccountMapper; import com.ai.da.mapper.primary.entity.OrderInfo; import com.ai.da.model.dto.AlipayHKCallbackDTO; import com.ai.da.model.dto.AlipayHKRequestDTO; @@ -54,6 +56,8 @@ public class AlipayHKServiceImpl implements AlipayHKService { private AlipayHKEncryptionUtil alipayHKEncryptionUtil; @Resource private AlipayHKRequestUtil alipayHKRequestUtil; + @Resource + private AccountMapper accountMapper; /** @@ -251,6 +255,9 @@ public class AlipayHKServiceImpl implements AlipayHKService { "positive", orderByOrderNo.getOrderNo()); log.info("用户:{} 积分信息更新成功",orderByOrderNo.getAccountId()); + // 邮件通知Kim + String username = accountMapper.selectById(orderByOrderNo.getAccountId()).getUserName(); + SendEmailUtil.creditsPurchaseReminder(username, String.valueOf(quantity), totalAmount); } finally { //要主动释放锁 lock.unlock();