@solvapay/react 1.0.0-preview.4 → 1.0.0-preview.5

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 CHANGED
@@ -1,34 +1,167 @@
1
1
  # @solvapay/react
2
2
 
3
- Payment components for SolvaPay with Stripe integration.
3
+ Headless React components and hooks for SolvaPay payment integration with Stripe.
4
4
 
5
5
  ## Install
6
+
6
7
  ```bash
7
8
  pnpm add @solvapay/react
8
9
  ```
9
10
 
10
- ## Usage
11
+ ## Peer Dependencies
12
+
13
+ - `react` ^18.2.0 || ^19.0.0
14
+ - `react-dom` ^18.2.0 || ^19.0.0
15
+
16
+ ## Quick Start
17
+
11
18
  ```tsx
12
- import { SolvaPayProvider, PaymentForm } from '@solvapay/react';
19
+ import { SolvaPayProvider, UpgradeButton, useSubscription } from '@solvapay/react';
13
20
 
14
- export default function CheckoutPage() {
21
+ export default function App() {
15
22
  return (
16
23
  <SolvaPayProvider
17
- amount={999}
18
- currency="USD"
19
- planRef="pln_abc123"
24
+ customerRef={user?.id}
25
+ createPayment={async ({ planRef, customerRef }) => {
26
+ const res = await fetch('/api/payments/create', {
27
+ method: 'POST',
28
+ headers: { 'Content-Type': 'application/json' },
29
+ body: JSON.stringify({ planRef, customerRef }),
30
+ });
31
+ if (!res.ok) throw new Error('Failed to create payment');
32
+ return res.json();
33
+ }}
34
+ checkSubscription={async (customerRef) => {
35
+ const res = await fetch(`/api/subscriptions/${customerRef}`);
36
+ if (!res.ok) throw new Error('Failed to check subscription');
37
+ return res.json();
38
+ }}
20
39
  >
21
- <PaymentForm
22
- returnUrl="/checkout/success"
23
- onSuccess={(paymentIntent) => {
24
- console.log('Payment successful!', paymentIntent);
25
- }}
26
- />
40
+ <CheckoutPage />
27
41
  </SolvaPayProvider>
28
42
  );
29
43
  }
30
44
  ```
31
45
 
32
- Peer deps: react, react-dom
46
+ ## Components
47
+
48
+ ### SolvaPayProvider
49
+
50
+ Headless context provider that manages subscription state and payment methods.
51
+
52
+ **Props:**
53
+ - `createPayment: (params: { planRef: string; customerRef: string }) => Promise<PaymentIntentResult>` - Function to create payment intent
54
+ - `checkSubscription: (customerRef: string) => Promise<CustomerSubscriptionData>` - Function to check subscription status
55
+ - `customerRef?: string` - Optional customer reference
56
+ - `onCustomerRefUpdate?: (newCustomerRef: string) => void` - Callback when customer ref updates
57
+ - `children: React.ReactNode` - Child components
58
+
59
+ ### UpgradeButton
60
+
61
+ Headless component for handling upgrade flows with render props pattern.
62
+
63
+ **Props:**
64
+ - `planRef: string` - Plan reference to upgrade to
65
+ - `onSuccess?: () => void` - Callback on successful payment
66
+ - `onError?: (error: Error) => void` - Callback on payment error
67
+ - `children: (props) => React.ReactNode` - Render prop function
68
+ - `renderPaymentForm?: (props) => React.ReactNode` - Custom payment form renderer
69
+ - `paymentFormContainerClassName?: string` - Optional className for container
70
+ - `cancelButtonClassName?: string` - Optional className for cancel button
71
+
72
+ **Example:**
73
+ ```tsx
74
+ <UpgradeButton planRef="pro_plan">
75
+ {({ onClick, loading, disabled, error }) => (
76
+ <button onClick={onClick} disabled={disabled}>
77
+ {loading ? 'Processing...' : 'Upgrade to Pro'}
78
+ {error && <span>Error: {error.message}</span>}
79
+ </button>
80
+ )}
81
+ </UpgradeButton>
82
+ ```
83
+
84
+ ### PaymentForm
85
+
86
+ Payment form component using Stripe PaymentElement. Must be wrapped in Stripe Elements provider (provided by UpgradeButton or manually).
87
+
88
+ **Props:**
89
+ - `onSuccess?: (paymentIntent: PaymentIntent) => void` - Callback on successful payment
90
+ - `onError?: (error: Error) => void` - Callback on payment error
91
+ - `returnUrl?: string` - Return URL after payment
92
+ - `submitButtonText?: string` - Submit button text (default: "Pay Now")
93
+ - `className?: string` - Form className (legacy, use formClassName)
94
+ - `formClassName?: string` - Form element className
95
+ - `messageClassName?: string` - Message container className
96
+ - `buttonClassName?: string` - Submit button className
97
+
98
+ ### PlanBadge
99
+
100
+ Displays current subscription plan with render props or className pattern.
101
+
102
+ **Props:**
103
+ - `children?: (props) => React.ReactNode` - Render prop function
104
+ - `as?: React.ElementType` - Component to render (default: "div")
105
+ - `className?: string | ((props) => string)` - ClassName or function
106
+
107
+ **Example:**
108
+ ```tsx
109
+ <PlanBadge className="badge badge-primary" />
110
+ ```
111
+
112
+ ### SubscriptionGate
113
+
114
+ Controls access to content based on subscription status.
115
+
116
+ **Props:**
117
+ - `requirePlan?: string` - Optional plan name to require
118
+ - `children: (props) => React.ReactNode` - Render prop function
119
+
120
+ **Example:**
121
+ ```tsx
122
+ <SubscriptionGate requirePlan="Pro Plan">
123
+ {({ hasAccess, loading, subscriptions }) => {
124
+ if (loading) return <Loading />;
125
+ if (!hasAccess) return <Paywall />;
126
+ return <PremiumContent />;
127
+ }}
128
+ </SubscriptionGate>
129
+ ```
130
+
131
+ ## Hooks
132
+
133
+ ### useSubscription
134
+
135
+ Access subscription status and refetch function.
136
+
137
+ ```tsx
138
+ const { subscriptions, loading, hasActiveSubscription, hasPlan, refetch } = useSubscription();
139
+ ```
140
+
141
+ ### useCheckout
142
+
143
+ Manage checkout flow for a specific plan.
144
+
145
+ ```tsx
146
+ const { loading, error, startCheckout, reset } = useCheckout('plan_ref');
147
+ ```
148
+
149
+ ### useSolvaPay
150
+
151
+ Access SolvaPay context directly.
152
+
153
+ ```tsx
154
+ const { subscription, createPayment, customerRef } = useSolvaPay();
155
+ ```
156
+
157
+ ## TypeScript
158
+
159
+ All components and hooks are fully typed. Import types as needed:
160
+
161
+ ```tsx
162
+ import type { PaymentFormProps, SubscriptionStatus, PaymentIntentResult } from '@solvapay/react';
163
+ ```
164
+
165
+ ## More Information
33
166
 
34
- More: docs/architecture.md
167
+ See `docs/architecture.md` for detailed architecture documentation.