@moontra/moonui 3.0.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/use-paddle.ts DELETED
@@ -1,138 +0,0 @@
1
- import { useState, useEffect } from 'react';
2
- import { getPaddleInstance } from '@/lib/paddle';
3
- import type { Paddle } from '@paddle/paddle-js';
4
-
5
- interface UsePaddleReturn {
6
- paddle: Paddle | null;
7
- isLoading: boolean;
8
- error: string | null;
9
- openCheckout: (planId: string) => Promise<void>;
10
- }
11
-
12
- /**
13
- * Custom hook for Paddle integration
14
- * Handles Paddle initialization and checkout operations
15
- */
16
- export function usePaddle(): UsePaddleReturn {
17
- const [paddle, setPaddle] = useState<Paddle | null>(null);
18
- const [isLoading, setIsLoading] = useState(true);
19
- const [error, setError] = useState<string | null>(null);
20
-
21
- // Initialize Paddle on mount
22
- useEffect(() => {
23
- let mounted = true;
24
-
25
- const initPaddle = async () => {
26
- try {
27
- setIsLoading(true);
28
- setError(null);
29
-
30
- const paddleInstance = await getPaddleInstance();
31
-
32
- if (mounted) {
33
- if (paddleInstance) {
34
- setPaddle(paddleInstance);
35
- } else {
36
- setError('Failed to initialize Paddle');
37
- }
38
- }
39
- } catch (err) {
40
- if (mounted) {
41
- setError(err instanceof Error ? err.message : 'Unknown error');
42
- }
43
- } finally {
44
- if (mounted) {
45
- setIsLoading(false);
46
- }
47
- }
48
- };
49
-
50
- initPaddle();
51
-
52
- return () => {
53
- mounted = false;
54
- };
55
- }, []);
56
-
57
- /**
58
- * Open Paddle checkout for a specific plan
59
- */
60
- const openCheckout = async (planId: string) => {
61
- if (!paddle) {
62
- throw new Error('Paddle not initialized');
63
- }
64
-
65
- try {
66
- // Call our API to handle the checkout
67
- const response = await fetch('/api/paddle', {
68
- method: 'POST',
69
- headers: {
70
- 'Content-Type': 'application/json',
71
- },
72
- body: JSON.stringify({ plan: planId }),
73
- });
74
-
75
- if (!response.ok) {
76
- const errorText = await response.text();
77
- throw new Error(errorText || 'Failed to create checkout');
78
- }
79
-
80
- const result = await response.json();
81
-
82
- if (!result.success) {
83
- throw new Error(result.message || 'Checkout failed');
84
- }
85
-
86
- // For mock mode, we need to manually trigger checkout
87
- if (process.env.NEXT_PUBLIC_USE_MOCK_PADDLE === 'true') {
88
- console.log('🧪 [Hook] Mock mode detected, triggering checkout');
89
-
90
- // Import paddle config to get price ID
91
- const { PADDLE_CONFIG } = await import('@/lib/paddle');
92
- const priceId = PADDLE_CONFIG.priceIds[planId as keyof typeof PADDLE_CONFIG.priceIds];
93
-
94
- console.log('🧪 [Hook] Using price ID:', priceId, 'for plan:', planId);
95
- console.log('🧪 [Hook] Paddle instance:', paddle);
96
-
97
- // Trigger mock checkout
98
- const checkoutData = {
99
- items: [{ priceId, quantity: 1 }],
100
- settings: {
101
- displayMode: 'overlay' as const,
102
- theme: 'light' as const,
103
- locale: 'en' as const,
104
- successUrl: `${window.location.origin}/dashboard?success=true`
105
- },
106
- customData: {
107
- planId,
108
- userId: 'test_user',
109
- timestamp: new Date().toISOString()
110
- }
111
- };
112
-
113
- console.log('🧪 [Hook] Opening checkout with data:', checkoutData);
114
-
115
- try {
116
- paddle.Checkout.open(checkoutData);
117
- console.log('🧪 [Hook] Checkout.open called successfully');
118
- } catch (checkoutError) {
119
- console.error('🧪 [Hook] Checkout.open error:', checkoutError);
120
- }
121
- }
122
-
123
- // Checkout is opened inline by Paddle
124
- console.log('✅ Paddle checkout opened for plan:', planId);
125
-
126
- } catch (error) {
127
- console.error('❌ Checkout error:', error);
128
- throw error;
129
- }
130
- };
131
-
132
- return {
133
- paddle,
134
- isLoading,
135
- error,
136
- openCheckout,
137
- };
138
- }