|
| 1 | +package com.bitscoderdotcom.link_generator_system.service.service.serviceImpl; |
| 2 | + |
| 3 | +import com.bitscoderdotcom.link_generator_system.constant.Status; |
| 4 | +import com.bitscoderdotcom.link_generator_system.dto.ApiResponse; |
| 5 | +import com.bitscoderdotcom.link_generator_system.dto.EmailDetails; |
| 6 | +import com.bitscoderdotcom.link_generator_system.dto.InvoiceDto; |
| 7 | +import com.bitscoderdotcom.link_generator_system.entities.Company; |
| 8 | +import com.bitscoderdotcom.link_generator_system.entities.Invoice; |
| 9 | +import com.bitscoderdotcom.link_generator_system.entities.PaymentLink; |
| 10 | +import com.bitscoderdotcom.link_generator_system.exception.ResourceNotFoundException; |
| 11 | +import com.bitscoderdotcom.link_generator_system.repository.CompanyRepository; |
| 12 | +import com.bitscoderdotcom.link_generator_system.repository.InvoiceRepository; |
| 13 | +import com.bitscoderdotcom.link_generator_system.repository.PaymentLinkRepository; |
| 14 | +import com.bitscoderdotcom.link_generator_system.service.EmailService; |
| 15 | +import com.bitscoderdotcom.link_generator_system.service.service.InvoiceService; |
| 16 | +import lombok.AllArgsConstructor; |
| 17 | +import lombok.extern.slf4j.Slf4j; |
| 18 | +import org.springframework.http.ResponseEntity; |
| 19 | +import org.springframework.stereotype.Service; |
| 20 | +import org.springframework.transaction.annotation.Transactional; |
| 21 | +import org.thymeleaf.TemplateEngine; |
| 22 | +import org.thymeleaf.context.Context; |
| 23 | + |
| 24 | +import java.security.Principal; |
| 25 | +import java.time.LocalDate; |
| 26 | +import java.time.LocalDateTime; |
| 27 | +import java.util.UUID; |
| 28 | + |
| 29 | +@Service |
| 30 | +@Slf4j |
| 31 | +@AllArgsConstructor |
| 32 | +public class InvoiceServiceImpl implements InvoiceService { |
| 33 | + |
| 34 | + private final InvoiceRepository invoiceRepository; |
| 35 | + private final CompanyRepository companyRepository; |
| 36 | + private final PaymentLinkRepository paymentLinkRepository; |
| 37 | + private final EmailService emailService; |
| 38 | + private final TemplateEngine templateEngine; |
| 39 | + |
| 40 | + @Transactional |
| 41 | + public ResponseEntity<ApiResponse<InvoiceDto.Response>> generateInvoice(InvoiceDto invoiceDto, Principal principal) { |
| 42 | + log.info("Generating invoice for company"); |
| 43 | + |
| 44 | + Company company = companyRepository.findUserByCompanyEmail(principal.getName()) |
| 45 | + .orElseThrow(() -> new ResourceNotFoundException("Company", "User email", principal.getName())); |
| 46 | + |
| 47 | + // Create a new invoice |
| 48 | + Invoice invoice = new Invoice(); |
| 49 | + invoice.setCompany(company); |
| 50 | + invoice.setCustomerName(invoiceDto.getCustomerName()); |
| 51 | + invoice.setCustomerEmail(invoiceDto.getCustomerEmail()); |
| 52 | + invoice.setDescription(invoiceDto.getDescription()); |
| 53 | + invoice.setQuantity(invoiceDto.getQuantity()); |
| 54 | + invoice.setUnitPrice(invoiceDto.getUnitPrice()); |
| 55 | + invoice.setTotalAmount(invoiceDto.getTotalAmount()); |
| 56 | + invoice.setInvoiceGenerationDate(invoiceDto.getInvoiceGenerationDate()); |
| 57 | + invoice.setPaymentDueDate(invoiceDto.getPaymentDueDate()); |
| 58 | + invoice.setStatus(Status.Unpaid); |
| 59 | + |
| 60 | + invoiceRepository.save(invoice); |
| 61 | + |
| 62 | + PaymentLink paymentLink = new PaymentLink(); |
| 63 | + paymentLink.setInvoice(invoice); |
| 64 | + paymentLinkRepository.save(paymentLink); |
| 65 | + String baseUrl = "http://localhost:8080"; |
| 66 | + paymentLink.setUrl(baseUrl + generateUniqueUrl(invoice.getId())); |
| 67 | + paymentLinkRepository.save(paymentLink); |
| 68 | + |
| 69 | + // Send the payment link to the customer via email |
| 70 | + EmailDetails emailDetails = new EmailDetails(); |
| 71 | + emailDetails.setRecipient(invoice.getCustomerEmail()); |
| 72 | + emailDetails.setSubject("Invoice Payment Link"); |
| 73 | + emailDetails.setMessageBody("Please click the following link to view and pay your invoice: <a href=\"" + paymentLink.getUrl() + "\">" + paymentLink.getUrl() + "</a>"); |
| 74 | + emailService.sendEmail(emailDetails); |
| 75 | + |
| 76 | + log.info("Invoice generated successfully for company with id: {}", company.getId()); |
| 77 | + |
| 78 | + InvoiceDto.Response invoiceResponseDto = convertEntityToDto(invoice); |
| 79 | + |
| 80 | + ApiResponse<InvoiceDto.Response> apiResponse = new ApiResponse<>( |
| 81 | + LocalDateTime.now(), |
| 82 | + UUID.randomUUID().toString(), |
| 83 | + true, |
| 84 | + "Invoice generated successfully", |
| 85 | + invoiceResponseDto |
| 86 | + ); |
| 87 | + |
| 88 | + return ResponseEntity.ok(apiResponse); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Invoice getInvoiceById(String id) { |
| 93 | + return invoiceRepository.findById(id) |
| 94 | + .orElseThrow(() -> new ResourceNotFoundException("Invoice", "id", id)); |
| 95 | + } |
| 96 | + |
| 97 | + @Transactional |
| 98 | + public String processPayment(String linkId, String invoiceId) { |
| 99 | + // Find the payment link by linkId |
| 100 | + PaymentLink paymentLink = paymentLinkRepository.findById(linkId) |
| 101 | + .orElseThrow(() -> new ResourceNotFoundException("PaymentLink", "id", linkId)); |
| 102 | + |
| 103 | + if (!paymentLink.getInvoice().getId().equals(invoiceId)) { |
| 104 | + throw new IllegalArgumentException("The provided invoiceId does not match the invoiceId associated with the payment link."); |
| 105 | + } |
| 106 | + |
| 107 | + Invoice invoice = paymentLink.getInvoice(); |
| 108 | + |
| 109 | + // Check if the invoice has already been paid |
| 110 | + if (invoice.getStatus() == Status.Paid) { |
| 111 | + return "This invoice has already been paid for."; |
| 112 | + } |
| 113 | + |
| 114 | + // If the invoice has not been paid, process the payment |
| 115 | + invoice.setStatus(Status.Paid); |
| 116 | + invoiceRepository.save(invoice); |
| 117 | + |
| 118 | + String receipt = generateReceipt(invoice); |
| 119 | + |
| 120 | + // Send a success email to the customer with the receipt |
| 121 | + EmailDetails emailDetails = new EmailDetails(); |
| 122 | + emailDetails.setRecipient(invoice.getCustomerEmail()); |
| 123 | + emailDetails.setSubject("Payment Successful, please check your email for details. Thank you and please come again."); |
| 124 | + emailDetails.setMessageBody(receipt); |
| 125 | + emailService.sendEmail(emailDetails); |
| 126 | + |
| 127 | + return "Payment for invoice " + invoice.getId() + " was successful."; |
| 128 | + } |
| 129 | + |
| 130 | + private String generateUniqueUrl(String invoiceId) { |
| 131 | + String baseUrl = "/lgsApp/v1"; |
| 132 | + return baseUrl + "/invoice/" + invoiceId + "/payment"; |
| 133 | + } |
| 134 | + |
| 135 | + private InvoiceDto.Response convertEntityToDto(Invoice invoice) { |
| 136 | + return new InvoiceDto.Response( |
| 137 | + invoice.getId(), |
| 138 | + invoice.getCustomerName(), |
| 139 | + invoice.getCustomerEmail(), |
| 140 | + invoice.getDescription(), |
| 141 | + invoice.getQuantity(), |
| 142 | + invoice.getUnitPrice(), |
| 143 | + invoice.getTotalAmount(), |
| 144 | + invoice.getInvoiceGenerationDate(), |
| 145 | + invoice.getPaymentDueDate(), |
| 146 | + invoice.getStatus() |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + private String generateReceipt(Invoice invoice) { |
| 151 | + Context context = new Context(); |
| 152 | + context.setVariable("invoice", invoice); |
| 153 | + context.setVariable("paymentDate", LocalDate.now()); |
| 154 | + return templateEngine.process("receipt", context); |
| 155 | + } |
| 156 | +} |
0 commit comments