@deiondz/better-auth-razorpay 2.0.17 → 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) {
@@ -4799,15 +4815,16 @@ var updateSubscriptionRecord = async (adapter, subscriptionRecordId, whereField,
4799
4815
  throw err;
4800
4816
  }
4801
4817
  };
4802
- var createStatusHandler = (status, extraFields) => async (adapter, razorpaySubscriptionId, _record, subscription) => {
4818
+ var createStatusHandler = (status, extraFields) => async (adapter, razorpaySubscriptionId, record, subscription) => {
4803
4819
  const periodStart = subscription.current_start ? new Date(subscription.current_start * 1e3) : null;
4804
4820
  const periodEnd = subscription.current_end ? new Date(subscription.current_end * 1e3) : null;
4821
+ const includeStatus = shouldUpdateStatus(record.status, status);
4805
4822
  await updateSubscriptionRecord(
4806
4823
  adapter,
4807
4824
  razorpaySubscriptionId,
4808
4825
  "razorpaySubscriptionId",
4809
4826
  {
4810
- status,
4827
+ ...includeStatus && { status },
4811
4828
  planId: subscription.plan_id,
4812
4829
  ...periodStart !== null && { periodStart },
4813
4830
  ...periodEnd !== null && { periodEnd },