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 CHANGED
@@ -1,10 +1,15 @@
1
1
  # stripe-no-webhooks
2
2
 
3
- Opinionated & Open Source library that automatically syncs Stripe to your database and gives you useful helpers to implement subscriptions.
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
- Stripe documentation lacks the ability to clearly point you to an easy way to implement Stripe. Depending on what you google you might end up in a weird place and shoot yourself in the foot.
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://postgres.[USER]:[PASSWORD]@[DB_URL]/postgres
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
- ### 4. Create your plans
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 (automatically created during config)
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
- amount: 1000, // $10
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
- ### 5. Implement a checkout button in your frontend:
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
- ```javascript
70
- "use client";
71
- import { checkout } from "stripe-no-webhooks/client";
72
-
73
- export default function Home() {
74
- return (
75
- <div className="min-h-screen flex items-center justify-center">
76
- <button
77
- className="bg-blue-500 text-white px-4 py-2 rounded-md cursor-pointer"
78
- onClick={() =>
79
- checkout({
80
- planName: "Premium",
81
- interval: "month",
82
- })
83
- }
84
- >
85
- Checkout
86
- </button>
87
- </div>
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
+ ```