Merge branch 'dev' into 'staging'
fix: afterpay See merge request frontend/squiz!229
This commit is contained in:
commit
4fcd7babe5
@ -41,6 +41,7 @@ import { isAxiosError } from "axios";
|
||||
import { useEffect, useLayoutEffect, useRef } from "react";
|
||||
import RecoverPassword from "./pages/auth/RecoverPassword";
|
||||
import OutdatedLink from "./pages/auth/OutdatedLink";
|
||||
import { useAfterpay } from "@utils/hooks/useAfterpay";
|
||||
|
||||
import type { OriginalUserAccount } from "@root/user";
|
||||
|
||||
@ -192,6 +193,8 @@ export default function App() {
|
||||
/>
|
||||
);
|
||||
|
||||
useAfterpay();
|
||||
|
||||
return (
|
||||
<>
|
||||
<ContactFormModal />
|
||||
|
||||
22
src/api/cart.ts
Normal file
22
src/api/cart.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { UserAccount, makeRequest } from "@frontend/kitui";
|
||||
import { AxiosError } from "axios";
|
||||
|
||||
import { parseAxiosError } from "@utils/parse-error";
|
||||
|
||||
const apiUrl = process.env.REACT_APP_DOMAIN + "/customer";
|
||||
|
||||
export async function payCart(): Promise<[UserAccount | null, string?]> {
|
||||
try {
|
||||
const payCartResponse = await makeRequest<never, UserAccount>({
|
||||
url: apiUrl + "/cart/pay",
|
||||
method: "POST",
|
||||
useToken: true,
|
||||
});
|
||||
|
||||
return [payCartResponse];
|
||||
} catch (nativeError) {
|
||||
const [error] = parseAxiosError(nativeError);
|
||||
|
||||
return [null, `Не удалось оплатить товар из корзины. ${error}`];
|
||||
}
|
||||
}
|
||||
@ -141,8 +141,7 @@ function TariffPage() {
|
||||
var link = document.createElement("a");
|
||||
link.href = `https://${isTestServer ? "s" : ""}hub.pena.digital/quizpayment?action=squizpay&dif=${
|
||||
(price - Number(user.wallet.cash)) * 100
|
||||
}&data=${token}&userid=${userId}
|
||||
`;
|
||||
}&data=${token}&userid=${userId}`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
}
|
||||
|
||||
53
src/utils/hooks/useAfterpay.ts
Normal file
53
src/utils/hooks/useAfterpay.ts
Normal file
@ -0,0 +1,53 @@
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
import { payCart } from "@api/cart";
|
||||
|
||||
const MINUTE = 1000 * 60;
|
||||
|
||||
export const useAfterpay = () => {
|
||||
const navigate = useNavigate();
|
||||
|
||||
const checkPayment = () => {
|
||||
const redirectUrl = new URL(window.location.href);
|
||||
redirectUrl.searchParams.set("afterpay", "false");
|
||||
navigate(redirectUrl);
|
||||
console.log("pay");
|
||||
|
||||
const payCartPendingRequestDeadline = localStorage.getItem(
|
||||
"payCartPendingRequestDeadline",
|
||||
);
|
||||
const deadline = payCartPendingRequestDeadline
|
||||
? Number(payCartPendingRequestDeadline)
|
||||
: Date.now() + 20 * MINUTE;
|
||||
|
||||
localStorage.setItem("payCartPendingRequestDeadline", deadline.toString());
|
||||
|
||||
let tryCount = 0;
|
||||
tryPayCart();
|
||||
|
||||
async function tryPayCart() {
|
||||
tryCount += 1;
|
||||
|
||||
const [, payCartError] = await payCart();
|
||||
|
||||
if (!payCartError || Date.now() > deadline) {
|
||||
localStorage.removeItem("payCartPendingRequestDeadline");
|
||||
return;
|
||||
}
|
||||
|
||||
setTimeout(tryPayCart, tryCount > 5 ? MINUTE / 2 : MINUTE / 6);
|
||||
}
|
||||
};
|
||||
|
||||
const checkParams = () => {
|
||||
const afterpay = new URLSearchParams(window.location.search).get(
|
||||
"afterpay",
|
||||
);
|
||||
|
||||
if (afterpay) {
|
||||
checkPayment();
|
||||
}
|
||||
};
|
||||
|
||||
setInterval(checkParams, 5000);
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user