w-aura/src/api/resources/Payment.ts
Daniil Chemerkin c9eacda411 develop
2025-01-23 11:06:28 +00:00

84 lines
2.1 KiB
TypeScript

import routes from "@/routes";
import { getAuthHeaders, getBaseHeaders } from "../utils";
export interface Payload {
token: string;
}
export interface PayloadPost extends Payload {
productId: string;
placementId: string;
paywallId: string;
paymentToken?: string;
}
interface ResponsePostSuccess {
status: "payment_intent_created" | "paid" | unknown,
type: "setup" | "payment",
data: {
client_secret: string,
paymentIntentId: string,
return_url?: string,
public_key: string,
product: {
id: string,
name: string,
description?: string,
price: {
id: string,
unit_amount: number,
currency: "USD" | string
}
}
}
}
interface ResponsePostSinglePaymentSuccess {
payment: {
status: string;
invoiceId: string;
};
}
interface ResponsePostError {
status: string;
message: string;
}
export type ResponsePost = ResponsePostSuccess | ResponsePostSinglePaymentSuccess | ResponsePostError;
export const createRequestPost = ({ token, productId, placementId, paywallId, paymentToken }: PayloadPost): Request => {
const url = new URL(routes.server.makePayment());
const body = JSON.stringify({
productId,
placementId,
paywallId,
paymentToken
});
return new Request(url, { method: "POST", headers: getAuthHeaders(token), body });
};
export interface IPaymentConfigResponse {
status: "success" | string,
data: {
nmi: {
publicKey: string
}
}
}
export const getConfigRequest = (): Request => {
const url = new URL(routes.server.getPaymentConfig());
return new Request(url, { method: "GET", headers: getBaseHeaders() });
};
export interface IPaymentMethodsResponse {
status: "success" | "error",
message: string,
}
export const getMethodsRequest = ({ token }: Payload): Request => {
const url = new URL(routes.server.getPaymentMethods());
return new Request(url, { method: "GET", headers: getAuthHeaders(token) });
};