@deiondz/better-auth-razorpay 2.0.16 → 2.0.18

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/dist/index.js CHANGED
@@ -4767,6 +4767,22 @@ function toLocalStatus2(razorpayStatus) {
4767
4767
  };
4768
4768
  return map[razorpayStatus] ?? "pending";
4769
4769
  }
4770
+ var STATUS_ORDER = {
4771
+ created: 0,
4772
+ pending: 1,
4773
+ trialing: 1,
4774
+ active: 2,
4775
+ halted: 2,
4776
+ cancelled: 3,
4777
+ completed: 3,
4778
+ expired: 3
4779
+ };
4780
+ function shouldUpdateStatus(currentStatus, newStatus) {
4781
+ if (["cancelled", "completed", "expired"].includes(newStatus)) return true;
4782
+ const currentOrder = STATUS_ORDER[currentStatus] ?? 0;
4783
+ const newOrder = STATUS_ORDER[newStatus] ?? 0;
4784
+ return newOrder >= currentOrder;
4785
+ }
4770
4786
  var WEBHOOK_DEBUG = process.env.NODE_ENV === "development" || process.env.RAZORPAY_WEBHOOK_DEBUG === "true";
4771
4787
  var log = (msg, data) => {
4772
4788
  if (WEBHOOK_DEBUG) {
@@ -4779,7 +4795,7 @@ var updateSubscriptionRecord = async (adapter, subscriptionRecordId, whereField,
4779
4795
  const params = {
4780
4796
  model: "subscription",
4781
4797
  where: [{ field: whereField, value: subscriptionRecordId }],
4782
- update: { data: updateData }
4798
+ update: updateData
4783
4799
  };
4784
4800
  log("updateSubscriptionRecord call", {
4785
4801
  subscriptionRecordId,
@@ -4799,33 +4815,22 @@ var updateSubscriptionRecord = async (adapter, subscriptionRecordId, whereField,
4799
4815
  throw err;
4800
4816
  }
4801
4817
  };
4802
- function getSubscriptionPrimaryKey(record) {
4803
- if (record.id != null && record.id !== "") {
4804
- return { value: record.id, field: "id" };
4805
- }
4806
- if (record._id != null && record._id !== "") {
4807
- return { value: record._id, field: "_id" };
4808
- }
4809
- return { value: "", field: "id" };
4810
- }
4811
- var createStatusHandler = (status, extraFields) => async (adapter, _razorpaySubscriptionId, record, subscription) => {
4812
- const primaryKey = getSubscriptionPrimaryKey(record);
4813
- if (!primaryKey.value) {
4814
- console.error("[razorpay-webhook] record has no id or _id", {
4815
- recordKeys: Object.keys(record),
4816
- razorpaySubscriptionId: record.razorpaySubscriptionId
4817
- });
4818
- throw new Error("Subscription record has no primary key (id or _id)");
4819
- }
4818
+ var createStatusHandler = (status, extraFields) => async (adapter, razorpaySubscriptionId, record, subscription) => {
4820
4819
  const periodStart = subscription.current_start ? new Date(subscription.current_start * 1e3) : null;
4821
4820
  const periodEnd = subscription.current_end ? new Date(subscription.current_end * 1e3) : null;
4822
- await updateSubscriptionRecord(adapter, primaryKey.value, primaryKey.field, {
4823
- status,
4824
- planId: subscription.plan_id,
4825
- ...periodStart !== null && { periodStart },
4826
- ...periodEnd !== null && { periodEnd },
4827
- ...extraFields?.(subscription) ?? {}
4828
- });
4821
+ const includeStatus = shouldUpdateStatus(record.status, status);
4822
+ await updateSubscriptionRecord(
4823
+ adapter,
4824
+ razorpaySubscriptionId,
4825
+ "razorpaySubscriptionId",
4826
+ {
4827
+ ...includeStatus && { status },
4828
+ planId: subscription.plan_id,
4829
+ ...periodStart !== null && { periodStart },
4830
+ ...periodEnd !== null && { periodEnd },
4831
+ ...extraFields?.(subscription) ?? {}
4832
+ }
4833
+ );
4829
4834
  };
4830
4835
  var eventHandlers = {
4831
4836
  "subscription.authenticated": createStatusHandler("pending"),
@@ -4928,12 +4933,10 @@ async function processWebhookEvent(adapter, rawBody, fallbackBody, onWebhookEven
4928
4933
  message: isDev ? `Subscription record not found for ${subscriptionEntity.id}` : "Subscription record not found"
4929
4934
  };
4930
4935
  }
4931
- const primaryKey = getSubscriptionPrimaryKey(record);
4932
4936
  log("record found", {
4933
4937
  event,
4934
4938
  razorpaySubscriptionId: subscriptionEntity.id,
4935
- recordId: primaryKey.value,
4936
- whereField: primaryKey.field,
4939
+ whereField: "razorpaySubscriptionId",
4937
4940
  hasId: "id" in record && record.id != null,
4938
4941
  has_id: "_id" in record && record._id != null,
4939
4942
  status: record.status
@@ -4952,13 +4955,13 @@ async function processWebhookEvent(adapter, rawBody, fallbackBody, onWebhookEven
4952
4955
  message: isDev ? `Unhandled event: ${event}` : "Unhandled webhook event"
4953
4956
  };
4954
4957
  }
4955
- log("calling handler", { event, recordId: primaryKey.value });
4958
+ log("calling handler", { event, razorpaySubscriptionId: subscriptionEntity.id });
4956
4959
  try {
4957
4960
  await handler(adapter, subscriptionEntity.id, record, subscriptionEntity);
4958
4961
  } catch (handlerError) {
4959
4962
  console.error("[razorpay-webhook] handler failed", {
4960
4963
  event,
4961
- recordId: primaryKey.value,
4964
+ razorpaySubscriptionId: subscriptionEntity.id,
4962
4965
  error: handlerError instanceof Error ? handlerError.message : String(handlerError)
4963
4966
  });
4964
4967
  throw handlerError;