|
| 1 | +package com.yas.order.consumer; |
| 2 | + |
| 3 | +import static com.yas.order.utils.JsonUtils.convertObjectToString; |
| 4 | +import static com.yas.order.utils.JsonUtils.createJsonErrorObject; |
| 5 | +import static com.yas.order.utils.JsonUtils.getAttributesNode; |
| 6 | +import static com.yas.order.utils.JsonUtils.getJsonValueOrThrow; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 10 | +import com.google.gson.Gson; |
| 11 | +import com.google.gson.JsonObject; |
| 12 | +import com.yas.commonlibrary.exception.BadRequestException; |
| 13 | +import com.yas.commonlibrary.exception.NotFoundException; |
| 14 | +import com.yas.order.model.Checkout; |
| 15 | +import com.yas.order.model.enumeration.CheckoutProgress; |
| 16 | +import com.yas.order.model.enumeration.CheckoutState; |
| 17 | +import com.yas.order.repository.CheckoutRepository; |
| 18 | +import com.yas.order.service.PaymentService; |
| 19 | +import com.yas.order.utils.Constants; |
| 20 | +import com.yas.order.viewmodel.payment.CheckoutPaymentVm; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Objects; |
| 23 | +import java.util.Optional; |
| 24 | +import lombok.RequiredArgsConstructor; |
| 25 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | +import org.springframework.kafka.annotation.KafkaListener; |
| 29 | +import org.springframework.kafka.annotation.RetryableTopic; |
| 30 | +import org.springframework.stereotype.Service; |
| 31 | + |
| 32 | +@Service |
| 33 | +@RequiredArgsConstructor |
| 34 | +public class OrderStatusConsumer { |
| 35 | + |
| 36 | + private static final Logger LOGGER = LoggerFactory.getLogger(OrderStatusConsumer.class); |
| 37 | + private final PaymentService paymentService; |
| 38 | + private final CheckoutRepository checkoutRepository; |
| 39 | + private final ObjectMapper objectMapper; |
| 40 | + private final Gson gson; |
| 41 | + |
| 42 | + @KafkaListener( |
| 43 | + topics = "${cdc.event.checkout.status.topic-name}", |
| 44 | + groupId = "${cdc.event.checkout.status.group-id}" |
| 45 | + ) |
| 46 | + @RetryableTopic( |
| 47 | + attempts = "1" |
| 48 | + ) |
| 49 | + public void listen(ConsumerRecord<?, ?> consumerRecord) { |
| 50 | + |
| 51 | + if (Objects.isNull(consumerRecord)) { |
| 52 | + LOGGER.info("ConsumerRecord is null"); |
| 53 | + return; |
| 54 | + } |
| 55 | + JsonObject valueObject = gson.fromJson((String) consumerRecord.value(), JsonObject.class); |
| 56 | + processCheckoutEvent(valueObject); |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + private void processCheckoutEvent(JsonObject valueObject) { |
| 61 | + Optional.ofNullable(valueObject) |
| 62 | + .filter(value -> value.has("after")) |
| 63 | + .map(value -> value.getAsJsonObject("after")) |
| 64 | + .ifPresent(this::handleAfterJson); |
| 65 | + } |
| 66 | + |
| 67 | + private void handleAfterJson(JsonObject after) { |
| 68 | + |
| 69 | + String id = getJsonValueOrThrow(after, Constants.Column.ID_COLUMN, |
| 70 | + Constants.ErrorCode.ID_NOT_EXISTED); |
| 71 | + String status = getJsonValueOrThrow(after, Constants.Column.STATUS_COLUMN, |
| 72 | + Constants.ErrorCode.STATUS_NOT_EXISTED, id); |
| 73 | + String progress = getJsonValueOrThrow(after, Constants.Column.CHECKOUT_PROGRESS_COLUMN, |
| 74 | + Constants.ErrorCode.PROGRESS_NOT_EXISTED, id); |
| 75 | + |
| 76 | + if (!isPaymentProcessing(status, progress)) { |
| 77 | + LOGGER.info("Checkout record with ID {} lacks the status 'PAYMENT_PROCESSING' and progress 'STOCK_LOCKED'", |
| 78 | + id); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + LOGGER.info("Checkout record with ID {} has the status 'PAYMENT_PROCESSING' and the process 'STOCK_LOCKED'", |
| 83 | + id); |
| 84 | + |
| 85 | + Checkout checkout = checkoutRepository |
| 86 | + .findById(id) |
| 87 | + .orElseThrow(() -> new NotFoundException(Constants.ErrorCode.CHECKOUT_NOT_FOUND, id)); |
| 88 | + |
| 89 | + processPaymentAndUpdateCheckout(checkout); |
| 90 | + } |
| 91 | + |
| 92 | + private boolean isPaymentProcessing(String status, String process) { |
| 93 | + return CheckoutState.PAYMENT_PROCESSING.name().equalsIgnoreCase(status) |
| 94 | + && CheckoutProgress.STOCK_LOCKED.name().equalsIgnoreCase(process); |
| 95 | + } |
| 96 | + |
| 97 | + private void processPaymentAndUpdateCheckout(Checkout checkout) { |
| 98 | + |
| 99 | + try { |
| 100 | + Long paymentId = processPayment(checkout); |
| 101 | + checkout.setProgress(CheckoutProgress.PAYMENT_CREATED); |
| 102 | + checkout.setLastError(null); |
| 103 | + |
| 104 | + ObjectNode updatedAttributes = updateAttributesWithPayment(checkout.getAttributes(), paymentId); |
| 105 | + checkout.setAttributes(convertObjectToString(objectMapper, updatedAttributes)); |
| 106 | + |
| 107 | + } catch (Exception e) { |
| 108 | + |
| 109 | + checkout.setProgress(CheckoutProgress.PAYMENT_CREATED_FAILED); |
| 110 | + |
| 111 | + ObjectNode error = createJsonErrorObject(objectMapper, CheckoutProgress.PAYMENT_CREATED_FAILED.name(), |
| 112 | + e.getMessage()); |
| 113 | + checkout.setLastError(convertObjectToString(objectMapper, error)); |
| 114 | + |
| 115 | + LOGGER.error(e.getMessage()); |
| 116 | + throw new BadRequestException(Constants.ErrorCode.PROCESS_CHECKOUT_FAILED, checkout.getId()); |
| 117 | + |
| 118 | + } finally { |
| 119 | + checkoutRepository.save(checkout); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private Long processPayment(Checkout checkout) { |
| 124 | + |
| 125 | + CheckoutPaymentVm requestDto = new CheckoutPaymentVm( |
| 126 | + checkout.getId(), |
| 127 | + checkout.getPaymentMethodId(), |
| 128 | + checkout.getTotalAmount() |
| 129 | + ); |
| 130 | + |
| 131 | + Long paymentId = paymentService.createPaymentFromEvent(requestDto); |
| 132 | + LOGGER.info("Payment created successfully with ID: {}", paymentId); |
| 133 | + |
| 134 | + return paymentId; |
| 135 | + } |
| 136 | + |
| 137 | + private ObjectNode updateAttributesWithPayment(String attributes, Long paymentId) throws IOException { |
| 138 | + |
| 139 | + ObjectNode attributesNode = getAttributesNode(objectMapper, attributes); |
| 140 | + attributesNode.put(Constants.Column.CHECKOUT_ATTRIBUTES_PAYMENT_ID_FIELD, paymentId); |
| 141 | + |
| 142 | + return attributesNode; |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments