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