disable auto topup after fail
This commit is contained in:
parent
a9136284bf
commit
523ed19e44
@ -2,7 +2,11 @@
|
|||||||
|
|
||||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
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 type { IChatMessage } from "@/entities/chats/types";
|
||||||
import { useSingleCheckout } from "@/hooks/payment/useSingleCheckout";
|
import { useSingleCheckout } from "@/hooks/payment/useSingleCheckout";
|
||||||
import { useSocketEvent } from "@/hooks/socket/useSocketEvent";
|
import { useSocketEvent } from "@/hooks/socket/useSocketEvent";
|
||||||
@ -126,17 +130,33 @@ export const useChatSocket = (
|
|||||||
const { handleSingleCheckout, isLoading: isAutoTopUpLoading } =
|
const { handleSingleCheckout, isLoading: isAutoTopUpLoading } =
|
||||||
useSingleCheckout({
|
useSingleCheckout({
|
||||||
onSuccess: fetchBalance,
|
onSuccess: fetchBalance,
|
||||||
onError: () => {
|
onError: async () => {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.error("Auto top-up payment failed");
|
console.error("Auto top-up payment failed - disabling auto top-up");
|
||||||
// Throttle retries: keep the lock for 30 seconds after an error
|
|
||||||
|
// 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) {
|
if (autoTopUpUnlockTimerRef.current) {
|
||||||
clearTimeout(autoTopUpUnlockTimerRef.current);
|
clearTimeout(autoTopUpUnlockTimerRef.current);
|
||||||
}
|
|
||||||
autoTopUpUnlockTimerRef.current = setTimeout(() => {
|
|
||||||
autoTopUpInProgressRef.current = false;
|
|
||||||
autoTopUpUnlockTimerRef.current = null;
|
autoTopUpUnlockTimerRef.current = null;
|
||||||
}, 30_000);
|
}
|
||||||
},
|
},
|
||||||
returnUrl: typeof window !== "undefined" ? window.location.href : "",
|
returnUrl: typeof window !== "undefined" ? window.location.href : "",
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user