diff --git a/src/App.tsx b/src/App.tsx
index 00332eb7..e3198f44 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -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 (
<>
diff --git a/src/api/cart.ts b/src/api/cart.ts
new file mode 100644
index 00000000..740e8507
--- /dev/null
+++ b/src/api/cart.ts
@@ -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({
+ url: apiUrl + "/cart/pay",
+ method: "POST",
+ useToken: true,
+ });
+
+ return [payCartResponse];
+ } catch (nativeError) {
+ const [error] = parseAxiosError(nativeError);
+
+ return [null, `Не удалось оплатить товар из корзины. ${error}`];
+ }
+}
diff --git a/src/pages/Tariffs/Tariffs.tsx b/src/pages/Tariffs/Tariffs.tsx
index 7b531196..e771f1b8 100644
--- a/src/pages/Tariffs/Tariffs.tsx
+++ b/src/pages/Tariffs/Tariffs.tsx
@@ -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();
}
diff --git a/src/utils/hooks/useAfterpay.ts b/src/utils/hooks/useAfterpay.ts
new file mode 100644
index 00000000..b368b311
--- /dev/null
+++ b/src/utils/hooks/useAfterpay.ts
@@ -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);
+};