@dodopayments/convex 0.2.0 → 0.2.1

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.
Files changed (2) hide show
  1. package/README.md +45 -24
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -22,7 +22,6 @@ npm install @dodopayments/convex
22
22
 
23
23
  ## Quick Start
24
24
 
25
-
26
25
  ### 1. Add Component to Convex Config
27
26
 
28
27
  ```typescript
@@ -46,6 +45,7 @@ DODO_PAYMENTS_API_KEY=your-dodo-payments-api-key
46
45
  DODO_PAYMENTS_ENVIRONMENT=test_mode
47
46
  DODO_PAYMENTS_WEBHOOK_SECRET=your-webhook-secret (if using webhooks)
48
47
  ```
48
+
49
49
  ```sh
50
50
  npx convex dashboard
51
51
  ```
@@ -124,8 +124,8 @@ For handling Dodo Payments webhooks, create a file `convex/http.ts`:
124
124
 
125
125
  ```typescript
126
126
  // convex/http.ts
127
- import { httpRouter } from "convex/server";
128
127
  import { createDodoWebhookHandler } from "@dodopayments/convex";
128
+ import { httpRouter } from "convex/server";
129
129
  import { internal } from "./_generated/api";
130
130
 
131
131
  const http = httpRouter();
@@ -134,23 +134,31 @@ http.route({
134
134
  path: "/dodopayments-webhook",
135
135
  method: "POST",
136
136
  handler: createDodoWebhookHandler({
137
+ // Handle successful payments
137
138
  onPaymentSucceeded: async (ctx, payload) => {
138
- console.log("Payment succeeded:", payload.data.payment_id);
139
- // Update order status in your database
140
- await ctx.runMutation(internal.orders.markAsPaid, {
141
- orderId: payload.data.metadata.orderId,
139
+ console.log("🎉 Payment Succeeded!");
140
+ // Use Convex context to persist payment data
141
+ await ctx.runMutation(internal.webhooks.createPayment, {
142
142
  paymentId: payload.data.payment_id,
143
- amount: payload.data.amount,
143
+ businessId: payload.business_id,
144
+ customerEmail: payload.data.customer.email,
145
+ amount: payload.data.total_amount,
146
+ currency: payload.data.currency,
147
+ status: payload.data.status,
148
+ webhookPayload: JSON.stringify(payload),
144
149
  });
145
150
  },
146
-
151
+
152
+ // Handle subscription activation
147
153
  onSubscriptionActive: async (ctx, payload) => {
148
- console.log("Subscription activated:", payload.data.subscription_id);
149
- // Create or update subscription record in your database
150
- await ctx.runMutation(internal.subscriptions.createOrUpdate, {
154
+ console.log("🎉 Subscription Activated!");
155
+ // Use Convex context to persist subscription data
156
+ await ctx.runMutation(internal.webhooks.createSubscription, {
151
157
  subscriptionId: payload.data.subscription_id,
152
- customerId: payload.data.customer_id,
153
- status: "active",
158
+ businessId: payload.business_id,
159
+ customerEmail: payload.data.customer.email,
160
+ status: payload.data.status,
161
+ webhookPayload: JSON.stringify(payload),
154
162
  });
155
163
  },
156
164
  // Add other event handlers as needed
@@ -160,6 +168,8 @@ http.route({
160
168
  export default http;
161
169
  ```
162
170
 
171
+ **Important:** Make sure to define the corresponding database mutations in your Convex backend for each webhook event you want to handle. For example, create a `createPayment` mutation to record successful payments or a `createSubscription` mutation to manage subscription state.
172
+
163
173
  **Important:** All webhook handlers receive the Convex `ActionCtx` as the first parameter, allowing you to use `ctx.runQuery()` and `ctx.runMutation()` to interact with your database.
164
174
 
165
175
  Add your webhook secret in the Convex dashboard (**Settings** → **Environment Variables**):
@@ -447,8 +457,8 @@ Do not use .env files for backend functions; always set secrets in the Convex da
447
457
  Step 2: Create a file `convex/http.ts`:
448
458
 
449
459
  // convex/http.ts
450
- import { httpRouter } from "convex/server";
451
460
  import { createDodoWebhookHandler } from "@dodopayments/convex";
461
+ import { httpRouter } from "convex/server";
452
462
  import { internal } from "./_generated/api";
453
463
 
454
464
  const http = httpRouter();
@@ -457,21 +467,31 @@ http.route({
457
467
  path: "/dodopayments-webhook",
458
468
  method: "POST",
459
469
  handler: createDodoWebhookHandler({
470
+ // Handle successful payments
460
471
  onPaymentSucceeded: async (ctx, payload) => {
461
- console.log("Payment succeeded:", payload.data.payment_id);
462
- // Update order status in your database
463
- await ctx.runMutation(internal.orders.markAsPaid, {
464
- orderId: payload.data.metadata.orderId,
472
+ console.log("🎉 Payment Succeeded!");
473
+ // Use Convex context to persist payment data
474
+ await ctx.runMutation(internal.webhooks.createPayment, {
465
475
  paymentId: payload.data.payment_id,
476
+ businessId: payload.business_id,
477
+ customerEmail: payload.data.customer.email,
478
+ amount: payload.data.total_amount,
479
+ currency: payload.data.currency,
480
+ status: payload.data.status,
481
+ webhookPayload: JSON.stringify(payload),
466
482
  });
467
483
  },
468
-
484
+
485
+ // Handle subscription activation
469
486
  onSubscriptionActive: async (ctx, payload) => {
470
- console.log("Subscription activated:", payload.data.subscription_id);
471
- // Use ctx to create or update subscription records
472
- await ctx.runMutation(internal.subscriptions.createOrUpdate, {
487
+ console.log("🎉 Subscription Activated!");
488
+ // Use Convex context to persist subscription data
489
+ await ctx.runMutation(internal.webhooks.createSubscription, {
473
490
  subscriptionId: payload.data.subscription_id,
474
- customerId: payload.data.customer_id,
491
+ businessId: payload.business_id,
492
+ customerEmail: payload.data.customer.email,
493
+ status: payload.data.status,
494
+ webhookPayload: JSON.stringify(payload),
475
495
  });
476
496
  },
477
497
  // Add other event handlers as needed
@@ -480,6 +500,8 @@ http.route({
480
500
 
481
501
  export default http;
482
502
 
503
+ Note: Make sure to define the corresponding database mutations in your Convex backend for each webhook event you want to handle. For example, create a `createPayment` mutation to record successful payments or a `createSubscription` mutation to manage subscription state.
504
+
483
505
  Now, you can set the webhook endpoint URL in your Dodo Payments dashboard to `https://<your-convex-deployment-url>/dodopayments-webhook`.
484
506
 
485
507
  Environment Variable Setup:
@@ -505,4 +527,3 @@ If the user needs assistance setting up environment variables or deployment, ask
505
527
 
506
528
  Run `npx convex dev` after setting up the component to generate the necessary types.
507
529
  ```
508
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dodopayments/convex",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },