From 38c2edb8e46032c48c41c3e056de9ae388ab7940 Mon Sep 17 00:00:00 2001 From: m5r Date: Sun, 3 Oct 2021 18:38:39 +0200 Subject: [PATCH] replace got http calls by @devoxa/paddle-sdk where possible --- app/api/debug/cancel-subscription.ts | 3 +- app/settings/mutations/update-subscription.ts | 3 +- integrations/paddle.ts | 67 ++++++++----------- 3 files changed, 30 insertions(+), 43 deletions(-) diff --git a/app/api/debug/cancel-subscription.ts b/app/api/debug/cancel-subscription.ts index 5c803d2..08eae7d 100644 --- a/app/api/debug/cancel-subscription.ts +++ b/app/api/debug/cancel-subscription.ts @@ -9,9 +9,8 @@ const cancelSubscriptionHandler: BlitzApiHandler = async (req, res) => { const { subscriptionId } = req.body; logger.debug(`cancelling subscription for subscriptionId="${subscriptionId}"`); - const result = await cancelPaddleSubscription({ subscriptionId }); + await cancelPaddleSubscription({ subscriptionId }); logger.debug(`cancelled subscription for subscriptionId="${subscriptionId}"`); - logger.debug(result as object); res.status(200).end(); }; diff --git a/app/settings/mutations/update-subscription.ts b/app/settings/mutations/update-subscription.ts index bc70271..aee4a43 100644 --- a/app/settings/mutations/update-subscription.ts +++ b/app/settings/mutations/update-subscription.ts @@ -15,6 +15,5 @@ export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ } const subscriptionId = subscription.paddleSubscriptionId; - const result = await updateSubscriptionPlan({ planId, subscriptionId }); - console.log("result", result); + await updateSubscriptionPlan({ productId: planId, subscriptionId }); }); diff --git a/integrations/paddle.ts b/integrations/paddle.ts index 34cb492..db886f1 100644 --- a/integrations/paddle.ts +++ b/integrations/paddle.ts @@ -1,19 +1,23 @@ import { getConfig } from "blitz"; 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 { SubscriptionStatus } from "db"; const { publicRuntimeConfig, serverRuntimeConfig } = getConfig(); -const vendor_id = publicRuntimeConfig.paddle.vendorId; -const vendor_auth_code = serverRuntimeConfig.paddle.apiKey; +const vendorId = publicRuntimeConfig.paddle.vendorId; +const vendorAuthCode = serverRuntimeConfig.paddle.apiKey; export const paddleSdk = new PaddleSdk({ publicKey: serverRuntimeConfig.paddle.publicKey, - vendorId: vendor_id, - vendorAuthCode: vendor_auth_code, + vendorId, + vendorAuthCode, metadataCodec: stringifyMetadata(), }); @@ -38,21 +42,6 @@ export function translateSubscriptionStatus( } } -const client = got.extend({ - prefixUrl: "https://vendors.paddle.com/api/2.0", -}); - -async function request(path: string, data: any) { - return client.post(path, { - json: { - ...data, - vendor_id, - vendor_auth_code, - }, - responseType: "json", - }); -} - type GetPaymentsParams = { subscriptionId: number; }; @@ -84,8 +73,14 @@ export async function getPayments({ subscriptionId }: GetPaymentsParams) { type PaymentsResponse = PaymentsSuccessResponse | PaymentsErrorResponse; - const { body } = await request("subscription/payments", { subscription_id: subscriptionId }); - console.log("body", typeof body); + const { body } = await got.post("https://vendors.paddle.com/api/2.0/subscription/payments", { + json: { + subscription_id: subscriptionId, + vendor_id: vendorId, + vendor_auth_code: vendorAuthCode, + }, + responseType: "json", + }); if (!body.success) { throw new Error(body.error.message); } @@ -93,24 +88,18 @@ export async function getPayments({ subscriptionId }: GetPaymentsParams) { return body.response; } -type UpdateSubscriptionPlanParams = { - subscriptionId: number; - planId: number; - prorate?: boolean; -}; - -export async function updateSubscriptionPlan({ subscriptionId, planId, prorate = true }: UpdateSubscriptionPlanParams) { - const { body } = await request("subscription/users/update", { - subscription_id: subscriptionId, - plan_id: planId, - prorate, +export async function updateSubscriptionPlan({ + subscriptionId, + productId, + shouldProrate = true, +}: PaddleSdkUpdateSubscriptionRequest) { + return paddleSdk.updateSubscription({ + subscriptionId, + productId, + shouldProrate, }); - - return body; } -export async function cancelPaddleSubscription({ subscriptionId }: { subscriptionId: number }) { - const { body } = await request("subscription/users_cancel", { subscription_id: subscriptionId }); - - return body; +export async function cancelPaddleSubscription({ subscriptionId }: PaddleSdkCancelSubscriptionRequest) { + return paddleSdk.cancelSubscription({ subscriptionId }); }