@solvapay/react 1.0.0-preview.19 → 1.0.0-preview.20
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/README.md +29 -29
- package/dist/index.cjs +170 -166
- package/dist/index.d.cts +166 -147
- package/dist/index.d.ts +166 -147
- package/dist/index.js +160 -156
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -8,22 +8,35 @@ export { defaultAuthAdapter } from './adapters/auth.js';
|
|
|
8
8
|
* TypeScript type definitions for @solvapay/react
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
interface
|
|
11
|
+
interface PurchaseInfo {
|
|
12
12
|
reference: string;
|
|
13
|
+
productName: string;
|
|
14
|
+
productReference: string;
|
|
13
15
|
planName: string;
|
|
14
|
-
agentName: string;
|
|
15
16
|
status: string;
|
|
16
17
|
startDate: string;
|
|
17
18
|
endDate?: string;
|
|
18
19
|
cancelledAt?: string;
|
|
19
20
|
cancellationReason?: string;
|
|
20
21
|
amount?: number;
|
|
22
|
+
currency?: string;
|
|
23
|
+
planType?: string;
|
|
24
|
+
isRecurring?: boolean;
|
|
25
|
+
nextBillingDate?: string;
|
|
26
|
+
billingCycle?: string;
|
|
27
|
+
transactionId?: string;
|
|
28
|
+
usage?: {
|
|
29
|
+
used: number;
|
|
30
|
+
quota: number | null;
|
|
31
|
+
unit: string;
|
|
32
|
+
remaining: number | null;
|
|
33
|
+
};
|
|
21
34
|
}
|
|
22
|
-
interface
|
|
35
|
+
interface CustomerPurchaseData {
|
|
23
36
|
customerRef?: string;
|
|
24
37
|
email?: string;
|
|
25
38
|
name?: string;
|
|
26
|
-
|
|
39
|
+
purchases: PurchaseInfo[];
|
|
27
40
|
}
|
|
28
41
|
interface PaymentIntentResult {
|
|
29
42
|
clientSecret: string;
|
|
@@ -31,31 +44,31 @@ interface PaymentIntentResult {
|
|
|
31
44
|
accountId?: string;
|
|
32
45
|
customerRef?: string;
|
|
33
46
|
}
|
|
34
|
-
interface
|
|
47
|
+
interface PurchaseStatus {
|
|
35
48
|
loading: boolean;
|
|
36
49
|
customerRef?: string;
|
|
37
50
|
email?: string;
|
|
38
51
|
name?: string;
|
|
39
|
-
|
|
52
|
+
purchases: PurchaseInfo[];
|
|
40
53
|
hasPlan: (planName: string) => boolean;
|
|
41
54
|
/**
|
|
42
|
-
* Primary active
|
|
43
|
-
* Backend keeps
|
|
44
|
-
* null if no active
|
|
55
|
+
* Primary active purchase (paid or free) - most recent purchase with status === 'active'
|
|
56
|
+
* Backend keeps purchases as 'active' until expiration, even when cancelled.
|
|
57
|
+
* null if no active purchase exists
|
|
45
58
|
*/
|
|
46
|
-
|
|
59
|
+
activePurchase: PurchaseInfo | null;
|
|
47
60
|
/**
|
|
48
|
-
* Check if user has any active paid
|
|
49
|
-
* Checks
|
|
50
|
-
* Backend keeps
|
|
61
|
+
* Check if user has any active paid purchase (amount > 0)
|
|
62
|
+
* Checks purchases with status === 'active'.
|
|
63
|
+
* Backend keeps purchases as 'active' until expiration, even when cancelled.
|
|
51
64
|
*/
|
|
52
|
-
|
|
65
|
+
hasPaidPurchase: boolean;
|
|
53
66
|
/**
|
|
54
|
-
* Most recent active paid
|
|
55
|
-
* Returns
|
|
56
|
-
* null if no active paid
|
|
67
|
+
* Most recent active paid purchase (sorted by startDate)
|
|
68
|
+
* Returns purchase with status === 'active' and amount > 0.
|
|
69
|
+
* null if no active paid purchase exists
|
|
57
70
|
*/
|
|
58
|
-
|
|
71
|
+
activePaidPurchase: PurchaseInfo | null;
|
|
59
72
|
}
|
|
60
73
|
/**
|
|
61
74
|
* SolvaPay Provider Configuration
|
|
@@ -67,7 +80,7 @@ interface SolvaPayConfig {
|
|
|
67
80
|
* Defaults to standard Next.js API routes
|
|
68
81
|
*/
|
|
69
82
|
api?: {
|
|
70
|
-
|
|
83
|
+
checkPurchase?: string;
|
|
71
84
|
createPayment?: string;
|
|
72
85
|
processPayment?: string;
|
|
73
86
|
};
|
|
@@ -125,15 +138,15 @@ interface SolvaPayConfig {
|
|
|
125
138
|
onError?: (error: Error, context: string) => void;
|
|
126
139
|
}
|
|
127
140
|
interface SolvaPayContextValue {
|
|
128
|
-
|
|
129
|
-
|
|
141
|
+
purchase: PurchaseStatus;
|
|
142
|
+
refetchPurchase: () => Promise<void>;
|
|
130
143
|
createPayment: (params: {
|
|
131
144
|
planRef: string;
|
|
132
|
-
|
|
145
|
+
productRef?: string;
|
|
133
146
|
}) => Promise<PaymentIntentResult>;
|
|
134
147
|
processPayment?: (params: {
|
|
135
148
|
paymentIntentId: string;
|
|
136
|
-
|
|
149
|
+
productRef: string;
|
|
137
150
|
planRef?: string;
|
|
138
151
|
}) => Promise<ProcessPaymentResult>;
|
|
139
152
|
customerRef?: string;
|
|
@@ -151,33 +164,34 @@ interface SolvaPayProviderProps {
|
|
|
151
164
|
*/
|
|
152
165
|
createPayment?: (params: {
|
|
153
166
|
planRef: string;
|
|
154
|
-
|
|
167
|
+
productRef?: string;
|
|
155
168
|
}) => Promise<PaymentIntentResult>;
|
|
156
|
-
|
|
169
|
+
checkPurchase?: () => Promise<CustomerPurchaseData>;
|
|
157
170
|
processPayment?: (params: {
|
|
158
171
|
paymentIntentId: string;
|
|
159
|
-
|
|
172
|
+
productRef: string;
|
|
160
173
|
planRef?: string;
|
|
161
174
|
}) => Promise<ProcessPaymentResult>;
|
|
162
175
|
children: React.ReactNode;
|
|
163
176
|
}
|
|
164
177
|
interface PlanBadgeProps {
|
|
165
178
|
children?: (props: {
|
|
166
|
-
|
|
179
|
+
purchases: PurchaseInfo[];
|
|
167
180
|
loading: boolean;
|
|
168
181
|
displayPlan: string | null;
|
|
169
182
|
shouldShow: boolean;
|
|
170
183
|
}) => React.ReactNode;
|
|
171
184
|
as?: React.ElementType;
|
|
172
185
|
className?: string | ((props: {
|
|
173
|
-
|
|
186
|
+
purchases: PurchaseInfo[];
|
|
174
187
|
}) => string);
|
|
175
188
|
}
|
|
176
|
-
interface
|
|
189
|
+
interface PurchaseGateProps {
|
|
177
190
|
requirePlan?: string;
|
|
191
|
+
requireProduct?: string;
|
|
178
192
|
children: (props: {
|
|
179
193
|
hasAccess: boolean;
|
|
180
|
-
|
|
194
|
+
purchases: PurchaseInfo[];
|
|
181
195
|
loading: boolean;
|
|
182
196
|
}) => React.ReactNode;
|
|
183
197
|
}
|
|
@@ -189,7 +203,7 @@ interface PaymentError extends Error {
|
|
|
189
203
|
type?: string;
|
|
190
204
|
}
|
|
191
205
|
/**
|
|
192
|
-
* Plan interface for
|
|
206
|
+
* Plan interface for plans
|
|
193
207
|
*/
|
|
194
208
|
interface Plan {
|
|
195
209
|
reference: string;
|
|
@@ -209,11 +223,11 @@ interface UsePlansOptions {
|
|
|
209
223
|
/**
|
|
210
224
|
* Fetcher function to retrieve plans
|
|
211
225
|
*/
|
|
212
|
-
fetcher: (
|
|
226
|
+
fetcher: (productRef: string) => Promise<Plan[]>;
|
|
213
227
|
/**
|
|
214
|
-
*
|
|
228
|
+
* Product reference to fetch plans for
|
|
215
229
|
*/
|
|
216
|
-
|
|
230
|
+
productRef?: string;
|
|
217
231
|
/**
|
|
218
232
|
* Optional filter function to filter plans
|
|
219
233
|
*/
|
|
@@ -245,13 +259,13 @@ interface UsePlansReturn {
|
|
|
245
259
|
*/
|
|
246
260
|
interface PlanSelectorProps {
|
|
247
261
|
/**
|
|
248
|
-
*
|
|
262
|
+
* Product reference to fetch plans for
|
|
249
263
|
*/
|
|
250
|
-
|
|
264
|
+
productRef?: string;
|
|
251
265
|
/**
|
|
252
266
|
* Fetcher function to retrieve plans
|
|
253
267
|
*/
|
|
254
|
-
fetcher: (
|
|
268
|
+
fetcher: (productRef: string) => Promise<Plan[]>;
|
|
255
269
|
/**
|
|
256
270
|
* Optional filter function
|
|
257
271
|
*/
|
|
@@ -268,27 +282,27 @@ interface PlanSelectorProps {
|
|
|
268
282
|
* Render prop function
|
|
269
283
|
*/
|
|
270
284
|
children: (props: UsePlansReturn & {
|
|
271
|
-
|
|
285
|
+
purchases: PurchaseInfo[];
|
|
272
286
|
isPaidPlan: (planName: string) => boolean;
|
|
273
287
|
isCurrentPlan: (planName: string) => boolean;
|
|
274
288
|
}) => React.ReactNode;
|
|
275
289
|
}
|
|
276
290
|
/**
|
|
277
|
-
* Return type for
|
|
291
|
+
* Return type for usePurchaseStatus hook
|
|
278
292
|
*
|
|
279
|
-
* Provides advanced
|
|
280
|
-
* Focuses on cancelled
|
|
281
|
-
* For basic
|
|
293
|
+
* Provides advanced purchase status helpers and utilities.
|
|
294
|
+
* Focuses on cancelled purchase logic and date formatting.
|
|
295
|
+
* For basic purchase data and paid status, use usePurchase() instead.
|
|
282
296
|
*/
|
|
283
|
-
interface
|
|
297
|
+
interface PurchaseStatusReturn {
|
|
284
298
|
/**
|
|
285
|
-
* Most recent cancelled paid
|
|
286
|
-
* null if no cancelled paid
|
|
299
|
+
* Most recent cancelled paid purchase (sorted by startDate)
|
|
300
|
+
* null if no cancelled paid purchase exists
|
|
287
301
|
*/
|
|
288
|
-
|
|
302
|
+
cancelledPurchase: PurchaseInfo | null;
|
|
289
303
|
/**
|
|
290
|
-
* Whether to show cancelled
|
|
291
|
-
* true if
|
|
304
|
+
* Whether to show cancelled purchase notice
|
|
305
|
+
* true if cancelledPurchase exists
|
|
292
306
|
*/
|
|
293
307
|
shouldShowCancelledNotice: boolean;
|
|
294
308
|
/**
|
|
@@ -312,9 +326,9 @@ interface PaymentFormProps {
|
|
|
312
326
|
*/
|
|
313
327
|
planRef: string;
|
|
314
328
|
/**
|
|
315
|
-
*
|
|
329
|
+
* Product reference. Required for processing payment after confirmation.
|
|
316
330
|
*/
|
|
317
|
-
|
|
331
|
+
productRef?: string;
|
|
318
332
|
/**
|
|
319
333
|
* Callback when payment succeeds
|
|
320
334
|
*/
|
|
@@ -340,16 +354,17 @@ interface PaymentFormProps {
|
|
|
340
354
|
*/
|
|
341
355
|
buttonClassName?: string;
|
|
342
356
|
}
|
|
357
|
+
type PurchaseStatusValue = 'pending' | 'active' | 'trialing' | 'past_due' | 'cancelled' | 'expired' | 'suspended' | 'refunded';
|
|
343
358
|
|
|
344
359
|
/**
|
|
345
360
|
* SolvaPay Provider - Headless Context Provider for React.
|
|
346
361
|
*
|
|
347
|
-
* Provides
|
|
362
|
+
* Provides purchase state, payment methods, and customer data to child components
|
|
348
363
|
* via React Context. This is the root component that must wrap your app to use
|
|
349
364
|
* SolvaPay React hooks and components.
|
|
350
365
|
*
|
|
351
366
|
* Features:
|
|
352
|
-
* - Automatic
|
|
367
|
+
* - Automatic purchase status checking
|
|
353
368
|
* - Customer reference caching in localStorage
|
|
354
369
|
* - Payment intent creation and processing
|
|
355
370
|
* - Authentication adapter support (Supabase, custom, etc.)
|
|
@@ -358,7 +373,7 @@ interface PaymentFormProps {
|
|
|
358
373
|
* @param props - Provider configuration
|
|
359
374
|
* @param props.config - Configuration object for API routes and authentication
|
|
360
375
|
* @param props.config.api - API route configuration (optional, uses defaults if not provided)
|
|
361
|
-
* @param props.config.api.
|
|
376
|
+
* @param props.config.api.checkPurchase - Endpoint for checking purchase status (default: '/api/check-purchase')
|
|
362
377
|
* @param props.config.api.createPayment - Endpoint for creating payment intents (default: '/api/create-payment-intent')
|
|
363
378
|
* @param props.config.api.processPayment - Endpoint for processing payments (default: '/api/process-payment')
|
|
364
379
|
* @param props.config.auth - Authentication configuration (optional)
|
|
@@ -384,7 +399,7 @@ interface PaymentFormProps {
|
|
|
384
399
|
* <SolvaPayProvider
|
|
385
400
|
* config={{
|
|
386
401
|
* api: {
|
|
387
|
-
*
|
|
402
|
+
* checkPurchase: '/custom/api/purchase',
|
|
388
403
|
* createPayment: '/custom/api/payment'
|
|
389
404
|
* }
|
|
390
405
|
* }}
|
|
@@ -415,7 +430,7 @@ interface PaymentFormProps {
|
|
|
415
430
|
* }
|
|
416
431
|
* ```
|
|
417
432
|
*
|
|
418
|
-
* @see {@link
|
|
433
|
+
* @see {@link usePurchase} for accessing purchase data
|
|
419
434
|
* @see {@link useCheckout} for payment checkout flow
|
|
420
435
|
* @see {@link useSolvaPay} for accessing provider methods
|
|
421
436
|
* @since 1.0.0
|
|
@@ -434,11 +449,11 @@ declare const SolvaPayProvider: React$1.FC<SolvaPayProviderProps>;
|
|
|
434
449
|
* - Payment intent creation on mount
|
|
435
450
|
* - Card input and validation
|
|
436
451
|
* - Payment processing with error handling
|
|
437
|
-
* - Automatic
|
|
452
|
+
* - Automatic purchase refresh after payment
|
|
438
453
|
*
|
|
439
454
|
* @param props - Payment form configuration
|
|
440
|
-
* @param props.planRef - Plan reference to
|
|
441
|
-
* @param props.
|
|
455
|
+
* @param props.planRef - Plan reference to purchase (required)
|
|
456
|
+
* @param props.productRef - Product reference for usage tracking
|
|
442
457
|
* @param props.onSuccess - Callback when payment succeeds
|
|
443
458
|
* @param props.onError - Callback when payment fails
|
|
444
459
|
* @param props.returnUrl - Optional return URL after payment (for redirects)
|
|
@@ -457,7 +472,7 @@ declare const SolvaPayProvider: React$1.FC<SolvaPayProviderProps>;
|
|
|
457
472
|
* return (
|
|
458
473
|
* <PaymentForm
|
|
459
474
|
* planRef="pln_premium"
|
|
460
|
-
*
|
|
475
|
+
* productRef="prd_myapi"
|
|
461
476
|
* onSuccess={() => {
|
|
462
477
|
* console.log('Payment successful!');
|
|
463
478
|
* router.push('/dashboard');
|
|
@@ -479,20 +494,20 @@ declare const PaymentForm: React$1.FC<PaymentFormProps>;
|
|
|
479
494
|
/**
|
|
480
495
|
* Headless Plan Badge Component
|
|
481
496
|
*
|
|
482
|
-
* Displays
|
|
497
|
+
* Displays purchase status with complete styling control.
|
|
483
498
|
* Supports render props, custom components, or className patterns.
|
|
484
499
|
*
|
|
485
|
-
* Prevents flickering by hiding the badge during initial load and when no
|
|
486
|
-
* Shows the badge once loading completes AND an active
|
|
500
|
+
* Prevents flickering by hiding the badge during initial load and when no purchase exists.
|
|
501
|
+
* Shows the badge once loading completes AND an active purchase exists (paid or free).
|
|
487
502
|
* Badge only updates when the plan name actually changes (prevents unnecessary re-renders).
|
|
488
503
|
*
|
|
489
|
-
* Displays the primary active
|
|
504
|
+
* Displays the primary active purchase (paid or free) to show current plan status.
|
|
490
505
|
*
|
|
491
506
|
* @example
|
|
492
507
|
* ```tsx
|
|
493
508
|
* // Render prop pattern
|
|
494
509
|
* <PlanBadge>
|
|
495
|
-
* {({
|
|
510
|
+
* {({ purchases, loading, displayPlan, shouldShow }) => (
|
|
496
511
|
* shouldShow ? (
|
|
497
512
|
* <div>{displayPlan}</div>
|
|
498
513
|
* ) : null
|
|
@@ -506,42 +521,42 @@ declare const PaymentForm: React$1.FC<PaymentFormProps>;
|
|
|
506
521
|
declare const PlanBadge: React$1.FC<PlanBadgeProps>;
|
|
507
522
|
|
|
508
523
|
/**
|
|
509
|
-
* Headless
|
|
524
|
+
* Headless Purchase Gate Component
|
|
510
525
|
*
|
|
511
|
-
* Controls access to content based on
|
|
526
|
+
* Controls access to content based on purchase status.
|
|
512
527
|
* Uses render props to give developers full control over locked/unlocked states.
|
|
513
528
|
*
|
|
514
529
|
* @example
|
|
515
530
|
* ```tsx
|
|
516
|
-
* <
|
|
531
|
+
* <PurchaseGate requirePlan="Pro Plan">
|
|
517
532
|
* {({ hasAccess, loading }) => {
|
|
518
533
|
* if (loading) return <Skeleton />;
|
|
519
534
|
* if (!hasAccess) return <Paywall />;
|
|
520
535
|
* return <PremiumContent />;
|
|
521
536
|
* }}
|
|
522
|
-
* </
|
|
537
|
+
* </PurchaseGate>
|
|
523
538
|
* ```
|
|
524
539
|
*/
|
|
525
|
-
declare const
|
|
540
|
+
declare const PurchaseGate: React$1.FC<PurchaseGateProps>;
|
|
526
541
|
|
|
527
542
|
/**
|
|
528
543
|
* Headless Plan Selector Component
|
|
529
544
|
*
|
|
530
545
|
* Provides plan selection logic with complete styling control via render props.
|
|
531
|
-
* Integrates plan fetching,
|
|
546
|
+
* Integrates plan fetching, purchase status, and selection state management.
|
|
532
547
|
*
|
|
533
548
|
* Features:
|
|
534
549
|
* - Fetches and manages plans
|
|
535
550
|
* - Tracks selected plan
|
|
536
551
|
* - Provides helpers for checking if plan is current/paid
|
|
537
|
-
* - Integrates with
|
|
552
|
+
* - Integrates with purchase context
|
|
538
553
|
*
|
|
539
554
|
* @example
|
|
540
555
|
* ```tsx
|
|
541
556
|
* <PlanSelector
|
|
542
|
-
*
|
|
543
|
-
* fetcher={async (
|
|
544
|
-
* const res = await fetch(`/api/list-plans?
|
|
557
|
+
* productRef="prd_123"
|
|
558
|
+
* fetcher={async (productRef) => {
|
|
559
|
+
* const res = await fetch(`/api/list-plans?productRef=${productRef}`);
|
|
545
560
|
* const data = await res.json();
|
|
546
561
|
* return data.plans;
|
|
547
562
|
* }}
|
|
@@ -596,36 +611,36 @@ interface StripePaymentFormWrapperProps {
|
|
|
596
611
|
declare const StripePaymentFormWrapper: React$1.FC<StripePaymentFormWrapperProps>;
|
|
597
612
|
|
|
598
613
|
/**
|
|
599
|
-
* Hook to get current
|
|
614
|
+
* Hook to get current purchase status and information.
|
|
600
615
|
*
|
|
601
|
-
* Returns the current user's
|
|
602
|
-
*
|
|
616
|
+
* Returns the current user's purchase status, including active
|
|
617
|
+
* purchases, plan details, and payment information. Automatically
|
|
603
618
|
* syncs with the SolvaPay backend and handles loading and error states.
|
|
604
619
|
*
|
|
605
|
-
* @returns
|
|
606
|
-
* @returns
|
|
607
|
-
* @returns
|
|
620
|
+
* @returns Purchase data and status
|
|
621
|
+
* @returns purchases - Array of active purchases
|
|
622
|
+
* @returns hasPaidPurchase - Whether user has any paid purchase
|
|
608
623
|
* @returns isLoading - Loading state
|
|
609
|
-
* @returns error - Error state if
|
|
610
|
-
* @returns refetch - Function to manually refetch
|
|
624
|
+
* @returns error - Error state if purchase check fails
|
|
625
|
+
* @returns refetch - Function to manually refetch purchase data
|
|
611
626
|
*
|
|
612
627
|
* @example
|
|
613
628
|
* ```tsx
|
|
614
|
-
* import {
|
|
629
|
+
* import { usePurchase } from '@solvapay/react';
|
|
615
630
|
*
|
|
616
631
|
* function Dashboard() {
|
|
617
|
-
* const {
|
|
632
|
+
* const { purchases, hasPaidPurchase, isLoading, refetch } = usePurchase();
|
|
618
633
|
*
|
|
619
634
|
* if (isLoading) return <Spinner />;
|
|
620
635
|
*
|
|
621
|
-
* if (!
|
|
636
|
+
* if (!hasPaidPurchase) {
|
|
622
637
|
* return <UpgradePrompt />;
|
|
623
638
|
* }
|
|
624
639
|
*
|
|
625
640
|
* return (
|
|
626
641
|
* <div>
|
|
627
642
|
* <h2>Welcome, Premium User!</h2>
|
|
628
|
-
* <p>Active
|
|
643
|
+
* <p>Active purchases: {purchases.length}</p>
|
|
629
644
|
* <button onClick={() => refetch()}>Refresh</button>
|
|
630
645
|
* </div>
|
|
631
646
|
* );
|
|
@@ -633,10 +648,10 @@ declare const StripePaymentFormWrapper: React$1.FC<StripePaymentFormWrapperProps
|
|
|
633
648
|
* ```
|
|
634
649
|
*
|
|
635
650
|
* @see {@link SolvaPayProvider} for required context provider
|
|
636
|
-
* @see {@link
|
|
651
|
+
* @see {@link usePurchaseStatus} for detailed status information
|
|
637
652
|
* @since 1.0.0
|
|
638
653
|
*/
|
|
639
|
-
declare function
|
|
654
|
+
declare function usePurchase(): PurchaseStatus & {
|
|
640
655
|
refetch: () => Promise<void>;
|
|
641
656
|
};
|
|
642
657
|
|
|
@@ -663,7 +678,7 @@ interface CustomerInfo {
|
|
|
663
678
|
}
|
|
664
679
|
/**
|
|
665
680
|
* Hook to access customer information
|
|
666
|
-
* Returns customer data (email, name, customerRef) separate from
|
|
681
|
+
* Returns customer data (email, name, customerRef) separate from purchase data
|
|
667
682
|
*
|
|
668
683
|
* @example
|
|
669
684
|
* ```tsx
|
|
@@ -698,8 +713,9 @@ interface UseCheckoutReturn {
|
|
|
698
713
|
* manages the checkout state including loading, errors, Stripe instance,
|
|
699
714
|
* and client secret. Use this for programmatic checkout flows.
|
|
700
715
|
*
|
|
701
|
-
* @param
|
|
702
|
-
* @param
|
|
716
|
+
* @param options - Checkout options
|
|
717
|
+
* @param options.planRef - Plan reference to purchase (required)
|
|
718
|
+
* @param options.productRef - Optional product reference for usage tracking
|
|
703
719
|
* @returns Checkout state and methods
|
|
704
720
|
* @returns loading - Whether checkout is in progress
|
|
705
721
|
* @returns error - Error state if checkout fails
|
|
@@ -714,10 +730,10 @@ interface UseCheckoutReturn {
|
|
|
714
730
|
* import { PaymentElement } from '@stripe/react-stripe-js';
|
|
715
731
|
*
|
|
716
732
|
* function CustomCheckout() {
|
|
717
|
-
* const { loading, error, stripePromise, clientSecret, startCheckout } = useCheckout(
|
|
718
|
-
* 'pln_premium',
|
|
719
|
-
* '
|
|
720
|
-
* );
|
|
733
|
+
* const { loading, error, stripePromise, clientSecret, startCheckout } = useCheckout({
|
|
734
|
+
* planRef: 'pln_premium',
|
|
735
|
+
* productRef: 'prd_myapi',
|
|
736
|
+
* });
|
|
721
737
|
*
|
|
722
738
|
* useEffect(() => {
|
|
723
739
|
* startCheckout();
|
|
@@ -739,46 +755,49 @@ interface UseCheckoutReturn {
|
|
|
739
755
|
* @see {@link SolvaPayProvider} for required context provider
|
|
740
756
|
* @since 1.0.0
|
|
741
757
|
*/
|
|
742
|
-
declare function useCheckout(
|
|
758
|
+
declare function useCheckout(options: {
|
|
759
|
+
planRef: string;
|
|
760
|
+
productRef?: string;
|
|
761
|
+
}): UseCheckoutReturn;
|
|
743
762
|
|
|
744
763
|
/**
|
|
745
764
|
* Hook to access SolvaPay context and provider methods.
|
|
746
765
|
*
|
|
747
766
|
* This is the base hook that provides access to all SolvaPay functionality
|
|
748
|
-
* including
|
|
749
|
-
* Other hooks like `
|
|
767
|
+
* including purchase data, payment methods, and customer information.
|
|
768
|
+
* Other hooks like `usePurchase` and `useCheckout` use this internally.
|
|
750
769
|
*
|
|
751
770
|
* Must be used within a `SolvaPayProvider` component.
|
|
752
771
|
*
|
|
753
772
|
* @returns SolvaPay context value with all provider methods and state
|
|
754
|
-
* @returns
|
|
773
|
+
* @returns purchase - Purchase status and data
|
|
755
774
|
* @returns createPayment - Function to create payment intents
|
|
756
775
|
* @returns processPayment - Function to process payments
|
|
757
776
|
* @returns customerRef - Current customer reference
|
|
758
|
-
* @returns
|
|
777
|
+
* @returns refetchPurchase - Function to refetch purchase data
|
|
759
778
|
*
|
|
760
779
|
* @example
|
|
761
780
|
* ```tsx
|
|
762
781
|
* import { useSolvaPay } from '@solvapay/react';
|
|
763
782
|
*
|
|
764
783
|
* function CustomComponent() {
|
|
765
|
-
* const {
|
|
784
|
+
* const { purchase, createPayment, processPayment } = useSolvaPay();
|
|
766
785
|
*
|
|
767
786
|
* const handlePayment = async () => {
|
|
768
787
|
* const intent = await createPayment({
|
|
769
788
|
* planRef: 'pln_premium',
|
|
770
|
-
*
|
|
789
|
+
* productRef: 'prd_myapi'
|
|
771
790
|
* });
|
|
772
791
|
* // Process payment...
|
|
773
792
|
* };
|
|
774
793
|
*
|
|
775
|
-
* return <div>
|
|
794
|
+
* return <div>Purchase status: {purchase.hasPaidPurchase ? 'Active' : 'None'}</div>;
|
|
776
795
|
* }
|
|
777
796
|
* ```
|
|
778
797
|
*
|
|
779
798
|
* @throws {Error} If used outside of SolvaPayProvider
|
|
780
799
|
* @see {@link SolvaPayProvider} for required context provider
|
|
781
|
-
* @see {@link
|
|
800
|
+
* @see {@link usePurchase} for purchase-specific hook
|
|
782
801
|
* @see {@link useCheckout} for checkout-specific hook
|
|
783
802
|
* @since 1.0.0
|
|
784
803
|
*/
|
|
@@ -787,16 +806,16 @@ declare function useSolvaPay(): SolvaPayContextValue;
|
|
|
787
806
|
/**
|
|
788
807
|
* Hook to manage plan fetching and selection
|
|
789
808
|
*
|
|
790
|
-
* Provides a reusable way to fetch, filter, sort and select
|
|
809
|
+
* Provides a reusable way to fetch, filter, sort and select plans.
|
|
791
810
|
* Handles loading and error states automatically.
|
|
792
|
-
* Uses a global cache to prevent duplicate fetches when multiple components use the same
|
|
811
|
+
* Uses a global cache to prevent duplicate fetches when multiple components use the same productRef.
|
|
793
812
|
*
|
|
794
813
|
* @example
|
|
795
814
|
* ```tsx
|
|
796
815
|
* const plans = usePlans({
|
|
797
|
-
*
|
|
798
|
-
* fetcher: async (
|
|
799
|
-
* const res = await fetch(`/api/list-plans?
|
|
816
|
+
* productRef: 'prd_123',
|
|
817
|
+
* fetcher: async (productRef) => {
|
|
818
|
+
* const res = await fetch(`/api/list-plans?productRef=${productRef}`);
|
|
800
819
|
* const data = await res.json();
|
|
801
820
|
* return data.plans;
|
|
802
821
|
* },
|
|
@@ -812,78 +831,78 @@ declare function useSolvaPay(): SolvaPayContextValue;
|
|
|
812
831
|
declare function usePlans(options: UsePlansOptions): UsePlansReturn;
|
|
813
832
|
|
|
814
833
|
/**
|
|
815
|
-
* Hook providing advanced status and helper functions for
|
|
834
|
+
* Hook providing advanced status and helper functions for purchase management
|
|
816
835
|
*
|
|
817
|
-
* Focuses on cancelled
|
|
818
|
-
* For basic
|
|
836
|
+
* Focuses on cancelled purchase logic and date formatting utilities.
|
|
837
|
+
* For basic purchase data and paid status checks, use usePurchase() instead.
|
|
819
838
|
*
|
|
820
839
|
* @example
|
|
821
840
|
* ```tsx
|
|
822
|
-
* const {
|
|
841
|
+
* const { cancelledPurchase, shouldShowCancelledNotice, formatDate, getDaysUntilExpiration } = usePurchaseStatus();
|
|
823
842
|
*
|
|
824
|
-
* if (shouldShowCancelledNotice &&
|
|
825
|
-
* const formattedDate = formatDate(
|
|
826
|
-
* const daysLeft = getDaysUntilExpiration(
|
|
843
|
+
* if (shouldShowCancelledNotice && cancelledPurchase) {
|
|
844
|
+
* const formattedDate = formatDate(cancelledPurchase.endDate);
|
|
845
|
+
* const daysLeft = getDaysUntilExpiration(cancelledPurchase.endDate);
|
|
827
846
|
* }
|
|
828
847
|
* ```
|
|
829
848
|
*/
|
|
830
|
-
declare function
|
|
849
|
+
declare function usePurchaseStatus(): PurchaseStatusReturn;
|
|
831
850
|
|
|
832
851
|
/**
|
|
833
|
-
*
|
|
852
|
+
* Purchase utility functions
|
|
834
853
|
*
|
|
835
|
-
* Provides shared logic for filtering and prioritizing
|
|
854
|
+
* Provides shared logic for filtering and prioritizing purchases
|
|
836
855
|
*/
|
|
837
856
|
|
|
838
857
|
/**
|
|
839
|
-
* Filter
|
|
858
|
+
* Filter purchases to only include active ones
|
|
840
859
|
*
|
|
841
860
|
* Rules:
|
|
842
|
-
* - Keep
|
|
843
|
-
* - Filter out
|
|
861
|
+
* - Keep purchases with status === 'active'
|
|
862
|
+
* - Filter out purchases with status === 'cancelled', 'expired', 'suspended', 'refunded', etc.
|
|
844
863
|
*
|
|
845
|
-
* Note: Backend now keeps
|
|
864
|
+
* Note: Backend now keeps purchases with status 'active' until expiration,
|
|
846
865
|
* even when cancelled. Cancellation is tracked via cancelledAt field.
|
|
847
866
|
*/
|
|
848
|
-
declare function
|
|
867
|
+
declare function filterPurchases(purchases: PurchaseInfo[]): PurchaseInfo[];
|
|
849
868
|
/**
|
|
850
|
-
* Get active
|
|
869
|
+
* Get active purchases
|
|
851
870
|
*
|
|
852
|
-
* Returns
|
|
853
|
-
* Note: Backend keeps
|
|
854
|
-
* Use cancelledAt field to check if a
|
|
871
|
+
* Returns purchases with status === 'active'.
|
|
872
|
+
* Note: Backend keeps purchases as 'active' until expiration, even when cancelled.
|
|
873
|
+
* Use cancelledAt field to check if a purchase is cancelled.
|
|
855
874
|
*/
|
|
856
|
-
declare function
|
|
875
|
+
declare function getActivePurchases(purchases: PurchaseInfo[]): PurchaseInfo[];
|
|
857
876
|
/**
|
|
858
|
-
* Get cancelled
|
|
877
|
+
* Get cancelled purchases with valid endDate (not expired)
|
|
859
878
|
*
|
|
860
|
-
* Returns
|
|
861
|
-
* Backend keeps cancelled
|
|
879
|
+
* Returns purchases with cancelledAt set and status === 'active' that have a future endDate.
|
|
880
|
+
* Backend keeps cancelled purchases as 'active' until expiration.
|
|
862
881
|
*/
|
|
863
|
-
declare function
|
|
882
|
+
declare function getCancelledPurchasesWithEndDate(purchases: PurchaseInfo[]): PurchaseInfo[];
|
|
864
883
|
/**
|
|
865
|
-
* Get the most recent
|
|
884
|
+
* Get the most recent purchase by startDate
|
|
866
885
|
*/
|
|
867
|
-
declare function
|
|
886
|
+
declare function getMostRecentPurchase(purchases: PurchaseInfo[]): PurchaseInfo | null;
|
|
868
887
|
/**
|
|
869
|
-
* Get the primary
|
|
888
|
+
* Get the primary purchase to display
|
|
870
889
|
*
|
|
871
890
|
* Prioritization:
|
|
872
|
-
* 1. Active
|
|
873
|
-
* 2. null if no valid
|
|
891
|
+
* 1. Active purchases (most recent by startDate)
|
|
892
|
+
* 2. null if no valid purchases
|
|
874
893
|
*
|
|
875
|
-
* Note: Backend keeps
|
|
876
|
-
* need to check for active
|
|
894
|
+
* Note: Backend keeps purchases as 'active' until expiration, so we only
|
|
895
|
+
* need to check for active purchases. Cancelled purchases are still
|
|
877
896
|
* active until their endDate.
|
|
878
897
|
*/
|
|
879
|
-
declare function
|
|
898
|
+
declare function getPrimaryPurchase(purchases: PurchaseInfo[]): PurchaseInfo | null;
|
|
880
899
|
/**
|
|
881
|
-
* Check if a
|
|
882
|
-
* Uses
|
|
900
|
+
* Check if a purchase is paid
|
|
901
|
+
* Uses purchase amount field: amount > 0 = paid, amount === 0 or undefined = free
|
|
883
902
|
*
|
|
884
|
-
* @param
|
|
885
|
-
* @returns true if
|
|
903
|
+
* @param purchase - Purchase to check
|
|
904
|
+
* @returns true if purchase is paid (amount > 0)
|
|
886
905
|
*/
|
|
887
|
-
declare function
|
|
906
|
+
declare function isPaidPurchase(purchase: PurchaseInfo): boolean;
|
|
888
907
|
|
|
889
|
-
export { AuthAdapter, type CustomerInfo, type
|
|
908
|
+
export { AuthAdapter, type CustomerInfo, type CustomerPurchaseData, type PaymentError, PaymentForm, type PaymentFormProps, type PaymentIntentResult, type Plan, PlanBadge, type PlanBadgeProps, PlanSelector, type PlanSelectorProps, PurchaseGate, type PurchaseGateProps, type PurchaseInfo, type PurchaseStatus, type PurchaseStatusReturn, type PurchaseStatusValue, type SolvaPayConfig, type SolvaPayContextValue, SolvaPayProvider, type SolvaPayProviderProps, Spinner, StripePaymentFormWrapper, type UsePlansOptions, type UsePlansReturn, filterPurchases, getActivePurchases, getCancelledPurchasesWithEndDate, getMostRecentPurchase, getPrimaryPurchase, isPaidPurchase, useCheckout, useCustomer, usePlans, usePurchase, usePurchaseStatus, useSolvaPay };
|