📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Centralise Paddle.Checkout.open into usePaddle hook with error handling
Blue Mouse46116f69bad8
diff --git a/web/app/components/pricing/BillingPricing.tsx b/web/app/components/pricing/BillingPricing.tsxindex 060274ed6..61bcb4e24 100644--- a/web/app/components/pricing/BillingPricing.tsx+++ b/web/app/components/pricing/BillingPricing.tsx@@ -34,6 +34,7 @@ import routes from '~/utils/routes'interface BillingPricingProps {lastEvent?: { event: string } | nullmetainfo?: Metainfo+ openCheckout: (options: Record<string, any>) => boolean}const formatEventsLong = (value: number): string =>@@ -42,6 +43,7 @@ const formatEventsLong = (value: number): string =>const BillingPricing = ({lastEvent,metainfo = DEFAULT_METAINFO,+ openCheckout,}: BillingPricingProps) => {const {t,@@ -203,13 +205,7 @@ const BillingPricing = ({setPlanCodeLoading(tier.planCode)- if (!window.Paddle) {- toast.error('Payment script has not yet loaded! Please, try again.')- setPlanCodeLoading(null)- return- }-- window.Paddle.Checkout.open({+ const opened = openCheckout({product:billingFrequency === BillingFrequency.monthly ? tier.pid : tier.ypid,email: user.email,@@ -219,6 +215,12 @@ const BillingPricing = ({displayModeTheme: theme,country: metainfo.country,})++ if (!opened) {+ toast.error(t('billing.paddleStillLoading'))+ setPlanCodeLoading(null)+ return+ }}const closeUpdateModal = (force?: boolean) => {diff --git a/web/app/hooks/usePaddle.ts b/web/app/hooks/usePaddle.tsindex afbf8ca96..40ffcfee7 100644--- a/web/app/hooks/usePaddle.ts+++ b/web/app/hooks/usePaddle.ts@@ -1,4 +1,4 @@-import { useEffect, useRef, useState } from 'react'+import { useCallback, useEffect, useRef, useState } from 'react'import { PADDLE_JS_URL, PADDLE_VENDOR_ID } from '~/lib/constants'import { loadScript } from '~/utils/generic'@@ -13,6 +13,7 @@ interface UsePaddleOptions {interface UsePaddleResult {isPaddleLoaded: booleanpaddleLoadError: boolean+ openCheckout: (options: Record<string, any>) => boolean}export function usePaddle(options: UsePaddleOptions = {}): UsePaddleResult {@@ -50,5 +51,19 @@ export function usePaddle(options: UsePaddleOptions = {}): UsePaddleResult {return () => clearInterval(interval)}, [])- return { isPaddleLoaded, paddleLoadError }+ const openCheckout = useCallback(+ (checkoutOptions: Record<string, any>): boolean => {+ if (!(window as any)?.Paddle) return false++ try {+ ;(window as any).Paddle.Checkout.open(checkoutOptions)+ return true+ } catch {+ return false+ }+ },+ [],+ )++ return { isPaddleLoaded, paddleLoadError, openCheckout }}diff --git a/web/app/pages/Checkout/Checkout.tsx b/web/app/pages/Checkout/Checkout.tsxindex c86edbdd4..612a62a58 100644--- a/web/app/pages/Checkout/Checkout.tsx+++ b/web/app/pages/Checkout/Checkout.tsx@@ -54,7 +54,7 @@ const Checkout = () => {}}, [])- const { isPaddleLoaded, paddleLoadError } = usePaddle({+ const { isPaddleLoaded, paddleLoadError, openCheckout } = usePaddle({onEvent: onPaddleEvent,})@@ -95,8 +95,7 @@ const Checkout = () => {? (tier as any).pid: (tier as any).ypid- setIsCheckoutOpen(true)- ;(window as any).Paddle.Checkout.open({+ const opened = openCheckout({product,method: 'inline',frameTarget: 'checkout-container',@@ -110,6 +109,14 @@ const Checkout = () => {country: metainfo.country,})+ if (!opened) {+ setIsCheckoutOpen(false)+ toast.error(t('apiNotifications.somethingWentWrong'))+ return+ }++ setIsCheckoutOpen(true)+setTimeout(() => {document.querySelector('#checkout-container')diff --git a/web/app/pages/UserSettings/UserSettings.tsx b/web/app/pages/UserSettings/UserSettings.tsxindex af5ead8f3..83640098a 100644--- a/web/app/pages/UserSettings/UserSettings.tsx+++ b/web/app/pages/UserSettings/UserSettings.tsx@@ -268,7 +268,7 @@ const UserSettings = () => {useState(false)const [lastEvent, setLastEvent] = useState<{ event: string } | null>(null)- usePaddle({ onEvent: setLastEvent })+ const { openCheckout } = usePaddle({ onEvent: setLastEvent })const metainfo = useMemo(() => {if (metainfoFetcher.data?.success && metainfoFetcher.data.data) {@@ -371,12 +371,7 @@ const UserSettings = () => {return}- if (!(window as any).Paddle) {- window.location.replace(subUpdateURL)- return- }-- ;(window as any).Paddle.Checkout.open({+ const opened = openCheckout({override: subUpdateURL,method: 'inline',frameTarget: 'checkout-container',@@ -387,6 +382,12 @@ const UserSettings = () => {displayModeTheme: theme,country: metainfo.country,})++ if (!opened) {+ window.location.replace(subUpdateURL)+ return+ }+setTimeout(() => {document.querySelector('#checkout-container')?.scrollIntoView()}, 500)@@ -1529,6 +1530,7 @@ const UserSettings = () => {<BillingPricinglastEvent={lastEvent}metainfo={metainfo}+ openCheckout={openCheckout}/><div className='mt-4 flex flex-wrap gap-3'>