|
| 1 | +package com.yas.order.consumer; |
| 2 | + |
| 3 | +import static com.yas.order.utils.JsonUtils.convertObjectToString; |
| 4 | +import static com.yas.order.utils.JsonUtils.getAttributesNode; |
| 5 | +import static com.yas.order.utils.JsonUtils.getJsonValueOrNull; |
| 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.order.model.Checkout; |
| 13 | +import com.yas.order.model.enumeration.CheckoutProgress; |
| 14 | +import com.yas.order.model.enumeration.CheckoutState; |
| 15 | +import com.yas.order.service.CheckoutService; |
| 16 | +import com.yas.order.utils.Constants; |
| 17 | +import java.util.Objects; |
| 18 | +import java.util.Optional; |
| 19 | +import lombok.RequiredArgsConstructor; |
| 20 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | +import org.springframework.kafka.annotation.KafkaListener; |
| 24 | +import org.springframework.kafka.annotation.RetryableTopic; |
| 25 | +import org.springframework.stereotype.Service; |
| 26 | + |
| 27 | +@Service |
| 28 | +@RequiredArgsConstructor |
| 29 | +public class PaymentConsumer { |
| 30 | + |
| 31 | + private static final Logger LOGGER = LoggerFactory.getLogger(PaymentConsumer.class); |
| 32 | + private final CheckoutService checkoutService; |
| 33 | + private final ObjectMapper objectMapper; |
| 34 | + private final Gson gson; |
| 35 | + |
| 36 | + @KafkaListener( |
| 37 | + topics = "${cdc.event.payment.topic-name}", |
| 38 | + groupId = "${cdc.event.payment.update.group-id}" |
| 39 | + ) |
| 40 | + @RetryableTopic |
| 41 | + public void listen(ConsumerRecord<?, ?> consumerRecord) { |
| 42 | + |
| 43 | + if (Objects.isNull(consumerRecord)) { |
| 44 | + LOGGER.info("Consumer Record is null"); |
| 45 | + return; |
| 46 | + } |
| 47 | + JsonObject valueObject = gson.fromJson((String) consumerRecord.value(), JsonObject.class); |
| 48 | + processPaymentEvent(valueObject); |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + private void processPaymentEvent(JsonObject valueObject) { |
| 53 | + Optional.ofNullable(valueObject) |
| 54 | + .filter( |
| 55 | + value -> value.has("op") && "u".equals(value.get("op").getAsString()) |
| 56 | + ) |
| 57 | + .filter(value -> value.has("before") && value.has("after")) |
| 58 | + .ifPresent(this::handleJsonForUpdateCheckout); |
| 59 | + } |
| 60 | + |
| 61 | + private void handleJsonForUpdateCheckout(JsonObject valueObject) { |
| 62 | + |
| 63 | + JsonObject before = valueObject.getAsJsonObject("before"); |
| 64 | + JsonObject after = valueObject.getAsJsonObject("after"); |
| 65 | + |
| 66 | + String id = getJsonValueOrThrow(after, Constants.Column.ID_COLUMN, |
| 67 | + Constants.ErrorCode.ID_NOT_EXISTED); |
| 68 | + |
| 69 | + String beforePaypalOrderId = getJsonValueOrNull(before, |
| 70 | + |
| 71 | + Constants.Column.CHECKOUT_ATTRIBUTES_PAYMENT_PROVIDER_CHECKOUT_ID_FIELD); |
| 72 | + String afterPaypalOrderId = getJsonValueOrNull(after, Constants.Column.CHECKOUT_ATTRIBUTES_PAYMENT_PROVIDER_CHECKOUT_ID_FIELD); |
| 73 | + |
| 74 | + if (!Objects.isNull(afterPaypalOrderId) && !afterPaypalOrderId.equals(beforePaypalOrderId)) { |
| 75 | + |
| 76 | + LOGGER.info("Handle json for update Checkout with Payment {}", id); |
| 77 | + |
| 78 | + String checkoutId = getJsonValueOrThrow(after, Constants.Column.CHECKOUT_ID_COLUMN, |
| 79 | + Constants.ErrorCode.CHECKOUT_ID_NOT_EXISTED); |
| 80 | + updateCheckOut(checkoutId, afterPaypalOrderId); |
| 81 | + } else { |
| 82 | + LOGGER.info("It's not an event to create an Order on PayPal with Payment ID {}", id); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void updateCheckOut(String checkoutId, String paymentProviderCheckoutId) { |
| 87 | + |
| 88 | + Checkout checkout = checkoutService.findCheckoutById(checkoutId); |
| 89 | + checkout.setCheckoutState(CheckoutState.PAYMENT_PROCESSING); |
| 90 | + checkout.setProgress(CheckoutProgress.PAYMENT_CREATED); |
| 91 | + |
| 92 | + ObjectNode updatedAttributes = updateAttributesWithCheckout(checkout.getAttributes(), |
| 93 | + paymentProviderCheckoutId); |
| 94 | + checkout.setAttributes(convertObjectToString(objectMapper, updatedAttributes)); |
| 95 | + |
| 96 | + checkoutService.updateCheckout(checkout); |
| 97 | + } |
| 98 | + |
| 99 | + private ObjectNode updateAttributesWithCheckout(String attributes, String paymentProviderCheckoutId) { |
| 100 | + |
| 101 | + ObjectNode attributesNode = getAttributesNode(objectMapper, attributes); |
| 102 | + attributesNode.put(Constants.Column.CHECKOUT_ATTRIBUTES_PAYMENT_PROVIDER_CHECKOUT_ID_FIELD, |
| 103 | + paymentProviderCheckoutId); |
| 104 | + |
| 105 | + return attributesNode; |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments