-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1237: [Order Refactor][Step 2] Create Order in PayPal and get order id
- Loading branch information
tuannguyenh1
committed
Oct 29, 2024
1 parent
a5dfd9f
commit ea686af
Showing
28 changed files
with
557 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
order/src/main/java/com/yas/order/consumer/PaymentConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.yas.order.consumer; | ||
|
||
import static com.yas.order.utils.JsonUtils.convertObjectToString; | ||
import static com.yas.order.utils.JsonUtils.getAttributesNode; | ||
import static com.yas.order.utils.JsonUtils.getJsonValueOrNull; | ||
import static com.yas.order.utils.JsonUtils.getJsonValueOrThrow; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonObject; | ||
import com.yas.order.model.Checkout; | ||
import com.yas.order.model.enumeration.CheckoutProgress; | ||
import com.yas.order.model.enumeration.CheckoutState; | ||
import com.yas.order.service.CheckoutService; | ||
import com.yas.order.utils.Constants; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.kafka.annotation.RetryableTopic; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PaymentConsumer { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(PaymentConsumer.class); | ||
private final CheckoutService checkoutService; | ||
private final ObjectMapper objectMapper; | ||
private final Gson gson; | ||
|
||
@KafkaListener( | ||
topics = "${cdc.event.payment.topic-name}", | ||
groupId = "${cdc.event.payment.group-id}" | ||
) | ||
@RetryableTopic | ||
public void listen(ConsumerRecord<?, ?> consumerRecord) { | ||
|
||
if (Objects.isNull(consumerRecord)) { | ||
LOGGER.info("Consumer Record is null"); | ||
return; | ||
} | ||
JsonObject valueObject = gson.fromJson((String) consumerRecord.value(), JsonObject.class); | ||
processPaymentEvent(valueObject); | ||
|
||
} | ||
|
||
private void processPaymentEvent(JsonObject valueObject) { | ||
Optional.ofNullable(valueObject) | ||
.filter( | ||
value -> value.has("op") && "u".equals(value.get("op").getAsString()) | ||
) | ||
.filter(value -> value.has("before") && value.has("after")) | ||
.ifPresent(this::handleJsonForUpdateCheckout); | ||
} | ||
|
||
private void handleJsonForUpdateCheckout(JsonObject valueObject) { | ||
|
||
JsonObject before = valueObject.getAsJsonObject("before"); | ||
JsonObject after = valueObject.getAsJsonObject("after"); | ||
|
||
String id = getJsonValueOrThrow(after, Constants.Column.ID_COLUMN, | ||
Constants.ErrorCode.ID_NOT_EXISTED); | ||
|
||
String beforePaypalOrderId = getJsonValueOrNull(before, | ||
|
||
Constants.Column.CHECKOUT_ATTRIBUTES_PAYMENT_PROVIDER_CHECKOUT_ID_FIELD); | ||
String afterPaypalOrderId = getJsonValueOrNull(after, Constants.Column.CHECKOUT_ATTRIBUTES_PAYMENT_PROVIDER_CHECKOUT_ID_FIELD); | ||
|
||
if (!Objects.isNull(afterPaypalOrderId) && !afterPaypalOrderId.equals(beforePaypalOrderId)) { | ||
|
||
LOGGER.info("Handle json for update Checkout with Payment {}", id); | ||
|
||
String checkoutId = getJsonValueOrThrow(after, Constants.Column.CHECKOUT_ID_COLUMN, | ||
Constants.ErrorCode.CHECKOUT_ID_NOT_EXISTED); | ||
updateCheckOut(checkoutId, afterPaypalOrderId); | ||
} else { | ||
LOGGER.info("It's not an event to create an Order on PayPal with Payment ID {}", id); | ||
} | ||
} | ||
|
||
private void updateCheckOut(String checkoutId, String paymentProviderCheckoutId) { | ||
|
||
Checkout checkout = checkoutService.findCheckoutById(checkoutId); | ||
checkout.setCheckoutState(CheckoutState.PAYMENT_PROCESSING); | ||
checkout.setProgress(CheckoutProgress.PAYMENT_CREATED); | ||
|
||
ObjectNode updatedAttributes = updateAttributesWithCheckout(checkout.getAttributes(), | ||
paymentProviderCheckoutId); | ||
checkout.setAttributes(convertObjectToString(objectMapper, updatedAttributes)); | ||
|
||
checkoutService.updateCheckout(checkout); | ||
} | ||
|
||
private ObjectNode updateAttributesWithCheckout(String attributes, String paymentProviderCheckoutId) { | ||
|
||
ObjectNode attributesNode = getAttributesNode(objectMapper, attributes); | ||
attributesNode.put(Constants.Column.CHECKOUT_ATTRIBUTES_PAYMENT_PROVIDER_CHECKOUT_ID_FIELD, | ||
paymentProviderCheckoutId); | ||
|
||
return attributesNode; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.