front-hub/src/api/wallet.ts

85 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-05-28 13:38:01 +00:00
import makeRequest from "@api/makeRequest";
2024-02-14 14:22:12 +00:00
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-07-22 18:32:41 +00:00
const API_URL = `${process.env.REACT_APP_DOMAIN}/customer/v1.0.1`;
2023-07-07 13:53:08 +00:00
interface PaymentBody {
type: string;
amount: number;
}
2023-07-07 13:53:08 +00:00
2024-05-28 13:38:01 +00:00
export const sendPayment = async ({
2024-04-08 12:56:14 +00:00
userId,
body,
2024-03-30 17:51:04 +00:00
fromSquiz,
paymentPurpose,
2024-02-14 14:22:12 +00:00
}: {
2024-04-08 12:56:14 +00:00
userId: string;
body: PaymentBody;
2024-02-14 14:22:12 +00:00
fromSquiz: boolean;
2024-03-30 17:51:04 +00:00
paymentPurpose: "paycart" | "replenishwallet";
2024-05-28 13:38:01 +00:00
}): Promise<[SendPaymentResponse | null, string?]> => {
const reqeustBody = {
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"
}&purpose=${paymentPurpose}&userid=${userId}`,
...body,
};
2024-02-14 14:22:12 +00:00
try {
const sendPaymentResponse = await makeRequest<
2023-08-30 14:02:56 +00:00
SendPaymentRequest,
SendPaymentResponse
>({
2024-02-14 14:22:12 +00:00
method: "POST",
2024-05-28 13:38:01 +00:00
url: `${API_URL}/wallet`,
body: reqeustBody,
contentType: true,
2024-02-14 14:22:12 +00:00
useToken: true,
withCredentials: false,
});
return [sendPaymentResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Ошибка оплаты. ${error}`];
}
2024-05-28 13:38:01 +00:00
};
2024-02-14 14:22:12 +00:00
2024-05-29 11:58:54 +00:00
export const sendRSPayment = async (
money: number
): Promise<[string | null, string?]> => {
2024-02-14 14:22:12 +00:00
try {
2024-05-29 11:58:54 +00:00
const sendRSPaymentResponse = await makeRequest<{ money: number }, string>({
2024-02-14 14:22:12 +00:00
method: "POST",
2024-05-28 13:38:01 +00:00
url: `${API_URL}/wallet/rspay`,
2024-05-29 11:58:54 +00:00
body: { money },
2024-05-28 13:38:01 +00:00
useToken: true,
2024-02-14 14:22:12 +00:00
withCredentials: false,
});
2024-05-29 11:58:54 +00:00
return [sendRSPaymentResponse];
2024-02-14 14:22:12 +00:00
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
2024-05-29 11:58:54 +00:00
return [null, `Ошибка оплаты. ${error}`];
2024-02-14 14:22:12 +00:00
}
};