@deiondz/better-auth-razorpay 2.0.13 → 2.0.15

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 CHANGED
@@ -354,7 +354,7 @@ All endpoints are prefixed with `/api/auth/razorpay/` (or your configured `baseP
354
354
  | Cancel | `POST` | `subscription/cancel` | Cancel by local subscription ID. Body: `subscriptionId`, `immediately?`. |
355
355
  | Restore | `POST` | `subscription/restore` | Restore a subscription scheduled to cancel. Body: `subscriptionId`. |
356
356
  | List | `GET` | `subscription/list` | List active/trialing subscriptions. Query: `referenceId?` (default: current user). |
357
- | Get plans | `GET` | `get-plans` | Return configured plans (name, monthlyPlanId, annualPlanId, limits, freeTrial). |
357
+ | Get plans | `GET` | `get-plans` | Return configured plans with price details (name, monthlyPlanId, annualPlanId, limits, freeTrial, monthly/annual amount, currency, period from Razorpay). |
358
358
  | Webhook | `POST` | `webhook` | Razorpay webhook URL; configure in Razorpay Dashboard. |
359
359
 
360
360
  ### 1. Get Plans
@@ -363,6 +363,8 @@ Retrieve all configured subscription plans (from plugin config; no Razorpay API
363
363
 
364
364
  **Endpoint:** `GET /api/auth/razorpay/get-plans`
365
365
 
366
+ Each plan in the response includes optional **price details** (`monthly`, `annual`) when available from Razorpay: `amount` (smallest currency unit, e.g. paise/cents), `currency`, `period` (e.g. monthly, yearly), and `interval`. Omitted if the plan fetch fails or the variant is not configured.
367
+
366
368
  **Authentication:** Not required (public endpoint)
367
369
 
368
370
  **Response:**
@@ -370,7 +372,7 @@ Retrieve all configured subscription plans (from plugin config; no Razorpay API
370
372
  ```typescript
371
373
  {
372
374
  success: true,
373
- data: Array<{ name: string; monthlyPlanId: string; annualPlanId?: string; limits?: Record<string, number>; freeTrial?: { days: number } }>
375
+ data: Array<{ name: string; monthlyPlanId: string; annualPlanId?: string; limits?: Record<string, number>; freeTrial?: { days: number }; monthly?: { amount: number; currency: string; period: string; interval?: number }; annual?: { amount: number; currency: string; period: string; interval?: number } }>
374
376
  }
375
377
  ```
376
378
 
@@ -1,10 +1,10 @@
1
- import { S as SubscriptionRecord } from '../types-BC0odu_S.js';
1
+ import { S as SubscriptionRecord } from '../types-CpjgfbVJ.js';
2
2
  import * as _tanstack_react_query from '@tanstack/react-query';
3
3
  import { UseMutationOptions, UseQueryOptions } from '@tanstack/react-query';
4
4
  import * as react from 'react';
5
5
  import { ReactNode } from 'react';
6
- import { c as RazorpayAuthClient, d as CancelSubscriptionResponse, a as CancelSubscriptionInput, e as CreateOrUpdateSubscriptionResponse, C as CreateOrUpdateSubscriptionInput, P as PlanSummary, f as RestoreSubscriptionResponse, b as RestoreSubscriptionInput, g as ListSubscriptionsResponse, h as VerifyPaymentResponse, V as VerifyPaymentInput, L as ListSubscriptionsInput } from '../types-B91GdcpF.js';
7
- export { G as GetPlansResponse, i as RazorpayApiError, R as RazorpayApiResult, j as RazorpayClientActions } from '../types-B91GdcpF.js';
6
+ import { c as RazorpayAuthClient, d as CancelSubscriptionResponse, a as CancelSubscriptionInput, e as CreateOrUpdateSubscriptionResponse, C as CreateOrUpdateSubscriptionInput, P as PlanSummary, f as RestoreSubscriptionResponse, b as RestoreSubscriptionInput, g as ListSubscriptionsResponse, h as VerifyPaymentResponse, V as VerifyPaymentInput, L as ListSubscriptionsInput } from '../types-I97EgC8r.js';
7
+ export { G as GetPlansResponse, i as RazorpayApiError, R as RazorpayApiResult, j as RazorpayClientActions } from '../types-I97EgC8r.js';
8
8
  import 'razorpay';
9
9
 
10
10
  /**
package/dist/client.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { S as SubscriptionRecord } from './types-BC0odu_S.js';
1
+ import { S as SubscriptionRecord } from './types-CpjgfbVJ.js';
2
2
  import { razorpayPlugin } from './index.js';
3
- import { R as RazorpayApiResult, P as PlanSummary, L as ListSubscriptionsInput, C as CreateOrUpdateSubscriptionInput, a as CancelSubscriptionInput, b as RestoreSubscriptionInput, V as VerifyPaymentInput } from './types-B91GdcpF.js';
3
+ import { R as RazorpayApiResult, P as PlanSummary, L as ListSubscriptionsInput, C as CreateOrUpdateSubscriptionInput, a as CancelSubscriptionInput, b as RestoreSubscriptionInput, V as VerifyPaymentInput } from './types-I97EgC8r.js';
4
4
  import 'razorpay';
5
5
  import 'better-auth';
6
6
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { BetterAuthPlugin } from 'better-auth';
2
- import { R as RazorpayPluginOptions } from './types-BC0odu_S.js';
3
- export { O as OnWebhookEventCallback, a as RazorpayApiResponse, b as RazorpayErrorResponse, c as RazorpayPlan, d as RazorpaySubscription, e as RazorpaySuccessResponse, f as RazorpayUserRecord, g as RazorpayWebhookContext, h as RazorpayWebhookEvent, i as RazorpayWebhookPayload, S as SubscriptionRecord, j as SubscriptionStatus } from './types-BC0odu_S.js';
2
+ import { R as RazorpayPluginOptions } from './types-CpjgfbVJ.js';
3
+ export { O as OnWebhookEventCallback, a as RazorpayApiResponse, b as RazorpayErrorResponse, c as RazorpayPlan, d as RazorpaySubscription, e as RazorpaySuccessResponse, f as RazorpayUserRecord, g as RazorpayWebhookContext, h as RazorpayWebhookEvent, i as RazorpayWebhookPayload, S as SubscriptionRecord, j as SubscriptionStatus } from './types-CpjgfbVJ.js';
4
4
  import 'razorpay';
5
5
 
6
6
  interface WebhookResult {
package/dist/index.js CHANGED
@@ -4312,6 +4312,7 @@ var createOrUpdateSubscription = (razorpay, options) => createAuthEndpoint2(
4312
4312
  subscription: {
4313
4313
  id: existing.id,
4314
4314
  plan: existing.plan,
4315
+ planId: existing.planId ?? null,
4315
4316
  status: existing.status,
4316
4317
  razorpaySubscriptionId: existing.razorpaySubscriptionId ?? null,
4317
4318
  cancelAtPeriodEnd: existing.cancelAtPeriodEnd ?? false,
@@ -4363,6 +4364,7 @@ var createOrUpdateSubscription = (razorpay, options) => createAuthEndpoint2(
4363
4364
  const tempSub = {
4364
4365
  id: appTrialSub?.id ?? "",
4365
4366
  plan: plan.name,
4367
+ planId,
4366
4368
  referenceId: userId,
4367
4369
  status: "created",
4368
4370
  cancelAtPeriodEnd: false,
@@ -4396,6 +4398,7 @@ var createOrUpdateSubscription = (razorpay, options) => createAuthEndpoint2(
4396
4398
  update: {
4397
4399
  data: {
4398
4400
  plan: plan.name,
4401
+ planId,
4399
4402
  razorpaySubscriptionId: rpSubscription.id,
4400
4403
  status: newStatus,
4401
4404
  trialEnd: now,
@@ -4410,6 +4413,7 @@ var createOrUpdateSubscription = (razorpay, options) => createAuthEndpoint2(
4410
4413
  const updatedRecord = {
4411
4414
  ...appTrialSub,
4412
4415
  plan: plan.name,
4416
+ planId,
4413
4417
  razorpaySubscriptionId: rpSubscription.id,
4414
4418
  status: newStatus,
4415
4419
  trialEnd: now,
@@ -4436,6 +4440,7 @@ var createOrUpdateSubscription = (razorpay, options) => createAuthEndpoint2(
4436
4440
  const subscriptionRecord = {
4437
4441
  id: localId,
4438
4442
  plan: plan.name,
4443
+ planId,
4439
4444
  referenceId: userId,
4440
4445
  razorpayCustomerId: user.razorpayCustomerId ?? null,
4441
4446
  razorpaySubscriptionId: rpSubscription.id,
@@ -4481,24 +4486,48 @@ import { createAuthEndpoint as createAuthEndpoint3 } from "better-auth/api";
4481
4486
  async function resolvePlans2(plans) {
4482
4487
  return typeof plans === "function" ? plans() : plans;
4483
4488
  }
4484
- var getPlans = (options) => createAuthEndpoint3("/razorpay/get-plans", { method: "GET" }, async (_ctx) => {
4489
+ async function fetchPlanPrice(razorpay, planId) {
4490
+ try {
4491
+ const plan = await razorpay.plans.fetch(planId);
4492
+ const item = plan?.item;
4493
+ if (item && typeof item.amount === "number" && typeof item.currency === "string") {
4494
+ return {
4495
+ amount: item.amount,
4496
+ currency: item.currency,
4497
+ period: typeof plan.period === "string" ? plan.period : "monthly",
4498
+ interval: typeof plan.interval === "number" ? plan.interval : void 0
4499
+ };
4500
+ }
4501
+ } catch {
4502
+ }
4503
+ return void 0;
4504
+ }
4505
+ var getPlans = (razorpay, options) => createAuthEndpoint3("/razorpay/get-plans", { method: "GET" }, async (_ctx) => {
4485
4506
  try {
4486
4507
  const subOpts = options.subscription;
4487
4508
  if (!subOpts?.enabled) {
4488
4509
  return { success: true, data: [] };
4489
4510
  }
4490
4511
  const plans = await resolvePlans2(subOpts.plans);
4491
- return {
4492
- success: true,
4493
- data: plans.map((p) => ({
4494
- name: p.name,
4495
- monthlyPlanId: p.monthlyPlanId,
4496
- annualPlanId: p.annualPlanId,
4497
- description: p.description,
4498
- limits: p.limits,
4499
- freeTrial: p.freeTrial ? { days: p.freeTrial.days } : void 0
4500
- }))
4501
- };
4512
+ const data = await Promise.all(
4513
+ plans.map(async (p) => {
4514
+ const [monthly, annual] = await Promise.all([
4515
+ fetchPlanPrice(razorpay, p.monthlyPlanId),
4516
+ p.annualPlanId ? fetchPlanPrice(razorpay, p.annualPlanId) : Promise.resolve(void 0)
4517
+ ]);
4518
+ return {
4519
+ name: p.name,
4520
+ monthlyPlanId: p.monthlyPlanId,
4521
+ annualPlanId: p.annualPlanId,
4522
+ description: p.description,
4523
+ limits: p.limits,
4524
+ freeTrial: p.freeTrial ? { days: p.freeTrial.days } : void 0,
4525
+ monthly,
4526
+ annual
4527
+ };
4528
+ })
4529
+ );
4530
+ return { success: true, data };
4502
4531
  } catch (error) {
4503
4532
  return handleRazorpayError(error);
4504
4533
  }
@@ -4711,18 +4740,19 @@ function toLocalStatus2(razorpayStatus) {
4711
4740
  };
4712
4741
  return map[razorpayStatus] ?? "pending";
4713
4742
  }
4714
- var updateSubscriptionRecord = async (adapter, razorpaySubscriptionId, data) => {
4743
+ var updateSubscriptionRecord = async (adapter, subscriptionRecordId, data) => {
4715
4744
  await adapter.update({
4716
4745
  model: "subscription",
4717
- where: [{ field: "razorpaySubscriptionId", value: razorpaySubscriptionId }],
4746
+ where: [{ field: "id", value: subscriptionRecordId }],
4718
4747
  update: { data: { ...data, updatedAt: /* @__PURE__ */ new Date() } }
4719
4748
  });
4720
4749
  };
4721
- var createStatusHandler = (status, extraFields) => async (adapter, razorpaySubscriptionId, record, subscription) => {
4750
+ var createStatusHandler = (status, extraFields) => async (adapter, _razorpaySubscriptionId, record, subscription) => {
4722
4751
  const periodStart = subscription.current_start ? new Date(subscription.current_start * 1e3) : null;
4723
4752
  const periodEnd = subscription.current_end ? new Date(subscription.current_end * 1e3) : null;
4724
- await updateSubscriptionRecord(adapter, razorpaySubscriptionId, {
4753
+ await updateSubscriptionRecord(adapter, record.id, {
4725
4754
  status,
4755
+ planId: subscription.plan_id,
4726
4756
  ...periodStart !== null && { periodStart },
4727
4757
  ...periodEnd !== null && { periodEnd },
4728
4758
  ...extraFields?.(subscription) ?? {}
@@ -4967,7 +4997,7 @@ var razorpayPlugin = (options) => {
4967
4997
  "subscription/cancel": cancelSubscription(razorpay),
4968
4998
  "subscription/restore": restoreSubscription(razorpay),
4969
4999
  "subscription/list": listSubscriptions({ subscription: subOpts }),
4970
- "get-plans": getPlans({ subscription: subOpts }),
5000
+ "get-plans": getPlans(razorpay, { subscription: subOpts }),
4971
5001
  ...razorpayKeySecret ? { "verify-payment": verifyPayment(razorpayKeySecret) } : {},
4972
5002
  webhook: webhook(razorpayWebhookSecret, options.onWebhookEvent ?? void 0, {
4973
5003
  subscription: subOpts,