disable auto topup after fail

This commit is contained in:
dev.daminik00 2025-10-06 19:30:26 +02:00
parent a9136284bf
commit 523ed19e44

View File

@ -2,7 +2,11 @@
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { fetchChatMessages } from "@/entities/chats/actions";
import {
fetchChatMessages,
fetchMyChatSettings,
updateChatSettings,
} from "@/entities/chats/actions";
import type { IChatMessage } from "@/entities/chats/types";
import { useSingleCheckout } from "@/hooks/payment/useSingleCheckout";
import { useSocketEvent } from "@/hooks/socket/useSocketEvent";
@ -126,17 +130,33 @@ export const useChatSocket = (
const { handleSingleCheckout, isLoading: isAutoTopUpLoading } =
useSingleCheckout({
onSuccess: fetchBalance,
onError: () => {
onError: async () => {
// eslint-disable-next-line no-console
console.error("Auto top-up payment failed");
// Throttle retries: keep the lock for 30 seconds after an error
console.error("Auto top-up payment failed - disabling auto top-up");
// Disable auto top-up on payment failure
try {
// Fetch current settings to preserve topUpAmount
const settingsResponse = await fetchMyChatSettings();
if (settingsResponse.data?.settings) {
await updateChatSettings({
...settingsResponse.data.settings,
autoTopUp: false,
});
// eslint-disable-next-line no-console
console.info("Auto top-up disabled successfully after payment failure");
}
} catch (error) {
// eslint-disable-next-line no-console
console.error("Failed to disable auto top-up:", error);
}
// Release the lock immediately after disabling
autoTopUpInProgressRef.current = false;
if (autoTopUpUnlockTimerRef.current) {
clearTimeout(autoTopUpUnlockTimerRef.current);
}
autoTopUpUnlockTimerRef.current = setTimeout(() => {
autoTopUpInProgressRef.current = false;
autoTopUpUnlockTimerRef.current = null;
}, 30_000);
}
},
returnUrl: typeof window !== "undefined" ? window.location.href : "",
});