replace got http calls by @devoxa/paddle-sdk where possible

This commit is contained in:
m5r 2021-10-03 18:38:39 +02:00
parent 4ce6ccc142
commit 38c2edb8e4
3 changed files with 30 additions and 43 deletions

View File

@ -9,9 +9,8 @@ const cancelSubscriptionHandler: BlitzApiHandler = async (req, res) => {
const { subscriptionId } = req.body; const { subscriptionId } = req.body;
logger.debug(`cancelling subscription for subscriptionId="${subscriptionId}"`); logger.debug(`cancelling subscription for subscriptionId="${subscriptionId}"`);
const result = await cancelPaddleSubscription({ subscriptionId }); await cancelPaddleSubscription({ subscriptionId });
logger.debug(`cancelled subscription for subscriptionId="${subscriptionId}"`); logger.debug(`cancelled subscription for subscriptionId="${subscriptionId}"`);
logger.debug(result as object);
res.status(200).end(); res.status(200).end();
}; };

View File

@ -15,6 +15,5 @@ export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({
} }
const subscriptionId = subscription.paddleSubscriptionId; const subscriptionId = subscription.paddleSubscriptionId;
const result = await updateSubscriptionPlan({ planId, subscriptionId }); await updateSubscriptionPlan({ productId: planId, subscriptionId });
console.log("result", result);
}); });

View File

@ -1,19 +1,23 @@
import { getConfig } from "blitz"; import { getConfig } from "blitz";
import got from "got"; import got from "got";
import type { PaddleSdkSubscriptionCreatedEvent } from "@devoxa/paddle-sdk"; import type {
PaddleSdkSubscriptionCreatedEvent,
PaddleSdkCancelSubscriptionRequest,
PaddleSdkUpdateSubscriptionRequest,
} from "@devoxa/paddle-sdk";
import { PaddleSdk, PaddleSdkSubscriptionStatus, stringifyMetadata } from "@devoxa/paddle-sdk"; import { PaddleSdk, PaddleSdkSubscriptionStatus, stringifyMetadata } from "@devoxa/paddle-sdk";
import { SubscriptionStatus } from "db"; import { SubscriptionStatus } from "db";
const { publicRuntimeConfig, serverRuntimeConfig } = getConfig(); const { publicRuntimeConfig, serverRuntimeConfig } = getConfig();
const vendor_id = publicRuntimeConfig.paddle.vendorId; const vendorId = publicRuntimeConfig.paddle.vendorId;
const vendor_auth_code = serverRuntimeConfig.paddle.apiKey; const vendorAuthCode = serverRuntimeConfig.paddle.apiKey;
export const paddleSdk = new PaddleSdk({ export const paddleSdk = new PaddleSdk({
publicKey: serverRuntimeConfig.paddle.publicKey, publicKey: serverRuntimeConfig.paddle.publicKey,
vendorId: vendor_id, vendorId,
vendorAuthCode: vendor_auth_code, vendorAuthCode,
metadataCodec: stringifyMetadata(), metadataCodec: stringifyMetadata(),
}); });
@ -38,21 +42,6 @@ export function translateSubscriptionStatus(
} }
} }
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, {
json: {
...data,
vendor_id,
vendor_auth_code,
},
responseType: "json",
});
}
type GetPaymentsParams = { type GetPaymentsParams = {
subscriptionId: number; subscriptionId: number;
}; };
@ -84,8 +73,14 @@ export async function getPayments({ subscriptionId }: GetPaymentsParams) {
type PaymentsResponse = PaymentsSuccessResponse | PaymentsErrorResponse; type PaymentsResponse = PaymentsSuccessResponse | PaymentsErrorResponse;
const { body } = await request<PaymentsResponse>("subscription/payments", { subscription_id: subscriptionId }); const { body } = await got.post<PaymentsResponse>("https://vendors.paddle.com/api/2.0/subscription/payments", {
console.log("body", typeof body); json: {
subscription_id: subscriptionId,
vendor_id: vendorId,
vendor_auth_code: vendorAuthCode,
},
responseType: "json",
});
if (!body.success) { if (!body.success) {
throw new Error(body.error.message); throw new Error(body.error.message);
} }
@ -93,24 +88,18 @@ export async function getPayments({ subscriptionId }: GetPaymentsParams) {
return body.response; return body.response;
} }
type UpdateSubscriptionPlanParams = { export async function updateSubscriptionPlan({
subscriptionId: number; subscriptionId,
planId: number; productId,
prorate?: boolean; shouldProrate = true,
}; }: PaddleSdkUpdateSubscriptionRequest<Metadata>) {
return paddleSdk.updateSubscription({
export async function updateSubscriptionPlan({ subscriptionId, planId, prorate = true }: UpdateSubscriptionPlanParams) { subscriptionId,
const { body } = await request("subscription/users/update", { productId,
subscription_id: subscriptionId, shouldProrate,
plan_id: planId,
prorate,
}); });
return body;
} }
export async function cancelPaddleSubscription({ subscriptionId }: { subscriptionId: number }) { export async function cancelPaddleSubscription({ subscriptionId }: PaddleSdkCancelSubscriptionRequest) {
const { body } = await request("subscription/users_cancel", { subscription_id: subscriptionId }); return paddleSdk.cancelSubscription({ subscriptionId });
return body;
} }