|
@@ -1,8 +1,6 @@
|
|
package cn.kdan.compdf.service.impl;
|
|
package cn.kdan.compdf.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
-import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
-import cn.hutool.core.date.DateUtil;
|
|
|
|
import cn.kdan.compdf.constant.MyConstant;
|
|
import cn.kdan.compdf.constant.MyConstant;
|
|
import cn.kdan.compdf.dto.ApplyInvoiceDTO;
|
|
import cn.kdan.compdf.dto.ApplyInvoiceDTO;
|
|
import cn.kdan.compdf.dto.BillSendEmailDTO;
|
|
import cn.kdan.compdf.dto.BillSendEmailDTO;
|
|
@@ -16,9 +14,8 @@ import cn.kdan.compdf.service.OrderCashFlowService;
|
|
import cn.kdan.compdf.service.OrdersService;
|
|
import cn.kdan.compdf.service.OrdersService;
|
|
import cn.kdan.compdf.service.UserBillingInformationService;
|
|
import cn.kdan.compdf.service.UserBillingInformationService;
|
|
import cn.kdan.compdf.utils.TemplatesUtil;
|
|
import cn.kdan.compdf.utils.TemplatesUtil;
|
|
-import cn.kdan.compdf.vo.ApplyInvoiceVO;
|
|
|
|
-import cn.kdan.compdf.vo.OrderBillVo;
|
|
|
|
-import cn.kdan.compdf.vo.UserBillingInfoVO;
|
|
|
|
|
|
+import cn.kdan.compdf.utils.TokenUtil;
|
|
|
|
+import cn.kdan.compdf.vo.*;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
@@ -45,11 +42,14 @@ import javax.mail.internet.MimeMessage;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Field;
|
|
-import java.net.MalformedURLException;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.math.MathContext;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
-import java.time.Instant;
|
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -74,6 +74,47 @@ public class UserBillingInformationServiceImpl extends ServiceImpl<UserBillingIn
|
|
|
|
|
|
private final OrderCashFlowService orderCashFlowService;
|
|
private final OrderCashFlowService orderCashFlowService;
|
|
|
|
|
|
|
|
+ private static final String EMAIL_USER_NAME = "#\"userName\"";
|
|
|
|
+
|
|
|
|
+ private final static String emailTemplate_EN = "<!DOCTYPE html>\n" +
|
|
|
|
+ "<html>\n" +
|
|
|
|
+ "<head>\n" +
|
|
|
|
+ "<meta charset=\"utf-8\">\n" +
|
|
|
|
+ "<title>HTML Editor - LDDGO.NET</title>\n" +
|
|
|
|
+ "<link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.5.1/build/styles/default.min.css\" type=\"text/css\">\n" +
|
|
|
|
+ "</head>\n" +
|
|
|
|
+ "<body>\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "<p>Dear "+EMAIL_USER_NAME+",</p>\n" +
|
|
|
|
+ "<p>Your invoice has been sent to your email. Please check the attached PDF file. </p>\n" +
|
|
|
|
+ "<p>Thank you for your support of ComPDFKit!</p>\n" +
|
|
|
|
+ "<p><br />Thanks,<br />ComPDFKit Team<br /><a href=\"https://www.compdf.com\">https://www.compdf.com</a></p>\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "<script src=\"https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.5.1/build/highlight.min.js\" type=\"text/javascript\"></script>\n" +
|
|
|
|
+ "<script type=\"text/javascript\">hljs.highlightAll();</script>\n" +
|
|
|
|
+ "</body>\n" +
|
|
|
|
+ "</html>";
|
|
|
|
+
|
|
|
|
+ private final static String emailTemplate_CN = "<!DOCTYPE html>\n" +
|
|
|
|
+ "<html>\n" +
|
|
|
|
+ "<head>\n" +
|
|
|
|
+ "<meta charset=\"utf-8\">\n" +
|
|
|
|
+ "<title>HTML Editor - LDDGO.NET</title>\n" +
|
|
|
|
+ "<link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.5.1/build/styles/default.min.css\" type=\"text/css\">\n" +
|
|
|
|
+ "</head>\n" +
|
|
|
|
+ "<body>\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "<p>尊敬的 "+EMAIL_USER_NAME+",</p>\n" +
|
|
|
|
+ "<p>您好!您的发票已通过电子邮件发送至您的邮箱,请查收附件中的PDF文件。</p>\n" +
|
|
|
|
+ "<p>感谢您对ComPDFKit的支持!</p>\n" +
|
|
|
|
+ "<p><br />ComPDFKit 团队<br /><a href=\"https://www.compdf.com/zh-cn\">https://www.compdf.com/zh-cn</a></p>\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "<script src=\"https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.5.1/build/highlight.min.js\" type=\"text/javascript\"></script>\n" +
|
|
|
|
+ "<script type=\"text/javascript\">hljs.highlightAll();</script>\n" +
|
|
|
|
+ "</body>\n" +
|
|
|
|
+ "</html>";
|
|
|
|
+
|
|
|
|
+
|
|
static {
|
|
static {
|
|
try {
|
|
try {
|
|
invoiceHtml = new String(StreamUtils.copyToByteArray(UserBillingInformationServiceImpl.class.getClassLoader().getResourceAsStream("templates/invoice.html")), StandardCharsets.UTF_8);
|
|
invoiceHtml = new String(StreamUtils.copyToByteArray(UserBillingInformationServiceImpl.class.getClassLoader().getResourceAsStream("templates/invoice.html")), StandardCharsets.UTF_8);
|
|
@@ -162,12 +203,54 @@ public class UserBillingInformationServiceImpl extends ServiceImpl<UserBillingIn
|
|
checkFieldNotNullOrEmpty(userBillingInfo, field);
|
|
checkFieldNotNullOrEmpty(userBillingInfo, field);
|
|
}
|
|
}
|
|
// 生成发票号
|
|
// 生成发票号
|
|
- Date now = new Date();
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
|
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
|
|
|
|
+
|
|
|
|
+ Date now = new Date();
|
|
String billNo = sdf.format(now.getTime()).concat(String.valueOf(new Random().nextInt(9)));
|
|
String billNo = sdf.format(now.getTime()).concat(String.valueOf(new Random().nextInt(9)));
|
|
|
|
+
|
|
|
|
+ OrderBillingInfoVO orderBillingInfoVO = orderCashFlowService.getOrderBillingInfo(applyInvoiceDTO.getOrderId());
|
|
|
|
+ // 是订阅
|
|
|
|
+ String startDateStr = "";
|
|
|
|
+ String endDateStr = "";
|
|
|
|
+ String paymentCycle = "";
|
|
|
|
+ if (orderBillingInfoVO.getPackageType().equals(1)) {
|
|
|
|
+ if ("Monthly".equals(orderBillingInfoVO.getSubscriptionType())) {
|
|
|
|
+ LocalDateTime startDate = orderBillingInfoVO.getPayTime().plusMonths(orderBillingInfoVO.getPeriod());
|
|
|
|
+ LocalDateTime endDate = orderBillingInfoVO.getPayTime().plusMonths(orderBillingInfoVO.getPeriod() + 1);
|
|
|
|
+ startDateStr = startDate.format(formatter);
|
|
|
|
+ endDateStr = endDate.format(formatter);
|
|
|
|
+ paymentCycle = "Monthly Payments";
|
|
|
|
+ } else if ("Annually".equals(orderBillingInfoVO.getSubscriptionType())) {
|
|
|
|
+ LocalDateTime startDate = orderBillingInfoVO.getPayTime().plusYears(orderBillingInfoVO.getPeriod());
|
|
|
|
+ LocalDateTime endDate = orderBillingInfoVO.getPayTime().plusYears(orderBillingInfoVO.getPeriod() + 1);
|
|
|
|
+ startDateStr = startDate.format(formatter);
|
|
|
|
+ endDateStr = endDate.format(formatter);
|
|
|
|
+ paymentCycle = "Annually Payments";
|
|
|
|
+ }
|
|
|
|
+ } else if (orderBillingInfoVO.getPackageType().equals(2)) {
|
|
|
|
+ paymentCycle = "Package Payments";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ BillingInfoVO billingInfoVO = new BillingInfoVO(userBillingInfo);
|
|
|
|
+ billingInfoVO.setStartDate(startDateStr);
|
|
|
|
+ billingInfoVO.setEndDate(endDateStr);
|
|
|
|
+ billingInfoVO.setPaymentCycle(paymentCycle);
|
|
|
|
+ billingInfoVO.setGearLevel(orderBillingInfoVO.getGearLevel());
|
|
|
|
+ billingInfoVO.setPrice(orderBillingInfoVO.getPrice().toString());
|
|
|
|
+ BigDecimal totalTax = orderBillingInfoVO.getPrice().multiply(new BigDecimal("0.05"));
|
|
|
|
+ billingInfoVO.setTotalExcludingTax(totalTax.toString());
|
|
|
|
+ billingInfoVO.setTotal(orderBillingInfoVO.getPrice().add(totalTax).toString());
|
|
|
|
+ billingInfoVO.setBillNo(billNo);
|
|
|
|
+ // 01/15/2024
|
|
|
|
+
|
|
|
|
+ LocalDate date = LocalDate.now();
|
|
|
|
+ String invoiceDate = date.format(formatter);
|
|
|
|
+ billingInfoVO.setInvoiceDate(invoiceDate);
|
|
|
|
+
|
|
// 获取html模板
|
|
// 获取html模板
|
|
// 填充数据
|
|
// 填充数据
|
|
- String htmlStr = TemplatesUtil.replaceStringUsingFreeMarker(invoiceHtml, userBillingInfo);
|
|
|
|
|
|
+ String htmlStr = TemplatesUtil.replaceStringUsingFreeMarker(invoiceHtml, billingInfoVO);
|
|
// 调用PHP接口生成PDF发票
|
|
// 调用PHP接口生成PDF发票
|
|
// 设置请求header
|
|
// 设置请求header
|
|
HttpHeaders headers = new HttpHeaders();
|
|
HttpHeaders headers = new HttpHeaders();
|
|
@@ -193,6 +276,9 @@ public class UserBillingInformationServiceImpl extends ServiceImpl<UserBillingIn
|
|
@Override
|
|
@Override
|
|
public void billSendEmail(BillSendEmailDTO billSendEmailDTO) {
|
|
public void billSendEmail(BillSendEmailDTO billSendEmailDTO) {
|
|
OrderBillVo orderBillVo = orderCashFlowService.getOrderCashFlowByOrderId(billSendEmailDTO.getOrderId());
|
|
OrderBillVo orderBillVo = orderCashFlowService.getOrderCashFlowByOrderId(billSendEmailDTO.getOrderId());
|
|
|
|
+
|
|
|
|
+ UserBillingInfoVO userBillingInfo = this.getUserBillingInfo(TokenUtil.getRequestHeader().getUsersId());
|
|
|
|
+
|
|
try {
|
|
try {
|
|
// 发送邮件并发送附件
|
|
// 发送邮件并发送附件
|
|
MimeMessage message = javaMailSender.createMimeMessage();
|
|
MimeMessage message = javaMailSender.createMimeMessage();
|
|
@@ -200,9 +286,24 @@ public class UserBillingInformationServiceImpl extends ServiceImpl<UserBillingIn
|
|
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
|
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
|
helper.setFrom(fromEmail);
|
|
helper.setFrom(fromEmail);
|
|
helper.setTo(billSendEmailDTO.getEmail());
|
|
helper.setTo(billSendEmailDTO.getEmail());
|
|
- helper.setSubject("发票");
|
|
|
|
- //第2个参数:是否解析html
|
|
|
|
- helper.setText("这是一个发票", true);
|
|
|
|
|
|
+
|
|
|
|
+ String emailContent;
|
|
|
|
+ if (billSendEmailDTO.getIsCN()) {
|
|
|
|
+ helper.setSubject("ComPDFKit API 发票号:"+orderBillVo.getBillNo());
|
|
|
|
+ emailContent = emailTemplate_CN.replaceAll(EMAIL_USER_NAME,userBillingInfo.getFirstName()
|
|
|
|
+ + " " + userBillingInfo.getLastName());
|
|
|
|
+ }else {
|
|
|
|
+ LocalDate date = LocalDate.now();
|
|
|
|
+ // 01/15/2024
|
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
|
|
|
|
+ String formattedDate = date.format(formatter);
|
|
|
|
+ helper.setSubject("Invoice "+orderBillVo.getBillNo()+" for ComPDFKit API due "+formattedDate);
|
|
|
|
+ //第2个参数:是否解析html
|
|
|
|
+ emailContent = emailTemplate_EN.replaceAll(EMAIL_USER_NAME,userBillingInfo.getFirstName()
|
|
|
|
+ + " " + userBillingInfo.getLastName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ helper.setText(emailContent, true);
|
|
//添加附件
|
|
//添加附件
|
|
File file = new File("invoice.pdf");
|
|
File file = new File("invoice.pdf");
|
|
FileUtils.copyURLToFile(new URL(orderBillVo.getBillUrl()), file);
|
|
FileUtils.copyURLToFile(new URL(orderBillVo.getBillUrl()), file);
|