diff --git a/app/[lang]/(live)/(public)/hotelreservation/(payment-callback)/payment-callback/page.tsx b/app/[lang]/(live)/(public)/hotelreservation/(payment-callback)/payment-callback/page.tsx index 43374e32e..f462960ce 100644 --- a/app/[lang]/(live)/(public)/hotelreservation/(payment-callback)/payment-callback/page.tsx +++ b/app/[lang]/(live)/(public)/hotelreservation/(payment-callback)/payment-callback/page.tsx @@ -41,10 +41,14 @@ export default async function PaymentCallbackPage({ const bookingStatus = await serverClient().booking.status({ confirmationNumber, }) + + // TODO: how to handle errors for multiple rooms? + const error = bookingStatus.errors.find((e) => e.errorCode) + searchObject.set( "errorCode", - bookingStatus?.metadata?.errorCode - ? bookingStatus.metadata.errorCode.toString() + error + ? error.errorCode.toString() : PaymentErrorCodeEnum.Failed.toString() ) } catch (error) { diff --git a/components/HotelReservation/EnterDetails/Payment/PaymentClient.tsx b/components/HotelReservation/EnterDetails/Payment/PaymentClient.tsx index b66c26567..e3a58ba17 100644 --- a/components/HotelReservation/EnterDetails/Payment/PaymentClient.tsx +++ b/components/HotelReservation/EnterDetails/Payment/PaymentClient.tsx @@ -75,7 +75,7 @@ export default function PaymentClient({ (state) => state.actions.setIsSubmittingDisabled ) - const [confirmationNumber, setConfirmationNumber] = useState("") + const [bookingNumber, setBookingNumber] = useState("") const [isPollingForBookingStatus, setIsPollingForBookingStatus] = useState(false) @@ -105,13 +105,17 @@ export default function PaymentClient({ const initiateBooking = trpc.booking.create.useMutation({ onSuccess: (result) => { - if (result?.confirmationNumber) { - setConfirmationNumber(result.confirmationNumber) + if (result) { + setBookingNumber(result.id) - if (result.metadata?.priceChangedMetadata) { + const priceChange = result.rooms.find( + (r) => r.priceChangedMetadata + )?.priceChangedMetadata + + if (priceChange) { setPriceChangeData({ oldPrice: roomPrice.publicPrice, - newPrice: result.metadata.priceChangedMetadata.totalPrice, + newPrice: priceChange.totalPrice, }) } else { setIsPollingForBookingStatus(true) @@ -136,7 +140,7 @@ export default function PaymentClient({ const priceChange = trpc.booking.priceChange.useMutation({ onSuccess: (result) => { - if (result?.confirmationNumber) { + if (result?.id) { setIsPollingForBookingStatus(true) } else { toast.error(intl.formatMessage({ id: "payment.error.failed" })) @@ -152,7 +156,7 @@ export default function PaymentClient({ }) const bookingStatus = useHandleBookingStatus({ - confirmationNumber, + confirmationNumber: bookingNumber, expectedStatus: BookingStatusEnum.BookingCompleted, maxRetries, retryInterval, @@ -430,7 +434,9 @@ export default function PaymentClient({ : "" router.push(`${selectRate(lang)}${allSearchParams}`) }} - onAccept={() => priceChange.mutate({ confirmationNumber })} + onAccept={() => + priceChange.mutate({ confirmationNumber: bookingNumber }) + } /> ) : null} diff --git a/server/routers/booking/output.ts b/server/routers/booking/output.ts index 0260d4484..14006d07a 100644 --- a/server/routers/booking/output.ts +++ b/server/routers/booking/output.ts @@ -11,23 +11,36 @@ export const createBookingSchema = z .object({ data: z.object({ attributes: z.object({ - confirmationNumber: z.string(), - cancellationNumber: z.string().nullable(), reservationStatus: z.string(), paymentUrl: z.string().nullable(), - metadata: z - .object({ - errorCode: z.number().nullable().optional(), - errorMessage: z.string().nullable().optional(), - priceChangedMetadata: z - .object({ - roomPrice: z.number(), - totalPrice: z.number(), - }) - .nullable() - .optional(), - }) - .nullable(), + rooms: z + .array( + z.object({ + confirmationNumber: z.string(), + cancellationNumber: z.string().nullable(), + priceChangedMetadata: z + .object({ + roomPrice: z.number(), + totalPrice: z.number(), + }) + .nullable() + .optional(), + }) + ) + .default([]), + errors: z + .array( + z.object({ + confirmationNumber: z.string(), + errorCode: z.string(), + description: z.string(), + meta: z + .record(z.string(), z.union([z.string(), z.number()])) + .nullable() + .optional(), + }) + ) + .default([]), }), type: z.string(), id: z.string(), @@ -45,11 +58,10 @@ export const createBookingSchema = z id: d.data.id, links: d.data.links, type: d.data.type, - confirmationNumber: d.data.attributes.confirmationNumber, - cancellationNumber: d.data.attributes.cancellationNumber, reservationStatus: d.data.attributes.reservationStatus, paymentUrl: d.data.attributes.paymentUrl, - metadata: d.data.attributes.metadata, + rooms: d.data.attributes.rooms, + errors: d.data.attributes.errors, })) // QUERY