@alphawebai/medusa-custom-payments-react 0.1.0-rc.50 → 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.
@@ -50,6 +50,7 @@ function safeError(value) {
50
50
  }
51
51
  function ClickTimePayPalButton(props) {
52
52
  const submittingRef = useRef2(false);
53
+ const completingRef = useRef2(false);
53
54
  const createdOrderIdRef = useRef2(null);
54
55
  const [submitting, setSubmittingState] = useState(false);
55
56
  const [message, setMessage] = useState(null);
@@ -72,6 +73,8 @@ function ClickTimePayPalButton(props) {
72
73
  className: props.classNames?.button,
73
74
  disabled: props.disabled || submitting,
74
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);
75
78
  setMessage(null);
76
79
  const created = await props.onCreateOrder();
77
80
  const orderId = typeof created?.orderId === "string" && created.orderId.length > 0 ? created.orderId : null;
@@ -87,16 +90,16 @@ function ClickTimePayPalButton(props) {
87
90
  commit: true,
88
91
  presentationMode: "auto",
89
92
  onApprove: async (data) => {
90
- if (submittingRef.current) return;
93
+ if (completingRef.current) return;
91
94
  if (!createdOrderIdRef.current || data.orderId !== createdOrderIdRef.current) {
92
95
  const error = new Error(
93
96
  "PayPal approved a stale payment session. Refresh checkout and try again."
94
97
  );
95
- setMessage(error.message);
96
- props.onError(error);
98
+ handleError(error);
97
99
  return;
98
100
  }
99
101
  setSubmitting(true);
102
+ completingRef.current = true;
100
103
  setMessage(null);
101
104
  try {
102
105
  await props.onProviderApproved({
@@ -110,6 +113,7 @@ function ClickTimePayPalButton(props) {
110
113
  setMessage(normalized.message);
111
114
  props.onError(normalized);
112
115
  } finally {
116
+ completingRef.current = false;
113
117
  setSubmitting(false);
114
118
  }
115
119
  },
@@ -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-Dufdvsa0.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-Dufdvsa0.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-Dufdvsa0.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-Dufdvsa0.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-Dufdvsa0.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-Dufdvsa0.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-Dufdvsa0.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-Dufdvsa0.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
  /**
@@ -74,6 +74,7 @@ function safeError(value) {
74
74
  }
75
75
  function ClickTimePayPalButton(props) {
76
76
  const submittingRef = (0, import_react2.useRef)(false);
77
+ const completingRef = (0, import_react2.useRef)(false);
77
78
  const createdOrderIdRef = (0, import_react2.useRef)(null);
78
79
  const [submitting, setSubmittingState] = (0, import_react2.useState)(false);
79
80
  const [message, setMessage] = (0, import_react2.useState)(null);
@@ -96,6 +97,8 @@ function ClickTimePayPalButton(props) {
96
97
  className: props.classNames?.button,
97
98
  disabled: props.disabled || submitting,
98
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);
99
102
  setMessage(null);
100
103
  const created = await props.onCreateOrder();
101
104
  const orderId = typeof created?.orderId === "string" && created.orderId.length > 0 ? created.orderId : null;
@@ -111,16 +114,16 @@ function ClickTimePayPalButton(props) {
111
114
  commit: true,
112
115
  presentationMode: "auto",
113
116
  onApprove: async (data) => {
114
- if (submittingRef.current) return;
117
+ if (completingRef.current) return;
115
118
  if (!createdOrderIdRef.current || data.orderId !== createdOrderIdRef.current) {
116
119
  const error = new Error(
117
120
  "PayPal approved a stale payment session. Refresh checkout and try again."
118
121
  );
119
- setMessage(error.message);
120
- props.onError(error);
122
+ handleError(error);
121
123
  return;
122
124
  }
123
125
  setSubmitting(true);
126
+ completingRef.current = true;
124
127
  setMessage(null);
125
128
  try {
126
129
  await props.onProviderApproved({
@@ -134,6 +137,7 @@ function ClickTimePayPalButton(props) {
134
137
  setMessage(normalized.message);
135
138
  props.onError(normalized);
136
139
  } finally {
140
+ completingRef.current = false;
137
141
  setSubmitting(false);
138
142
  }
139
143
  },
@@ -192,6 +196,60 @@ var import_react_stripe_js = require("@stripe/react-stripe-js");
192
196
  var import_stripe_js = require("@stripe/stripe-js");
193
197
  var import_react3 = require("react");
194
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
+ }
252
+
195
253
  // src/prism/outcome.ts
196
254
  function normalizeStripePaymentIntent(paymentIntent) {
197
255
  switch (paymentIntent.status) {
@@ -322,6 +380,27 @@ function DeferredPaymentElementForm(props) {
322
380
  const [ready, setReady] = (0, import_react3.useState)(false);
323
381
  const [submitting, setSubmitting] = (0, import_react3.useState)(false);
324
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]);
325
404
  const callbacks = {
326
405
  onAuthorized: props.onAuthorized,
327
406
  onPending: props.onPending,
@@ -335,7 +414,9 @@ function DeferredPaymentElementForm(props) {
335
414
  }
336
415
  async function submit(event) {
337
416
  event.preventDefault();
338
- 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;
339
420
  setSubmitting(true);
340
421
  props.onSubmittingChange?.(true);
341
422
  setMessage(null);
@@ -387,6 +468,7 @@ function DeferredPaymentElementForm(props) {
387
468
  const safeError3 = error instanceof Error ? error : new Error("Payment could not be completed.");
388
469
  reportError(safeError3, hostErrorContext(phase));
389
470
  } finally {
471
+ submittingRef.current = false;
390
472
  setSubmitting(false);
391
473
  props.onSubmittingChange?.(false);
392
474
  }
@@ -422,7 +504,7 @@ function DeferredPaymentElementForm(props) {
422
504
  "button",
423
505
  {
424
506
  type: "submit",
425
- disabled: !stripe || !elements || !ready || submitting || props.disabled,
507
+ disabled: !stripe || !elements || !ready || !amountReady || submitting || props.disabled,
426
508
  className: props.classNames?.submitButton,
427
509
  children: submitting ? "Processing payment..." : props.submitLabel ?? "Pay now"
428
510
  }
@@ -452,7 +534,7 @@ function deferredStripeElementsContract(props) {
452
534
  const excludedPaymentMethodTypes = props.context.excludedPaymentMethodTypes ? [...props.context.excludedPaymentMethodTypes].sort() : void 0;
453
535
  const elementsContractKey = [
454
536
  props.context.publishableKey,
455
- props.amountMinor,
537
+ props.retainOnAmountChange ? "retained" : props.amountMinor,
456
538
  props.currency,
457
539
  captureMethod,
458
540
  props.setupFutureUsage ?? "none",
@@ -480,7 +562,12 @@ function PrismDeferredPaymentElement(props) {
480
562
  );
481
563
  if (validationError) throw new Error(validationError);
482
564
  const { elementsContractKey, options } = deferredStripeElementsContract(props);
483
- 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 }) });
484
571
  }
485
572
 
486
573
  // src/prism/PrismDeferredPaymentSurface.tsx
@@ -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-IXQEWGRG.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-Dufdvsa0.mjs';
3
- export { d as PrismClickTimePayPalOrder, g as PrismPayPalApprovalOutcome, h as PrismPayPalCheckoutProviderContext, i as PrismPayPalPaymentButtonClassNames, q as PrismPreparedPayPalPaymentSession } from './types-Dufdvsa0.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-Dufdvsa0.js';
3
- export { d as PrismClickTimePayPalOrder, g as PrismPayPalApprovalOutcome, h as PrismPayPalCheckoutProviderContext, i as PrismPayPalPaymentButtonClassNames, q as PrismPreparedPayPalPaymentSession } from './types-Dufdvsa0.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
@@ -74,6 +74,7 @@ function safeError(value) {
74
74
  }
75
75
  function ClickTimePayPalButton(props) {
76
76
  const submittingRef = (0, import_react2.useRef)(false);
77
+ const completingRef = (0, import_react2.useRef)(false);
77
78
  const createdOrderIdRef = (0, import_react2.useRef)(null);
78
79
  const [submitting, setSubmittingState] = (0, import_react2.useState)(false);
79
80
  const [message, setMessage] = (0, import_react2.useState)(null);
@@ -96,6 +97,8 @@ function ClickTimePayPalButton(props) {
96
97
  className: props.classNames?.button,
97
98
  disabled: props.disabled || submitting,
98
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);
99
102
  setMessage(null);
100
103
  const created = await props.onCreateOrder();
101
104
  const orderId = typeof created?.orderId === "string" && created.orderId.length > 0 ? created.orderId : null;
@@ -111,16 +114,16 @@ function ClickTimePayPalButton(props) {
111
114
  commit: true,
112
115
  presentationMode: "auto",
113
116
  onApprove: async (data) => {
114
- if (submittingRef.current) return;
117
+ if (completingRef.current) return;
115
118
  if (!createdOrderIdRef.current || data.orderId !== createdOrderIdRef.current) {
116
119
  const error = new Error(
117
120
  "PayPal approved a stale payment session. Refresh checkout and try again."
118
121
  );
119
- setMessage(error.message);
120
- props.onError(error);
122
+ handleError(error);
121
123
  return;
122
124
  }
123
125
  setSubmitting(true);
126
+ completingRef.current = true;
124
127
  setMessage(null);
125
128
  try {
126
129
  await props.onProviderApproved({
@@ -134,6 +137,7 @@ function ClickTimePayPalButton(props) {
134
137
  setMessage(normalized.message);
135
138
  props.onError(normalized);
136
139
  } finally {
140
+ completingRef.current = false;
137
141
  setSubmitting(false);
138
142
  }
139
143
  },
package/dist/paypal.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  PrismClickTimePayPalButton,
3
3
  PrismPayPalPaymentButton
4
- } from "./chunk-IXQEWGRG.mjs";
4
+ } from "./chunk-OFMKLE5Y.mjs";
5
5
  export {
6
6
  PrismClickTimePayPalButton,
7
7
  PrismPayPalPaymentButton
@@ -127,6 +127,8 @@ interface PrismDeferredStripePreparedPayment {
127
127
  clientSecret: string;
128
128
  }
129
129
  interface PrismDeferredPaymentElementProps {
130
+ /** Opt in to retaining card fields across amount-only changes. Other checkout terms still remount. */
131
+ retainOnAmountChange?: boolean;
130
132
  context: PrismStripeCheckoutProviderContext;
131
133
  connector: "stripe";
132
134
  intent: "checkout";
@@ -153,6 +155,8 @@ interface PrismClickTimePayPalOrder {
153
155
  orderId: string;
154
156
  }
155
157
  interface PrismClickTimePayPalButtonProps {
158
+ /** Keep the host locked from order creation through the buyer's approval or cancellation. */
159
+ lockDuringApproval?: boolean;
156
160
  context: PrismPayPalCheckoutProviderContext;
157
161
  connector: "paypal";
158
162
  intent: "checkout";
@@ -127,6 +127,8 @@ interface PrismDeferredStripePreparedPayment {
127
127
  clientSecret: string;
128
128
  }
129
129
  interface PrismDeferredPaymentElementProps {
130
+ /** Opt in to retaining card fields across amount-only changes. Other checkout terms still remount. */
131
+ retainOnAmountChange?: boolean;
130
132
  context: PrismStripeCheckoutProviderContext;
131
133
  connector: "stripe";
132
134
  intent: "checkout";
@@ -153,6 +155,8 @@ interface PrismClickTimePayPalOrder {
153
155
  orderId: string;
154
156
  }
155
157
  interface PrismClickTimePayPalButtonProps {
158
+ /** Keep the host locked from order creation through the buyer's approval or cancellation. */
159
+ lockDuringApproval?: boolean;
156
160
  context: PrismPayPalCheckoutProviderContext;
157
161
  connector: "paypal";
158
162
  intent: "checkout";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alphawebai/medusa-custom-payments-react",
3
- "version": "0.1.0-rc.50",
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",