@alphawebai/medusa-custom-payments-react 0.1.0-rc.49 → 0.1.0-rc.51

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,16 +1,57 @@
1
1
  // src/prism/PrismClickTimePayPalButton.tsx
2
+ import { PayPalProvider } from "@paypal/react-paypal-js/sdk-v6";
3
+ import { useRef as useRef2, useState } from "react";
4
+
5
+ // src/prism/PayPalOneTimePaymentButton.tsx
2
6
  import {
3
- PayPalOneTimePaymentButton,
4
- PayPalProvider
7
+ usePayPal,
8
+ usePayPalOneTimePaymentSession
5
9
  } from "@paypal/react-paypal-js/sdk-v6";
6
- import { useRef, useState } from "react";
10
+ import { createElement, useEffect, useRef } from "react";
11
+ function PayPalOneTimePaymentButton({
12
+ type = "pay",
13
+ disabled = false,
14
+ onStartError,
15
+ ...hookProps
16
+ }) {
17
+ const errorNotifications = useRef(0);
18
+ const { error, isPending, handleClick } = usePayPalOneTimePaymentSession({
19
+ ...hookProps,
20
+ onError: (error2) => {
21
+ errorNotifications.current += 1;
22
+ hookProps.onError?.(error2);
23
+ }
24
+ });
25
+ const { isHydrated } = usePayPal();
26
+ useEffect(() => {
27
+ if (error) console.error(error);
28
+ }, [error]);
29
+ return isHydrated ? createElement("paypal-button", {
30
+ type,
31
+ disabled: disabled || isPending || error !== null ? true : void 0,
32
+ onClick: async () => {
33
+ if (disabled || isPending || error !== null) return;
34
+ const notificationsBeforeStart = errorNotifications.current;
35
+ try {
36
+ await handleClick();
37
+ } catch (error2) {
38
+ if (errorNotifications.current === notificationsBeforeStart) {
39
+ onStartError(error2);
40
+ }
41
+ }
42
+ }
43
+ }) : createElement("div", null);
44
+ }
45
+
46
+ // src/prism/PrismClickTimePayPalButton.tsx
7
47
  import { jsx, jsxs } from "react/jsx-runtime";
8
48
  function safeError(value) {
9
49
  return value instanceof Error ? value : new Error("PayPal could not complete payment approval.");
10
50
  }
