Store PayU payment metadata and attempts
All checks were successful
build-and-deploy / build-deploy (push) Successful in 37s

This commit is contained in:
androidlover5842
2026-01-30 09:34:50 +05:30
parent 52bcdd3648
commit e324eee065
7 changed files with 256 additions and 15 deletions

View File

@@ -2,8 +2,10 @@ package com.android.trisolarisserver.controller
import com.android.trisolarisserver.models.booking.Payment
import com.android.trisolarisserver.models.booking.PaymentMethod
import com.android.trisolarisserver.models.payment.PayuPaymentAttempt
import com.android.trisolarisserver.models.payment.PayuWebhookLog
import com.android.trisolarisserver.db.repo.BookingRepo
import com.android.trisolarisserver.repo.PayuPaymentAttemptRepo
import com.android.trisolarisserver.repo.PayuWebhookLogRepo
import com.android.trisolarisserver.repo.PaymentRepo
import com.android.trisolarisserver.repo.PropertyRepo
@@ -32,6 +34,7 @@ class PayuWebhookCapture(
private val propertyRepo: PropertyRepo,
private val bookingRepo: BookingRepo,
private val paymentRepo: PaymentRepo,
private val payuPaymentAttemptRepo: PayuPaymentAttemptRepo,
private val payuWebhookLogRepo: PayuWebhookLogRepo
) {
@@ -63,26 +66,58 @@ class PayuWebhookCapture(
val status = data["status"]?.lowercase() ?: data["unmappedstatus"]?.lowercase()
val isSuccess = status == "success" || status == "captured"
val isRefund = status == "refund" || status == "refunded"
if (!isSuccess && !isRefund) return
val bookingId = data["udf1"]?.let { runCatching { UUID.fromString(it) }.getOrNull() }
if (bookingId == null) return
val booking = bookingRepo.findById(bookingId).orElse(null) ?: return
if (booking.property.id != propertyId) return
val referenceId = data["mihpayid"]?.ifBlank { null } ?: data["txnid"]?.ifBlank { null }
val reference = referenceId?.let { "payu:$it" }
if (reference != null && paymentRepo.findByReference(reference) != null) return
val booking = bookingId?.let { bookingRepo.findById(it).orElse(null) }
if (booking != null && booking.property.id != propertyId) return
val amountRaw = data["amount"]?.ifBlank { null } ?: data["net_amount_debit"]?.ifBlank { null }
val amount = parseAmount(amountRaw) ?: return
val signedAmount = if (isRefund) -amount else amount
val amount = parseAmount(amountRaw)
val gatewayPaymentId = data["mihpayid"]?.ifBlank { null }
val gatewayTxnId = data["txnid"]?.ifBlank { null }
val bankRef = data["bank_ref_num"]?.ifBlank { null } ?: data["bank_ref_no"]?.ifBlank { null }
val mode = data["mode"]?.ifBlank { null }
val pgType = data["PG_TYPE"]?.ifBlank { null }
val payerVpa = data["field3"]?.ifBlank { null }
val payerName = data["field6"]?.ifBlank { null }
val paymentSource = data["payment_source"]?.ifBlank { null }
val errorCode = data["error"]?.ifBlank { null }
val errorMessage = data["error_Message"]?.ifBlank { null }
val receivedAt = parseAddedOn(data["addedon"], booking?.property?.timezone)
val receivedAt = parseAddedOn(data["addedon"], booking.property.timezone)
payuPaymentAttemptRepo.save(
PayuPaymentAttempt(
property = property,
booking = booking,
status = status,
unmappedStatus = data["unmappedstatus"]?.ifBlank { null },
amount = amount,
currency = booking?.property?.currency ?: property.currency,
gatewayPaymentId = gatewayPaymentId,
gatewayTxnId = gatewayTxnId,
bankRefNum = bankRef,
mode = mode,
pgType = pgType,
payerVpa = payerVpa,
payerName = payerName,
paymentSource = paymentSource,
errorCode = errorCode,
errorMessage = errorMessage,
payload = body,
receivedAt = receivedAt
)
)
if (!isSuccess && !isRefund) return
if (booking == null) return
if (gatewayPaymentId != null && paymentRepo.findByGatewayPaymentId(gatewayPaymentId) != null) return
if (gatewayPaymentId == null && gatewayTxnId != null && paymentRepo.findByGatewayTxnId(gatewayTxnId) != null) return
val signedAmount = amount?.let { if (isRefund) -it else it } ?: return
val notes = buildString {
append("payu status=").append(status)
data["txnid"]?.let { append(" txnid=").append(it) }
data["bank_ref_num"]?.let { append(" bank_ref=").append(it) }
gatewayTxnId?.let { append(" txnid=").append(it) }
bankRef?.let { append(" bank_ref=").append(it) }
}
paymentRepo.save(
@@ -92,7 +127,15 @@ class PayuWebhookCapture(
amount = signedAmount,
currency = booking.property.currency,
method = PaymentMethod.ONLINE,
reference = reference,
gatewayPaymentId = gatewayPaymentId,
gatewayTxnId = gatewayTxnId,
bankRefNum = bankRef,
mode = mode,
pgType = pgType,
payerVpa = payerVpa,
payerName = payerName,
paymentSource = paymentSource,
reference = gatewayPaymentId?.let { "payu:$it" } ?: gatewayTxnId?.let { "payu:$it" },
notes = notes,
receivedAt = receivedAt
)