@funnelsgrove/payments 0.1.26 → 0.1.28

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.
@@ -1,5 +1,5 @@
1
1
  'use client';
2
- import { useCallback, useEffect, useMemo, useRef, useState, } from 'react';
2
+ import { useCallback, useLayoutEffect, useMemo, useRef, useState, } from 'react';
3
3
  import { createEmptyCheckoutPreparationLoading, } from '../paymentProvider.types.js';
4
4
  import { createStripeSubscriptionCheckout, getStripePromise, isStripeConfigured, updateStripeSubscriptionCheckoutPlan, } from '../../services/stripe.service.js';
5
5
  const getFriendlyStripeCheckoutError = (error) => {
@@ -23,6 +23,7 @@ export function useStripeSubscriptionCheckoutSession({ checkoutMode, analyticsMe
23
23
  const [loading, setLoading] = useState(createEmptyCheckoutPreparationLoading);
24
24
  const [runtimeStripePublishableKey, setRuntimeStripePublishableKey] = useState(null);
25
25
  const creatingIntentRef = useRef(null);
26
+ const lastIntentKeyRef = useRef(null);
26
27
  const requestIdRef = useRef(0);
27
28
  const runtimeConfigSignature = [
28
29
  normalizeRuntimeConfigValue(runtimeConfig === null || runtimeConfig === void 0 ? void 0 : runtimeConfig.apiBaseUrl),
@@ -30,7 +31,6 @@ export function useStripeSubscriptionCheckoutSession({ checkoutMode, analyticsMe
30
31
  normalizeRuntimeConfigValue(runtimeConfig === null || runtimeConfig === void 0 ? void 0 : runtimeConfig.funnelVersionId),
31
32
  normalizeRuntimeConfigValue(runtimeConfig === null || runtimeConfig === void 0 ? void 0 : runtimeConfig.funnelSdkPublishableKey),
32
33
  ].join('|');
33
- const analyticsMetadataSignature = JSON.stringify(analyticsMetadata !== null && analyticsMetadata !== void 0 ? analyticsMetadata : {});
34
34
  const configured = enabled &&
35
35
  (isStripeConfigured(checkoutMode) ||
36
36
  Boolean(normalizeRuntimeConfigValue(runtimeConfig === null || runtimeConfig === void 0 ? void 0 : runtimeConfig.funnelSdkPublishableKey)));
@@ -38,7 +38,6 @@ export function useStripeSubscriptionCheckoutSession({ checkoutMode, analyticsMe
38
38
  'stripe',
39
39
  checkoutMode,
40
40
  runtimeConfigSignature,
41
- analyticsMetadataSignature,
42
41
  (couponId === null || couponId === void 0 ? void 0 : couponId.trim()) || '',
43
42
  returnUrl.trim(),
44
43
  ].join('|');
@@ -77,7 +76,15 @@ export function useStripeSubscriptionCheckoutSession({ checkoutMode, analyticsMe
77
76
  setRuntimeStripePublishableKey(null);
78
77
  setError(null);
79
78
  }, [clearActiveCheckoutSession, setError]);
80
- useEffect(() => {
79
+ useLayoutEffect(() => {
80
+ if (lastIntentKeyRef.current === null) {
81
+ lastIntentKeyRef.current = intentKey;
82
+ return;
83
+ }
84
+ if (lastIntentKeyRef.current === intentKey) {
85
+ return;
86
+ }
87
+ lastIntentKeyRef.current = intentKey;
81
88
  reset();
82
89
  }, [intentKey, reset]);
83
90
  const setSourceLoading = useCallback((source, value) => {
@@ -65,6 +65,28 @@ type CreatePaymentIntentResponse = {
65
65
  environment?: 'test' | 'live';
66
66
  };
67
67
  export declare function createStripePaymentIntent(input: CreatePaymentIntentInput): Promise<CreatePaymentIntentResponse>;
68
+ type ChargeOneClickPaymentInput = {
69
+ planId: string;
70
+ amountCents: number;
71
+ analyticsMetadata?: Record<string, unknown> | null;
72
+ checkoutSessionId?: string | null;
73
+ currency?: string | null;
74
+ idempotencyKey?: string | null;
75
+ stripe_customer_id?: string | null;
76
+ user_id?: string | null;
77
+ environment?: RuntimeMode;
78
+ runtimeConfig?: StripeRuntimeConfigOverrides;
79
+ };
80
+ type ChargeOneClickPaymentResponse = {
81
+ paymentIntentId: string;
82
+ status: string;
83
+ amountCents?: number;
84
+ clientSecret?: string;
85
+ currency?: string;
86
+ environment?: 'test' | 'live';
87
+ stripePublishableKey?: string;
88
+ };
89
+ export declare function chargeStripeOneClickPayment(input: ChargeOneClickPaymentInput): Promise<ChargeOneClickPaymentResponse>;
68
90
  type CreateSubscriptionCheckoutInput = {
69
91
  plan: CheckoutSessionPlanInput;
70
92
  couponId?: string | null;
@@ -598,6 +598,35 @@ export async function createStripePaymentIntent(input) {
598
598
  }
599
599
  return (await response.json());
600
600
  }
601
+ export async function chargeStripeOneClickPayment(input) {
602
+ var _a, _b, _c, _d, _e;
603
+ const runtimeConfig = resolveStripeRuntimeConfig(input.runtimeConfig);
604
+ const response = await fetch(buildStripeRuntimeApiUrl('/sdk/public/payments/one-click', runtimeConfig), {
605
+ method: 'POST',
606
+ headers: buildStripeRuntimeHeaders(runtimeConfig, {
607
+ 'Content-Type': 'application/json',
608
+ }),
609
+ body: JSON.stringify({
610
+ publishableKey: runtimeConfig.funnelSdkPublishableKey || undefined,
611
+ funnelId: runtimeConfig.funnelId || undefined,
612
+ funnelVersionId: runtimeConfig.funnelVersionId || undefined,
613
+ planId: input.planId,
614
+ amountCents: input.amountCents,
615
+ currency: ((_a = input.currency) === null || _a === void 0 ? void 0 : _a.trim()) || undefined,
616
+ analyticsMetadata: normalizeAnalyticsMetadata(input.analyticsMetadata),
617
+ user_id: ((_b = input.user_id) === null || _b === void 0 ? void 0 : _b.trim()) || undefined,
618
+ stripe_customer_id: ((_c = input.stripe_customer_id) === null || _c === void 0 ? void 0 : _c.trim()) || undefined,
619
+ checkoutSessionId: ((_d = input.checkoutSessionId) === null || _d === void 0 ? void 0 : _d.trim()) || undefined,
620
+ idempotencyKey: ((_e = input.idempotencyKey) === null || _e === void 0 ? void 0 : _e.trim()) || undefined,
621
+ environment: input.environment || undefined,
622
+ }),
623
+ });
624
+ if (!response.ok) {
625
+ const errorMessage = await parseErrorMessage(response, 'Unable to charge one-click payment');
626
+ throw new Error(errorMessage);
627
+ }
628
+ return (await response.json());
629
+ }
601
630
  export async function createStripeSubscriptionCheckout(input) {
602
631
  var _a, _b, _c, _d, _e;
603
632
  const recurring = resolveCheckoutPlanRecurringConfig(input.plan);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/payments",
3
- "version": "0.1.26",
3
+ "version": "0.1.28",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./dist/index.js",