11
51
  function ClickTimePayPalButton(props) {
12
- const submittingRef = useRef(false);
13
- const createdOrderIdRef = useRef(null);
52
+ const submittingRef = useRef2(false);
53
+ const completingRef = useRef2(false);
54
+ const createdOrderIdRef = useRef2(null);
14
55
  const [submitting, setSubmittingState] = useState(false);
15
56
  const [message, setMessage] = useState(null);
16
57
  function setSubmitting(next) {
@@ -18,6 +59,12 @@ function ClickTimePayPalButton(props) {
18
59
  setSubmittingState(next);
19
60
  props.onSubmittingChange?.(next);
20
61
  }
62
+ function handleError(error) {
63
+ const normalized = safeError(error);
64
+ setSubmitting(false);
65
+ setMessage(normalized.message);
66
+ props.onError(normalized);
67
+ }
21
68
  return /* @__PURE__ */ jsxs("div", { "aria-busy": submitting, className: props.classNames?.container, children: [
22
69
  /* @__PURE__ */ jsx(
23
70
  PayPalOneTimePaymentButton,
@@ -26,6 +73,8 @@ function ClickTimePayPalButton(props) {
26
73
  className: props.classNames?.button,
27
74
  disabled: props.disabled || submitting,
28
75
  createOrder: async () => {
76
+ if (props.disabled || submittingRef.current) throw new Error("Payment is not ready. Please try again.");
77
+ if (props.lockDuringApproval) setSubmitting(true);
29
78
  setMessage(null);
30
79
  const created = await props.onCreateOrder();
31
80
  const orderId = typeof created?.orderId === "string" && created.orderId.length > 0 ? created.orderId : null;
@@ -41,16 +90,16 @@ function ClickTimePayPalButton(props) {
41
90
  commit: true,
42
91
  presentationMode: "auto",
43
92
  onApprove: async (data) => {
44
- if (submittingRef.current) return;
93
+ if (completingRef.current) return;
45
94
  if (!createdOrderIdRef.current || data.orderId !== createdOrderIdRef.current) {
46
95
  const error = new Error(
47
96
  "PayPal approved a stale payment session. Refresh checkout and try again."
48
97
  );
49
- setMessage(error.message);
50
- props.onError(error);
98
+ handleError(error);
51
99
  return;
52
100
  }
53
101
  setSubmitting(true);
102
+ completingRef.current = true;
54
103
  setMessage(null);
55
104
  try {
56
105
  await props.onProviderApproved({
@@ -64,6 +113,7 @@ function ClickTimePayPalButton(props) {
64
113
  setMessage(normalized.message);
65
114
  props.onError(normalized);
66
115
  } finally {
116
+ completingRef.current = false;
67
117
  setSubmitting(false);
68
118
  }
69
119
  },
@@ -77,15 +127,11 @@ function ClickTimePayPalButton(props) {
77
127
  orderId: createdOrderIdRef.current
78
128
  });
79
129
  },
80
- onError: (error) => {
81
- const normalized = safeError(error);
82
- setSubmitting(false);
83
- setMessage(normalized.message);
84
- props.onError(normalized);
85
- }
130
+ onError: handleError,
131
+ onStartError: handleError
86
132
  }
87
133
  ),
88
- message ? /* @__PURE__ */ jsx("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
134
+ message && props.errorPresentation !== "external" ? /* @__PURE__ */ jsx("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
89
135
  /* @__PURE__ */ jsx(
90
136
  "span",
91
137
  {
@@ -122,17 +168,14 @@ function PrismClickTimePayPalButton(props) {
122
168
  }
123
169
 
124
170
  // src/prism/PrismPayPalPaymentButton.tsx
125
- import {
126
- PayPalOneTimePaymentButton as PayPalOneTimePaymentButton2,
127
- PayPalProvider as PayPalProvider2
128
- } from "@paypal/react-paypal-js/sdk-v6";
129
- import { useRef as useRef2, useState as useState2 } from "react";
171
+ import { PayPalProvider as PayPalProvider2 } from "@paypal/react-paypal-js/sdk-v6";
172
+ import { useRef as useRef3, useState as useState2 } from "react";
130
173
  import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
131
174
  function safeError2(value) {
132
175
  return value instanceof Error ? value : new Error("PayPal could not complete payment approval.");
133
176
  }
134
177
  function PayPalButton(props) {
135
- const submittingRef = useRef2(false);
178
+ const submittingRef = useRef3(false);
136
179
  const [submitting, setSubmittingState] = useState2(false);
137
180
  const [message, setMessage] = useState2(null);
138
181
  function setSubmitting(next) {
@@ -140,6 +183,12 @@ function PayPalButton(props) {
140
183
  setSubmittingState(next);
141
184
  props.onSubmittingChange?.(next);
142
185
  }
186
+ function handleError(error) {
187
+ const normalized = safeError2(error);
188
+ setSubmitting(false);
189
+ setMessage(normalized.message);
190
+ props.onError(normalized);
191
+ }
143
192
  return /* @__PURE__ */ jsxs2(
144
193
  "div",
145
194
  {
@@ -147,7 +196,7 @@ function PayPalButton(props) {
147
196
  className: props.classNames?.container,
148
197
  children: [
149
198
  /* @__PURE__ */ jsx2(
150
- PayPalOneTimePaymentButton2,
199
+ PayPalOneTimePaymentButton,
151
200
  {
152
201
  type: "pay",
153
202
  className: props.classNames?.button,
@@ -191,15 +240,11 @@ function PayPalButton(props) {
191
240
  orderId: props.session.orderId
192
241
  });
193
242
  },
194
- onError: (error) => {
195
- const normalized = safeError2(error);
196
- setSubmitting(false);
197
- setMessage(normalized.message);
198
- props.onError(normalized);
199
- }
243
+ onError: handleError,
244
+ onStartError: handleError
200
245
  }
201
246
  ),
202
- message ? /* @__PURE__ */ jsx2("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
247
+ message && props.errorPresentation !== "external" ? /* @__PURE__ */ jsx2("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
203
248
  /* @__PURE__ */ jsx2(
204
249
  "span",
205
250
  {
@@ -8,7 +8,61 @@ import {
8
8
  import {
9
9
  loadStripe
10
10
  } from "@stripe/stripe-js";
11
- import { useMemo, useState } from "react";
11
+ import { useEffect, useMemo, useRef, useState } from "react";
12
+
13
+ // src/prism/stripe-amount-updater.ts
14
+ function stripeAmountUpdater(elements, initial, applied, failed) {
15
+ let current = initial;
16
+ let desired = initial;
17
+ let pending = null;
18
+ let disposed = false;
19
+ let broken = false;
20
+ let timer;
21
+ const report = (error) => {
22
+ if (disposed) return;
23
+ broken = true;
24
+ applied(null);
25
+ clearTimeout(timer);
26
+ failed(error instanceof Error ? error : new Error("Payment options could not be updated."));
27
+ };
28
+ const drain = () => {
29
+ if (disposed || broken || pending !== null || current === desired) return;
30
+ pending = desired;
31
+ applied(null);
32
+ timer = setTimeout(() => report(new Error("Payment options could not be updated. Please refresh checkout.")), 15e3);
33
+ try {
34
+ const result = elements.update({ amount: pending });
35
+ if (result && typeof result.catch === "function") {
36
+ void result.catch(report);
37
+ }
38
+ } catch (error) {
39
+ report(error);
40
+ }
41
+ };
42
+ const complete = () => {
43
+ if (disposed || broken || pending === null) return;
44
+ clearTimeout(timer);
45
+ current = pending;
46
+ pending = null;
47
+ applied(current);
48
+ drain();
49
+ };
50
+ elements.on("update-end", complete);
51
+ return {
52
+ set(amount) {
53
+ desired = amount;
54
+ drain();
55
+ },
56
+ isApplied(amount) {
57
+ return !disposed && !broken && pending === null && current === amount;
58
+ },
59
+ // Elements has no public off(): ignore late events until its owning Elements is destroyed.
60
+ dispose() {
61
+ disposed = true;
62
+ clearTimeout(timer);
63
+ }
64
+ };
65
+ }
12
66
 
13
67
  // src/prism/outcome.ts
14
68
  function normalizeStripePaymentIntent(paymentIntent) {
@@ -140,6 +194,27 @@ function DeferredPaymentElementForm(props) {
140
194
  const [ready, setReady] = useState(false);
141
195
  const [submitting, setSubmitting] = useState(false);
142
196
  const [message, setMessage] = useState(null);
197
+ const [appliedAmount, setAppliedAmount] = useState(props.initialAmount ?? props.amountMinor);
198
+ const amountUpdater = useRef(null);
199
+ const latest = useRef(props);
200
+ latest.current = props;
201
+ const submittingRef = useRef(false);
202
+ const amountReady = !props.retainOnAmountChange || appliedAmount === props.amountMinor;
203
+ useEffect(() => {
204
+ if (!elements || !props.retainOnAmountChange) return;
205
+ const updater = stripeAmountUpdater(elements, props.initialAmount ?? props.amountMinor, setAppliedAmount, (error) => {
206
+ latest.current.onError(error, hostErrorContext("details"));
207
+ });
208
+ amountUpdater.current = updater;
209
+ updater.set(latest.current.amountMinor);
210
+ return () => {
211
+ updater.dispose();
212
+ amountUpdater.current = null;
213
+ };
214
+ }, [elements, props.retainOnAmountChange, props.initialAmount]);
215
+ useEffect(() => {
216
+ amountUpdater.current?.set(props.amountMinor);
217
+ }, [props.amountMinor]);
143
218
  const callbacks = {
144
219
  onAuthorized: props.onAuthorized,
145
220
  onPending: props.onPending,
@@ -153,7 +228,9 @@ function DeferredPaymentElementForm(props) {
153
228
  }
154
229
  async function submit(event) {
155
230
  event.preventDefault();
156
- if (!stripe || !elements || submitting || props.disabled) return;
231
+ if (!stripe || !elements || !ready || !amountReady || submittingRef.current || props.disabled) return;
232
+ if (props.retainOnAmountChange && !amountUpdater.current?.isApplied(latest.current.amountMinor)) return;
233
+ submittingRef.current = true;
157
234
  setSubmitting(true);
158
235
  props.onSubmittingChange?.(true);
159
236
  setMessage(null);
@@ -205,6 +282,7 @@ function DeferredPaymentElementForm(props) {
205
282
  const safeError = error instanceof Error ? error : new Error("Payment could not be completed.");
206
283
  reportError(safeError, hostErrorContext(phase));
207
284
  } finally {
285
+ submittingRef.current = false;
208
286
  setSubmitting(false);
209
287
  props.onSubmittingChange?.(false);
210
288
  }
@@ -240,7 +318,7 @@ function DeferredPaymentElementForm(props) {
240
318
  "button",
241
319
  {
242
320
  type: "submit",
243
- disabled: !stripe || !elements || !ready || submitting || props.disabled,
321
+ disabled: !stripe || !elements || !ready || !amountReady || submitting || props.disabled,
244
322
  className: props.classNames?.submitButton,
245
323
  children: submitting ? "Processing payment..." : props.submitLabel ?? "Pay now"
246
324
  }
@@ -270,7 +348,7 @@ function deferredStripeElementsContract(props) {
270
348
  const excludedPaymentMethodTypes = props.context.excludedPaymentMethodTypes ? [...props.context.excludedPaymentMethodTypes].sort() : void 0;
271
349
  const elementsContractKey = [
272
350
  props.context.publishableKey,
273
- props.amountMinor,
351
+ props.retainOnAmountChange ? "retained" : props.amountMinor,
274
352
  props.currency,
275
353
  captureMethod,
276
354
  props.setupFutureUsage ?? "none",
@@ -298,7 +376,12 @@ function PrismDeferredPaymentElement(props) {
298
376
  );
299
377
  if (validationError) throw new Error(validationError);
300
378
  const { elementsContractKey, options } = deferredStripeElementsContract(props);
301
- return /* @__PURE__ */ jsx(Elements, { stripe, options, children: /* @__PURE__ */ jsx(DeferredPaymentElementForm, { ...props }) }, elementsContractKey);
379
+ if (!props.retainOnAmountChange) return /* @__PURE__ */ jsx(Elements, { stripe, options, children: /* @__PURE__ */ jsx(DeferredPaymentElementForm, { ...props }) }, elementsContractKey);
380
+ return /* @__PURE__ */ jsx(DeferredElements, { stripe, options, payment: props }, elementsContractKey);
381
+ }
382
+ function DeferredElements({ stripe, options, payment }) {
383
+ const [initial] = useState(() => ({ options, amount: payment.amountMinor }));
384
+ return /* @__PURE__ */ jsx(Elements, { stripe, options: initial.options, children: /* @__PURE__ */ jsx(DeferredPaymentElementForm, { ...payment, initialAmount: initial.amount }) });
302
385
  }
303
386
 
304
387
  // src/prism/PrismPaymentElement.tsx
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react from 'react';
2
- import { P as PrismDeferredPaymentElementProps, a as PrismPaymentElementProps } from './types-CzSrJFo2.mjs';
3
- export { b as PrismCheckoutProviderContext, c as PrismClickTimePayPalButtonProps, d as PrismClickTimePayPalOrder, e as PrismDeferredPaymentSurfaceProps, f as PrismDeferredStripePreparedPayment, g as PrismPayPalApprovalOutcome, h as PrismPayPalCheckoutProviderContext, i as PrismPayPalPaymentButtonClassNames, j as PrismPayPalPaymentButtonProps, k as PrismPaymentBillingDetails, l as PrismPaymentErrorContext, m as PrismPaymentErrorPhase, n as PrismPaymentErrorPresentation, o as PrismPaymentOutcome, p as PrismPaymentSurfaceProps, q as PrismPreparedPayPalPaymentSession, r as PrismPreparedPaymentSession, s as PrismPreparedStripePaymentSession, t as PrismStripeCheckoutProviderContext, u as PrismStripeDisplayPolicy, S as StripeSupportedTermMethod } from './types-CzSrJFo2.mjs';
2
+ import { P as PrismDeferredPaymentElementProps, a as PrismPaymentElementProps } from './types-ChWEfkib.mjs';
3
+ export { b as PrismCheckoutProviderContext, c as PrismClickTimePayPalButtonProps, d as PrismClickTimePayPalOrder, e as PrismDeferredPaymentSurfaceProps, f as PrismDeferredStripePreparedPayment, g as PrismPayPalApprovalOutcome, h as PrismPayPalCheckoutProviderContext, i as PrismPayPalPaymentButtonClassNames, j as PrismPayPalPaymentButtonProps, k as PrismPaymentBillingDetails, l as PrismPaymentErrorContext, m as PrismPaymentErrorPhase, n as PrismPaymentErrorPresentation, o as PrismPaymentOutcome, p as PrismPaymentSurfaceProps, q as PrismPreparedPayPalPaymentSession, r as PrismPreparedPaymentSession, s as PrismPreparedStripePaymentSession, t as PrismStripeCheckoutProviderContext, u as PrismStripeDisplayPolicy, S as StripeSupportedTermMethod } from './types-ChWEfkib.mjs';
4
4
  import '@stripe/stripe-js';
5
5
 
6
6
  declare function PrismDeferredPaymentElement(props: PrismDeferredPaymentElementProps): react.JSX.Element;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react from 'react';
2
- import { P as PrismDeferredPaymentElementProps, a as PrismPaymentElementProps } from './types-CzSrJFo2.js';
3
- export { b as PrismCheckoutProviderContext, c as PrismClickTimePayPalButtonProps, d as PrismClickTimePayPalOrder, e as PrismDeferredPaymentSurfaceProps, f as PrismDeferredStripePreparedPayment, g as PrismPayPalApprovalOutcome, h as PrismPayPalCheckoutProviderContext, i as PrismPayPalPaymentButtonClassNames, j as PrismPayPalPaymentButtonProps, k as PrismPaymentBillingDetails, l as PrismPaymentErrorContext, m as PrismPaymentErrorPhase, n as PrismPaymentErrorPresentation, o as PrismPaymentOutcome, p as PrismPaymentSurfaceProps, q as PrismPreparedPayPalPaymentSession, r as PrismPreparedPaymentSession, s as PrismPreparedStripePaymentSession, t as PrismStripeCheckoutProviderContext, u as PrismStripeDisplayPolicy, S as StripeSupportedTermMethod } from './types-CzSrJFo2.js';
2
+ import { P as PrismDeferredPaymentElementProps, a as PrismPaymentElementProps } from './types-ChWEfkib.js';
3
+ export { b as PrismCheckoutProviderContext, c as PrismClickTimePayPalButtonProps, d as PrismClickTimePayPalOrder, e as PrismDeferredPaymentSurfaceProps, f as PrismDeferredStripePreparedPayment, g as PrismPayPalApprovalOutcome, h as PrismPayPalCheckoutProviderContext, i as PrismPayPalPaymentButtonClassNames, j as PrismPayPalPaymentButtonProps, k as PrismPaymentBillingDetails, l as PrismPaymentErrorContext, m as PrismPaymentErrorPhase, n as PrismPaymentErrorPresentation, o as PrismPaymentOutcome, p as PrismPaymentSurfaceProps, q as PrismPreparedPayPalPaymentSession, r as PrismPreparedPaymentSession, s as PrismPreparedStripePaymentSession, t as PrismStripeCheckoutProviderContext, u as PrismStripeDisplayPolicy, S as StripeSupportedTermMethod } from './types-ChWEfkib.js';
4
4
  import '@stripe/stripe-js';
5
5
 
6
6
  declare function PrismDeferredPaymentElement(props: PrismDeferredPaymentElementProps): react.JSX.Element;
package/dist/index.js CHANGED
@@ -30,6 +30,60 @@ var import_react_stripe_js = require("@stripe/react-stripe-js");
30
30
  var import_stripe_js = require("@stripe/stripe-js");
31
31
  var import_react = require("react");
32
32
 
33
+ // src/prism/stripe-amount-updater.ts
34
+ function stripeAmountUpdater(elements, initial, applied, failed) {
35
+ let current = initial;
36
+ let desired = initial;
37
+ let pending = null;
38
+ let disposed = false;
39
+ let broken = false;
40
+ let timer;
41
+ const report = (error) => {
42
+ if (disposed) return;
43
+ broken = true;
44
+ applied(null);
45
+ clearTimeout(timer);
46
+ failed(error instanceof Error ? error : new Error("Payment options could not be updated."));
47
+ };
48
+ const drain = () => {
49
+ if (disposed || broken || pending !== null || current === desired) return;
50
+ pending = desired;
51
+ applied(null);
52
+ timer = setTimeout(() => report(new Error("Payment options could not be updated. Please refresh checkout.")), 15e3);
53
+ try {
54
+ const result = elements.update({ amount: pending });
55
+ if (result && typeof result.catch === "function") {
56
+ void result.catch(report);
57
+ }
58
+ } catch (error) {
59
+ report(error);
60
+ }
61
+ };
62
+ const complete = () => {
63
+ if (disposed || broken || pending === null) return;
64
+ clearTimeout(timer);
65
+ current = pending;
66
+ pending = null;
67
+ applied(current);
68
+ drain();
69
+ };
70
+ elements.on("update-end", complete);
71
+ return {
72
+ set(amount) {
73
+ desired = amount;
74
+ drain();
75
+ },
76
+ isApplied(amount) {
77
+ return !disposed && !broken && pending === null && current === amount;
78
+ },
79
+ // Elements has no public off(): ignore late events until its owning Elements is destroyed.
80
+ dispose() {
81
+ disposed = true;
82
+ clearTimeout(timer);
83
+ }
84
+ };
85
+ }
86
+
33
87
  // src/prism/outcome.ts
34
88
  function normalizeStripePaymentIntent(paymentIntent) {
35
89
  switch (paymentIntent.status) {
@@ -160,6 +214,27 @@ function DeferredPaymentElementForm(props) {
160
214
  const [ready, setReady] = (0, import_react.useState)(false);
161
215
  const [submitting, setSubmitting] = (0, import_react.useState)(false);
162
216
  const [message, setMessage] = (0, import_react.useState)(null);
217
+ const [appliedAmount, setAppliedAmount] = (0, import_react.useState)(props.initialAmount ?? props.amountMinor);
218
+ const amountUpdater = (0, import_react.useRef)(null);
219
+ const latest = (0, import_react.useRef)(props);
220
+ latest.current = props;
221
+ const submittingRef = (0, import_react.useRef)(false);
222
+ const amountReady = !props.retainOnAmountChange || appliedAmount === props.amountMinor;
223
+ (0, import_react.useEffect)(() => {
224
+ if (!elements || !props.retainOnAmountChange) return;
225
+ const updater = stripeAmountUpdater(elements, props.initialAmount ?? props.amountMinor, setAppliedAmount, (error) => {
226
+ latest.current.onError(error, hostErrorContext("details"));
227
+ });
228
+ amountUpdater.current = updater;
229
+ updater.set(latest.current.amountMinor);
230
+ return () => {
231
+ updater.dispose();
232
+ amountUpdater.current = null;
233
+ };
234
+ }, [elements, props.retainOnAmountChange, props.initialAmount]);
235
+ (0, import_react.useEffect)(() => {
236
+ amountUpdater.current?.set(props.amountMinor);
237
+ }, [props.amountMinor]);
163
238
  const callbacks = {
164
239
  onAuthorized: props.onAuthorized,
165
240
  onPending: props.onPending,
@@ -173,7 +248,9 @@ function DeferredPaymentElementForm(props) {
173
248
  }
174
249
  async function submit(event) {
175
250
  event.preventDefault();
176
- if (!stripe || !elements || submitting || props.disabled) return;
251
+ if (!stripe || !elements || !ready || !amountReady || submittingRef.current || props.disabled) return;
252
+ if (props.retainOnAmountChange && !amountUpdater.current?.isApplied(latest.current.amountMinor)) return;
253
+ submittingRef.current = true;
177
254
  setSubmitting(true);
178
255
  props.onSubmittingChange?.(true);
179
256
  setMessage(null);
@@ -225,6 +302,7 @@ function DeferredPaymentElementForm(props) {
225
302
  const safeError = error instanceof Error ? error : new Error("Payment could not be completed.");
226
303
  reportError(safeError, hostErrorContext(phase));
227
304
  } finally {
305
+ submittingRef.current = false;
228
306
  setSubmitting(false);
229
307
  props.onSubmittingChange?.(false);
230
308
  }
@@ -260,7 +338,7 @@ function DeferredPaymentElementForm(props) {
260
338
  "button",
261
339
  {
262
340
  type: "submit",
263
- disabled: !stripe || !elements || !ready || submitting || props.disabled,
341
+ disabled: !stripe || !elements || !ready || !amountReady || submitting || props.disabled,
264
342
  className: props.classNames?.submitButton,
265
343
  children: submitting ? "Processing payment..." : props.submitLabel ?? "Pay now"
266
344
  }
@@ -290,7 +368,7 @@ function deferredStripeElementsContract(props) {
290
368
  const excludedPaymentMethodTypes = props.context.excludedPaymentMethodTypes ? [...props.context.excludedPaymentMethodTypes].sort() : void 0;
291
369
  const elementsContractKey = [
292
370
  props.context.publishableKey,
293
- props.amountMinor,
371
+ props.retainOnAmountChange ? "retained" : props.amountMinor,
294
372
  props.currency,
295
373
  captureMethod,
296
374
  props.setupFutureUsage ?? "none",
@@ -318,7 +396,12 @@ function PrismDeferredPaymentElement(props) {
318
396
  );
319
397
  if (validationError) throw new Error(validationError);
320
398
  const { elementsContractKey, options } = deferredStripeElementsContract(props);
321
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_stripe_js.Elements, { stripe, options, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DeferredPaymentElementForm, { ...props }) }, elementsContractKey);
399
+ if (!props.retainOnAmountChange) return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_stripe_js.Elements, { stripe, options, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DeferredPaymentElementForm, { ...props }) }, elementsContractKey);
400
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DeferredElements, { stripe, options, payment: props }, elementsContractKey);
401
+ }
402
+ function DeferredElements({ stripe, options, payment }) {
403
+ const [initial] = (0, import_react.useState)(() => ({ options, amount: payment.amountMinor }));
404
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_stripe_js.Elements, { stripe, options: initial.options, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DeferredPaymentElementForm, { ...payment, initialAmount: initial.amount }) });
322
405
  }
323
406
 
324
407
  // src/prism/PrismPaymentElement.tsx
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  PrismDeferredPaymentElement,
3
3
  PrismPaymentElement
4
- } from "./chunk-HHLAGEBO.mjs";
4
+ } from "./chunk-XNEJKU5P.mjs";
5
5
  export {
6
6
  PrismDeferredPaymentElement,
7
7
  PrismPaymentElement
@@ -1,6 +1,6 @@
1
1
  import * as react from 'react';
2
- import { e as PrismDeferredPaymentSurfaceProps, p as PrismPaymentSurfaceProps } from './types-CzSrJFo2.mjs';
3
- export { b as PrismCheckoutProviderContext, h as PrismPayPalCheckoutProviderContext, l as PrismPaymentErrorContext, m as PrismPaymentErrorPhase, n as PrismPaymentErrorPresentation, r as PrismPreparedPaymentSession, t as PrismStripeCheckoutProviderContext } from './types-CzSrJFo2.mjs';
2
+ import { e as PrismDeferredPaymentSurfaceProps, p as PrismPaymentSurfaceProps } from './types-ChWEfkib.mjs';
3
+ export { b as PrismCheckoutProviderContext, h as PrismPayPalCheckoutProviderContext, l as PrismPaymentErrorContext, m as PrismPaymentErrorPhase, n as PrismPaymentErrorPresentation, r as PrismPreparedPaymentSession, t as PrismStripeCheckoutProviderContext } from './types-ChWEfkib.mjs';
4
4
  import '@stripe/stripe-js';
5
5
 
6
6
  /**
@@ -1,6 +1,6 @@
1
1
  import * as react from 'react';
2
- import { e as PrismDeferredPaymentSurfaceProps, p as PrismPaymentSurfaceProps } from './types-CzSrJFo2.js';
3
- export { b as PrismCheckoutProviderContext, h as PrismPayPalCheckoutProviderContext, l as PrismPaymentErrorContext, m as PrismPaymentErrorPhase, n as PrismPaymentErrorPresentation, r as PrismPreparedPaymentSession, t as PrismStripeCheckoutProviderContext } from './types-CzSrJFo2.js';
2
+ import { e as PrismDeferredPaymentSurfaceProps, p as PrismPaymentSurfaceProps } from './types-ChWEfkib.js';
3
+ export { b as PrismCheckoutProviderContext, h as PrismPayPalCheckoutProviderContext, l as PrismPaymentErrorContext, m as PrismPaymentErrorPhase, n as PrismPaymentErrorPresentation, r as PrismPreparedPaymentSession, t as PrismStripeCheckoutProviderContext } from './types-ChWEfkib.js';
4
4
  import '@stripe/stripe-js';
5
5
 
6
6
  /**
@@ -26,30 +26,79 @@ __export(payment_surface_exports, {
26
26
  module.exports = __toCommonJS(payment_surface_exports);
27
27
 
28
28
  // src/prism/PrismClickTimePayPalButton.tsx
29
+ var import_sdk_v62 = require("@paypal/react-paypal-js/sdk-v6");
30
+ var import_react2 = require("react");
31
+
32
+ // src/prism/PayPalOneTimePaymentButton.tsx
29
33
  var import_sdk_v6 = require("@paypal/react-paypal-js/sdk-v6");
30
34
  var import_react = require("react");
35
+ function PayPalOneTimePaymentButton({
36
+ type = "pay",
37
+ disabled = false,
38
+ onStartError,
39
+ ...hookProps
40
+ }) {
41
+ const errorNotifications = (0, import_react.useRef)(0);
42
+ const { error, isPending, handleClick } = (0, import_sdk_v6.usePayPalOneTimePaymentSession)({
43
+ ...hookProps,
44
+ onError: (error2) => {
45
+ errorNotifications.current += 1;
46
+ hookProps.onError?.(error2);
47
+ }
48
+ });
49
+ const { isHydrated } = (0, import_sdk_v6.usePayPal)();
50
+ (0, import_react.useEffect)(() => {
51
+ if (error) console.error(error);
52
+ }, [error]);
53
+ return isHydrated ? (0, import_react.createElement)("paypal-button", {
54
+ type,
55
+ disabled: disabled || isPending || error !== null ? true : void 0,
56
+ onClick: async () => {
57
+ if (disabled || isPending || error !== null) return;
58
+ const notificationsBeforeStart = errorNotifications.current;
59
+ try {
60
+ await handleClick();
61
+ } catch (error2) {
62
+ if (errorNotifications.current === notificationsBeforeStart) {
63
+ onStartError(error2);
64
+ }
65
+ }
66
+ }
67
+ }) : (0, import_react.createElement)("div", null);
68
+ }
69
+
70
+ // src/prism/PrismClickTimePayPalButton.tsx
31
71
  var import_jsx_runtime = require("react/jsx-runtime");
32
72
  function safeError(value) {
33
73
  return value instanceof Error ? value : new Error("PayPal could not complete payment approval.");
34
74
  }
35
75
  function ClickTimePayPalButton(props) {
36
- const submittingRef = (0, import_react.useRef)(false);
37
- const createdOrderIdRef = (0, import_react.useRef)(null);
38
- const [submitting, setSubmittingState] = (0, import_react.useState)(false);
39
- const [message, setMessage] = (0, import_react.useState)(null);
76
+ const submittingRef = (0, import_react2.useRef)(false);
77
+ const completingRef = (0, import_react2.useRef)(false);
78
+ const createdOrderIdRef = (0, import_react2.useRef)(null);
79
+ const [submitting, setSubmittingState] = (0, import_react2.useState)(false);
80
+ const [message, setMessage] = (0, import_react2.useState)(null);
40
81
  function setSubmitting(next) {
41
82
  submittingRef.current = next;
42
83
  setSubmittingState(next);
43
84
  props.onSubmittingChange?.(next);
44
85
  }
86
+ function handleError(error) {
87
+ const normalized = safeError(error);
88
+ setSubmitting(false);
89
+ setMessage(normalized.message);
90
+ props.onError(normalized);
91
+ }
45
92
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { "aria-busy": submitting, className: props.classNames?.container, children: [
46
93
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
47
- import_sdk_v6.PayPalOneTimePaymentButton,
94
+ PayPalOneTimePaymentButton,
48
95
  {
49
96
  type: "pay",
50
97
  className: props.classNames?.button,
51
98
  disabled: props.disabled || submitting,
52
99
  createOrder: async () => {
100
+ if (props.disabled || submittingRef.current) throw new Error("Payment is not ready. Please try again.");
101
+ if (props.lockDuringApproval) setSubmitting(true);
53
102
  setMessage(null);
54
103
  const created = await props.onCreateOrder();
55
104
  const orderId = typeof created?.orderId === "string" && created.orderId.length > 0 ? created.orderId : null;
@@ -65,16 +114,16 @@ function ClickTimePayPalButton(props) {
65
114
  commit: true,
66
115
  presentationMode: "auto",
67
116
  onApprove: async (data) => {
68
- if (submittingRef.current) return;
117
+ if (completingRef.current) return;
69
118
  if (!createdOrderIdRef.current || data.orderId !== createdOrderIdRef.current) {
70
119
  const error = new Error(
71
120
  "PayPal approved a stale payment session. Refresh checkout and try again."
72
121
  );
73
- setMessage(error.message);
74
- props.onError(error);
122
+ handleError(error);
75
123
  return;
76
124
  }
77
125
  setSubmitting(true);
126
+ completingRef.current = true;
78
127
  setMessage(null);
79
128
  try {
80
129
  await props.onProviderApproved({
@@ -88,6 +137,7 @@ function ClickTimePayPalButton(props) {
88
137
  setMessage(normalized.message);
89
138
  props.onError(normalized);
90
139
  } finally {
140
+ completingRef.current = false;
91
141
  setSubmitting(false);
92
142
  }
93
143
  },
@@ -101,15 +151,11 @@ function ClickTimePayPalButton(props) {
101
151
  orderId: createdOrderIdRef.current
102
152
  });
103
153
  },
104
- onError: (error) => {
105
- const normalized = safeError(error);
106
- setSubmitting(false);
107
- setMessage(normalized.message);
108
- props.onError(normalized);
109
- }
154
+ onError: handleError,
155
+ onStartError: handleError
110
156
  }
111
157
  ),
112
- message ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
158
+ message && props.errorPresentation !== "external" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
113
159
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
114
160
  "span",
115
161
  {
@@ -133,7 +179,7 @@ function PrismClickTimePayPalButton(props) {
133
179
  throw new Error("Prism PayPal checkout context is invalid.");
134
180
  }
135
181
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
136
- import_sdk_v6.PayPalProvider,
182
+ import_sdk_v62.PayPalProvider,
137
183
  {
138
184
  clientId: props.context.clientId,
139
185
  environment: props.context.environment,
@@ -148,7 +194,61 @@ function PrismClickTimePayPalButton(props) {
148
194
  // src/prism/PrismDeferredPaymentElement.tsx
149
195
  var import_react_stripe_js = require("@stripe/react-stripe-js");
150
196
  var import_stripe_js = require("@stripe/stripe-js");
151
- var import_react2 = require("react");
197
+ var import_react3 = require("react");
198
+
199
+ // src/prism/stripe-amount-updater.ts
200
+ function stripeAmountUpdater(elements, initial, applied, failed) {
201
+ let current = initial;
202
+ let desired = initial;
203
+ let pending = null;
204
+ let disposed = false;
205
+ let broken = false;
206
+ let timer;
207
+ const report = (error) => {
208
+ if (disposed) return;
209
+ broken = true;
210
+ applied(null);
211
+ clearTimeout(timer);
212
+ failed(error instanceof Error ? error : new Error("Payment options could not be updated."));
213
+ };
214
+ const drain = () => {
215
+ if (disposed || broken || pending !== null || current === desired) return;
216
+ pending = desired;
217
+ applied(null);
218
+ timer = setTimeout(() => report(new Error("Payment options could not be updated. Please refresh checkout.")), 15e3);
219
+ try {
220
+ const result = elements.update({ amount: pending });
221
+ if (result && typeof result.catch === "function") {
222
+ void result.catch(report);
223
+ }
224
+ } catch (error) {
225
+ report(error);
226
+ }
227
+ };
228
+ const complete = () => {
229
+ if (disposed || broken || pending === null) return;
230
+ clearTimeout(timer);
231
+ current = pending;
232
+ pending = null;
233
+ applied(current);
234
+ drain();
235
+ };
236
+ elements.on("update-end", complete);
237
+ return {
238
+ set(amount) {
239
+ desired = amount;
240
+ drain();
241
+ },
242
+ isApplied(amount) {
243
+ return !disposed && !broken && pending === null && current === amount;
244
+ },
245
+ // Elements has no public off(): ignore late events until its owning Elements is destroyed.
246
+ dispose() {
247
+ disposed = true;
248
+ clearTimeout(timer);
249
+ }
250
+ };
251
+ }
152
252
 
153
253
  // src/prism/outcome.ts
154
254
  function normalizeStripePaymentIntent(paymentIntent) {
@@ -277,9 +377,30 @@ async function dispatchOutcome(outcome, callbacks) {
277
377
  function DeferredPaymentElementForm(props) {
278
378
  const stripe = (0, import_react_stripe_js.useStripe)();
279
379
  const elements = (0, import_react_stripe_js.useElements)();
280
- const [ready, setReady] = (0, import_react2.useState)(false);
281
- const [submitting, setSubmitting] = (0, import_react2.useState)(false);
282
- const [message, setMessage] = (0, import_react2.useState)(null);
380
+ const [ready, setReady] = (0, import_react3.useState)(false);
381
+ const [submitting, setSubmitting] = (0, import_react3.useState)(false);
382
+ const [message, setMessage] = (0, import_react3.useState)(null);
383
+ const [appliedAmount, setAppliedAmount] = (0, import_react3.useState)(props.initialAmount ?? props.amountMinor);
384
+ const amountUpdater = (0, import_react3.useRef)(null);
385
+ const latest = (0, import_react3.useRef)(props);
386
+ latest.current = props;
387
+ const submittingRef = (0, import_react3.useRef)(false);
388
+ const amountReady = !props.retainOnAmountChange || appliedAmount === props.amountMinor;
389
+ (0, import_react3.useEffect)(() => {
390
+ if (!elements || !props.retainOnAmountChange) return;
391
+ const updater = stripeAmountUpdater(elements, props.initialAmount ?? props.amountMinor, setAppliedAmount, (error) => {
392
+ latest.current.onError(error, hostErrorContext("details"));
393
+ });
394
+ amountUpdater.current = updater;
395
+ updater.set(latest.current.amountMinor);
396
+ return () => {
397
+ updater.dispose();
398
+ amountUpdater.current = null;
399
+ };
400
+ }, [elements, props.retainOnAmountChange, props.initialAmount]);
401
+ (0, import_react3.useEffect)(() => {
402
+ amountUpdater.current?.set(props.amountMinor);
403
+ }, [props.amountMinor]);
283
404
  const callbacks = {
284
405
  onAuthorized: props.onAuthorized,
285
406
  onPending: props.onPending,
@@ -293,7 +414,9 @@ function DeferredPaymentElementForm(props) {
293
414
  }
294
415
  async function submit(event) {
295
416
  event.preventDefault();
296
- if (!stripe || !elements || submitting || props.disabled) return;
417
+ if (!stripe || !elements || !ready || !amountReady || submittingRef.current || props.disabled) return;
418
+ if (props.retainOnAmountChange && !amountUpdater.current?.isApplied(latest.current.amountMinor)) return;
419
+ submittingRef.current = true;
297
420
  setSubmitting(true);
298
421
  props.onSubmittingChange?.(true);
299
422
  setMessage(null);
@@ -345,6 +468,7 @@ function DeferredPaymentElementForm(props) {
345
468
  const safeError3 = error instanceof Error ? error : new Error("Payment could not be completed.");
346
469
  reportError(safeError3, hostErrorContext(phase));
347
470
  } finally {
471
+ submittingRef.current = false;
348
472
  setSubmitting(false);
349
473
  props.onSubmittingChange?.(false);
350
474
  }
@@ -380,7 +504,7 @@ function DeferredPaymentElementForm(props) {
380
504
  "button",
381
505
  {
382
506
  type: "submit",
383
- disabled: !stripe || !elements || !ready || submitting || props.disabled,
507
+ disabled: !stripe || !elements || !ready || !amountReady || submitting || props.disabled,
384
508
  className: props.classNames?.submitButton,
385
509
  children: submitting ? "Processing payment..." : props.submitLabel ?? "Pay now"
386
510
  }
@@ -410,7 +534,7 @@ function deferredStripeElementsContract(props) {
410
534
  const excludedPaymentMethodTypes = props.context.excludedPaymentMethodTypes ? [...props.context.excludedPaymentMethodTypes].sort() : void 0;
411
535
  const elementsContractKey = [
412
536
  props.context.publishableKey,
413
- props.amountMinor,
537
+ props.retainOnAmountChange ? "retained" : props.amountMinor,
414
538
  props.currency,
415
539
  captureMethod,
416
540
  props.setupFutureUsage ?? "none",
@@ -432,13 +556,18 @@ function deferredStripeElementsContract(props) {
432
556
  }
433
557
  function PrismDeferredPaymentElement(props) {
434
558
  const validationError = props.connector !== "stripe" || props.context.connector !== "stripe" ? "Unsupported Prism payment connector." : props.context.providerId !== "pp_hyperswitch-prism_stripe" || !/^pk_(test|live)_/.test(props.context.publishableKey) || !Number.isSafeInteger(props.amountMinor) || props.amountMinor <= 0 || !/^[a-z]{3}$/.test(props.currency) ? "Prism Stripe checkout context is invalid." : null;
435
- const stripe = (0, import_react2.useMemo)(
559
+ const stripe = (0, import_react3.useMemo)(
436
560
  () => validationError ? null : stripePromise(props.context.publishableKey),
437
561
  [props.context.publishableKey, validationError]
438
562
  );
439
563
  if (validationError) throw new Error(validationError);
440
564
  const { elementsContractKey, options } = deferredStripeElementsContract(props);
441
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_stripe_js.Elements, { stripe, options, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(DeferredPaymentElementForm, { ...props }) }, elementsContractKey);
565
+ if (!props.retainOnAmountChange) return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_stripe_js.Elements, { stripe, options, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(DeferredPaymentElementForm, { ...props }) }, elementsContractKey);
566
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(DeferredElements, { stripe, options, payment: props }, elementsContractKey);
567
+ }
568
+ function DeferredElements({ stripe, options, payment }) {
569
+ const [initial] = (0, import_react3.useState)(() => ({ options, amount: payment.amountMinor }));
570
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_stripe_js.Elements, { stripe, options: initial.options, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(DeferredPaymentElementForm, { ...payment, initialAmount: initial.amount }) });
442
571
  }
443
572
 
444
573
  // src/prism/PrismDeferredPaymentSurface.tsx
@@ -451,21 +580,27 @@ function PrismDeferredPaymentSurface(props) {
451
580
  }
452
581
 
453
582
  // src/prism/PrismPayPalPaymentButton.tsx
454
- var import_sdk_v62 = require("@paypal/react-paypal-js/sdk-v6");
455
- var import_react3 = require("react");
583
+ var import_sdk_v63 = require("@paypal/react-paypal-js/sdk-v6");
584
+ var import_react4 = require("react");
456
585
  var import_jsx_runtime4 = require("react/jsx-runtime");
457
586
  function safeError2(value) {
458
587
  return value instanceof Error ? value : new Error("PayPal could not complete payment approval.");
459
588
  }
460
589
  function PayPalButton(props) {
461
- const submittingRef = (0, import_react3.useRef)(false);
462
- const [submitting, setSubmittingState] = (0, import_react3.useState)(false);
463
- const [message, setMessage] = (0, import_react3.useState)(null);
590
+ const submittingRef = (0, import_react4.useRef)(false);
591
+ const [submitting, setSubmittingState] = (0, import_react4.useState)(false);
592
+ const [message, setMessage] = (0, import_react4.useState)(null);
464
593
  function setSubmitting(next) {
465
594
  submittingRef.current = next;
466
595
  setSubmittingState(next);
467
596
  props.onSubmittingChange?.(next);
468
597
  }
598
+ function handleError(error) {
599
+ const normalized = safeError2(error);
600
+ setSubmitting(false);
601
+ setMessage(normalized.message);
602
+ props.onError(normalized);
603
+ }
469
604
  return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
470
605
  "div",
471
606
  {
@@ -473,7 +608,7 @@ function PayPalButton(props) {
473
608
  className: props.classNames?.container,
474
609
  children: [
475
610
  /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
476
- import_sdk_v62.PayPalOneTimePaymentButton,
611
+ PayPalOneTimePaymentButton,
477
612
  {
478
613
  type: "pay",
479
614
  className: props.classNames?.button,
@@ -517,15 +652,11 @@ function PayPalButton(props) {
517
652
  orderId: props.session.orderId
518
653
  });
519
654
  },
520
- onError: (error) => {
521
- const normalized = safeError2(error);
522
- setSubmitting(false);
523
- setMessage(normalized.message);
524
- props.onError(normalized);
525
- }
655
+ onError: handleError,
656
+ onStartError: handleError
526
657
  }
527
658
  ),
528
- message ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
659
+ message && props.errorPresentation !== "external" ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
529
660
  /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
530
661
  "span",
531
662
  {
@@ -551,7 +682,7 @@ function PrismPayPalPaymentButton(props) {
551
682
  throw new Error("Prism PayPal payment session is invalid.");
552
683
  }
553
684
  return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
554
- import_sdk_v62.PayPalProvider,
685
+ import_sdk_v63.PayPalProvider,
555
686
  {
556
687
  clientId: props.session.clientId,
557
688
  environment: props.session.environment,
@@ -566,7 +697,7 @@ function PrismPayPalPaymentButton(props) {
566
697
  // src/prism/PrismPaymentElement.tsx
567
698
  var import_react_stripe_js2 = require("@stripe/react-stripe-js");
568
699
  var import_stripe_js2 = require("@stripe/stripe-js");
569
- var import_react4 = require("react");
700
+ var import_react5 = require("react");
570
701
  var import_jsx_runtime5 = require("react/jsx-runtime");
571
702
  var stripeByPublishableKey2 = /* @__PURE__ */ new Map();
572
703
  function stripePromise2(publishableKey) {
@@ -618,9 +749,9 @@ async function dispatchOutcome2(outcome, callbacks) {
618
749
  function PaymentElementForm(props) {
619
750
  const stripe = (0, import_react_stripe_js2.useStripe)();
620
751
  const elements = (0, import_react_stripe_js2.useElements)();
621
- const [ready, setReady] = (0, import_react4.useState)(false);
622
- const [submitting, setSubmitting] = (0, import_react4.useState)(false);
623
- const [message, setMessage] = (0, import_react4.useState)(null);
752
+ const [ready, setReady] = (0, import_react5.useState)(false);
753
+ const [submitting, setSubmitting] = (0, import_react5.useState)(false);
754
+ const [message, setMessage] = (0, import_react5.useState)(null);
624
755
  const callbacks = {
625
756
  onAuthorized: props.onAuthorized,
626
757
  onPending: props.onPending,
@@ -738,7 +869,7 @@ function PaymentElementForm(props) {
738
869
  }
739
870
  function PrismPaymentElement(props) {
740
871
  const validationError = props.connector !== "stripe" || props.session.connector !== "stripe" ? "Unsupported Prism payment connector." : props.session.providerId !== "pp_hyperswitch-prism_stripe" || !props.session.clientSecret.startsWith("pi_") || !props.session.clientSecret.includes("_secret_") || !/^pk_(test|live)_/.test(props.session.publishableKey) ? "Prism Stripe payment session is invalid." : null;
741
- const stripe = (0, import_react4.useMemo)(
872
+ const stripe = (0, import_react5.useMemo)(
742
873
  () => validationError ? null : stripePromise2(props.session.publishableKey),
743
874
  [props.session.publishableKey, validationError]
744
875
  );
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  PrismDeferredPaymentElement,
3
3
  PrismPaymentElement
4
- } from "./chunk-HHLAGEBO.mjs";
4
+ } from "./chunk-XNEJKU5P.mjs";
5
5
  import {
6
6
  PrismClickTimePayPalButton,
7
7
  PrismPayPalPaymentButton
8
- } from "./chunk-XCUOIQ2U.mjs";
8
+ } from "./chunk-OFMKLE5Y.mjs";
9
9
 
10
10
  // src/prism/PrismDeferredPaymentSurface.tsx
11
11
  import { jsx } from "react/jsx-runtime";
package/dist/paypal.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react from 'react';
2
- import { c as PrismClickTimePayPalButtonProps, j as PrismPayPalPaymentButtonProps } from './types-CzSrJFo2.mjs';
3
- export { d as PrismClickTimePayPalOrder, g as PrismPayPalApprovalOutcome, h as PrismPayPalCheckoutProviderContext, i as PrismPayPalPaymentButtonClassNames, q as PrismPreparedPayPalPaymentSession } from './types-CzSrJFo2.mjs';
2
+ import { c as PrismClickTimePayPalButtonProps, j as PrismPayPalPaymentButtonProps } from './types-ChWEfkib.mjs';
3
+ export { d as PrismClickTimePayPalOrder, g as PrismPayPalApprovalOutcome, h as PrismPayPalCheckoutProviderContext, i as PrismPayPalPaymentButtonClassNames, q as PrismPreparedPayPalPaymentSession } from './types-ChWEfkib.mjs';
4
4
  import '@stripe/stripe-js';
5
5
 
6
6
  declare function PrismClickTimePayPalButton(props: PrismClickTimePayPalButtonProps): react.JSX.Element;
package/dist/paypal.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react from 'react';
2
- import { c as PrismClickTimePayPalButtonProps, j as PrismPayPalPaymentButtonProps } from './types-CzSrJFo2.js';
3
- export { d as PrismClickTimePayPalOrder, g as PrismPayPalApprovalOutcome, h as PrismPayPalCheckoutProviderContext, i as PrismPayPalPaymentButtonClassNames, q as PrismPreparedPayPalPaymentSession } from './types-CzSrJFo2.js';
2
+ import { c as PrismClickTimePayPalButtonProps, j as PrismPayPalPaymentButtonProps } from './types-ChWEfkib.js';
3
+ export { d as PrismClickTimePayPalOrder, g as PrismPayPalApprovalOutcome, h as PrismPayPalCheckoutProviderContext, i as PrismPayPalPaymentButtonClassNames, q as PrismPreparedPayPalPaymentSession } from './types-ChWEfkib.js';
4
4
  import '@stripe/stripe-js';
5
5
 
6
6
  declare function PrismClickTimePayPalButton(props: PrismClickTimePayPalButtonProps): react.JSX.Element;
package/dist/paypal.js CHANGED
@@ -26,30 +26,79 @@ __export(paypal_exports, {
26
26
  module.exports = __toCommonJS(paypal_exports);
27
27
 
28
28
  // src/prism/PrismClickTimePayPalButton.tsx
29
+ var import_sdk_v62 = require("@paypal/react-paypal-js/sdk-v6");
30
+ var import_react2 = require("react");
31
+
32
+ // src/prism/PayPalOneTimePaymentButton.tsx
29
33
  var import_sdk_v6 = require("@paypal/react-paypal-js/sdk-v6");
30
34
  var import_react = require("react");
35
+ function PayPalOneTimePaymentButton({
36
+ type = "pay",
37
+ disabled = false,
38
+ onStartError,
39
+ ...hookProps
40
+ }) {
41
+ const errorNotifications = (0, import_react.useRef)(0);
42
+ const { error, isPending, handleClick } = (0, import_sdk_v6.usePayPalOneTimePaymentSession)({
43
+ ...hookProps,
44
+ onError: (error2) => {
45
+ errorNotifications.current += 1;
46
+ hookProps.onError?.(error2);
47
+ }
48
+ });
49
+ const { isHydrated } = (0, import_sdk_v6.usePayPal)();
50
+ (0, import_react.useEffect)(() => {
51
+ if (error) console.error(error);
52
+ }, [error]);
53
+ return isHydrated ? (0, import_react.createElement)("paypal-button", {
54
+ type,
55
+ disabled: disabled || isPending || error !== null ? true : void 0,
56
+ onClick: async () => {
57
+ if (disabled || isPending || error !== null) return;
58
+ const notificationsBeforeStart = errorNotifications.current;
59
+ try {
60
+ await handleClick();
61
+ } catch (error2) {
62
+ if (errorNotifications.current === notificationsBeforeStart) {
63
+ onStartError(error2);
64
+ }
65
+ }
66
+ }
67
+ }) : (0, import_react.createElement)("div", null);
68
+ }
69
+
70
+ // src/prism/PrismClickTimePayPalButton.tsx
31
71
  var import_jsx_runtime = require("react/jsx-runtime");
32
72
  function safeError(value) {
33
73
  return value instanceof Error ? value : new Error("PayPal could not complete payment approval.");
34
74
  }
35
75
  function ClickTimePayPalButton(props) {
36
- const submittingRef = (0, import_react.useRef)(false);
37
- const createdOrderIdRef = (0, import_react.useRef)(null);
38
- const [submitting, setSubmittingState] = (0, import_react.useState)(false);
39
- const [message, setMessage] = (0, import_react.useState)(null);
76
+ const submittingRef = (0, import_react2.useRef)(false);
77
+ const completingRef = (0, import_react2.useRef)(false);
78
+ const createdOrderIdRef = (0, import_react2.useRef)(null);
79
+ const [submitting, setSubmittingState] = (0, import_react2.useState)(false);
80
+ const [message, setMessage] = (0, import_react2.useState)(null);
40
81
  function setSubmitting(next) {
41
82
  submittingRef.current = next;
42
83
  setSubmittingState(next);
43
84
  props.onSubmittingChange?.(next);
44
85
  }
86
+ function handleError(error) {
87
+ const normalized = safeError(error);
88
+ setSubmitting(false);
89
+ setMessage(normalized.message);
90
+ props.onError(normalized);
91
+ }
45
92
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { "aria-busy": submitting, className: props.classNames?.container, children: [
46
93
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
47
- import_sdk_v6.PayPalOneTimePaymentButton,
94
+ PayPalOneTimePaymentButton,
48
95
  {
49
96
  type: "pay",
50
97
  className: props.classNames?.button,
51
98
  disabled: props.disabled || submitting,
52
99
  createOrder: async () => {
100
+ if (props.disabled || submittingRef.current) throw new Error("Payment is not ready. Please try again.");
101
+ if (props.lockDuringApproval) setSubmitting(true);
53
102
  setMessage(null);
54
103
  const created = await props.onCreateOrder();
55
104
  const orderId = typeof created?.orderId === "string" && created.orderId.length > 0 ? created.orderId : null;
@@ -65,16 +114,16 @@ function ClickTimePayPalButton(props) {
65
114
  commit: true,
66
115
  presentationMode: "auto",
67
116
  onApprove: async (data) => {
68
- if (submittingRef.current) return;
117
+ if (completingRef.current) return;
69
118
  if (!createdOrderIdRef.current || data.orderId !== createdOrderIdRef.current) {
70
119
  const error = new Error(
71
120
  "PayPal approved a stale payment session. Refresh checkout and try again."
72
121
  );
73
- setMessage(error.message);
74
- props.onError(error);
122
+ handleError(error);
75
123
  return;
76
124
  }
77
125
  setSubmitting(true);
126
+ completingRef.current = true;
78
127
  setMessage(null);
79
128
  try {
80
129
  await props.onProviderApproved({
@@ -88,6 +137,7 @@ function ClickTimePayPalButton(props) {
88
137
  setMessage(normalized.message);
89
138
  props.onError(normalized);
90
139
  } finally {
140
+ completingRef.current = false;
91
141
  setSubmitting(false);
92
142
  }
93
143
  },
@@ -101,15 +151,11 @@ function ClickTimePayPalButton(props) {
101
151
  orderId: createdOrderIdRef.current
102
152
  });
103
153
  },
104
- onError: (error) => {
105
- const normalized = safeError(error);
106
- setSubmitting(false);
107
- setMessage(normalized.message);
108
- props.onError(normalized);
109
- }
154
+ onError: handleError,
155
+ onStartError: handleError
110
156
  }
111
157
  ),
112
- message ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
158
+ message && props.errorPresentation !== "external" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
113
159
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
114
160
  "span",
115
161
  {
@@ -133,7 +179,7 @@ function PrismClickTimePayPalButton(props) {
133
179
  throw new Error("Prism PayPal checkout context is invalid.");
134
180
  }
135
181
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
136
- import_sdk_v6.PayPalProvider,
182
+ import_sdk_v62.PayPalProvider,
137
183
  {
138
184
  clientId: props.context.clientId,
139
185
  environment: props.context.environment,
@@ -146,21 +192,27 @@ function PrismClickTimePayPalButton(props) {
146
192
  }
147
193
 
148
194
  // src/prism/PrismPayPalPaymentButton.tsx
149
- var import_sdk_v62 = require("@paypal/react-paypal-js/sdk-v6");
150
- var import_react2 = require("react");
195
+ var import_sdk_v63 = require("@paypal/react-paypal-js/sdk-v6");
196
+ var import_react3 = require("react");
151
197
  var import_jsx_runtime2 = require("react/jsx-runtime");
152
198
  function safeError2(value) {
153
199
  return value instanceof Error ? value : new Error("PayPal could not complete payment approval.");
154
200
  }
155
201
  function PayPalButton(props) {
156
- const submittingRef = (0, import_react2.useRef)(false);
157
- const [submitting, setSubmittingState] = (0, import_react2.useState)(false);
158
- const [message, setMessage] = (0, import_react2.useState)(null);
202
+ const submittingRef = (0, import_react3.useRef)(false);
203
+ const [submitting, setSubmittingState] = (0, import_react3.useState)(false);
204
+ const [message, setMessage] = (0, import_react3.useState)(null);
159
205
  function setSubmitting(next) {
160
206
  submittingRef.current = next;
161
207
  setSubmittingState(next);
162
208
  props.onSubmittingChange?.(next);
163
209
  }
210
+ function handleError(error) {
211
+ const normalized = safeError2(error);
212
+ setSubmitting(false);
213
+ setMessage(normalized.message);
214
+ props.onError(normalized);
215
+ }
164
216
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
165
217
  "div",
166
218
  {
@@ -168,7 +220,7 @@ function PayPalButton(props) {
168
220
  className: props.classNames?.container,
169
221
  children: [
170
222
  /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
171
- import_sdk_v62.PayPalOneTimePaymentButton,
223
+ PayPalOneTimePaymentButton,
172
224
  {
173
225
  type: "pay",
174
226
  className: props.classNames?.button,
@@ -212,15 +264,11 @@ function PayPalButton(props) {
212
264
  orderId: props.session.orderId
213
265
  });
214
266
  },
215
- onError: (error) => {
216
- const normalized = safeError2(error);
217
- setSubmitting(false);
218
- setMessage(normalized.message);
219
- props.onError(normalized);
220
- }
267
+ onError: handleError,
268
+ onStartError: handleError
221
269
  }
222
270
  ),
223
- message ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
271
+ message && props.errorPresentation !== "external" ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
224
272
  /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
225
273
  "span",
226
274
  {
@@ -246,7 +294,7 @@ function PrismPayPalPaymentButton(props) {
246
294
  throw new Error("Prism PayPal payment session is invalid.");
247
295
  }
248
296
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
249
- import_sdk_v62.PayPalProvider,
297
+ import_sdk_v63.PayPalProvider,
250
298
  {
251
299
  clientId: props.session.clientId,
252
300
  environment: props.session.environment,
package/dist/paypal.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  PrismClickTimePayPalButton,
3
3
  PrismPayPalPaymentButton
4
- } from "./chunk-XCUOIQ2U.mjs";
4
+ } from "./chunk-OFMKLE5Y.mjs";
5
5
  export {
6
6
  PrismClickTimePayPalButton,
7
7
  PrismPayPalPaymentButton
@@ -97,6 +97,7 @@ interface PrismPayPalPaymentButtonProps {
97
97
  connector: "paypal";
98
98
  intent: "checkout";
99
99
  classNames?: PrismPayPalPaymentButtonClassNames;
100
+ errorPresentation?: "inline" | "external";
100
101
  disabled?: boolean;
101
102
  onSubmittingChange?: (submitting: boolean) => void;
102
103
  onProviderApproved: (outcome: PrismPayPalApprovalOutcome) => void | Promise<void>;
@@ -126,6 +127,8 @@ interface PrismDeferredStripePreparedPayment {
126
127
  clientSecret: string;
127
128
  }
128
129
  interface PrismDeferredPaymentElementProps {
130
+ /** Opt in to retaining card fields across amount-only changes. Other checkout terms still remount. */
131
+ retainOnAmountChange?: boolean;
129
132
  context: PrismStripeCheckoutProviderContext;
130
133
  connector: "stripe";
131
134
  intent: "checkout";
@@ -152,11 +155,14 @@ interface PrismClickTimePayPalOrder {
152
155
  orderId: string;
153
156
  }
154
157
  interface PrismClickTimePayPalButtonProps {
158
+ /** Keep the host locked from order creation through the buyer's approval or cancellation. */
159
+ lockDuringApproval?: boolean;
155
160
  context: PrismPayPalCheckoutProviderContext;
156
161
  connector: "paypal";
157
162
  intent: "checkout";
158
163
  onCreateOrder: () => Promise<PrismClickTimePayPalOrder>;
159
164
  classNames?: PrismPayPalPaymentButtonClassNames;
165
+ errorPresentation?: "inline" | "external";
160
166
  disabled?: boolean;
161
167
  onSubmittingChange?: (submitting: boolean) => void;
162
168
  onProviderApproved: (outcome: PrismPayPalApprovalOutcome) => void | Promise<void>;
@@ -97,6 +97,7 @@ interface PrismPayPalPaymentButtonProps {
97
97
  connector: "paypal";
98
98
  intent: "checkout";
99
99
  classNames?: PrismPayPalPaymentButtonClassNames;
100
+ errorPresentation?: "inline" | "external";
100
101
  disabled?: boolean;
101
102
  onSubmittingChange?: (submitting: boolean) => void;
102
103
  onProviderApproved: (outcome: PrismPayPalApprovalOutcome) => void | Promise<void>;
@@ -126,6 +127,8 @@ interface PrismDeferredStripePreparedPayment {
126
127
  clientSecret: string;
127
128
  }
128
129
  interface PrismDeferredPaymentElementProps {
130
+ /** Opt in to retaining card fields across amount-only changes. Other checkout terms still remount. */
131
+ retainOnAmountChange?: boolean;
129
132
  context: PrismStripeCheckoutProviderContext;
130
133
  connector: "stripe";
131
134
  intent: "checkout";
@@ -152,11 +155,14 @@ interface PrismClickTimePayPalOrder {
152
155
  orderId: string;
153
156
  }
154
157
  interface PrismClickTimePayPalButtonProps {
158
+ /** Keep the host locked from order creation through the buyer's approval or cancellation. */
159
+ lockDuringApproval?: boolean;
155
160
  context: PrismPayPalCheckoutProviderContext;
156
161
  connector: "paypal";
157
162
  intent: "checkout";
158
163
  onCreateOrder: () => Promise<PrismClickTimePayPalOrder>;
159
164
  classNames?: PrismPayPalPaymentButtonClassNames;
165
+ errorPresentation?: "inline" | "external";
160
166
  disabled?: boolean;
161
167
  onSubmittingChange?: (submitting: boolean) => void;
162
168
  onProviderApproved: (outcome: PrismPayPalApprovalOutcome) => void | Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alphawebai/medusa-custom-payments-react",
3
- "version": "0.1.0-rc.49",
3
+ "version": "0.1.0-rc.51",
4
4
  "description": "AlphaWeb-owned Prism Stripe and PayPal checkout surfaces for Medusa storefronts",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",