front-hub/src/api/wallet.ts

75 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-02-14 14:22:12 +00:00
import { makeRequest } from "@frontend/kitui";
import { SendPaymentRequest, SendPaymentResponse } from "@root/model/wallet";
import { parseAxiosError } from "@root/utils/parse-error";
2023-07-07 13:53:08 +00:00
const isStaging = (() => {
const host = window.location.hostname;
return host.includes("s") ? "s" : "";
})();
2024-02-14 14:22:12 +00:00
const apiUrl = process.env.REACT_APP_DOMAIN + "/customer";
2023-07-07 13:53:08 +00:00
interface PaymentBody {
type: string;
amount: number;
}
2023-07-07 13:53:08 +00:00
2024-02-14 14:22:12 +00:00
export async function sendPayment({
body,
fromSquiz
2024-02-14 14:22:12 +00:00
}: {
body: PaymentBody;
2024-02-14 14:22:12 +00:00
fromSquiz: boolean;
}): Promise<[SendPaymentResponse | null, string?]> {
try {
const sendPaymentResponse = await makeRequest<
2023-08-30 14:02:56 +00:00
SendPaymentRequest,
SendPaymentResponse
>({
2024-02-14 14:22:12 +00:00
url: apiUrl + "/wallet",
contentType: true,
method: "POST",
useToken: true,
withCredentials: false,
body: {
currency: "RUB",
bankCard: {
number: "RUB",
expiryYear: "2021",
expiryMonth: "05",
csc: "05",
cardholder: "IVAN IVANOV",
},
phoneNumber: "79000000000",
login: "login_test",
returnUrl: `https://${isStaging}hub.pena.digital/afterpay?from=${fromSquiz ? "quiz" : "hub"}`,
...body
}
2024-02-14 14:22:12 +00:00
});
return [sendPaymentResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Ошибка оплаты. ${error}`];
}
2023-07-22 10:38:45 +00:00
}
2024-02-14 14:22:12 +00:00
2024-02-23 00:02:22 +00:00
export const sendRSPayment = async (money: number): Promise<string | null> => {
2024-02-14 14:22:12 +00:00
try {
await makeRequest<unknown, string>({
2024-02-14 14:22:12 +00:00
url: apiUrl + "/wallet/rspay",
method: "POST",
useToken: true,
body: { money: money },
2024-02-14 14:22:12 +00:00
withCredentials: false,
});
return null;
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return `Ошибка оплаты. ${error}`;
}
};