failed tests btw
This commit is contained in:
@@ -51,8 +51,10 @@ export function AuthProvider({ children }) {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
const status = err instanceof ApiError ? err.status : err?.status;
|
const status = ApiError && err instanceof ApiError ? err.status : err?.status;
|
||||||
if (status !== 401) {
|
const message = typeof err?.message === "string" ? err.message : "";
|
||||||
|
const isUnauthorized = status === 401 || message.includes("401");
|
||||||
|
if (!isUnauthorized) {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,13 +26,19 @@ export function usePaymentForm(bookingId = "", token = "") {
|
|||||||
const [status, setStatus] = useState("idle");
|
const [status, setStatus] = useState("idle");
|
||||||
const [result, setResult] = useState(null);
|
const [result, setResult] = useState(null);
|
||||||
const [error, setError] = useState("");
|
const [error, setError] = useState("");
|
||||||
const [idempotencyKey, setIdempotencyKey] = useState(generateIdempotencyKey);
|
const idempotencyRef = useRef(null);
|
||||||
|
if (!idempotencyRef.current) {
|
||||||
|
idempotencyRef.current = generateIdempotencyKey();
|
||||||
|
}
|
||||||
|
const [idempotencyKey, setIdempotencyKey] = useState(idempotencyRef.current);
|
||||||
const lastBookingId = useRef(bookingId);
|
const lastBookingId = useRef(bookingId);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (lastBookingId.current !== bookingIdInput) {
|
if (lastBookingId.current !== bookingIdInput) {
|
||||||
lastBookingId.current = bookingIdInput;
|
lastBookingId.current = bookingIdInput;
|
||||||
setIdempotencyKey(generateIdempotencyKey());
|
const nextKey = generateIdempotencyKey();
|
||||||
|
idempotencyRef.current = nextKey;
|
||||||
|
setIdempotencyKey(nextKey);
|
||||||
}
|
}
|
||||||
}, [bookingIdInput]);
|
}, [bookingIdInput]);
|
||||||
|
|
||||||
@@ -83,7 +89,7 @@ export function usePaymentForm(bookingId = "", token = "") {
|
|||||||
const payload = {
|
const payload = {
|
||||||
booking_id: Number(bookingIdInput),
|
booking_id: Number(bookingIdInput),
|
||||||
provider: "moyasar",
|
provider: "moyasar",
|
||||||
idempotency_key: idempotencyKey,
|
idempotency_key: idempotencyRef.current,
|
||||||
source,
|
source,
|
||||||
};
|
};
|
||||||
if (callbackUrl) {
|
if (callbackUrl) {
|
||||||
@@ -103,7 +109,7 @@ export function usePaymentForm(bookingId = "", token = "") {
|
|||||||
window.location.assign(data.redirect_url);
|
window.location.assign(data.redirect_url);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof ApiError && err.body) {
|
if (ApiError && err instanceof ApiError && err.body) {
|
||||||
const fieldMessage = getFirstFieldError(err.body);
|
const fieldMessage = getFirstFieldError(err.body);
|
||||||
if (fieldMessage) {
|
if (fieldMessage) {
|
||||||
console.warn("Payment validation error", err.body);
|
console.warn("Payment validation error", err.body);
|
||||||
@@ -112,8 +118,10 @@ export function usePaymentForm(bookingId = "", token = "") {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const message =
|
||||||
|
(typeof err?.message === "string" && err.message) || String(err) || "";
|
||||||
setStatus("error");
|
setStatus("error");
|
||||||
setError(err.message || t("payment.errors.generic"));
|
setError(message || t("payment.errors.generic"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user