better-auth-streampay 3.3.0 → 3.3.1

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
@@ -782,7 +782,9 @@ function projectSubscriptionFields(sub, existing) {
782
782
  currentCycleNumber: sub.current_cycle_number ?? null,
783
783
  // Only renewal evidence advances the baseline. Other writers only initialize it, so they can
784
784
  // neither absorb a renewal nor roll back one that was just delivered.
785
- ...existing?.renewedCycleNumber == null ? { renewedCycleNumber: existing?.currentCycleNumber ?? sub.current_cycle_number ?? null } : {},
785
+ ...existing?.renewedCycleNumber == null ? {
786
+ renewedCycleNumber: existing?.currentCycleNumber ?? (existing?.streampaySubscriptionId ? sub.current_cycle_number ?? null : firstLinkRenewalBaseline(sub.current_cycle_number))
787
+ } : {},
786
788
  trialStart: (sub.trial_end ? parseDate(sub.started_at) ?? periodStart : null) ?? existing?.trialStart ?? null,
787
789
  trialEnd: parseDate(sub.trial_end),
788
790
  cancelAtPeriodEnd: Boolean(sub.cancel_at_period_end),
@@ -797,6 +799,9 @@ function projectSubscriptionFields(sub, existing) {
797
799
  updatedAt: /* @__PURE__ */ new Date()
798
800
  };
799
801
  }
802
+ function firstLinkRenewalBaseline(currentCycle) {
803
+ return typeof currentCycle === "number" ? Math.min(currentCycle, 1) : null;
804
+ }
800
805
  function parseDate(value) {
801
806
  if (!value) return null;
802
807
  const normalized = /[T ]/.test(value) && !/(?:Z|[+-]\d{2}:?\d{2})$/i.test(value) ? `${value}Z` : value;
@@ -1501,7 +1506,7 @@ async function updateReconciledSubscription(ctx, client, existing, projected, st
1501
1506
  }
1502
1507
  return updated;
1503
1508
  }
1504
- async function reconcileFromStreamPay(ctx, payload, client, plans) {
1509
+ async function reconcileFromStreamPay(ctx, payload, client, plans, prefetched) {
1505
1510
  const streampaySubscriptionId = payload.entity_id;
1506
1511
  if (!streampaySubscriptionId) {
1507
1512
  logger(ctx).warn(`webhook ${payload.event_type} has no entity_id \u2014 cannot reconcile.`);
@@ -1509,7 +1514,7 @@ async function reconcileFromStreamPay(ctx, payload, client, plans) {
1509
1514
  }
1510
1515
  let stream;
1511
1516
  try {
1512
- stream = await client.getSubscription(streampaySubscriptionId);
1517
+ stream = prefetched ?? await client.getSubscription(streampaySubscriptionId);
1513
1518
  } catch (err) {
1514
1519
  const msg = err instanceof Error ? err.message : String(err);
1515
1520
  logger(ctx).error(
@@ -1742,6 +1747,37 @@ async function releaseLockOfCompletedRenewal(ctx, subscriptionRowId, lockEventId
1742
1747
  }
1743
1748
  return released !== null;
1744
1749
  }
1750
+ async function linkCheckoutFromRenewalEvidence(ctx, payload, client, plans, stream) {
1751
+ const paymentLinkId = stream.latest_invoice?.payment_link_id;
1752
+ if (!paymentLinkId || !stream.id) return null;
1753
+ const reservation = await ctx.context.adapter.findOne({
1754
+ model: SUBSCRIPTION_MODEL2,
1755
+ where: [{ field: "streampayPaymentLinkId", value: paymentLinkId }]
1756
+ });
1757
+ if (!reservation) return null;
1758
+ const link = await client.getPaymentLink(paymentLinkId);
1759
+ const { row } = await reconcileFromStreamPay(
1760
+ ctx,
1761
+ {
1762
+ ...payload,
1763
+ entity_id: stream.id,
1764
+ data: {
1765
+ metadata: link.custom_metadata ?? {},
1766
+ payment_link: { id: paymentLinkId, url: "" }
1767
+ }
1768
+ },
1769
+ client,
1770
+ plans,
1771
+ stream
1772
+ );
1773
+ if (row) {
1774
+ logger(ctx).warn(
1775
+ `webhook ${payload.event_type}: linked checkout row=${row.id} to StreamPay sub=${stream.id} from renewal evidence.`
1776
+ );
1777
+ return row;
1778
+ }
1779
+ return findSubscriptionByStreampayId(ctx, stream.id);
1780
+ }
1745
1781
  async function handleRenewalEvidence(ctx, payload, client, plans, callbacks, retryOnCallbackError, retryingRenewalCallback, inboxEnabled) {
1746
1782
  const invoiceId = payload.data?.invoice?.id ?? payload.entity_id;
1747
1783
  if (!invoiceId) return;
@@ -1768,7 +1804,7 @@ async function handleRenewalEvidence(ctx, payload, client, plans, callbacks, ret
1768
1804
  logger(ctx).error(`${payload.event_type}: getSubscription(${subscriptionId}) failed: ${msg}`);
1769
1805
  throw err;
1770
1806
  }
1771
- const existing = await findSubscriptionByStreampayId(ctx, subscriptionId);
1807
+ const existing = await findSubscriptionByStreampayId(ctx, subscriptionId) ?? await linkCheckoutFromRenewalEvidence(ctx, payload, client, plans, stream);
1772
1808
  if (!existing) return;
1773
1809
  const eventAt = parseDate(payload.timestamp);
1774
1810
  const eventIsStale = Boolean(
@@ -5942,7 +5978,7 @@ var onUserDelete = (options) => async (user, context) => {
5942
5978
 
5943
5979
  // package.json
5944
5980
  var package_default = {
5945
- version: "3.3.0"};
5981
+ version: "3.3.1"};
5946
5982
 
5947
5983
  // src/version.ts
5948
5984
  var version = package_default.version;