Skip to content

📈 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!

Paddle billing improvements

Blue Mouse9b5a193e6eb3
diff --git a/web/app/hooks/usePaddle.ts b/web/app/hooks/usePaddle.ts
index 40ffcfee7..9c3ec0b68 100644
--- a/web/app/hooks/usePaddle.ts
+++ b/web/app/hooks/usePaddle.ts
@@ -6,6 +6,49 @@ import { loadScript } from '~/utils/generic'
const POLL_INTERVAL_MS = 200
const MAX_ATTEMPTS = 50 // 10 seconds
+type PaddleListener = (eventData: any) => void
+
+let initStarted = false
+let paddleReady = false
+let paddleError = false
+const listeners = new Set<PaddleListener>()
+const stateSubscribers = new Set<() => void>()
+
+function notifyStateSubscribers() {
+ for (const cb of stateSubscribers) cb()
+}
+
+function initPaddle() {
+ if (initStarted) return
+ initStarted = true
+
+ loadScript(PADDLE_JS_URL)
+
+ let attempts = 0
+ const interval = setInterval(() => {
+ attempts += 1
+
+ if ((window as any)?.Paddle) {
+ ;(window as any).Paddle.Setup({
+ vendor: PADDLE_VENDOR_ID,
+ eventCallback: (eventData: any) => {
+ for (const listener of listeners) listener(eventData)
+ },
+ })
+ paddleReady = true
+ notifyStateSubscribers()
+ clearInterval(interval)
+ return
+ }
+
+ if (attempts >= MAX_ATTEMPTS) {
+ paddleError = true
+ notifyStateSubscribers()
+ clearInterval(interval)
+ }
+ }, POLL_INTERVAL_MS)
+}
+
interface UsePaddleOptions {
onEvent?: (eventData: any) => void
}
@@ -17,38 +60,32 @@ interface UsePaddleResult {
}
export function usePaddle(options: UsePaddleOptions = {}): UsePaddleResult {
- const [isPaddleLoaded, setIsPaddleLoaded] = useState(false)
- const [paddleLoadError, setPaddleLoadError] = useState(false)
+ const [isPaddleLoaded, setIsPaddleLoaded] = useState(paddleReady)
+ const [paddleLoadError, setPaddleLoadError] = useState(paddleError)
const onEventRef = useRef(options.onEvent)
onEventRef.current = options.onEvent
useEffect(() => {
- loadScript(PADDLE_JS_URL)
-
- let attempts = 0
-
- const interval = setInterval(() => {
- attempts += 1
-
- if ((window as any)?.Paddle) {
- ;(window as any).Paddle.Setup({
- vendor: PADDLE_VENDOR_ID,
- eventCallback: (eventData: any) => {
- onEventRef.current?.(eventData)
- },
- })
- setIsPaddleLoaded(true)
- clearInterval(interval)
- return
- }
+ const listener: PaddleListener = (eventData) => {
+ onEventRef.current?.(eventData)
+ }
+ listeners.add(listener)
- if (attempts >= MAX_ATTEMPTS) {
- setPaddleLoadError(true)
- clearInterval(interval)
- }
- }, POLL_INTERVAL_MS)
+ const stateSubscriber = () => {
+ setIsPaddleLoaded(paddleReady)
+ setPaddleLoadError(paddleError)
+ }
+ stateSubscribers.add(stateSubscriber)
+
+ // Sync state in case init already completed before this effect ran
+ stateSubscriber()
+
+ initPaddle()
- return () => clearInterval(interval)
+ return () => {
+ listeners.delete(listener)
+ stateSubscribers.delete(stateSubscriber)
+ }
}, [])
const openCheckout = useCallback(
diff --git a/web/app/pages/Checkout/Checkout.tsx b/web/app/pages/Checkout/Checkout.tsx
index 612a62a58..113ccddec 100644
--- a/web/app/pages/Checkout/Checkout.tsx
+++ b/web/app/pages/Checkout/Checkout.tsx
@@ -48,11 +48,16 @@ const Checkout = () => {
const [hasCompletedCheckout, setHasCompletedCheckout] = useState(false)
const [isCheckoutOpen, setIsCheckoutOpen] = useState(false)
- const onPaddleEvent = useCallback((eventData: any) => {
- if (eventData?.event === 'Checkout.Complete') {
- setHasCompletedCheckout(true)
- }
- }, [])
+ const onPaddleEvent = useCallback(
+ (eventData: any) => {
+ if (eventData?.event === 'Checkout.Complete') {
+ setHasCompletedCheckout(true)
+ } else if (eventData?.event === 'Checkout.Close') {
+ setIsCheckoutOpen(false)
+ }
+ },
+ [setIsCheckoutOpen, setHasCompletedCheckout],
+ )
const { isPaddleLoaded, paddleLoadError, openCheckout } = usePaddle({
onEvent: onPaddleEvent,
@@ -90,10 +95,22 @@ const Checkout = () => {
if (!tier) return
+ const hasPid = 'pid' in tier && typeof tier.pid === 'number'
+ const hasYpid = 'ypid' in tier && typeof tier.ypid === 'number'
+
const product =
selectedBillingFrequency === BillingFrequency.monthly
- ? (tier as any).pid
- : (tier as any).ypid
+ ? hasPid
+ ? tier.pid
+ : undefined
+ : hasYpid
+ ? tier.ypid
+ : undefined
+
+ if (!product) {
+ toast.error(t('apiNotifications.somethingWentWrong'))
+ return
+ }
const opened = openCheckout({
product,
@@ -124,9 +141,9 @@ const Checkout = () => {
}, 500)
}
- const today = new Date()
- const trialEndDate = new Date()
- trialEndDate.setDate(today.getDate() + TRIAL_DAYS)
+ const trialEndDate = user?.trialEndDate
+ ? new Date(user.trialEndDate)
+ : new Date()
const dateFormatter = new Intl.DateTimeFormat(i18n.language, {
month: 'long',