Handle payment fail and cancel events
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
|||||||
import { serverClient } from "@/lib/trpc/server"
|
import { serverClient } from "@/lib/trpc/server"
|
||||||
|
|
||||||
import PaymentCallback from "@/components/HotelReservation/EnterDetails/Payment/PaymentCallback"
|
import PaymentCallback from "@/components/HotelReservation/EnterDetails/Payment/PaymentCallback"
|
||||||
|
import { trackPaymentEvent } from "@/utils/tracking"
|
||||||
|
|
||||||
import type { LangParams, PageArgs } from "@/types/params"
|
import type { LangParams, PageArgs } from "@/types/params"
|
||||||
|
|
||||||
@@ -47,15 +48,31 @@ export default async function PaymentCallbackPage({
|
|||||||
? bookingStatus.metadata.errorCode.toString()
|
? bookingStatus.metadata.errorCode.toString()
|
||||||
: PaymentErrorCodeEnum.Failed.toString()
|
: PaymentErrorCodeEnum.Failed.toString()
|
||||||
)
|
)
|
||||||
|
trackPaymentEvent({
|
||||||
|
event: "paymentFail",
|
||||||
|
hotelId: searchObject.get("hotel"),
|
||||||
|
errorMessage:
|
||||||
|
bookingStatus?.metadata?.errorMessage ??
|
||||||
|
`No error message found for booking ${confirmationNumber}, status: ${status}`,
|
||||||
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(
|
console.error(
|
||||||
`[payment-callback] failed to get booking status for ${confirmationNumber}, status: ${status}`
|
`[payment-callback] failed to get booking status for ${confirmationNumber}, status: ${status}`
|
||||||
)
|
)
|
||||||
if (status === "cancel") {
|
if (status === "cancel") {
|
||||||
searchObject.set("errorCode", PaymentErrorCodeEnum.Cancelled.toString())
|
searchObject.set("errorCode", PaymentErrorCodeEnum.Cancelled.toString())
|
||||||
|
trackPaymentEvent({
|
||||||
|
event: "paymentCancel",
|
||||||
|
hotelId: searchObject.get("hotel"),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
if (status === "error") {
|
if (status === "error") {
|
||||||
searchObject.set("errorCode", PaymentErrorCodeEnum.Failed.toString())
|
searchObject.set("errorCode", PaymentErrorCodeEnum.Failed.toString())
|
||||||
|
trackPaymentEvent({
|
||||||
|
event: "paymentFail",
|
||||||
|
hotelId: searchObject.get("hotel"),
|
||||||
|
errorMessage: `Failed to get booking status for ${confirmationNumber}, status: ${status}`,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,20 +118,12 @@ export default function PaymentClient({
|
|||||||
setIsPollingForBookingStatus(true)
|
setIsPollingForBookingStatus(true)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
toast.error(
|
handlePaymentError("No confirmation number")
|
||||||
intl.formatMessage({
|
|
||||||
id: "payment.error.failed",
|
|
||||||
})
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
console.error("Error", error)
|
console.error("Error", error)
|
||||||
toast.error(
|
handlePaymentError(error.message)
|
||||||
intl.formatMessage({
|
|
||||||
id: "payment.error.failed",
|
|
||||||
})
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -140,7 +132,7 @@ export default function PaymentClient({
|
|||||||
if (result?.confirmationNumber) {
|
if (result?.confirmationNumber) {
|
||||||
setIsPollingForBookingStatus(true)
|
setIsPollingForBookingStatus(true)
|
||||||
} else {
|
} else {
|
||||||
toast.error(intl.formatMessage({ id: "payment.error.failed" }))
|
handlePaymentError("No confirmation number")
|
||||||
}
|
}
|
||||||
|
|
||||||
setPriceChangeData(null)
|
setPriceChangeData(null)
|
||||||
@@ -148,7 +140,7 @@ export default function PaymentClient({
|
|||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
console.error("Error", error)
|
console.error("Error", error)
|
||||||
setPriceChangeData(null)
|
setPriceChangeData(null)
|
||||||
toast.error(intl.formatMessage({ id: "payment.error.failed" }))
|
handlePaymentError(error.message)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -160,17 +152,38 @@ export default function PaymentClient({
|
|||||||
enabled: isPollingForBookingStatus,
|
enabled: isPollingForBookingStatus,
|
||||||
})
|
})
|
||||||
|
|
||||||
useEffect(() => {
|
const handlePaymentError = useCallback(
|
||||||
if (bookingStatus?.data?.paymentUrl) {
|
(errorMessage: string) => {
|
||||||
router.push(bookingStatus.data.paymentUrl)
|
|
||||||
} else if (bookingStatus.isTimeout) {
|
|
||||||
toast.error(
|
toast.error(
|
||||||
intl.formatMessage({
|
intl.formatMessage({
|
||||||
id: "payment.error.failed",
|
id: "payment.error.failed",
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
const currentPaymentMethod = methods.getValues("paymentMethod")
|
||||||
|
const smsEnable = methods.getValues("smsConfirmation")
|
||||||
|
const isSavedCreditCard = savedCreditCards?.some(
|
||||||
|
(card) => card.id === currentPaymentMethod
|
||||||
|
)
|
||||||
|
|
||||||
|
trackPaymentEvent({
|
||||||
|
event: "paymentFail",
|
||||||
|
hotelId: hotel,
|
||||||
|
method: currentPaymentMethod,
|
||||||
|
isSavedCreditCard: isSavedCreditCard,
|
||||||
|
smsEnable: smsEnable,
|
||||||
|
errorMessage,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
[intl, methods, savedCreditCards, hotel]
|
||||||
|
)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (bookingStatus?.data?.paymentUrl) {
|
||||||
|
router.push(bookingStatus.data.paymentUrl)
|
||||||
|
} else if (bookingStatus.isTimeout) {
|
||||||
|
handlePaymentError("Timeout")
|
||||||
}
|
}
|
||||||
}, [bookingStatus, router, intl])
|
}, [bookingStatus, router, intl, handlePaymentError])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setIsSubmittingDisabled(
|
setIsSubmittingDisabled(
|
||||||
|
|||||||
@@ -107,10 +107,10 @@ export type TrackingSDKData = TrackingSDKPageData & {
|
|||||||
|
|
||||||
export type PaymentEvent = {
|
export type PaymentEvent = {
|
||||||
event: string
|
event: string
|
||||||
hotelId: string
|
hotelId: string | null
|
||||||
method: string
|
method?: string
|
||||||
isSavedCreditCard: boolean
|
isSavedCreditCard?: boolean
|
||||||
smsEnable: boolean
|
smsEnable?: boolean
|
||||||
errorMessage?: string
|
errorMessage?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user