88 lines
2.4 KiB
TypeScript
88 lines
2.4 KiB
TypeScript
import { redirect } from "next/navigation"
|
|
|
|
import {
|
|
BOOKING_CONFIRMATION_NUMBER,
|
|
PaymentErrorCodeEnum,
|
|
} from "@/constants/booking"
|
|
import {
|
|
bookingConfirmation,
|
|
payment,
|
|
} from "@/constants/routes/hotelReservation"
|
|
import { serverClient } from "@/lib/trpc/server"
|
|
|
|
import PaymentCallback from "@/components/HotelReservation/EnterDetails/Payment/PaymentCallback"
|
|
import { trackPaymentEvent } from "@/utils/tracking"
|
|
|
|
import type { LangParams, PageArgs } from "@/types/params"
|
|
|
|
export default async function PaymentCallbackPage({
|
|
params,
|
|
searchParams,
|
|
}: PageArgs<
|
|
LangParams,
|
|
{
|
|
status: "error" | "success" | "cancel"
|
|
confirmationNumber?: string
|
|
hotel?: string
|
|
}
|
|
>) {
|
|
console.log(`[payment-callback] callback started`)
|
|
const lang = params.lang
|
|
const status = searchParams.status
|
|
const confirmationNumber = searchParams.confirmationNumber
|
|
|
|
if (status === "success" && confirmationNumber) {
|
|
const confirmationUrl = `${bookingConfirmation(lang)}?${BOOKING_CONFIRMATION_NUMBER}=${confirmationNumber}`
|
|
|
|
console.log(`[payment-callback] redirecting to: ${confirmationUrl}`)
|
|
redirect(confirmationUrl)
|
|
}
|
|
|
|
const returnUrl = payment(lang)
|
|
const searchObject = new URLSearchParams()
|
|
|
|
let errorMessage = undefined
|
|
|
|
if (confirmationNumber) {
|
|
try {
|
|
const bookingStatus = await serverClient().booking.status({
|
|
confirmationNumber,
|
|
})
|
|
|
|
// TODO: how to handle errors for multiple rooms?
|
|
const error = bookingStatus.errors.find((e) => e.errorCode)
|
|
|
|
errorMessage =
|
|
error?.description ??
|
|
`No error message found for booking ${confirmationNumber}, status: ${status}`
|
|
|
|
searchObject.set(
|
|
"errorCode",
|
|
error
|
|
? error.errorCode.toString()
|
|
: PaymentErrorCodeEnum.Failed.toString()
|
|
)
|
|
} catch (error) {
|
|
console.error(
|
|
`[payment-callback] failed to get booking status for ${confirmationNumber}, status: ${status}`
|
|
)
|
|
if (status === "cancel") {
|
|
searchObject.set("errorCode", PaymentErrorCodeEnum.Cancelled.toString())
|
|
}
|
|
if (status === "error") {
|
|
searchObject.set("errorCode", PaymentErrorCodeEnum.Failed.toString())
|
|
errorMessage = `Failed to get booking status for ${confirmationNumber}, status: ${status}`
|
|
}
|
|
}
|
|
}
|
|
|
|
return (
|
|
<PaymentCallback
|
|
returnUrl={returnUrl.toString()}
|
|
searchObject={searchObject}
|
|
status={status}
|
|
errorMessage={errorMessage}
|
|
/>
|
|
)
|
|
}
|