diff --git a/src/main/kotlin/com/android/trisolarisserver/controller/RazorpayWebhookCapture.kt b/src/main/kotlin/com/android/trisolarisserver/controller/RazorpayWebhookCapture.kt index dc6f290..e099267 100644 --- a/src/main/kotlin/com/android/trisolarisserver/controller/RazorpayWebhookCapture.kt +++ b/src/main/kotlin/com/android/trisolarisserver/controller/RazorpayWebhookCapture.kt @@ -88,6 +88,7 @@ class RazorpayWebhookCapture( val orderEntity = root.path("payload").path("order").path("entity") val qrEntity = root.path("payload").path("qr_code").path("entity") val paymentLinkEntity = root.path("payload").path("payment_link").path("entity") + val refundEntity = root.path("payload").path("refund").path("entity") val paymentId = paymentEntity.path("id").asText(null) val orderId = paymentEntity.path("order_id").asText(null)?.takeIf { it.isNotBlank() } ?: orderEntity.path("id").asText(null)?.takeIf { it.isNotBlank() } @@ -152,10 +153,20 @@ class RazorpayWebhookCapture( if (event == null || paymentId == null || booking == null) return if (event != "payment.captured" && event != "refund.processed") return - if (paymentRepo.findByGatewayPaymentId(paymentId) != null) return + val refundId = refundEntity.path("id").asText(null) + if (event == "refund.processed") { + refundId?.let { + val existingRefund = paymentRepo.findByReference("razorpay_refund:$it") + if (existingRefund != null) return + } + } else { + if (paymentRepo.findByGatewayPaymentId(paymentId) != null) return + } - val signedAmount = if (event == "refund.processed") -paiseToAmount(amountPaise) else paiseToAmount(amountPaise) - val notesText = "razorpay event=$event status=$status order_id=$orderId" + val refundAmountPaise = refundEntity.path("amount").asLong(0) + val resolvedAmountPaise = if (event == "refund.processed" && refundAmountPaise > 0) refundAmountPaise else amountPaise + val signedAmount = if (event == "refund.processed") -paiseToAmount(resolvedAmountPaise) else paiseToAmount(resolvedAmountPaise) + val notesText = "razorpay event=$event status=$status order_id=$orderId refund_id=${refundId ?: "-"}" paymentRepo.save( Payment( property = booking.property, @@ -165,7 +176,7 @@ class RazorpayWebhookCapture( method = PaymentMethod.ONLINE, gatewayPaymentId = paymentId, gatewayTxnId = orderId, - reference = "razorpay:$paymentId", + reference = if (event == "refund.processed" && refundId != null) "razorpay_refund:$refundId" else "razorpay:$paymentId", notes = notesText, receivedAt = OffsetDateTime.now() )