@dodopayments/convex 0.1.0 → 0.2.0
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 +123 -58
- package/dist/client/index.d.ts +0 -1
- package/dist/index.cjs +174 -148
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +18 -3
- package/dist/index.js +174 -148
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -18,7 +18,6 @@ interface DodoPaymentsComponent {
|
|
|
18
18
|
type DodoPaymentsClientConfig = {
|
|
19
19
|
identify: (ctx: GenericActionCtx<any>) => Promise<{
|
|
20
20
|
dodoCustomerId: string;
|
|
21
|
-
customerData?: any;
|
|
22
21
|
} | null>;
|
|
23
22
|
apiKey: string;
|
|
24
23
|
environment: "test_mode" | "live_mode";
|
|
@@ -55,13 +54,29 @@ type CustomerPortalResponse = {
|
|
|
55
54
|
portal_url: string;
|
|
56
55
|
};
|
|
57
56
|
|
|
57
|
+
type ConvexWebhookHandlerConfig = Omit<WebhookHandlerConfig<GenericActionCtx<any>>, 'webhookKey'>;
|
|
58
58
|
/**
|
|
59
59
|
* Creates a Convex HTTP action to securely handle Dodo Payments webhooks.
|
|
60
60
|
*
|
|
61
|
+
* All webhook handlers receive the Convex ActionCtx as the first parameter,
|
|
62
|
+
* allowing you to use ctx.runQuery() and ctx.runMutation() to interact with your database.
|
|
63
|
+
*
|
|
61
64
|
* @param handlers - An object containing your webhook event handlers (e.g., onPaymentSucceeded).
|
|
62
65
|
* @returns A Convex HTTP action.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* createDodoWebhookHandler({
|
|
70
|
+
* onPaymentSucceeded: async (ctx, payload) => {
|
|
71
|
+
* await ctx.runMutation(internal.orders.markAsPaid, {
|
|
72
|
+
* orderId: payload.data.metadata.orderId,
|
|
73
|
+
* paymentId: payload.data.payment_id,
|
|
74
|
+
* });
|
|
75
|
+
* },
|
|
76
|
+
* })
|
|
77
|
+
* ```
|
|
63
78
|
*/
|
|
64
|
-
declare const createDodoWebhookHandler: (handlers:
|
|
79
|
+
declare const createDodoWebhookHandler: (handlers: ConvexWebhookHandlerConfig) => convex_server.PublicHttpAction;
|
|
65
80
|
|
|
66
81
|
export { DodoPayments, _default as component, createDodoWebhookHandler };
|
|
67
|
-
export type { CheckoutResponse, CustomerPortalResponse, DodoPaymentsClientConfig };
|
|
82
|
+
export type { CheckoutResponse, ConvexWebhookHandlerConfig, CustomerPortalResponse, DodoPaymentsClientConfig };
|
package/dist/index.js
CHANGED
|
@@ -2231,153 +2231,163 @@ var __generator = (undefined && undefined.__generator) || function (thisArg, bod
|
|
|
2231
2231
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
2232
2232
|
}
|
|
2233
2233
|
};
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2234
|
+
// Implementation
|
|
2235
|
+
function handleWebhookPayload(payload, config, context) {
|
|
2236
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2237
|
+
var callHandler;
|
|
2238
|
+
return __generator(this, function (_a) {
|
|
2239
|
+
switch (_a.label) {
|
|
2240
|
+
case 0:
|
|
2241
|
+
callHandler = function (handler, payload) {
|
|
2242
|
+
if (!handler)
|
|
2243
|
+
return;
|
|
2244
|
+
if (context !== undefined) {
|
|
2245
|
+
return handler(context, payload);
|
|
2246
|
+
}
|
|
2247
|
+
return handler(payload);
|
|
2248
|
+
};
|
|
2249
|
+
if (!config.onPayload) return [3 /*break*/, 2];
|
|
2250
|
+
return [4 /*yield*/, callHandler(config.onPayload, payload)];
|
|
2251
|
+
case 1:
|
|
2252
|
+
_a.sent();
|
|
2253
|
+
_a.label = 2;
|
|
2254
|
+
case 2:
|
|
2255
|
+
if (!(payload.type === "payment.succeeded")) return [3 /*break*/, 4];
|
|
2256
|
+
return [4 /*yield*/, callHandler(config.onPaymentSucceeded, payload)];
|
|
2257
|
+
case 3:
|
|
2258
|
+
_a.sent();
|
|
2259
|
+
_a.label = 4;
|
|
2260
|
+
case 4:
|
|
2261
|
+
if (!(payload.type === "payment.failed")) return [3 /*break*/, 6];
|
|
2262
|
+
return [4 /*yield*/, callHandler(config.onPaymentFailed, payload)];
|
|
2263
|
+
case 5:
|
|
2264
|
+
_a.sent();
|
|
2265
|
+
_a.label = 6;
|
|
2266
|
+
case 6:
|
|
2267
|
+
if (!(payload.type === "payment.processing")) return [3 /*break*/, 8];
|
|
2268
|
+
return [4 /*yield*/, callHandler(config.onPaymentProcessing, payload)];
|
|
2269
|
+
case 7:
|
|
2270
|
+
_a.sent();
|
|
2271
|
+
_a.label = 8;
|
|
2272
|
+
case 8:
|
|
2273
|
+
if (!(payload.type === "payment.cancelled")) return [3 /*break*/, 10];
|
|
2274
|
+
return [4 /*yield*/, callHandler(config.onPaymentCancelled, payload)];
|
|
2275
|
+
case 9:
|
|
2276
|
+
_a.sent();
|
|
2277
|
+
_a.label = 10;
|
|
2278
|
+
case 10:
|
|
2279
|
+
if (!(payload.type === "refund.succeeded")) return [3 /*break*/, 12];
|
|
2280
|
+
return [4 /*yield*/, callHandler(config.onRefundSucceeded, payload)];
|
|
2281
|
+
case 11:
|
|
2282
|
+
_a.sent();
|
|
2283
|
+
_a.label = 12;
|
|
2284
|
+
case 12:
|
|
2285
|
+
if (!(payload.type === "refund.failed")) return [3 /*break*/, 14];
|
|
2286
|
+
return [4 /*yield*/, callHandler(config.onRefundFailed, payload)];
|
|
2287
|
+
case 13:
|
|
2288
|
+
_a.sent();
|
|
2289
|
+
_a.label = 14;
|
|
2290
|
+
case 14:
|
|
2291
|
+
if (!(payload.type === "dispute.opened")) return [3 /*break*/, 16];
|
|
2292
|
+
return [4 /*yield*/, callHandler(config.onDisputeOpened, payload)];
|
|
2293
|
+
case 15:
|
|
2294
|
+
_a.sent();
|
|
2295
|
+
_a.label = 16;
|
|
2296
|
+
case 16:
|
|
2297
|
+
if (!(payload.type === "dispute.expired")) return [3 /*break*/, 18];
|
|
2298
|
+
return [4 /*yield*/, callHandler(config.onDisputeExpired, payload)];
|
|
2299
|
+
case 17:
|
|
2300
|
+
_a.sent();
|
|
2301
|
+
_a.label = 18;
|
|
2302
|
+
case 18:
|
|
2303
|
+
if (!(payload.type === "dispute.accepted")) return [3 /*break*/, 20];
|
|
2304
|
+
return [4 /*yield*/, callHandler(config.onDisputeAccepted, payload)];
|
|
2305
|
+
case 19:
|
|
2306
|
+
_a.sent();
|
|
2307
|
+
_a.label = 20;
|
|
2308
|
+
case 20:
|
|
2309
|
+
if (!(payload.type === "dispute.cancelled")) return [3 /*break*/, 22];
|
|
2310
|
+
return [4 /*yield*/, callHandler(config.onDisputeCancelled, payload)];
|
|
2311
|
+
case 21:
|
|
2312
|
+
_a.sent();
|
|
2313
|
+
_a.label = 22;
|
|
2314
|
+
case 22:
|
|
2315
|
+
if (!(payload.type === "dispute.challenged")) return [3 /*break*/, 24];
|
|
2316
|
+
return [4 /*yield*/, callHandler(config.onDisputeChallenged, payload)];
|
|
2317
|
+
case 23:
|
|
2318
|
+
_a.sent();
|
|
2319
|
+
_a.label = 24;
|
|
2320
|
+
case 24:
|
|
2321
|
+
if (!(payload.type === "dispute.won")) return [3 /*break*/, 26];
|
|
2322
|
+
return [4 /*yield*/, callHandler(config.onDisputeWon, payload)];
|
|
2323
|
+
case 25:
|
|
2324
|
+
_a.sent();
|
|
2325
|
+
_a.label = 26;
|
|
2326
|
+
case 26:
|
|
2327
|
+
if (!(payload.type === "dispute.lost")) return [3 /*break*/, 28];
|
|
2328
|
+
return [4 /*yield*/, callHandler(config.onDisputeLost, payload)];
|
|
2329
|
+
case 27:
|
|
2330
|
+
_a.sent();
|
|
2331
|
+
_a.label = 28;
|
|
2332
|
+
case 28:
|
|
2333
|
+
if (!(payload.type === "subscription.active")) return [3 /*break*/, 30];
|
|
2334
|
+
return [4 /*yield*/, callHandler(config.onSubscriptionActive, payload)];
|
|
2335
|
+
case 29:
|
|
2336
|
+
_a.sent();
|
|
2337
|
+
_a.label = 30;
|
|
2338
|
+
case 30:
|
|
2339
|
+
if (!(payload.type === "subscription.on_hold")) return [3 /*break*/, 32];
|
|
2340
|
+
return [4 /*yield*/, callHandler(config.onSubscriptionOnHold, payload)];
|
|
2341
|
+
case 31:
|
|
2342
|
+
_a.sent();
|
|
2343
|
+
_a.label = 32;
|
|
2344
|
+
case 32:
|
|
2345
|
+
if (!(payload.type === "subscription.renewed")) return [3 /*break*/, 34];
|
|
2346
|
+
return [4 /*yield*/, callHandler(config.onSubscriptionRenewed, payload)];
|
|
2347
|
+
case 33:
|
|
2348
|
+
_a.sent();
|
|
2349
|
+
_a.label = 34;
|
|
2350
|
+
case 34:
|
|
2351
|
+
if (!(payload.type === "subscription.paused")) return [3 /*break*/, 36];
|
|
2352
|
+
return [4 /*yield*/, callHandler(config.onSubscriptionPaused, payload)];
|
|
2353
|
+
case 35:
|
|
2354
|
+
_a.sent();
|
|
2355
|
+
_a.label = 36;
|
|
2356
|
+
case 36:
|
|
2357
|
+
if (!(payload.type === "subscription.plan_changed")) return [3 /*break*/, 38];
|
|
2358
|
+
return [4 /*yield*/, callHandler(config.onSubscriptionPlanChanged, payload)];
|
|
2359
|
+
case 37:
|
|
2360
|
+
_a.sent();
|
|
2361
|
+
_a.label = 38;
|
|
2362
|
+
case 38:
|
|
2363
|
+
if (!(payload.type === "subscription.cancelled")) return [3 /*break*/, 40];
|
|
2364
|
+
return [4 /*yield*/, callHandler(config.onSubscriptionCancelled, payload)];
|
|
2365
|
+
case 39:
|
|
2366
|
+
_a.sent();
|
|
2367
|
+
_a.label = 40;
|
|
2368
|
+
case 40:
|
|
2369
|
+
if (!(payload.type === "subscription.failed")) return [3 /*break*/, 42];
|
|
2370
|
+
return [4 /*yield*/, callHandler(config.onSubscriptionFailed, payload)];
|
|
2371
|
+
case 41:
|
|
2372
|
+
_a.sent();
|
|
2373
|
+
_a.label = 42;
|
|
2374
|
+
case 42:
|
|
2375
|
+
if (!(payload.type === "subscription.expired")) return [3 /*break*/, 44];
|
|
2376
|
+
return [4 /*yield*/, callHandler(config.onSubscriptionExpired, payload)];
|
|
2377
|
+
case 43:
|
|
2378
|
+
_a.sent();
|
|
2379
|
+
_a.label = 44;
|
|
2380
|
+
case 44:
|
|
2381
|
+
if (!(payload.type === "license_key.created")) return [3 /*break*/, 46];
|
|
2382
|
+
return [4 /*yield*/, callHandler(config.onLicenseKeyCreated, payload)];
|
|
2383
|
+
case 45:
|
|
2384
|
+
_a.sent();
|
|
2385
|
+
_a.label = 46;
|
|
2386
|
+
case 46: return [2 /*return*/];
|
|
2387
|
+
}
|
|
2388
|
+
});
|
|
2379
2389
|
});
|
|
2380
|
-
}
|
|
2390
|
+
}
|
|
2381
2391
|
var verifyWebhookPayload = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
2382
2392
|
var standardWebhook, _c, success, payload, error;
|
|
2383
2393
|
var webhookKey = _b.webhookKey, headers = _b.headers, body = _b.body;
|
|
@@ -2403,11 +2413,26 @@ var verifyWebhookPayload = function (_a) { return __awaiter(void 0, [_a], void 0
|
|
|
2403
2413
|
/**
|
|
2404
2414
|
* Creates a Convex HTTP action to securely handle Dodo Payments webhooks.
|
|
2405
2415
|
*
|
|
2416
|
+
* All webhook handlers receive the Convex ActionCtx as the first parameter,
|
|
2417
|
+
* allowing you to use ctx.runQuery() and ctx.runMutation() to interact with your database.
|
|
2418
|
+
*
|
|
2406
2419
|
* @param handlers - An object containing your webhook event handlers (e.g., onPaymentSucceeded).
|
|
2407
2420
|
* @returns A Convex HTTP action.
|
|
2421
|
+
*
|
|
2422
|
+
* @example
|
|
2423
|
+
* ```typescript
|
|
2424
|
+
* createDodoWebhookHandler({
|
|
2425
|
+
* onPaymentSucceeded: async (ctx, payload) => {
|
|
2426
|
+
* await ctx.runMutation(internal.orders.markAsPaid, {
|
|
2427
|
+
* orderId: payload.data.metadata.orderId,
|
|
2428
|
+
* paymentId: payload.data.payment_id,
|
|
2429
|
+
* });
|
|
2430
|
+
* },
|
|
2431
|
+
* })
|
|
2432
|
+
* ```
|
|
2408
2433
|
*/
|
|
2409
2434
|
const createDodoWebhookHandler = (handlers) => {
|
|
2410
|
-
return httpAction(async (
|
|
2435
|
+
return httpAction(async (ctx, request) => {
|
|
2411
2436
|
const webhookSecret = process.env.DODO_PAYMENTS_WEBHOOK_SECRET;
|
|
2412
2437
|
if (!webhookSecret) {
|
|
2413
2438
|
throw new Error("DODO_PAYMENTS_WEBHOOK_SECRET environment variable is not set.");
|
|
@@ -2423,10 +2448,11 @@ const createDodoWebhookHandler = (handlers) => {
|
|
|
2423
2448
|
headers,
|
|
2424
2449
|
body,
|
|
2425
2450
|
});
|
|
2451
|
+
// Use the core library's handleWebhookPayload with ctx as the third parameter
|
|
2426
2452
|
await handleWebhookPayload(payload, {
|
|
2427
2453
|
webhookKey: webhookSecret,
|
|
2428
2454
|
...handlers,
|
|
2429
|
-
});
|
|
2455
|
+
}, ctx);
|
|
2430
2456
|
return new Response(null, { status: 200 });
|
|
2431
2457
|
}
|
|
2432
2458
|
catch (error) {
|