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