From d53d17996315a6d9c3f840e2b57d31fda3af1b3b Mon Sep 17 00:00:00 2001 From: androidlover5842 Date: Sun, 1 Feb 2026 10:52:46 +0530 Subject: [PATCH] Add Razorpay active QR fetch and close --- .../controller/RazorpayQrPayments.kt | 55 +++++++++++++++++++ .../repo/RazorpayQrRequestRepo.kt | 5 ++ 2 files changed, 60 insertions(+) diff --git a/src/main/kotlin/com/android/trisolarisserver/controller/RazorpayQrPayments.kt b/src/main/kotlin/com/android/trisolarisserver/controller/RazorpayQrPayments.kt index be87540..1aaadb9 100644 --- a/src/main/kotlin/com/android/trisolarisserver/controller/RazorpayQrPayments.kt +++ b/src/main/kotlin/com/android/trisolarisserver/controller/RazorpayQrPayments.kt @@ -17,6 +17,7 @@ import org.springframework.http.HttpMethod import org.springframework.http.MediaType import org.springframework.http.ResponseEntity import org.springframework.security.core.annotation.AuthenticationPrincipal +import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody @@ -140,6 +141,60 @@ class RazorpayQrPayments( ) } + @GetMapping("/qr/active") + fun getActiveQr( + @PathVariable propertyId: UUID, + @PathVariable bookingId: UUID, + @AuthenticationPrincipal principal: MyPrincipal? + ): RazorpayQrGenerateResponse? { + requireRole(propertyAccess, propertyId, principal, Role.ADMIN, Role.MANAGER, Role.STAFF) + val booking = bookingRepo.findById(bookingId).orElseThrow { + ResponseStatusException(HttpStatus.NOT_FOUND, "Booking not found") + } + if (booking.property.id != propertyId) { + throw ResponseStatusException(HttpStatus.NOT_FOUND, "Booking not found for property") + } + val active = qrRequestRepo.findTopByBookingIdAndStatusOrderByCreatedAtDesc(bookingId, "active") ?: return null + return RazorpayQrGenerateResponse( + qrId = active.qrId, + amount = active.amount, + currency = active.currency, + imageUrl = active.imageUrl + ) + } + + @PostMapping("/qr/close") + @Transactional + fun closeActiveQr( + @PathVariable propertyId: UUID, + @PathVariable bookingId: UUID, + @AuthenticationPrincipal principal: MyPrincipal? + ): RazorpayQrGenerateResponse? { + requireRole(propertyAccess, propertyId, principal, Role.ADMIN, Role.MANAGER, Role.STAFF) + val booking = bookingRepo.findById(bookingId).orElseThrow { + ResponseStatusException(HttpStatus.NOT_FOUND, "Booking not found") + } + if (booking.property.id != propertyId) { + throw ResponseStatusException(HttpStatus.NOT_FOUND, "Booking not found for property") + } + val active = qrRequestRepo.findTopByBookingIdAndStatusOrderByCreatedAtDesc(bookingId, "active") ?: return null + val settings = settingsRepo.findByPropertyId(propertyId) + ?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Razorpay settings not configured") + val qrId = active.qrId ?: return null + val response = postJson(resolveBaseUrl(settings.isTest) + "/payments/qr_codes/$qrId/close", settings, "{}") + if (!response.statusCode.is2xxSuccessful) { + throw ResponseStatusException(HttpStatus.BAD_GATEWAY, "Razorpay close request failed") + } + active.status = "closed" + qrRequestRepo.save(active) + return RazorpayQrGenerateResponse( + qrId = active.qrId, + amount = active.amount, + currency = active.currency, + imageUrl = active.imageUrl + ) + } + private fun postJson(url: String, settings: com.android.trisolarisserver.models.payment.RazorpaySettings, json: String): ResponseEntity { val headers = HttpHeaders() headers.contentType = MediaType.APPLICATION_JSON diff --git a/src/main/kotlin/com/android/trisolarisserver/repo/RazorpayQrRequestRepo.kt b/src/main/kotlin/com/android/trisolarisserver/repo/RazorpayQrRequestRepo.kt index 4f869f6..d34bdd0 100644 --- a/src/main/kotlin/com/android/trisolarisserver/repo/RazorpayQrRequestRepo.kt +++ b/src/main/kotlin/com/android/trisolarisserver/repo/RazorpayQrRequestRepo.kt @@ -11,4 +11,9 @@ interface RazorpayQrRequestRepo : JpaRepository { currency: String, status: String ): RazorpayQrRequest? + + fun findTopByBookingIdAndStatusOrderByCreatedAtDesc( + bookingId: UUID, + status: String + ): RazorpayQrRequest? }