Skip to content

Instantly share code, notes, and snippets.

@danielsiwiec
Last active May 25, 2022 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielsiwiec/21f5c04aca05eeb0349444060521e5e7 to your computer and use it in GitHub Desktop.
Save danielsiwiec/21f5c04aca05eeb0349444060521e5e7 to your computer and use it in GitHub Desktop.
Order Processing - Choreography - Payment Service
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