@deiondz/better-auth-razorpay 2.0.11 → 2.0.13
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 +66 -8
- package/dist/client/hooks.d.ts +81 -6
- package/dist/client/hooks.js +52 -0
- package/dist/client/hooks.js.map +1 -1
- package/dist/client.d.ts +3 -3
- package/dist/index.d.ts +3 -2
- package/dist/index.js +131 -27
- package/dist/index.js.map +1 -1
- package/dist/{types-C2IdKTof.d.ts → types-B91GdcpF.d.ts} +6 -3
- package/dist/{types-VZW_XCzQ.d.ts → types-BC0odu_S.d.ts} +10 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4122,7 +4122,9 @@ var createOrUpdateSubscriptionSchema = external_exports.object({
|
|
|
4122
4122
|
seats: external_exports.number().int().min(1).optional().default(1),
|
|
4123
4123
|
subscriptionId: external_exports.string().optional(),
|
|
4124
4124
|
successUrl: external_exports.string().url().optional(),
|
|
4125
|
-
disableRedirect: external_exports.boolean().optional().default(false)
|
|
4125
|
+
disableRedirect: external_exports.boolean().optional().default(false),
|
|
4126
|
+
/** When true, checkout runs in-page via Razorpay modal; no checkoutUrl redirect. */
|
|
4127
|
+
embed: external_exports.boolean().optional().default(false)
|
|
4126
4128
|
});
|
|
4127
4129
|
var cancelSubscriptionSchema = external_exports.object({
|
|
4128
4130
|
subscriptionId: external_exports.string().min(1, "Subscription ID (local) is required"),
|
|
@@ -4306,23 +4308,49 @@ var createOrUpdateSubscription = (razorpay, options) => createAuthEndpoint2(
|
|
|
4306
4308
|
}
|
|
4307
4309
|
const rpSub = existing.razorpaySubscriptionId ? await razorpay.subscriptions.fetch(existing.razorpaySubscriptionId) : null;
|
|
4308
4310
|
if (rpSub) {
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4312
|
-
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
|
|
4317
|
-
|
|
4318
|
-
cancelAtPeriodEnd: existing.cancelAtPeriodEnd,
|
|
4319
|
-
periodEnd: existing.periodEnd,
|
|
4320
|
-
seats: existing.seats
|
|
4321
|
-
}
|
|
4311
|
+
const data2 = {
|
|
4312
|
+
subscription: {
|
|
4313
|
+
id: existing.id,
|
|
4314
|
+
plan: existing.plan,
|
|
4315
|
+
status: existing.status,
|
|
4316
|
+
razorpaySubscriptionId: existing.razorpaySubscriptionId ?? null,
|
|
4317
|
+
cancelAtPeriodEnd: existing.cancelAtPeriodEnd ?? false,
|
|
4318
|
+
periodEnd: existing.periodEnd ?? null,
|
|
4319
|
+
seats: existing.seats ?? 1
|
|
4322
4320
|
}
|
|
4323
4321
|
};
|
|
4322
|
+
if (!body.embed) data2.checkoutUrl = rpSub.short_url;
|
|
4323
|
+
return { success: true, data: data2 };
|
|
4324
4324
|
}
|
|
4325
4325
|
}
|
|
4326
|
+
const existingSubs = await ctx.context.adapter.findMany({
|
|
4327
|
+
model: "subscription",
|
|
4328
|
+
where: [{ field: "referenceId", value: userId }]
|
|
4329
|
+
});
|
|
4330
|
+
const activeStatuses = [
|
|
4331
|
+
"active",
|
|
4332
|
+
"trialing",
|
|
4333
|
+
"pending",
|
|
4334
|
+
"created",
|
|
4335
|
+
"halted"
|
|
4336
|
+
];
|
|
4337
|
+
const subs = existingSubs ?? [];
|
|
4338
|
+
const activePaidSubs = subs.filter(
|
|
4339
|
+
(s) => activeStatuses.includes(s.status) && s.razorpaySubscriptionId
|
|
4340
|
+
);
|
|
4341
|
+
const appTrialSubs = subs.filter(
|
|
4342
|
+
(s) => s.status === "trialing" && !s.razorpaySubscriptionId
|
|
4343
|
+
);
|
|
4344
|
+
const appTrialSub = appTrialSubs.length === 1 ? appTrialSubs[0] : null;
|
|
4345
|
+
if (activePaidSubs.length > 0) {
|
|
4346
|
+
return {
|
|
4347
|
+
success: false,
|
|
4348
|
+
error: {
|
|
4349
|
+
code: "ALREADY_SUBSCRIBED",
|
|
4350
|
+
description: "You already have an active subscription. Cancel or let it expire before creating another."
|
|
4351
|
+
}
|
|
4352
|
+
};
|
|
4353
|
+
}
|
|
4326
4354
|
const totalCount = body.annual ? 1 : 12;
|
|
4327
4355
|
const subscriptionPayload = {
|
|
4328
4356
|
plan_id: planId,
|
|
@@ -4333,7 +4361,7 @@ var createOrUpdateSubscription = (razorpay, options) => createAuthEndpoint2(
|
|
|
4333
4361
|
};
|
|
4334
4362
|
if (subOpts.getSubscriptionCreateParams) {
|
|
4335
4363
|
const tempSub = {
|
|
4336
|
-
id: "",
|
|
4364
|
+
id: appTrialSub?.id ?? "",
|
|
4337
4365
|
plan: plan.name,
|
|
4338
4366
|
referenceId: userId,
|
|
4339
4367
|
status: "created",
|
|
@@ -4358,17 +4386,64 @@ var createOrUpdateSubscription = (razorpay, options) => createAuthEndpoint2(
|
|
|
4358
4386
|
const rpSubscription = await razorpay.subscriptions.create(
|
|
4359
4387
|
subscriptionPayload
|
|
4360
4388
|
);
|
|
4389
|
+
const periodStart = rpSubscription.current_start ? new Date(rpSubscription.current_start * 1e3) : null;
|
|
4390
|
+
const periodEnd = rpSubscription.current_end ? new Date(rpSubscription.current_end * 1e3) : null;
|
|
4391
|
+
const newStatus = toLocalStatus(rpSubscription.status);
|
|
4392
|
+
if (appTrialSub) {
|
|
4393
|
+
await ctx.context.adapter.update({
|
|
4394
|
+
model: "subscription",
|
|
4395
|
+
where: [{ field: "id", value: appTrialSub.id }],
|
|
4396
|
+
update: {
|
|
4397
|
+
data: {
|
|
4398
|
+
plan: plan.name,
|
|
4399
|
+
razorpaySubscriptionId: rpSubscription.id,
|
|
4400
|
+
status: newStatus,
|
|
4401
|
+
trialEnd: now,
|
|
4402
|
+
periodStart,
|
|
4403
|
+
periodEnd,
|
|
4404
|
+
seats: body.seats,
|
|
4405
|
+
updatedAt: now
|
|
4406
|
+
}
|
|
4407
|
+
}
|
|
4408
|
+
});
|
|
4409
|
+
if (subOpts.onSubscriptionCreated) {
|
|
4410
|
+
const updatedRecord = {
|
|
4411
|
+
...appTrialSub,
|
|
4412
|
+
plan: plan.name,
|
|
4413
|
+
razorpaySubscriptionId: rpSubscription.id,
|
|
4414
|
+
status: newStatus,
|
|
4415
|
+
trialEnd: now,
|
|
4416
|
+
periodStart,
|
|
4417
|
+
periodEnd,
|
|
4418
|
+
seats: body.seats,
|
|
4419
|
+
updatedAt: now
|
|
4420
|
+
};
|
|
4421
|
+
await subOpts.onSubscriptionCreated({
|
|
4422
|
+
razorpaySubscription: rpSubscription,
|
|
4423
|
+
subscription: updatedRecord,
|
|
4424
|
+
plan
|
|
4425
|
+
});
|
|
4426
|
+
}
|
|
4427
|
+
const data2 = {
|
|
4428
|
+
subscriptionId: appTrialSub.id,
|
|
4429
|
+
razorpaySubscriptionId: rpSubscription.id
|
|
4430
|
+
};
|
|
4431
|
+
if (!body.embed) {
|
|
4432
|
+
data2.checkoutUrl = body.disableRedirect ? rpSubscription.short_url : body.successUrl ? `${rpSubscription.short_url}?redirect=${encodeURIComponent(body.successUrl)}` : rpSubscription.short_url;
|
|
4433
|
+
}
|
|
4434
|
+
return { success: true, data: data2 };
|
|
4435
|
+
}
|
|
4361
4436
|
const subscriptionRecord = {
|
|
4362
4437
|
id: localId,
|
|
4363
4438
|
plan: plan.name,
|
|
4364
4439
|
referenceId: userId,
|
|
4365
4440
|
razorpayCustomerId: user.razorpayCustomerId ?? null,
|
|
4366
4441
|
razorpaySubscriptionId: rpSubscription.id,
|
|
4367
|
-
status:
|
|
4442
|
+
status: newStatus,
|
|
4368
4443
|
trialStart: null,
|
|
4369
4444
|
trialEnd: null,
|
|
4370
|
-
periodStart
|
|
4371
|
-
periodEnd
|
|
4445
|
+
periodStart,
|
|
4446
|
+
periodEnd,
|
|
4372
4447
|
cancelAtPeriodEnd: false,
|
|
4373
4448
|
seats: body.seats,
|
|
4374
4449
|
groupId: null,
|
|
@@ -4377,7 +4452,8 @@ var createOrUpdateSubscription = (razorpay, options) => createAuthEndpoint2(
|
|
|
4377
4452
|
};
|
|
4378
4453
|
await ctx.context.adapter.create({
|
|
4379
4454
|
model: "subscription",
|
|
4380
|
-
data: subscriptionRecord
|
|
4455
|
+
data: subscriptionRecord,
|
|
4456
|
+
forceAllowId: true
|
|
4381
4457
|
});
|
|
4382
4458
|
if (subOpts.onSubscriptionCreated) {
|
|
4383
4459
|
await subOpts.onSubscriptionCreated({
|
|
@@ -4386,15 +4462,14 @@ var createOrUpdateSubscription = (razorpay, options) => createAuthEndpoint2(
|
|
|
4386
4462
|
plan
|
|
4387
4463
|
});
|
|
4388
4464
|
}
|
|
4389
|
-
const
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
data: {
|
|
4393
|
-
checkoutUrl,
|
|
4394
|
-
subscriptionId: localId,
|
|
4395
|
-
razorpaySubscriptionId: rpSubscription.id
|
|
4396
|
-
}
|
|
4465
|
+
const data = {
|
|
4466
|
+
subscriptionId: localId,
|
|
4467
|
+
razorpaySubscriptionId: rpSubscription.id
|
|
4397
4468
|
};
|
|
4469
|
+
if (!body.embed) {
|
|
4470
|
+
data.checkoutUrl = body.disableRedirect ? rpSubscription.short_url : body.successUrl ? `${rpSubscription.short_url}?redirect=${encodeURIComponent(body.successUrl)}` : rpSubscription.short_url;
|
|
4471
|
+
}
|
|
4472
|
+
return { success: true, data };
|
|
4398
4473
|
} catch (error) {
|
|
4399
4474
|
return handleRazorpayError(error);
|
|
4400
4475
|
}
|
|
@@ -4834,6 +4909,7 @@ var razorpayPlugin = (options) => {
|
|
|
4834
4909
|
razorpayKeySecret,
|
|
4835
4910
|
razorpayWebhookSecret,
|
|
4836
4911
|
createCustomerOnSignUp = false,
|
|
4912
|
+
trialOnSignUp,
|
|
4837
4913
|
onCustomerCreate,
|
|
4838
4914
|
getCustomerCreateParams,
|
|
4839
4915
|
subscription: subOpts,
|
|
@@ -4930,6 +5006,34 @@ var razorpayPlugin = (options) => {
|
|
|
4930
5006
|
razorpayCustomer: { id: customer.id, ...customer }
|
|
4931
5007
|
});
|
|
4932
5008
|
}
|
|
5009
|
+
if (trialOnSignUp && typeof trialOnSignUp.days === "number" && trialOnSignUp.days > 0 && adapter.create) {
|
|
5010
|
+
const now = /* @__PURE__ */ new Date();
|
|
5011
|
+
const trialEnd = new Date(now.getTime() + trialOnSignUp.days * 24 * 60 * 60 * 1e3);
|
|
5012
|
+
const planName = trialOnSignUp.planName ?? "Trial";
|
|
5013
|
+
const localId = `sub_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
|
|
5014
|
+
const subscriptionRecord = {
|
|
5015
|
+
id: localId,
|
|
5016
|
+
plan: planName,
|
|
5017
|
+
referenceId: user.id,
|
|
5018
|
+
razorpayCustomerId: customer.id,
|
|
5019
|
+
razorpaySubscriptionId: null,
|
|
5020
|
+
status: "trialing",
|
|
5021
|
+
trialStart: now,
|
|
5022
|
+
trialEnd,
|
|
5023
|
+
periodStart: null,
|
|
5024
|
+
periodEnd: null,
|
|
5025
|
+
cancelAtPeriodEnd: false,
|
|
5026
|
+
seats: 1,
|
|
5027
|
+
groupId: null,
|
|
5028
|
+
createdAt: now,
|
|
5029
|
+
updatedAt: now
|
|
5030
|
+
};
|
|
5031
|
+
await adapter.create({
|
|
5032
|
+
model: "subscription",
|
|
5033
|
+
data: subscriptionRecord,
|
|
5034
|
+
forceAllowId: true
|
|
5035
|
+
});
|
|
5036
|
+
}
|
|
4933
5037
|
} catch (err) {
|
|
4934
5038
|
console.error("[better-auth-razorpay] Create customer on sign-up failed:", err);
|
|
4935
5039
|
}
|