stripe-no-webhooks 0.0.9 → 0.0.11
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 +112 -36
- package/bin/cli.js +41 -832
- package/bin/commands/backfill.js +389 -0
- package/bin/commands/config.js +272 -0
- package/bin/commands/generate.js +110 -0
- package/bin/commands/helpers/backfill-maps.js +279 -0
- package/bin/commands/helpers/dev-webhook-listener.js +76 -0
- package/bin/commands/helpers/sync-helpers.js +190 -0
- package/bin/commands/helpers/utils.js +168 -0
- package/bin/commands/migrate.js +104 -0
- package/bin/commands/sync.js +433 -0
- package/dist/BillingConfig-CpHPJg4Q.d.mts +54 -0
- package/dist/BillingConfig-CpHPJg4Q.d.ts +54 -0
- package/dist/client.d.mts +32 -8
- package/dist/client.d.ts +32 -8
- package/dist/client.js +82 -20
- package/dist/client.mjs +80 -19
- package/dist/index.d.mts +451 -91
- package/dist/index.d.ts +451 -91
- package/dist/index.js +2718 -261
- package/dist/index.mjs +2712 -260
- package/package.json +6 -2
- package/src/templates/PricingPage.tsx +450 -0
- package/src/templates/app-router.ts +23 -16
- package/src/templates/lib-billing.ts +27 -0
- package/src/templates/pages-router.ts +25 -18
package/README.md
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
# stripe-no-webhooks
|
|
2
2
|
|
|
3
|
-
Opinionated
|
|
3
|
+
Opinionated library to help you implement payments with Stripe. It syncs Stripe data to your database and gives you useful helpers to implement subscriptions and credits.
|
|
4
4
|
|
|
5
5
|
## Why this library?
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
This library is a wrapper on Stripe SDK (with some bells and whistles). It gives you an opinionated and clear path to implement payments:
|
|
8
|
+
|
|
9
|
+
1. Define plans in code which sync to Stripe
|
|
10
|
+
2. No manual webhook setup - the library handles webhooks and syncs Stripe data to your DB
|
|
11
|
+
3. Simple APIs for subscriptions and credits
|
|
12
|
+
4. Optional callbacks (`onSubscriptionCreated`, etc.) for custom logic
|
|
8
13
|
|
|
9
14
|
## Setup
|
|
10
15
|
|
|
@@ -19,7 +24,7 @@ Note: make sure you also have `.env` or `.env.local` in your project so it can s
|
|
|
19
24
|
### 2. Create tables where all Stripe data will be automatically synced
|
|
20
25
|
|
|
21
26
|
```bash
|
|
22
|
-
npx stripe-no-webhooks migrate postgresql://
|
|
27
|
+
npx stripe-no-webhooks migrate postgresql://[USER]:[PASSWORD]@[DB_URL]/postgres
|
|
23
28
|
```
|
|
24
29
|
|
|
25
30
|
### 3. Run `config` to generate files & webhook
|
|
@@ -28,11 +33,49 @@ npx stripe-no-webhooks migrate postgresql://postgres.[USER]:[PASSWORD]@[DB_URL]/
|
|
|
28
33
|
npx stripe-no-webhooks config
|
|
29
34
|
```
|
|
30
35
|
|
|
31
|
-
|
|
36
|
+
This creates:
|
|
37
|
+
|
|
38
|
+
- `lib/billing.ts` - Billing instance (optional, for credits/subscriptions API)
|
|
39
|
+
- `app/api/stripe/[...all]/route.ts` - HTTP handler
|
|
40
|
+
- `billing.config.ts` - Your plans
|
|
41
|
+
|
|
42
|
+
### 4. Connect your auth
|
|
43
|
+
|
|
44
|
+
Open `app/api/stripe/[...all]/route.ts` and add your auth:
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { billing } from "@/lib/billing";
|
|
48
|
+
import { auth } from "@clerk/nextjs/server"; // or your auth library
|
|
49
|
+
|
|
50
|
+
export const POST = billing.createHandler({
|
|
51
|
+
resolveUser: async () => {
|
|
52
|
+
const { userId } = await auth();
|
|
53
|
+
return userId ? { id: userId } : null;
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**Simple alternative**: If you don't need credits/subscriptions API, skip `lib/billing.ts`:
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { createHandler } from "stripe-no-webhooks";
|
|
62
|
+
import billingConfig from "@/billing.config";
|
|
63
|
+
|
|
64
|
+
export const POST = createHandler({
|
|
65
|
+
billingConfig,
|
|
66
|
+
resolveUser: async () => {
|
|
67
|
+
const { userId } = await auth();
|
|
68
|
+
return userId ? { id: userId } : null;
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### 5. Create your plans
|
|
32
74
|
|
|
33
75
|
```javascript
|
|
34
|
-
// billing.config.ts
|
|
76
|
+
// billing.config.ts
|
|
35
77
|
import type { BillingConfig } from "stripe-no-webhooks";
|
|
78
|
+
|
|
36
79
|
const billingConfig: BillingConfig = {
|
|
37
80
|
test: {
|
|
38
81
|
plans: [
|
|
@@ -40,17 +83,12 @@ const billingConfig: BillingConfig = {
|
|
|
40
83
|
name: "Premium",
|
|
41
84
|
description: "Access to all features",
|
|
42
85
|
price: [
|
|
43
|
-
{
|
|
44
|
-
|
|
45
|
-
currency: "usd",
|
|
46
|
-
interval: "month",
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
amount: 10000, // $100
|
|
50
|
-
currency: "usd",
|
|
51
|
-
interval: "year",
|
|
52
|
-
},
|
|
86
|
+
{ amount: 1000, currency: "usd", interval: "month" },
|
|
87
|
+
{ amount: 10000, currency: "usd", interval: "year" },
|
|
53
88
|
],
|
|
89
|
+
credits: {
|
|
90
|
+
api_calls: { allocation: 1000 },
|
|
91
|
+
},
|
|
54
92
|
},
|
|
55
93
|
],
|
|
56
94
|
},
|
|
@@ -64,27 +102,65 @@ Run sync:
|
|
|
64
102
|
npx stripe-no-webhooks sync
|
|
65
103
|
```
|
|
66
104
|
|
|
67
|
-
###
|
|
105
|
+
### 6. (optional) Write custom logic for subscriptions
|
|
106
|
+
|
|
107
|
+
You probably want something to happen when a new user subscribes or a subscription cancels. Define callbacks when creating the `Billing` instance:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// lib/billing.ts
|
|
111
|
+
import { Billing } from "stripe-no-webhooks";
|
|
112
|
+
import billingConfig from "../billing.config";
|
|
113
|
+
import type { Stripe } from "stripe";
|
|
114
|
+
|
|
115
|
+
export const billing = new Billing({
|
|
116
|
+
billingConfig,
|
|
117
|
+
callbacks: {
|
|
118
|
+
onSubscriptionCreated: async (subscription: Stripe.Subscription) => {
|
|
119
|
+
console.log("New subscription:", subscription.id);
|
|
120
|
+
},
|
|
121
|
+
onSubscriptionCancelled: async (subscription: Stripe.Subscription) => {
|
|
122
|
+
console.log("Subscription cancelled:", subscription.id);
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Supported callbacks:
|
|
68
129
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
130
|
+
- `onSubscriptionCreated`
|
|
131
|
+
- `onSubscriptionCancelled`
|
|
132
|
+
- `onSubscriptionRenewed`
|
|
133
|
+
- `onSubscriptionPlanChanged`
|
|
134
|
+
- `onCreditsGranted`
|
|
135
|
+
- `onCreditsRevoked`
|
|
136
|
+
- `onTopUpCompleted`
|
|
137
|
+
- `onAutoTopUpFailed`
|
|
138
|
+
- `onCreditsLow`
|
|
139
|
+
|
|
140
|
+
### 7. (optional) Generate a pricing page
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
npx stripe-no-webhooks generate pricing-page
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
This will create a `PricingPage` component in `@/components`. Feel free to edit styling manually or with AI.
|
|
147
|
+
|
|
148
|
+
It is ready-to-use with loading states, error handling, and styling. Import it whenever you want:
|
|
149
|
+
|
|
150
|
+
```tsx
|
|
151
|
+
import { PricingPage } from "@/components/PricingPage";
|
|
152
|
+
import billingConfig from "@/billing.config";
|
|
153
|
+
|
|
154
|
+
export default function Pricing() {
|
|
155
|
+
const plans = billingConfig.test?.plans || [];
|
|
156
|
+
return <PricingPage plans={plans} currentPlanId="free" />;
|
|
89
157
|
}
|
|
90
158
|
```
|
|
159
|
+
|
|
160
|
+
### 8. (optional) Backfill data
|
|
161
|
+
|
|
162
|
+
If you had data in Stripe before deploying `stripe-no-webhooks`, you can backfill your database by running:
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
npx stripe-no-webhooks backfill
|
|
166
|
+
```
|