mirror of
https://github.com/chillpadclub/bedolaga-cabinet.git
synced 2026-07-28 09:33:46 +00:00
refactor: extract shared payment status sets to utility module
- Move PAID_STATUSES/FAILED_STATUSES from Balance.tsx and TopUpResult.tsx to src/utils/paymentStatus.ts - Eliminates code duplication between the two pages
This commit is contained in:
@@ -13,6 +13,7 @@ import type { PaginatedResponse, Transaction } from '../types';
|
|||||||
import { Card } from '@/components/data-display/Card';
|
import { Card } from '@/components/data-display/Card';
|
||||||
import { Button } from '@/components/primitives/Button';
|
import { Button } from '@/components/primitives/Button';
|
||||||
import { staggerContainer, staggerItem } from '@/components/motion/transitions';
|
import { staggerContainer, staggerItem } from '@/components/motion/transitions';
|
||||||
|
import { isPaidStatus, isFailedStatus } from '../utils/paymentStatus';
|
||||||
|
|
||||||
// Icons
|
// Icons
|
||||||
const ChevronDownIcon = ({ className = 'h-5 w-5' }: { className?: string }) => (
|
const ChevronDownIcon = ({ className = 'h-5 w-5' }: { className?: string }) => (
|
||||||
@@ -64,32 +65,10 @@ export default function Balance() {
|
|||||||
if (paymentHandledRef.current) return;
|
if (paymentHandledRef.current) return;
|
||||||
|
|
||||||
const paymentStatus = searchParams.get('payment') || searchParams.get('status');
|
const paymentStatus = searchParams.get('payment') || searchParams.get('status');
|
||||||
const paidSet = new Set([
|
|
||||||
'succeeded',
|
|
||||||
'success',
|
|
||||||
'paid',
|
|
||||||
'paid_over',
|
|
||||||
'overpaid',
|
|
||||||
'completed',
|
|
||||||
'confirmed',
|
|
||||||
'closed',
|
|
||||||
]);
|
|
||||||
const failedSet = new Set([
|
|
||||||
'fail',
|
|
||||||
'failed',
|
|
||||||
'error',
|
|
||||||
'canceled',
|
|
||||||
'cancelled',
|
|
||||||
'declined',
|
|
||||||
'expired',
|
|
||||||
'cancel',
|
|
||||||
'system_fail',
|
|
||||||
'refund_paid',
|
|
||||||
]);
|
|
||||||
|
|
||||||
const normalised = paymentStatus?.toLowerCase() ?? '';
|
const normalised = paymentStatus?.toLowerCase() ?? '';
|
||||||
const isSuccess = paidSet.has(normalised) || searchParams.get('success') === 'true';
|
const isSuccess = isPaidStatus(normalised) || searchParams.get('success') === 'true';
|
||||||
const isFailed = failedSet.has(normalised);
|
const isFailed = isFailedStatus(normalised);
|
||||||
|
|
||||||
if (isSuccess) {
|
if (isSuccess) {
|
||||||
paymentHandledRef.current = true;
|
paymentHandledRef.current = true;
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import { Spinner } from '@/components/ui/Spinner';
|
|||||||
import { AnimatedCheckmark } from '@/components/ui/AnimatedCheckmark';
|
import { AnimatedCheckmark } from '@/components/ui/AnimatedCheckmark';
|
||||||
import { AnimatedCrossmark } from '@/components/ui/AnimatedCrossmark';
|
import { AnimatedCrossmark } from '@/components/ui/AnimatedCrossmark';
|
||||||
import { loadTopUpPendingInfo, clearTopUpPendingInfo } from '../utils/topUpStorage';
|
import { loadTopUpPendingInfo, clearTopUpPendingInfo } from '../utils/topUpStorage';
|
||||||
|
import { isPaidStatus, isFailedStatus } from '../utils/paymentStatus';
|
||||||
|
|
||||||
// ── Constants ────────────────────────────────────────────────
|
// ── Constants ────────────────────────────────────────────────
|
||||||
const MAX_POLL_MS = 10 * 60 * 1000; // 10 minutes
|
const MAX_POLL_MS = 10 * 60 * 1000; // 10 minutes
|
||||||
@@ -177,39 +178,6 @@ function TimeoutState({ onRetry, onGoBack }: { onRetry: () => void; onGoBack: ()
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Determine paid status from provider-specific status strings ──
|
|
||||||
const PAID_STATUSES = new Set([
|
|
||||||
'succeeded',
|
|
||||||
'success',
|
|
||||||
'paid',
|
|
||||||
'paid_over',
|
|
||||||
'overpaid',
|
|
||||||
'completed',
|
|
||||||
'confirmed',
|
|
||||||
'closed',
|
|
||||||
]);
|
|
||||||
|
|
||||||
const FAILED_STATUSES = new Set([
|
|
||||||
'fail',
|
|
||||||
'failed',
|
|
||||||
'error',
|
|
||||||
'canceled',
|
|
||||||
'cancelled',
|
|
||||||
'declined',
|
|
||||||
'expired',
|
|
||||||
'cancel',
|
|
||||||
'system_fail',
|
|
||||||
'refund_paid',
|
|
||||||
]);
|
|
||||||
|
|
||||||
function isPaidStatus(status: string): boolean {
|
|
||||||
return PAID_STATUSES.has(status.toLowerCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
function isFailedStatus(status: string): boolean {
|
|
||||||
return FAILED_STATUSES.has(status.toLowerCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── Main Component ───────────────────────────────────────────
|
// ── Main Component ───────────────────────────────────────────
|
||||||
|
|
||||||
export default function TopUpResult() {
|
export default function TopUpResult() {
|
||||||
|
|||||||
31
src/utils/paymentStatus.ts
Normal file
31
src/utils/paymentStatus.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
export const PAID_STATUSES = new Set([
|
||||||
|
'succeeded',
|
||||||
|
'success',
|
||||||
|
'paid',
|
||||||
|
'paid_over',
|
||||||
|
'overpaid',
|
||||||
|
'completed',
|
||||||
|
'confirmed',
|
||||||
|
'closed',
|
||||||
|
]);
|
||||||
|
|
||||||
|
export const FAILED_STATUSES = new Set([
|
||||||
|
'fail',
|
||||||
|
'failed',
|
||||||
|
'error',
|
||||||
|
'canceled',
|
||||||
|
'cancelled',
|
||||||
|
'declined',
|
||||||
|
'expired',
|
||||||
|
'cancel',
|
||||||
|
'system_fail',
|
||||||
|
'refund_paid',
|
||||||
|
]);
|
||||||
|
|
||||||
|
export function isPaidStatus(status: string): boolean {
|
||||||
|
return PAID_STATUSES.has(status.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isFailedStatus(status: string): boolean {
|
||||||
|
return FAILED_STATUSES.has(status.toLowerCase());
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user