Add Razorpay active QR fetch and close
All checks were successful
build-and-deploy / build-deploy (push) Successful in 34s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 34s
This commit is contained in:
@@ -17,6 +17,7 @@ import org.springframework.http.HttpMethod
|
|||||||
import org.springframework.http.MediaType
|
import org.springframework.http.MediaType
|
||||||
import org.springframework.http.ResponseEntity
|
import org.springframework.http.ResponseEntity
|
||||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
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.PathVariable
|
||||||
import org.springframework.web.bind.annotation.PostMapping
|
import org.springframework.web.bind.annotation.PostMapping
|
||||||
import org.springframework.web.bind.annotation.RequestBody
|
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<String> {
|
private fun postJson(url: String, settings: com.android.trisolarisserver.models.payment.RazorpaySettings, json: String): ResponseEntity<String> {
|
||||||
val headers = HttpHeaders()
|
val headers = HttpHeaders()
|
||||||
headers.contentType = MediaType.APPLICATION_JSON
|
headers.contentType = MediaType.APPLICATION_JSON
|
||||||
|
|||||||
@@ -11,4 +11,9 @@ interface RazorpayQrRequestRepo : JpaRepository<RazorpayQrRequest, UUID> {
|
|||||||
currency: String,
|
currency: String,
|
||||||
status: String
|
status: String
|
||||||
): RazorpayQrRequest?
|
): RazorpayQrRequest?
|
||||||
|
|
||||||
|
fun findTopByBookingIdAndStatusOrderByCreatedAtDesc(
|
||||||
|
bookingId: UUID,
|
||||||
|
status: String
|
||||||
|
): RazorpayQrRequest?
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user