@funnelsgrove/payments 0.45.0 → 0.47.0

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.
@@ -47,6 +47,7 @@ export type BillingPlanMappingCatalog = Record<string, BillingPlanMappingConfig>
47
47
  export type BillingPlanInputCatalog = BillingPlanCatalog | BillingPlanMappingCatalog;
48
48
  export type BillingFirstDiscountConfig = {
49
49
  stage: 'first';
50
+ discountKey?: string;
50
51
  couponId: string;
51
52
  discountPercent: number;
52
53
  amountOffMinor?: number;
@@ -56,6 +57,7 @@ export type BillingFirstDiscountConfig = {
56
57
  };
57
58
  export type BillingSecondDiscountConfig = {
58
59
  stage: 'second';
60
+ discountKey?: string;
59
61
  couponId: string;
60
62
  discountPercent: number;
61
63
  amountOffMinor?: number;
@@ -1 +1 @@
1
- export declare const PAYMENTS_PACKAGE_VERSION = "0.45.0";
1
+ export declare const PAYMENTS_PACKAGE_VERSION = "0.47.0";
@@ -1,2 +1,2 @@
1
1
  // Generated by scripts/generate-package-version.mjs.
2
- export const PAYMENTS_PACKAGE_VERSION = "0.45.0";
2
+ export const PAYMENTS_PACKAGE_VERSION = "0.47.0";
@@ -32,13 +32,7 @@ async function request(input, path, signal, body) {
32
32
  }
33
33
  export async function preparePayPalCheckout(input, idempotencyKey, signal) {
34
34
  var _a;
35
- const result = await request(input, '/sdk/public/v2/payments/checkout-sessions', signal, {
36
- provider: 'paypal', checkoutType: 'subscription', uiMode: 'custom', environment: input.environment,
37
- paymentProfileId: input.option.paymentProfileId, providerPlanId: input.option.providerPlanId,
38
- funnelId: ((_a = input.runtimeConfig) === null || _a === void 0 ? void 0 : _a.funnelId) || FUNNEL_ID, funnelEndUserId: input.funnelEndUserId,
39
- offerSetId: input.offerSetId, planKey: input.funnelPlanKey, idempotencyKey,
40
- returnUrl: window.location.href, successUrl: window.location.href, failUrl: window.location.href,
41
- });
35
+ const result = await request(input, '/sdk/public/v2/payments/checkout-sessions', signal, Object.assign(Object.assign({ provider: 'paypal', checkoutType: 'subscription', uiMode: 'custom', environment: input.environment, paymentProfileId: input.option.paymentProfileId, providerPlanId: input.option.providerPlanId }, (input.option.discountKey ? { couponId: input.option.discountKey } : {})), { funnelId: ((_a = input.runtimeConfig) === null || _a === void 0 ? void 0 : _a.funnelId) || FUNNEL_ID, funnelEndUserId: input.funnelEndUserId, offerSetId: input.offerSetId, planKey: input.funnelPlanKey, idempotencyKey, returnUrl: window.location.href, successUrl: window.location.href, failUrl: window.location.href }));
42
36
  const initialization = result.initialization;
43
37
  if (typeof result.attemptId !== 'string' || !result.attemptId || !record(initialization)
44
38
  || initialization.kind !== 'paypal' || initialization.clientId !== input.option.clientId
@@ -29,7 +29,11 @@ export type StripeWalletFirstCheckoutProps = {
29
29
  /** The visitor closed the Apple Pay or Google Pay sheet without paying, e.g. to offer a discount. */
30
30
  onCancel?: () => void;
31
31
  onError?: (message: string | null) => void;
32
- onSuccess?: () => void | Promise<void>;
32
+ /**
33
+ * Called after a paid checkout. Return `true` when the caller has already moved the visitor on
34
+ * (e.g. client-side navigation to the next step); a card payment then skips the `returnUrl` reload.
35
+ */
36
+ onSuccess?: () => void | boolean | Promise<void | boolean>;
33
37
  returnUrl: string;
34
38
  stripePromise: Promise<Stripe | null> | null;
35
39
  summaryLabel: string;
@@ -113,18 +113,21 @@ function StripeWalletFirstCheckoutForm({ amountCents, checkoutAnalytics, checkou
113
113
  setError(message);
114
114
  onError === null || onError === void 0 ? void 0 : onError(message);
115
115
  };
116
+ /** Resolves true when the caller's onSuccess handled the navigation itself. */
116
117
  const trackSuccess = async (embedded = false) => {
117
118
  if (checkoutAnalytics)
118
119
  await trackPaidStripeSubscriptionCheckoutCompleted(Object.assign(Object.assign({}, checkoutAnalytics), { checkoutSessionId }));
119
- if (!embedded)
120
- await (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess());
120
+ if (embedded)
121
+ return false;
122
+ return (await (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess())) === true;
121
123
  };
122
124
  const finishPaidCheckout = async () => {
123
125
  if (await completePaywallCheckout('stripe', checkoutSessionId || '', {
124
126
  beforeNotify: async () => { setCompleted(true); await trackSuccess(true); },
125
127
  }))
126
128
  return;
127
- await trackSuccess();
129
+ if (await trackSuccess())
130
+ return;
128
131
  window.location.assign(returnUrl);
129
132
  };
130
133
  const submitCard = async (event) => {
@@ -178,7 +181,7 @@ function StripeWalletFirstCheckoutForm({ amountCents, checkoutAnalytics, checkou
178
181
  // where the customer paid, until the success screen takes over.
179
182
  const paymentStatus = completed ? copy.paymentConfirmed : providerConfirmed ? copy.verifyingPayment : null;
180
183
  const locked = Boolean(paymentStatus) && !error;
181
- return (_jsx(CheckoutLayout, { copy: copy, wallet: wallet, selected: selected, onSelect: onSelect, disabled: submitting || walletConfirming || locked, style: style, walletNote: selected === 'wallet' ? paymentStatus : null, walletSlot: _jsxs(_Fragment, { children: [_jsx("div", { className: 'wfc-wallet-button', "data-ready": walletAvailable === true, children: _jsx(StripeCheckoutExpressCheckoutButton, { amountCents: amountCents, availabilityPaymentMethods: [wallet], beforeConfirm: async () => { setWalletConfirming(true); return customerEmail || null; }, checkoutAnalytics: checkoutAnalytics, checkoutSessionId: checkoutSessionId, confirmEmail: false, customerEmail: customerEmail, customerName: customerName, onAvailabilityChange: setWalletAvailable, onCancel: () => { setWalletConfirming(false); onCancel === null || onCancel === void 0 ? void 0 : onCancel(); }, onEmbeddedComplete: () => setCompleted(true), onError: message => { setWalletConfirming(false); showError(message, undefined, true); }, onPaymentRejected: () => { setWalletConfirming(false); onSelect('card'); }, onSuccess: () => trackSuccess(), onVerificationPending: () => setProviderConfirmed(true), options: expressOptions, returnUrl: returnUrl, summaryLabel: summaryLabel }) }), walletAvailable === null ? _jsx("span", { className: 'wfc-progress wfc-progress--overlay', role: 'status', "aria-label": copy.processingLabel }) : null, walletAvailable === false ? _jsx("p", { className: 'wfc-wallet-unavailable', role: 'status', children: copy.walletUnavailableNote.replace('{wallet}', copy.walletLabels[wallet]) }) : null] }), cardBody: _jsxs("form", { className: 'wfc-card-form', onSubmit: event => void submitCard(event), children: [_jsx(PaymentElement, { options: paymentElementOptions, onChange: event => { setPaymentElementComplete(event.complete); if (!event.empty)
184
+ return (_jsx(CheckoutLayout, { copy: copy, wallet: wallet, selected: selected, onSelect: onSelect, disabled: submitting || walletConfirming || locked, style: style, walletNote: selected === 'wallet' ? paymentStatus : null, walletSlot: _jsxs(_Fragment, { children: [_jsx("div", { className: 'wfc-wallet-button', "data-ready": walletAvailable === true, children: _jsx(StripeCheckoutExpressCheckoutButton, { amountCents: amountCents, availabilityPaymentMethods: [wallet], beforeConfirm: async () => { setWalletConfirming(true); return customerEmail || null; }, checkoutAnalytics: checkoutAnalytics, checkoutSessionId: checkoutSessionId, confirmEmail: false, customerEmail: customerEmail, customerName: customerName, onAvailabilityChange: setWalletAvailable, onCancel: () => { setWalletConfirming(false); onCancel === null || onCancel === void 0 ? void 0 : onCancel(); }, onEmbeddedComplete: () => setCompleted(true), onError: message => { setWalletConfirming(false); showError(message, undefined, true); }, onPaymentRejected: () => { setWalletConfirming(false); onSelect('card'); }, onSuccess: async () => { await trackSuccess(); }, onVerificationPending: () => setProviderConfirmed(true), options: expressOptions, returnUrl: returnUrl, summaryLabel: summaryLabel }) }), walletAvailable === null ? _jsx("span", { className: 'wfc-progress wfc-progress--overlay', role: 'status', "aria-label": copy.processingLabel }) : null, walletAvailable === false ? _jsx("p", { className: 'wfc-wallet-unavailable', role: 'status', children: copy.walletUnavailableNote.replace('{wallet}', copy.walletLabels[wallet]) }) : null] }), cardBody: _jsxs("form", { className: 'wfc-card-form', onSubmit: event => void submitCard(event), children: [_jsx(PaymentElement, { options: paymentElementOptions, onChange: event => { setPaymentElementComplete(event.complete); if (!event.empty)
182
185
  onCardEngaged(); }, onReady: () => setPaymentElementReady(true) }), _jsx("button", { type: 'submit', className: 'wfc-submit', disabled: locked || submitting || !paymentElementReady, "aria-live": 'polite', children: locked && paymentStatus ? paymentStatus : submitting ? copy.processingLabel : copy.cardSubmitLabel }), _jsx("p", { className: 'wfc-secure', children: copy.secureNote })] }), footer: error ? _jsx("p", { className: 'wfc-error', role: 'alert', children: error }) : null }));
183
186
  }
184
187
  /** Shared by the loading and live states so switching between them moves nothing. */
@@ -9,6 +9,8 @@ export type PayPalBillingOption = {
9
9
  amountMinor: number;
10
10
  currency: string;
11
11
  summary: string;
12
+ discountKey?: string;
13
+ discountOptionsByKey?: Readonly<Record<string, PayPalBillingOption>>;
12
14
  };
13
15
  export type ResolvedPublishedBillingRuntime = {
14
16
  paypalOptionsByPlanKey?: Readonly<Record<string, PayPalBillingOption>>;
@@ -36,6 +38,8 @@ export type ResolvedPublishedBillingRuntime = {
36
38
  unavailablePlanKeys: readonly string[];
37
39
  };
38
40
  export declare const EMPTY_PUBLISHED_BILLING_RUNTIME: ResolvedPublishedBillingRuntime;
41
+ /** Select native terms without applying another provider's percentage to PayPal. */
42
+ export declare const resolvePayPalBillingOption: (runtime: Pick<ResolvedPublishedBillingRuntime, "paypalOptionsByPlanKey" | "discounts">, funnelPlanKey: string, discountKeyOrCouponId?: string | null) => PayPalBillingOption | null;
39
43
  export declare const resolvePublishedBillingRuntime: ({ config, featureFlags, mode, search, defaultOfferSetId, locale, baseCatalogsByMode, fallbackDiscounts, resolvedExperiments, }: {
40
44
  config: ResolvedFunnelRuntimeConfig | null;
41
45
  featureFlags: Readonly<Record<string, string | boolean>>;
@@ -9,8 +9,27 @@ export const EMPTY_PUBLISHED_BILLING_RUNTIME = Object.freeze({
9
9
  plans: [], planCatalog: {}, defaultPlanKey: '', discounts: null, upsells: [], downsells: [],
10
10
  unavailablePlanKeys: [],
11
11
  });
12
+ /** Select native terms without applying another provider's percentage to PayPal. */
13
+ export const resolvePayPalBillingOption = (runtime, funnelPlanKey, discountKeyOrCouponId) => {
14
+ var _a, _b;
15
+ const option = (_a = runtime.paypalOptionsByPlanKey) === null || _a === void 0 ? void 0 : _a[funnelPlanKey];
16
+ if (!option)
17
+ return null;
18
+ if (!discountKeyOrCouponId)
19
+ return option;
20
+ const discountKey = ((_b = Object.values(runtime.discounts || {}).find(discount => (discount.couponId === discountKeyOrCouponId))) === null || _b === void 0 ? void 0 : _b.discountKey) || discountKeyOrCouponId;
21
+ return option.discountOptionsByKey && Object.hasOwn(option.discountOptionsByKey, discountKey)
22
+ ? option.discountOptionsByKey[discountKey] : option;
23
+ };
24
+ const isRecord = (value) => (Boolean(value) && typeof value === 'object' && !Array.isArray(value));
25
+ const nativePayPalOption = (mapping, paymentProfileId, clientId) => {
26
+ if (!(mapping === null || mapping === void 0 ? void 0 : mapping.providerPlanId) || !mapping.checkoutSummaryLabel || mapping.amountCents == null || !mapping.currency)
27
+ return null;
28
+ return { paymentProfileId, clientId, providerPlanId: mapping.providerPlanId,
29
+ summary: mapping.checkoutSummaryLabel, amountMinor: mapping.amountCents, currency: mapping.currency };
30
+ };
12
31
  export const resolvePublishedBillingRuntime = ({ config, featureFlags, mode, search = '', defaultOfferSetId, locale, baseCatalogsByMode, fallbackDiscounts, resolvedExperiments = resolvePublishedRuntimeExperiments(config), }) => {
13
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
32
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
14
33
  const offerSets = (_a = config === null || config === void 0 ? void 0 : config.offerSets) !== null && _a !== void 0 ? _a : [];
15
34
  const selection = resolvePublishedOfferSetSelection({
16
35
  offerSets,
@@ -38,35 +57,54 @@ export const resolvePublishedBillingRuntime = ({ config, featureFlags, mode, sea
38
57
  const provider = runtime.getPaymentProvider(mode);
39
58
  const paymentProviderConfig = (_c = (_b = runtime.offerSet) === null || _b === void 0 ? void 0 : _b.paymentProviderConfigByMode) === null || _c === void 0 ? void 0 : _c[mode];
40
59
  const paypalOptionsByPlanKey = {};
41
- for (const item of ((_d = runtime.offerSet) === null || _d === void 0 ? void 0 : _d.items) || []) {
42
- const binding = (_e = item.paymentProfileBindings) === null || _e === void 0 ? void 0 : _e.find(candidate => candidate.provider === 'paypal');
43
- const profileId = (binding === null || binding === void 0 ? void 0 : binding.paymentProfileId) || (provider === 'paypal' ? (_f = runtime.offerSet) === null || _f === void 0 ? void 0 : _f.paymentProfileId : null);
60
+ const offerDiscounts = Array.isArray((_e = (_d = runtime.offerSet) === null || _d === void 0 ? void 0 : _d.metadata) === null || _e === void 0 ? void 0 : _e.discounts)
61
+ ? runtime.offerSet.metadata.discounts.filter(isRecord) : [];
62
+ for (const item of ((_f = runtime.offerSet) === null || _f === void 0 ? void 0 : _f.items) || []) {
63
+ const binding = (_g = item.paymentProfileBindings) === null || _g === void 0 ? void 0 : _g.find(candidate => candidate.provider === 'paypal');
64
+ const profileId = (binding === null || binding === void 0 ? void 0 : binding.paymentProfileId) || (provider === 'paypal' ? (_h = runtime.offerSet) === null || _h === void 0 ? void 0 : _h.paymentProfileId : null);
44
65
  if (!profileId)
45
66
  continue;
46
- const mapping = binding ? (_g = binding.providerMappingsByMode) === null || _g === void 0 ? void 0 : _g[mode] : (_h = item.providerMappingsByMode) === null || _h === void 0 ? void 0 : _h[mode];
47
- const profileConfig = ((_l = (_k = (_j = runtime.offerSet) === null || _j === void 0 ? void 0 : _j.paymentProviderConfigByProfileId) === null || _k === void 0 ? void 0 : _k[profileId]) === null || _l === void 0 ? void 0 : _l[mode])
67
+ const mapping = binding ? (_j = binding.providerMappingsByMode) === null || _j === void 0 ? void 0 : _j[mode] : (_k = item.providerMappingsByMode) === null || _k === void 0 ? void 0 : _k[mode];
68
+ const profileConfig = ((_o = (_m = (_l = runtime.offerSet) === null || _l === void 0 ? void 0 : _l.paymentProviderConfigByProfileId) === null || _m === void 0 ? void 0 : _m[profileId]) === null || _o === void 0 ? void 0 : _o[mode])
48
69
  || (provider === 'paypal' ? paymentProviderConfig : null);
49
70
  const clientId = (profileConfig === null || profileConfig === void 0 ? void 0 : profileConfig.provider) === 'paypal' ? profileConfig.paypalClientId : null;
50
- if (!(mapping === null || mapping === void 0 ? void 0 : mapping.providerPlanId) || !clientId || !mapping.checkoutSummaryLabel || mapping.amountCents == null || !mapping.currency)
71
+ if (!clientId)
72
+ continue;
73
+ const option = nativePayPalOption(mapping, profileId, clientId);
74
+ if (!option)
51
75
  continue;
52
- paypalOptionsByPlanKey[item.funnelPlanKey] = { paymentProfileId: profileId, clientId,
53
- providerPlanId: mapping.providerPlanId, summary: mapping.checkoutSummaryLabel,
54
- amountMinor: mapping.amountCents, currency: mapping.currency };
76
+ const discountOptionsByKey = {};
77
+ for (const discount of offerDiscounts) {
78
+ if (!discount.key)
79
+ continue;
80
+ const variants = (_p = discount.metadata) === null || _p === void 0 ? void 0 : _p.paypalPlanMappings;
81
+ if (!Array.isArray(variants))
82
+ continue;
83
+ const variant = variants.filter(isRecord).find(candidate => (candidate.funnelPlanKey === item.funnelPlanKey && candidate.paymentProfileId === profileId));
84
+ const nativeMapping = (_q = variant === null || variant === void 0 ? void 0 : variant.providerMappingsByMode) === null || _q === void 0 ? void 0 : _q[mode];
85
+ const selectedPlanId = mode === 'test' ? variant === null || variant === void 0 ? void 0 : variant.testProviderPlanId : variant === null || variant === void 0 ? void 0 : variant.liveProviderPlanId;
86
+ if (!selectedPlanId || (nativeMapping === null || nativeMapping === void 0 ? void 0 : nativeMapping.providerPlanId) !== selectedPlanId)
87
+ continue;
88
+ const discountOption = nativePayPalOption(nativeMapping, profileId, clientId);
89
+ if (discountOption)
90
+ discountOptionsByKey[discount.key] = Object.assign(Object.assign({}, discountOption), { discountKey: discount.key });
91
+ }
92
+ paypalOptionsByPlanKey[item.funnelPlanKey] = Object.assign(Object.assign({}, option), (Object.keys(discountOptionsByKey).length ? { discountOptionsByKey } : {}));
55
93
  }
56
94
  const pinnedFeatureFlags = Object.assign(Object.assign({}, featureFlags), (selection.experimentId && selection.variantKey
57
95
  ? { [selection.experimentId]: selection.variantKey }
58
96
  : {}));
59
97
  return {
60
98
  paypalOptionsByPlanKey,
61
- revisionId: (_m = config === null || config === void 0 ? void 0 : config.revisionId) !== null && _m !== void 0 ? _m : null,
62
- sourceVersionId: (_o = config === null || config === void 0 ? void 0 : config.sourceVersionId) !== null && _o !== void 0 ? _o : null,
99
+ revisionId: (_r = config === null || config === void 0 ? void 0 : config.revisionId) !== null && _r !== void 0 ? _r : null,
100
+ sourceVersionId: (_s = config === null || config === void 0 ? void 0 : config.sourceVersionId) !== null && _s !== void 0 ? _s : null,
63
101
  paymentsApiVersion: 'v2',
64
102
  mode,
65
103
  offerSetId: selection.offerSetId,
66
- paymentProfileId: ((_q = (_p = runtime.offerSet) === null || _p === void 0 ? void 0 : _p.paymentProfileId) === null || _q === void 0 ? void 0 : _q.trim()) || null,
104
+ paymentProfileId: ((_u = (_t = runtime.offerSet) === null || _t === void 0 ? void 0 : _t.paymentProfileId) === null || _u === void 0 ? void 0 : _u.trim()) || null,
67
105
  provider,
68
106
  stripePublishableKey: provider === 'stripe' && (paymentProviderConfig === null || paymentProviderConfig === void 0 ? void 0 : paymentProviderConfig.provider) === 'stripe'
69
- ? ((_r = paymentProviderConfig === null || paymentProviderConfig === void 0 ? void 0 : paymentProviderConfig.stripePublishableKey) === null || _r === void 0 ? void 0 : _r.trim()) || null
107
+ ? ((_v = paymentProviderConfig === null || paymentProviderConfig === void 0 ? void 0 : paymentProviderConfig.stripePublishableKey) === null || _v === void 0 ? void 0 : _v.trim()) || null
70
108
  : null,
71
109
  experimentId: selection.experimentId,
72
110
  variantKey: selection.variantKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/payments",
3
- "version": "0.45.0",
3
+ "version": "0.47.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/The-Solid-Grove/funnelsgrove.git",
@@ -46,11 +46,11 @@
46
46
  "jsdom": "20.0.3",
47
47
  "typescript": "^5",
48
48
  "vitest": "^3.2.4",
49
- "@funnelsgrove/runtime": "0.34.0",
50
- "@funnelsgrove/analytics": "0.16.0"
49
+ "@funnelsgrove/runtime": "0.35.0",
50
+ "@funnelsgrove/analytics": "0.17.0"
51
51
  },
52
52
  "peerDependencies": {
53
- "@funnelsgrove/runtime": "^0.34.0",
54
- "@funnelsgrove/analytics": "^0.16.0"
53
+ "@funnelsgrove/runtime": "^0.35.0",
54
+ "@funnelsgrove/analytics": "^0.17.0"
55
55
  }
56
56
  }