Last active
May 25, 2022 08:53
-
-
Save danielsiwiec/21f5c04aca05eeb0349444060521e5e7 to your computer and use it in GitHub Desktop.
Order Processing - Choreography - Payment Service
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
class PaymentService( | |
private val customerAccounts: MutableMap<Int, Int> = mutableMapOf(), | |
private val skuPrices: MutableMap<Int, Double> = mutableMapOf() | |
) { | |
@KafkaListener(topics = [ORDERS]) | |
fun onOrder(order: Order) { | |
if (order.status != SUBMITTED) return | |
val total = order.items.sumOf { skuPrices[it] ?: throw RuntimeException("Customer unknown") } | |
val status = paymentGateway.processPayment( | |
total, | |
customerAccounts[order.customerId] ?: throw RuntimeException("Customer unknown") | |
) | |
sendMessage(PAYMENTS, order.withStatus(if (status) PAID else PAYMENT_FAILED)) | |
} | |
@KafkaListener(topics = [SKUS]) | |
fun onSku(sku: Sku) { | |
if (sku.onStock > 0) skuPrices[sku.id] = sku.price else skuPrices.remove(sku.id) | |
} | |
@KafkaListener(topics = [CUSTOMERS]) | |
fun onCustomer(customer: Customer) { | |
customerAccounts[customer.id] = customer.account | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment