refactor(payment): centralize route lifecycle
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import type { PayChannel } from "@/data/dto/payment";
|
||||
|
||||
export type PaymentSearchParamValue = string | string[] | undefined;
|
||||
export type PaymentSearchParams = Record<string, PaymentSearchParamValue>;
|
||||
|
||||
export interface PaymentReturnSearchParams {
|
||||
initialPayChannel: PayChannel | null;
|
||||
shouldResumePendingOrder: boolean;
|
||||
}
|
||||
|
||||
export function getFirstPaymentSearchParam(
|
||||
value: PaymentSearchParamValue,
|
||||
): string | null {
|
||||
return Array.isArray(value) ? (value[0] ?? null) : (value ?? null);
|
||||
}
|
||||
|
||||
export function parsePaymentPayChannel(
|
||||
value: PaymentSearchParamValue,
|
||||
): PayChannel | null {
|
||||
const channel = getFirstPaymentSearchParam(value);
|
||||
return channel === "ezpay" || channel === "stripe" ? channel : null;
|
||||
}
|
||||
|
||||
export function parsePaymentReturnSearchParams(
|
||||
searchParams: PaymentSearchParams,
|
||||
): PaymentReturnSearchParams {
|
||||
return {
|
||||
initialPayChannel: parsePaymentPayChannel(searchParams.payChannel),
|
||||
shouldResumePendingOrder:
|
||||
getFirstPaymentSearchParam(searchParams.paymentReturn) === "1",
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user