Record refund webhooks as negative payments
All checks were successful
build-and-deploy / build-deploy (push) Successful in 34s

This commit is contained in:
androidlover5842
2026-02-01 16:05:44 +05:30
parent 66fc03d855
commit 5e8651d82f

View File

@@ -88,6 +88,7 @@ class RazorpayWebhookCapture(
val orderEntity = root.path("payload").path("order").path("entity") val orderEntity = root.path("payload").path("order").path("entity")
val qrEntity = root.path("payload").path("qr_code").path("entity") val qrEntity = root.path("payload").path("qr_code").path("entity")
val paymentLinkEntity = root.path("payload").path("payment_link").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 paymentId = paymentEntity.path("id").asText(null)
val orderId = paymentEntity.path("order_id").asText(null)?.takeIf { it.isNotBlank() } val orderId = paymentEntity.path("order_id").asText(null)?.takeIf { it.isNotBlank() }
?: orderEntity.path("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 == null || paymentId == null || booking == null) return
if (event != "payment.captured" && event != "refund.processed") return if (event != "payment.captured" && event != "refund.processed") 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 if (paymentRepo.findByGatewayPaymentId(paymentId) != null) return
}
val signedAmount = if (event == "refund.processed") -paiseToAmount(amountPaise) else paiseToAmount(amountPaise) val refundAmountPaise = refundEntity.path("amount").asLong(0)
val notesText = "razorpay event=$event status=$status order_id=$orderId" 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( paymentRepo.save(
Payment( Payment(
property = booking.property, property = booking.property,
@@ -165,7 +176,7 @@ class RazorpayWebhookCapture(
method = PaymentMethod.ONLINE, method = PaymentMethod.ONLINE,
gatewayPaymentId = paymentId, gatewayPaymentId = paymentId,
gatewayTxnId = orderId, gatewayTxnId = orderId,
reference = "razorpay:$paymentId", reference = if (event == "refund.processed" && refundId != null) "razorpay_refund:$refundId" else "razorpay:$paymentId",
notes = notesText, notes = notesText,
receivedAt = OffsetDateTime.now() receivedAt = OffsetDateTime.now()
) )