148 lines
3.3 KiB
TypeScript
148 lines
3.3 KiB
TypeScript
import routes from "@/routes";
|
|
import { AuthPayload } from "../types";
|
|
import { getAuthHeaders } from "../utils";
|
|
|
|
export interface GetPayload extends AuthPayload {
|
|
id: string;
|
|
}
|
|
|
|
export interface ChargebeeReceiptPayload extends AuthPayload {
|
|
itemPriceId: string;
|
|
gwToken: string;
|
|
referenceId?: string;
|
|
}
|
|
|
|
export interface AppleReceiptPayload extends AuthPayload {
|
|
receiptData: string;
|
|
autorenewable?: boolean;
|
|
sandbox?: boolean;
|
|
}
|
|
|
|
export interface StripeReceiptPayload extends AuthPayload {
|
|
way: "stripe";
|
|
subscription_receipt: {
|
|
sub_plan_id: string;
|
|
};
|
|
}
|
|
|
|
export interface PayPalReceiptPayload extends AuthPayload {
|
|
subscription_receipt: {
|
|
sub_plan_id: string;
|
|
};
|
|
way: "paypal";
|
|
}
|
|
|
|
export type Payload =
|
|
| ChargebeeReceiptPayload
|
|
| AppleReceiptPayload
|
|
| StripeReceiptPayload
|
|
| PayPalReceiptPayload;
|
|
|
|
export interface Response {
|
|
subscription_receipt: SubscriptionReceipt;
|
|
}
|
|
|
|
export interface SubscriptionReceipt {
|
|
id: string;
|
|
user_id: number;
|
|
status: number;
|
|
expires_at: null | string;
|
|
requested_at: string;
|
|
created_at: string;
|
|
data: {
|
|
input: {
|
|
subscription_items: [
|
|
{
|
|
item_price_id: string;
|
|
}
|
|
];
|
|
payment_intent: {
|
|
gw_token: string;
|
|
gateway_account_id: string;
|
|
};
|
|
};
|
|
client_secret: string;
|
|
app_bundle_id: string;
|
|
autorenewable: boolean;
|
|
error: string;
|
|
links?: IPayPalLink[];
|
|
};
|
|
}
|
|
|
|
interface IPayPalLink {
|
|
href: string;
|
|
rel: "approve" | "edit" | "self";
|
|
method: "GET" | "PATCH";
|
|
}
|
|
|
|
function createRequest({
|
|
token,
|
|
itemPriceId,
|
|
gwToken,
|
|
referenceId,
|
|
}: ChargebeeReceiptPayload): Request;
|
|
function createRequest({
|
|
token,
|
|
receiptData,
|
|
autorenewable,
|
|
sandbox,
|
|
}: AppleReceiptPayload): Request;
|
|
function createRequest({ token }: StripeReceiptPayload): Request;
|
|
function createRequest(payload: Payload): Request;
|
|
function createRequest(payload: Payload): Request {
|
|
const url = new URL(routes.server.subscriptionReceipts());
|
|
const data = getDataPayload(payload);
|
|
const body = JSON.stringify(data);
|
|
return new Request(url, {
|
|
method: "POST",
|
|
headers: getAuthHeaders(payload.token),
|
|
body,
|
|
});
|
|
}
|
|
|
|
function getDataPayload(payload: Payload) {
|
|
if ("itemPriceId" in payload && "gwToken" in payload) {
|
|
return {
|
|
way: "chargebee",
|
|
subscription_receipt: {
|
|
item_price_id: payload.itemPriceId,
|
|
gw_token: payload.gwToken,
|
|
reference_id: payload.referenceId,
|
|
},
|
|
};
|
|
}
|
|
if ("receiptData" in payload) {
|
|
return {
|
|
way: "apple",
|
|
subscription_receipt: {
|
|
receipt_data: payload.receiptData,
|
|
autorenewable: payload.autorenewable,
|
|
sandbox: payload.sandbox,
|
|
},
|
|
};
|
|
}
|
|
if ("way" in payload && payload.way === "paypal") {
|
|
return {
|
|
way: "paypal",
|
|
subscription_receipt: {
|
|
sub_plan_id: payload.subscription_receipt.sub_plan_id,
|
|
},
|
|
};
|
|
}
|
|
if ("way" in payload && payload.way === "stripe") {
|
|
return {
|
|
way: "stripe",
|
|
subscription_receipt: {
|
|
sub_plan_id: payload.subscription_receipt.sub_plan_id,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
function createGetRequest({ id, token }: GetPayload): Request {
|
|
const url = new URL(routes.server.subscriptionReceipt(id));
|
|
return new Request(url, { method: "GET", headers: getAuthHeaders(token) });
|
|
}
|
|
|
|
export { createRequest, createGetRequest };
|