94 lines
2.3 KiB
TypeScript
94 lines
2.3 KiB
TypeScript
import makeRequest from "@api/makeRequest";
|
|
import { SendPaymentRequest, SendPaymentResponse } from "@root/model/wallet";
|
|
import { allTypesOfPurchases } from "@root/stores/allTypesOfPurchases";
|
|
import { parseAxiosError } from "@root/utils/parse-error";
|
|
|
|
const isStaging = (() => {
|
|
const host = window.location.hostname;
|
|
return host.includes("s") ? "s" : "";
|
|
})();
|
|
const isLocalhost = (() => {
|
|
const host = window.location.hostname;
|
|
return host.includes("localhost");
|
|
})();
|
|
|
|
const API_URL = `${process.env.REACT_APP_DOMAIN}/customer/v1.0.1`;
|
|
|
|
interface PaymentBody {
|
|
type: string;
|
|
amount: number;
|
|
}
|
|
|
|
export const sendPayment = async ({
|
|
userId,
|
|
body,
|
|
}: {
|
|
userId: string;
|
|
body: PaymentBody;
|
|
}): Promise<[SendPaymentResponse | null, string?]> => {
|
|
|
|
const action = allTypesOfPurchases(state => state.action);
|
|
const fromDomain = allTypesOfPurchases(state => state.fromDomain);
|
|
const backWay = allTypesOfPurchases(state => state.backWay);
|
|
|
|
let returnUrl = `https://${fromDomain}/${backWay}?&userid=${userId}&action=${action}`
|
|
|
|
console.log("returnUrl")
|
|
console.log(returnUrl)
|
|
|
|
const reqeustBody = {
|
|
currency: "RUB",
|
|
bankCard: {
|
|
number: "RUB",
|
|
expiryYear: "2021",
|
|
expiryMonth: "05",
|
|
csc: "05",
|
|
cardholder: "IVAN IVANOV",
|
|
},
|
|
phoneNumber: "79000000000",
|
|
login: "login_test",
|
|
returnUrl,
|
|
...body,
|
|
};
|
|
|
|
try {
|
|
const sendPaymentResponse = await makeRequest<
|
|
SendPaymentRequest,
|
|
SendPaymentResponse
|
|
>({
|
|
method: "POST",
|
|
url: `${API_URL}/wallet`,
|
|
body: reqeustBody,
|
|
contentType: true,
|
|
useToken: true,
|
|
withCredentials: false,
|
|
});
|
|
|
|
return [sendPaymentResponse];
|
|
} catch (nativeError) {
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
return [null, `Ошибка оплаты. ${error}`];
|
|
}
|
|
};
|
|
|
|
export const sendRSPayment = async (
|
|
money: number
|
|
): Promise<[string | null, string?]> => {
|
|
try {
|
|
const sendRSPaymentResponse = await makeRequest<{ money: number }, string>({
|
|
method: "POST",
|
|
url: `${API_URL}/wallet/rspay`,
|
|
body: { money },
|
|
useToken: true,
|
|
withCredentials: false,
|
|
});
|
|
|
|
return [sendRSPaymentResponse];
|
|
} catch (nativeError) {
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
return [null, `Ошибка оплаты. ${error}`];
|
|
}
|
|
};
|