2021-09-26 22:08:02 +00:00
|
|
|
import { getConfig } from "blitz";
|
|
|
|
import got from "got";
|
|
|
|
|
|
|
|
const { publicRuntimeConfig, serverRuntimeConfig } = getConfig();
|
|
|
|
|
|
|
|
const vendor_id = publicRuntimeConfig.paddle.vendorId;
|
|
|
|
const vendor_auth_code = serverRuntimeConfig.paddle.apiKey;
|
|
|
|
|
|
|
|
const client = got.extend({
|
|
|
|
prefixUrl: "https://vendors.paddle.com/api/2.0",
|
|
|
|
});
|
|
|
|
|
|
|
|
async function request<T>(path: string, data: any) {
|
|
|
|
return client.post<T>(path, {
|
2021-09-30 22:59:35 +00:00
|
|
|
json: {
|
|
|
|
...data,
|
|
|
|
vendor_id,
|
|
|
|
vendor_auth_code,
|
|
|
|
},
|
|
|
|
responseType: "json",
|
2021-09-26 22:08:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-30 22:59:35 +00:00
|
|
|
type GetPaymentsParams = {
|
|
|
|
subscriptionId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function getPayments({ subscriptionId }: GetPaymentsParams) {
|
|
|
|
type Payment = {
|
|
|
|
id: number;
|
|
|
|
subscription_id: number;
|
|
|
|
amount: number;
|
|
|
|
currency: string;
|
|
|
|
payout_date: string;
|
|
|
|
is_paid: number;
|
|
|
|
is_one_off_charge: boolean;
|
|
|
|
receipt_url?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type PaymentsSuccessResponse = {
|
|
|
|
success: true;
|
|
|
|
response: Payment[];
|
|
|
|
};
|
|
|
|
|
|
|
|
type PaymentsErrorResponse = {
|
|
|
|
success: false;
|
|
|
|
error: {
|
|
|
|
code: number;
|
|
|
|
message: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
type PaymentsResponse = PaymentsSuccessResponse | PaymentsErrorResponse;
|
|
|
|
|
|
|
|
const { body } = await request<PaymentsResponse>("subscription/payments", { subscription_id: subscriptionId });
|
|
|
|
console.log("body", typeof body);
|
|
|
|
if (!body.success) {
|
|
|
|
throw new Error(body.error.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return body.response;
|
|
|
|
}
|
|
|
|
|
2021-09-26 22:08:02 +00:00
|
|
|
type UpdateSubscriptionPlanParams = {
|
|
|
|
subscriptionId: string;
|
|
|
|
planId: string;
|
|
|
|
prorate?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function updateSubscriptionPlan({ subscriptionId, planId, prorate = true }: UpdateSubscriptionPlanParams) {
|
2021-09-30 22:59:35 +00:00
|
|
|
const { body } = await request("subscription/users/update", {
|
2021-09-26 22:08:02 +00:00
|
|
|
subscription_id: subscriptionId,
|
|
|
|
plan_id: planId,
|
|
|
|
prorate,
|
|
|
|
});
|
|
|
|
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function cancelPaddleSubscription({ subscriptionId }: { subscriptionId: string }) {
|
2021-09-30 22:59:35 +00:00
|
|
|
const { body } = await request("subscription/users_cancel", { subscription_id: subscriptionId });
|
2021-09-26 22:08:02 +00:00
|
|
|
|
|
|
|
return body;
|
|
|
|
}
|