@raideno/convex-stripe 0.2.1 → 0.2.3
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 +55 -16
- package/dist/index.d.ts +96 -78
- package/dist/server.js +1877 -1780
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Stripe [syncing](./references/tables.md), subscriptions, [checkouts](#-checkout-
|
|
|
8
8
|
npm install @raideno/convex-stripe stripe
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Usage
|
|
12
12
|
|
|
13
13
|
### 1. Set up Stripe
|
|
14
14
|
- Create a Stripe account.
|
|
@@ -42,14 +42,19 @@ export default defineSchema({
|
|
|
42
42
|
|
|
43
43
|
### 4. Initialize the library
|
|
44
44
|
|
|
45
|
-
```ts
|
|
46
|
-
|
|
45
|
+
```ts
|
|
46
|
+
// convex/stripe.ts
|
|
47
|
+
|
|
48
|
+
import { internalConvexStripe, syncAllTables } from "@raideno/convex-stripe/server";
|
|
47
49
|
|
|
48
50
|
export const { stripe, store, sync } = internalConvexStripe({
|
|
49
51
|
stripe: {
|
|
50
52
|
secret_key: process.env.STRIPE_SECRET_KEY!,
|
|
51
53
|
account_webhook_secret: process.env.STRIPE_ACCOUNT_WEBHOOK_SECRET!,
|
|
52
54
|
},
|
|
55
|
+
sync: {
|
|
56
|
+
tables: syncAllTables(),
|
|
57
|
+
},
|
|
53
58
|
});
|
|
54
59
|
|
|
55
60
|
export const createCustomer = internalAction({
|
|
@@ -67,12 +72,14 @@ export const createCustomer = internalAction({
|
|
|
67
72
|
|
|
68
73
|
```
|
|
69
74
|
|
|
70
|
-
> **Note:** All exposed actions (store
|
|
75
|
+
> **Note:** All exposed actions (`store`, `sync`) are **internal**. Meaning they can only be called from other convex functions, you can wrap them in public actions when needed.
|
|
71
76
|
> **Important:** `store` must always be exported, as it is used internally.
|
|
72
77
|
|
|
73
78
|
### 5. Register HTTP routes
|
|
74
79
|
|
|
75
|
-
```ts
|
|
80
|
+
```ts
|
|
81
|
+
// convex/http.ts
|
|
82
|
+
|
|
76
83
|
import { httpRouter } from "convex/server";
|
|
77
84
|
import { stripe } from "./stripe";
|
|
78
85
|
|
|
@@ -89,7 +96,7 @@ export default http;
|
|
|
89
96
|
|
|
90
97
|
Ideally you want to create a stripe customer the moment a new entity (user, organization, etc) is created.
|
|
91
98
|
|
|
92
|
-
An `entityId` refers to something you are billing. It can be a user, organization or any other thing. With each entity must be associated a stripe customer and the stripe customer can be created using the [`
|
|
99
|
+
An `entityId` refers to something you are billing. It can be a user, organization or any other thing. With each entity must be associated a stripe customer and the stripe customer can be created using the [`stripe.customers.create` function](#stripecustomerscreate-function).
|
|
93
100
|
|
|
94
101
|
Below are with different auth providers examples where the user is the entity we are billing.
|
|
95
102
|
|
|
@@ -107,6 +114,7 @@ import { internal } from "./_generated/api";
|
|
|
107
114
|
export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
|
|
108
115
|
providers: [Password],
|
|
109
116
|
callbacks: {
|
|
117
|
+
// NOTE: create a customer immediately after a user is created
|
|
110
118
|
afterUserCreatedOrUpdated: async (context, args) => {
|
|
111
119
|
await context.scheduler.runAfter(0, internal.stripe.createCustomer, {
|
|
112
120
|
/*
|
|
@@ -148,17 +156,39 @@ It must be done in both your development and production deployments after instal
|
|
|
148
156
|
This might not be necessary if you are starting with a fresh empty stripe project.
|
|
149
157
|
|
|
150
158
|
### 8. Start building
|
|
159
|
+
|
|
151
160
|
Now you can use the provided functions to:
|
|
152
161
|
- Generate a subscription or payment link [`stripe.subscribe`](#subscribe-function), [`stripe.pay`](#pay-function) for a given entity.
|
|
153
162
|
- Generate a link to the entity's [`stripe.portal`](#portal-function) to manage their subscriptions.
|
|
154
163
|
- Create stripe connect accounts and link them, [`stripe.accounts.create`](#), [`stripe.accounts.link`](#).
|
|
155
|
-
- Consult the [synced tables](
|
|
164
|
+
- Consult the [synced tables](documentation/references/tables.md).
|
|
156
165
|
- Etc.
|
|
157
166
|
|
|
167
|
+
## Add Stripe Connect
|
|
158
168
|
|
|
159
|
-
|
|
169
|
+
If you wish to also add stripe connect, below is a guide on how to do it. You can find a full example in [`examples/marketplaces/README.md`](examples/marketplace/README.md).
|
|
170
|
+
|
|
171
|
+
### 0. Enable connect on your stripe dashboard.
|
|
172
|
+
...
|
|
160
173
|
|
|
161
|
-
|
|
174
|
+
### 1. Create a connect webhook using `sync` method.
|
|
175
|
+
...
|
|
176
|
+
|
|
177
|
+
### 2. Add the new webhook secret to the dashboard and configuration.
|
|
178
|
+
...
|
|
179
|
+
|
|
180
|
+
### 3. Create Stripe Accounts for Sellers & Onboard them
|
|
181
|
+
...
|
|
182
|
+
|
|
183
|
+
### 4. Create Products for Sellers
|
|
184
|
+
...
|
|
185
|
+
|
|
186
|
+
### 5. Send payouts
|
|
187
|
+
...
|
|
188
|
+
|
|
189
|
+
## References
|
|
190
|
+
|
|
191
|
+
The library automatically syncs the [following tables](documentation/references/tables.md).
|
|
162
192
|
|
|
163
193
|
You can query these tables at any time to:
|
|
164
194
|
|
|
@@ -168,7 +198,7 @@ You can query these tables at any time to:
|
|
|
168
198
|
- Etc.
|
|
169
199
|
|
|
170
200
|
|
|
171
|
-
### `customers.create` Function
|
|
201
|
+
### `stripe.customers.create` Function
|
|
172
202
|
|
|
173
203
|
Creates or updates a Stripe customer for a given entity (user or organization). Will call [`stripe.customers.create`](https://docs.stripe.com/api/customers/create) under the hood.
|
|
174
204
|
|
|
@@ -210,14 +240,23 @@ This action is typically manually called or setup to be automatically called in
|
|
|
210
240
|
|
|
211
241
|
**Parameters:**
|
|
212
242
|
|
|
213
|
-
|
|
214
|
-
|
|
243
|
+
```ts
|
|
244
|
+
{
|
|
245
|
+
data?: boolean | { withConnect: boolean };
|
|
246
|
+
webhook?: { account?: boolean; connect?: boolean };
|
|
247
|
+
portal?: boolean;
|
|
248
|
+
unstable_catalog?: boolean;
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
- `tables` (optional, default: `true`): Syncs all existing Stripe resources to Convex tables.
|
|
253
|
+
- `tables.withConnect` (option, default: `false`): Syncs all existing Stripe resources from linked accounts to Convex tables.
|
|
215
254
|
- `webhook.account` (optional, default: `false`): Creates/updates the account webhook endpoint. Returns the webhook secret if a new endpoint is created. You must set it in your convex environment variables as `STRIPE_ACCOUNT_WEBHOOK_SECRET`.
|
|
216
255
|
- `webhook.connect` (optional, default: `false`): Creates/updates the connect webhook endpoint. Returns the webhook secret if a new endpoint is created. You must set it in your convex environment variables as `STRIPE_CONNECT_WEBHOOK_SECRET`.
|
|
217
256
|
- `portal` (optional, default: `false`): Creates the default billing portal configuration if it doesn't exist.
|
|
218
257
|
- `unstable_catalog` (optional, default: `false`): Creates the default provided products and prices passed in the configuration.
|
|
219
258
|
|
|
220
|
-
### `subscribe` Function
|
|
259
|
+
### `stripe.subscribe` Function
|
|
221
260
|
|
|
222
261
|
Creates a Stripe Subscription Checkout session for a given entity. Will call [`stripe.checkout.sessions.create`](https://docs.stripe.com/api/checkout/sessions/create) under the hood, the same parameters can be passed.
|
|
223
262
|
|
|
@@ -253,7 +292,7 @@ export const createCheckout = action({
|
|
|
253
292
|
```
|
|
254
293
|
|
|
255
294
|
|
|
256
|
-
### `portal` Function
|
|
295
|
+
### `stripe.portal` Function
|
|
257
296
|
|
|
258
297
|
Allows an entity to manage their subscription via the Stripe Portal. Will call [`stripe.billingPortal.sessions.create`](https://docs.stripe.com/api/customer_portal/sessions/create) under the hood, the same parameters can be passed.
|
|
259
298
|
|
|
@@ -285,7 +324,7 @@ export const portal = action({
|
|
|
285
324
|
The provided entityId must have a customerId associated to it otherwise the action will throw an error.
|
|
286
325
|
|
|
287
326
|
|
|
288
|
-
### `pay` Function
|
|
327
|
+
### `stripe.pay` Function
|
|
289
328
|
|
|
290
329
|
Creates a Stripe One Time Payment Checkout session for a given entity. Will call [`stripe.checkout.sessions.create`](https://docs.stripe.com/api/checkout/sessions/create) under the hood, the same parameters can be passed.
|
|
291
330
|
|
|
@@ -324,7 +363,7 @@ export const pay = action({
|
|
|
324
363
|
|
|
325
364
|
## Best Practices
|
|
326
365
|
|
|
327
|
-
- Always create a Stripe customer (`
|
|
366
|
+
- Always create a Stripe customer (`stripe.customers.create`) when a new entity is created.
|
|
328
367
|
- Use `metadata` or `marketing_features` on products to store feature flags or limits.
|
|
329
368
|
- Run `sync` when you first configure the extension to sync already existing stripe resources.
|
|
330
369
|
- Never expose internal actions directly to clients, wrap them in public actions with proper authorization.
|