From f269d207f235152a7832ea3151584208a4ef306f Mon Sep 17 00:00:00 2001 From: Yizack Rangel Date: Sun, 23 Jun 2024 14:29:27 -0500 Subject: [PATCH] chore: remove unused paypal code --- .config/nuxt.ts | 8 --- app/locales/en.ts | 1 - app/plugins/paypal.client.ts | 35 ---------- package.json | 1 - pnpm-lock.yaml | 15 ---- server/api/webhooks/paypal.post.ts | 33 --------- server/utils/helpers.ts | 1 - server/utils/paypal.ts | 17 ----- types/enums/paypal.ts | 4 -- types/paypal.d.ts | 107 ----------------------------- 10 files changed, 222 deletions(-) delete mode 100644 app/plugins/paypal.client.ts delete mode 100644 server/api/webhooks/paypal.post.ts delete mode 100644 server/utils/paypal.ts delete mode 100644 types/enums/paypal.ts delete mode 100644 types/paypal.d.ts diff --git a/.config/nuxt.ts b/.config/nuxt.ts index affe1ca..43386ee 100644 --- a/.config/nuxt.ts +++ b/.config/nuxt.ts @@ -119,10 +119,6 @@ export default defineNuxtConfig({ key: "", secret: "" }, - paypal: { - secret: "", - webhookId: "" - }, paddle: { secret: "", webhookId: "" @@ -133,10 +129,6 @@ export default defineNuxtConfig({ } }, public: { - paypal: { - clientId: process.env.NUXT_PAYPAL_CLIENT_ID || "", - planId: process.env.NUXT_PAYPAL_PLAN_ID || "" - }, paddle: { clientId: process.env.NUXT_PADDLE_CLIENT_ID || "", planId: process.env.NUXT_PADDLE_PLAN_ID || "" diff --git a/app/locales/en.ts b/app/locales/en.ts index 5b4964d..15e03e6 100644 --- a/app/locales/en.ts +++ b/app/locales/en.ts @@ -302,7 +302,6 @@ export default { free_features: "Always free features", checkout: "Checkout", paddle_sdk_not_loaded: "Paddle SDK could not be loaded", - paypal_sdk_not_loaded: "PayPal SDK could not be loaded", payment_error: "Payment error occurred", transaction_not_found: "Transaction not found", invalid_headers: "headers are not valid", diff --git a/app/plugins/paypal.client.ts b/app/plugins/paypal.client.ts deleted file mode 100644 index e4d79db..0000000 --- a/app/plugins/paypal.client.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { loadScript } from "@paypal/paypal-js"; - -class PayPal { - constructor (public clientId: string, public planId: string) { - this.clientId = clientId; - this.planId = planId; - } - - async loadScript () { - const script = await loadScript({ - locale: t("lang_locale"), - clientId: this.clientId, - vault: true, - intent: "subscription" - }).catch(() => null); - - if (!script) { - throw createError({ - statusCode: ErrorCode.INTERNAL_SERVER_ERROR, - message: t("paypal_sdk_not_loaded"), - fatal: true - }); - } - - return script; - } -} - -export default defineNuxtPlugin(async () => { - const { clientId, planId } = useRuntimeConfig().public.paypal; - const paypal = new PayPal(clientId, planId); - return { - provide: { paypal } - }; -}); diff --git a/package.json b/package.json index cce3777..a834a90 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "@nuxtjs/turnstile": "^0.8.0", "@paddle/paddle-js": "^1.2.0", "@paddle/paddle-node-sdk": "^1.4.1", - "@paypal/paypal-js": "^8.1.0", "@types/bootstrap": "^5.2.10", "@types/leaflet": "^1.9.12", "@vue-email/components": "^0.0.17", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7fe58ad..edc4234 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -35,9 +35,6 @@ importers: '@paddle/paddle-node-sdk': specifier: ^1.4.1 version: 1.4.1(encoding@0.1.13) - '@paypal/paypal-js': - specifier: ^8.1.0 - version: 8.1.0 '@types/bootstrap': specifier: ^5.2.10 version: 5.2.10 @@ -1756,9 +1753,6 @@ packages: resolution: {integrity: sha512-HNjmfLQEVRZmHRET336f20H/8kOozUGwk7yajvsonjNxbj2wBTK1WsQuHkD5yYh9RxFGL2EyDHryOihOwUoKDA==} engines: {node: '>= 10.0.0'} - '@paypal/paypal-js@8.1.0': - resolution: {integrity: sha512-f64bom5xYwmxyeKPJUFS/XpM0tXojQEgjRIADPqe1R9WmK+PFqL4SEkT85cGU0ZXLVx4EGbjwREHhqEOR+OstA==} - '@pkgjs/parseargs@0.11.0': resolution: {integrity: sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==} engines: {node: '>=14'} @@ -5094,9 +5088,6 @@ packages: bluebird: optional: true - promise-polyfill@8.3.0: - resolution: {integrity: sha512-H5oELycFml5yto/atYqmjyigJoAo3+OXwolYiH7OfQuYlAqhxNvTfiNMbV9hsC6Yp83yE5r2KTVmtrG6R9i6Pg==} - promise-retry@2.0.1: resolution: {integrity: sha512-y+WKFlBR8BGXnsNlIHFGPZmyDf3DFMoLhaflAnyZgV6rG6xu+JwesTo2Q9R6XwYmtmwAFCkAk3e35jEdoeh/3g==} engines: {node: '>=10'} @@ -8083,10 +8074,6 @@ snapshots: '@parcel/watcher-win32-ia32': 2.4.1 '@parcel/watcher-win32-x64': 2.4.1 - '@paypal/paypal-js@8.1.0': - dependencies: - promise-polyfill: 8.3.0 - '@pkgjs/parseargs@0.11.0': optional: true @@ -12213,8 +12200,6 @@ snapshots: promise-inflight@1.0.1: {} - promise-polyfill@8.3.0: {} - promise-retry@2.0.1: dependencies: err-code: 2.0.3 diff --git a/server/api/webhooks/paypal.post.ts b/server/api/webhooks/paypal.post.ts deleted file mode 100644 index 375cf77..0000000 --- a/server/api/webhooks/paypal.post.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { eq, and } from "drizzle-orm"; - -export default defineEventHandler(async (event) => { - const webhook = await readBody(event) as PayPalWebhookEvent; - const headers = getHeaders(event); - - if (!headers) throw createError({ statusCode: ErrorCode.BAD_REQUEST, message: "invalid_headers" }); - const isValidWebhook = await isValidPaypalWebhook(event); - if (!isValidWebhook) throw createError({ statusCode: ErrorCode.BAD_REQUEST, message: "invalid_webhook" }); - - if (webhook.event_type !== PayPalWebhook.PAYMENT_SALE_COMPLETED) - throw createError({ statusCode: ErrorCode.BAD_REQUEST, message: "invalid_event_type" }); - if (webhook.resource.state !== "completed" || !webhook.resource.custom || !webhook.resource.billing_agreement_id) - throw createError({ statusCode: ErrorCode.BAD_REQUEST, message: "invalid_webhook_data" }); - - const subscription = await getPayPalSubscription(event, webhook.resource.billing_agreement_id); - if (!subscription) - throw createError({ statusCode: ErrorCode.BAD_REQUEST, message: "subscription_not_found" }); - if (subscription.status !== "ACTIVE") - throw createError({ statusCode: ErrorCode.BAD_REQUEST, message: "invalid_subscription_status" }); - - const DB = useDb(); - const today = Date.now(); - - await DB.update(tables.bonds).set({ - premium: 1, - subscriptionId: subscription.id, - nextPayment: new Date(subscription.billing_info.next_billing_time).getTime(), - updatedAt: today - }).where(and(eq(tables.bonds.id, Number(webhook.resource.custom)))).run(); - - return { success: isValidWebhook }; -}); diff --git a/server/utils/helpers.ts b/server/utils/helpers.ts index b847325..717a5ce 100644 --- a/server/utils/helpers.ts +++ b/server/utils/helpers.ts @@ -4,7 +4,6 @@ export { SITE } from "~/utils/site"; export { ErrorCode } from "~~/types/enums/errors"; export { Quota } from "~~/types/enums/quotas"; export { isPasswordValid } from "~/utils/validators"; -export { PayPalWebhook } from "~~/types/enums/paypal"; export { z } from "zod"; export const hash = (string: string, salt?: string) => { diff --git a/server/utils/paypal.ts b/server/utils/paypal.ts deleted file mode 100644 index 1d1e7d5..0000000 --- a/server/utils/paypal.ts +++ /dev/null @@ -1,17 +0,0 @@ -import type { H3Event } from "h3"; - -const { clientId } = useRuntimeConfig().public.paypal; -const baseAPI = import.meta.dev ? "https://api-m.sandbox.paypal.com/v1" : "https://api-m.paypal.com/v1"; - -export const getPayPalSubscription = async (event: H3Event, subscriptionId: string) => { - const { secret } = useRuntimeConfig(event).paypal; - const basicAuth = btoa(`${clientId}:${secret}`); - - const subscription = await $fetch(`${baseAPI}/billing/subscriptions/${subscriptionId}`, { - headers: { - Authorization: `Basic ${basicAuth}` - } - }).catch(() => null); - - return subscription; -}; diff --git a/types/enums/paypal.ts b/types/enums/paypal.ts deleted file mode 100644 index 24118e5..0000000 --- a/types/enums/paypal.ts +++ /dev/null @@ -1,4 +0,0 @@ -export const enum PayPalWebhook { - PAYMENT_SALE_COMPLETED = "PAYMENT.SALE.COMPLETED", - BILLING_SUBSCRIPTION_SUSPENDED = "BILLING.SUBSCRIPTION.SUSPENDED" -} diff --git a/types/paypal.d.ts b/types/paypal.d.ts deleted file mode 100644 index 73db281..0000000 --- a/types/paypal.d.ts +++ /dev/null @@ -1,107 +0,0 @@ -import type { PayPalWebhook } from "~/types/enums/paypal"; - -export {}; - -declare global { - type PayPalWebhookEvent = PaymentSaleCompletedEvent; - - interface PayPalWebhookBody { - id: string; - create_time: string; - resource_type: string; - event_type: T; - summary: string; - resource: R; - links: PayPalLink[]; - event_version: string; - } - - interface PaymentSaleCompletedEvent extends PayPalWebhookBody<"PAYMENT.SALE.COMPLETED", PaymentSaleCompletedResource> { - resource: { - id: string; - state: string; - amount: PayPalAmount; - payment_mode: string; - protection_eligibility: string; - protection_eligibility_type: string; - transaction_fee: PayPalBalance; - invoice_id?: string; - custom?: string; - billing_agreement_id: string; - parent_payment?: string; - update_time: string; - soft_descriptor?: string; - }; - } - - interface PayPalAmount { - total: string; - currency: string; - details: { - subtotal: string; - tax?: string; - shipping?: string; - handling_fee?: string; - shipping_discount?: string; - }; - } - - interface PayPalBalance { - value: string; - currency: string; - } - - interface PayPalLink { - href: string; - rel: string; - method: string; - } - - interface PayPalSubscription { - status: string; - status_update_time: string; - id: string; - plan_id: string; - start_time: string; - quantity: string; - shipping_amount: PayPalBalance; - subscriber: { - email_address: string; - payer_id: string; - name: { - given_name: string; - surname: string; - }; - shipping_address: { - address: { - address_line_1: string; - address_line_2: string; - admin_area_2: string; - admin_area_1: string; - postal_code: string; - country_code: string; - }; - }; - }; - billing_info: { - outstanding_balance: PayPalBalance; - cycle_executions: { - tenure_type: string; - sequence: number; - cycles_completed: number; - cycles_remaining: number; - current_pricing_scheme_version: number; - total_cycles: number; - }[]; - next_billing_time: string; - failed_payments_count: number; - }; - create_time: string; - update_time: string; - custom_id: string; - plan_overridden: boolean; - links: PayPalLink[]; - } - - type PayPalWebhook = typeof PayPalWebhook; -}