@alphawebai/medusa-custom-payments-react 0.1.0-rc.53 → 0.1.0-rc.55

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,6 +1,10 @@
1
1
  // src/prism/PrismClickTimePayPalButton.tsx
2
2
  import { PayPalProvider } from "@paypal/react-paypal-js/sdk-v6";
3
- import { useRef as useRef2, useState } from "react";
3
+ import { useRef as useRef3, useState } from "react";
4
+
5
+ // src/prism/PayPalCheckoutButton.tsx
6
+ import { usePayPal as usePayPal2, usePayPalSavePaymentSession } from "@paypal/react-paypal-js/sdk-v6";
7
+ import { createElement as createElement2, useRef as useRef2 } from "react";
4
8
 
5
9
  // src/prism/PayPalOneTimePaymentButton.tsx
6
10
  import {
@@ -43,15 +47,50 @@ function PayPalOneTimePaymentButton({
43
47
  }) : createElement("div", null);
44
48
  }
45
49
 
50
+ // src/prism/PayPalCheckoutButton.tsx
51
+ import { jsx } from "react/jsx-runtime";
52
+ function PayPalCheckoutButton({ setupOnly, ...props }) {
53
+ return setupOnly ? /* @__PURE__ */ jsx(SavePaymentButton, { ...props }) : /* @__PURE__ */ jsx(PayPalOneTimePaymentButton, { ...props });
54
+ }
55
+ function SavePaymentButton(props) {
56
+ const errorNotifications = useRef2(0);
57
+ const session = usePayPalSavePaymentSession({
58
+ presentationMode: props.presentationMode,
59
+ ...props.createOrder ? { createVaultToken: async () => ({ vaultSetupToken: (await props.createOrder()).orderId }) } : { vaultSetupToken: props.orderId },
60
+ onApprove: (data) => props.onApprove?.({ orderId: data.vaultSetupToken }),
61
+ onCancel: (data) => props.onCancel?.({ orderId: data.vaultSetupToken }),
62
+ onError: (error) => {
63
+ errorNotifications.current += 1;
64
+ props.onError?.(error);
65
+ }
66
+ });
67
+ const { isHydrated } = usePayPal2();
68
+ if (!isHydrated) return createElement2("div", null);
69
+ return createElement2("paypal-button", {
70
+ type: "subscribe",
71
+ className: props.className,
72
+ disabled: props.disabled || session.isPending || session.error !== null ? true : void 0,
73
+ onClick: async () => {
74
+ if (props.disabled || session.isPending || session.error !== null) return;
75
+ const previous = errorNotifications.current;
76
+ try {
77
+ await session.handleClick();
78
+ } catch (error) {
79
+ if (errorNotifications.current === previous) props.onStartError(error);
80
+ }
81
+ }
82
+ });
83
+ }
84
+
46
85
  // src/prism/PrismClickTimePayPalButton.tsx
47
- import { jsx, jsxs } from "react/jsx-runtime";
86
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
48
87
  function safeError(value) {
49
88
  return value instanceof Error ? value : new Error("PayPal could not complete payment approval.");
50
89
  }
51
90
  function ClickTimePayPalButton(props) {
52
- const submittingRef = useRef2(false);
53
- const completingRef = useRef2(false);
54
- const createdOrderIdRef = useRef2(null);
91
+ const submittingRef = useRef3(false);
92
+ const completingRef = useRef3(false);
93
+ const createdOrderIdRef = useRef3(null);
55
94
  const [submitting, setSubmittingState] = useState(false);
56
95
  const [message, setMessage] = useState(null);
57
96
  function setSubmitting(next) {
@@ -66,9 +105,10 @@ function ClickTimePayPalButton(props) {
66
105
  props.onError(normalized);
67
106
  }
68
107
  return /* @__PURE__ */ jsxs("div", { "aria-busy": submitting, className: props.classNames?.container, children: [
69
- /* @__PURE__ */ jsx(
70
- PayPalOneTimePaymentButton,
108
+ /* @__PURE__ */ jsx2(
109
+ PayPalCheckoutButton,
71
110
  {
111
+ setupOnly: props.setupOnly,
72
112
  type: "pay",
73
113
  className: props.classNames?.button,
74
114
  disabled: props.disabled || submitting,
@@ -77,6 +117,7 @@ function ClickTimePayPalButton(props) {
77
117
  if (props.lockDuringApproval) setSubmitting(true);
78
118
  setMessage(null);
79
119
  const created = await props.onCreateOrder();
120
+ if (Boolean(created.setupOnly) !== Boolean(props.setupOnly)) throw new Error("Your checkout total changed. Please review it before continuing.");
80
121
  const orderId = typeof created?.orderId === "string" && created.orderId.length > 0 ? created.orderId : null;
81
122
  if (!orderId) {
82
123
  throw new Error(
@@ -131,8 +172,8 @@ function ClickTimePayPalButton(props) {
131
172
  onStartError: handleError
132
173
  }
133
174
  ),
134
- message && props.errorPresentation !== "external" ? /* @__PURE__ */ jsx("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
135
- /* @__PURE__ */ jsx(
175
+ message && props.errorPresentation !== "external" ? /* @__PURE__ */ jsx2("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
176
+ /* @__PURE__ */ jsx2(
136
177
  "span",
137
178
  {
138
179
  role: "status",
@@ -154,14 +195,14 @@ function PrismClickTimePayPalButton(props) {
154
195
  if (props.connector !== "paypal" || props.context.connector !== "paypal" || props.context.providerId !== "pp_hyperswitch-prism_paypal" || props.context.clientId.length === 0) {
155
196
  throw new Error("Prism PayPal checkout context is invalid.");
156
197
  }
157
- return /* @__PURE__ */ jsx(
198
+ return /* @__PURE__ */ jsx2(
158
199
  PayPalProvider,
159
200
  {
160
201
  clientId: props.context.clientId,
161
202
  environment: props.context.environment,
162
203
  components: ["paypal-payments"],
163
204
  pageType: "checkout",
164
- children: /* @__PURE__ */ jsx(ClickTimePayPalButton, { ...props })
205
+ children: /* @__PURE__ */ jsx2(ClickTimePayPalButton, { ...props })
165
206
  },
166
207
  `${props.context.environment}:${props.context.clientId}`
167
208
  );
@@ -169,13 +210,13 @@ function PrismClickTimePayPalButton(props) {
169
210
 
170
211
  // src/prism/PrismPayPalPaymentButton.tsx
171
212
  import { PayPalProvider as PayPalProvider2 } from "@paypal/react-paypal-js/sdk-v6";
172
- import { useRef as useRef3, useState as useState2 } from "react";
173
- import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
213
+ import { useRef as useRef4, useState as useState2 } from "react";
214
+ import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
174
215
  function safeError2(value) {
175
216
  return value instanceof Error ? value : new Error("PayPal could not complete payment approval.");
176
217
  }
177
218
  function PayPalButton(props) {
178
- const submittingRef = useRef3(false);
219
+ const submittingRef = useRef4(false);
179
220
  const [submitting, setSubmittingState] = useState2(false);
180
221
  const [message, setMessage] = useState2(null);
181
222
  function setSubmitting(next) {
@@ -195,9 +236,10 @@ function PayPalButton(props) {
195
236
  "aria-busy": submitting,
196
237
  className: props.classNames?.container,
197
238
  children: [
198
- /* @__PURE__ */ jsx2(
199
- PayPalOneTimePaymentButton,
239
+ /* @__PURE__ */ jsx3(
240
+ PayPalCheckoutButton,
200
241
  {
242
+ setupOnly: props.session.setupOnly,
201
243
  type: "pay",
202
244
  className: props.classNames?.button,
203
245
  disabled: props.disabled || submitting,
@@ -244,8 +286,8 @@ function PayPalButton(props) {
244
286
  onStartError: handleError
245
287
  }
246
288
  ),
247
- message && props.errorPresentation !== "external" ? /* @__PURE__ */ jsx2("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
248
- /* @__PURE__ */ jsx2(
289
+ message && props.errorPresentation !== "external" ? /* @__PURE__ */ jsx3("p", { role: "alert", "aria-live": "assertive", className: props.classNames?.error, children: message }) : null,
290
+ /* @__PURE__ */ jsx3(
249
291
  "span",
250
292
  {
251
293
  role: "status",
@@ -269,14 +311,14 @@ function PrismPayPalPaymentButton(props) {
269
311
  if (props.connector !== "paypal" || props.session.connector !== "paypal" || props.session.providerId !== "pp_hyperswitch-prism_paypal" || props.session.orderId.length === 0 || props.session.clientId.length === 0) {
270
312
  throw new Error("Prism PayPal payment session is invalid.");
271
313
  }
272
- return /* @__PURE__ */ jsx2(
314
+ return /* @__PURE__ */ jsx3(
273
315
  PayPalProvider2,
274
316
  {
275
317
  clientId: props.session.clientId,
276
318
  environment: props.session.environment,
277
319
  components: ["paypal-payments"],
278
320
  pageType: "checkout",
279
- children: /* @__PURE__ */ jsx2(PayPalButton, { ...props }, props.session.orderId)
321
+ children: /* @__PURE__ */ jsx3(PayPalButton, { ...props }, props.session.orderId)
280
322
  },
281
323
  `${props.session.environment}:${props.session.clientId}`
282
324
  );
@@ -65,6 +65,14 @@ function stripeAmountUpdater(elements, initial, applied, failed) {
65
65
  }
66
66
 
67
67
  // src/prism/outcome.ts
68
+ function normalizeStripeSetupIntent(intent) {
69
+ if (intent.status === "succeeded") return { connector: "stripe", paymentIntentId: intent.id, status: "setup_completed" };
70
+ if (intent.status === "processing") return { connector: "stripe", paymentIntentId: intent.id, status: "pending_authorization" };
71
+ if (intent.status === "requires_action" || intent.status === "requires_confirmation") {
72
+ return { connector: "stripe", paymentIntentId: intent.id, status: "requires_customer_action" };
73
+ }
74
+ throw new Error(intent.status === "canceled" ? "Saving your payment method was canceled." : "Your payment method could not be saved. Please try another method.");
75
+ }
68
76
  function normalizeStripePaymentIntent(paymentIntent) {
69
77
  switch (paymentIntent.status) {
70
78
  case "requires_capture":
@@ -169,8 +177,8 @@ function paymentElementOptions(displayPolicy) {
169
177
  ...policy.paymentMethodOrder ? { paymentMethodOrder: policy.paymentMethodOrder } : {}
170
178
  };
171
179
  }
172
- function assertPreparedClientSecret(clientSecret) {
173
- if (typeof clientSecret !== "string" || !clientSecret.startsWith("pi_") || !clientSecret.includes("_secret_")) {
180
+ function assertPreparedClientSecret(clientSecret, setupOnly) {
181
+ if (typeof clientSecret !== "string" || !clientSecret.startsWith(setupOnly ? "seti_" : "pi_") || !clientSecret.includes("_secret_")) {
174
182
  throw new Error("Payment preparation did not return a valid Stripe session.");
175
183
  }
176
184
  return clientSecret;
@@ -179,6 +187,7 @@ async function dispatchOutcome(outcome, callbacks) {
179
187
  switch (outcome.status) {
180
188
  case "authorized":
181
189
  case "captured":
190
+ case "setup_completed":
182
191
  await callbacks.onAuthorized(outcome);
183
192
  return;
184
193
  case "pending_authorization":
@@ -199,9 +208,9 @@ function DeferredPaymentElementForm(props) {
199
208
  const latest = useRef(props);
200
209
  latest.current = props;
201
210
  const submittingRef = useRef(false);
202
- const amountReady = !props.retainOnAmountChange || appliedAmount === props.amountMinor;
211
+ const amountReady = props.setupOnly || !props.retainOnAmountChange || appliedAmount === props.amountMinor;
203
212
  useEffect(() => {
204
- if (!elements || !props.retainOnAmountChange) return;
213
+ if (!elements || !props.retainOnAmountChange || props.setupOnly) return;
205
214
  const updater = stripeAmountUpdater(elements, props.initialAmount ?? props.amountMinor, setAppliedAmount, (error) => {
206
215
  latest.current.onError(error, hostErrorContext("details"));
207
216
  });
@@ -211,7 +220,7 @@ function DeferredPaymentElementForm(props) {
211
220
  updater.dispose();
212
221
  amountUpdater.current = null;
213
222
  };
214
- }, [elements, props.retainOnAmountChange, props.initialAmount]);
223
+ }, [elements, props.retainOnAmountChange, props.initialAmount, props.setupOnly]);
215
224
  useEffect(() => {
216
225
  amountUpdater.current?.set(props.amountMinor);
217
226
  }, [props.amountMinor]);
@@ -229,7 +238,7 @@ function DeferredPaymentElementForm(props) {
229
238
  async function submit(event) {
230
239
  event.preventDefault();
231
240
  if (!stripe || !elements || !ready || !amountReady || submittingRef.current || props.disabled) return;
232
- if (props.retainOnAmountChange && !amountUpdater.current?.isApplied(latest.current.amountMinor)) return;
241
+ if (!props.setupOnly && props.retainOnAmountChange && !amountUpdater.current?.isApplied(latest.current.amountMinor)) return;
233
242
  submittingRef.current = true;
234
243
  setSubmitting(true);
235
244
  props.onSubmittingChange?.(true);
@@ -248,9 +257,10 @@ function DeferredPaymentElementForm(props) {
248
257
  }
249
258
  phase = "prepare";
250
259
  const prepared = await props.onPrepare();
251
- const clientSecret = assertPreparedClientSecret(prepared?.clientSecret);
260
+ if (Boolean(prepared.setupOnly) !== Boolean(props.setupOnly)) throw new Error("Your checkout total changed. Please review it before continuing.");
261
+ const clientSecret = assertPreparedClientSecret(prepared?.clientSecret, Boolean(props.setupOnly));
252
262
  phase = "confirm";
253
- const result = await stripe.confirmPayment({
263
+ const confirmation = {
254
264
  elements,
255
265
  clientSecret,
256
266
  confirmParams: {
@@ -262,7 +272,19 @@ function DeferredPaymentElementForm(props) {
262
272
  } : {}
263
273
  },
264
274
  redirect: "if_required"
265
- });
275
+ };
276
+ if (props.setupOnly) {
277
+ const result2 = await stripe.confirmSetup(confirmation);
278
+ if (result2.error) {
279
+ reportError(new Error(result2.error.message ?? "Your payment method could not be saved."), stripeErrorContext("confirm", result2.error));
280
+ return;
281
+ }
282
+ if (!result2.setupIntent) throw new Error("Stripe did not return a setup result.");
283
+ phase = "outcome";
284
+ await dispatchOutcome(normalizeStripeSetupIntent(result2.setupIntent), callbacks);
285
+ return;
286
+ }
287
+ const result = await stripe.confirmPayment(confirmation);
266
288
  if (result.error) {
267
289
  reportError(
268
290
  new Error(result.error.message ?? "Stripe could not confirm payment."),
@@ -348,6 +370,7 @@ function deferredStripeElementsContract(props) {
348
370
  const excludedPaymentMethodTypes = props.context.excludedPaymentMethodTypes ? [...props.context.excludedPaymentMethodTypes].sort() : void 0;
349
371
  const elementsContractKey = [
350
372
  props.context.publishableKey,
373
+ props.setupOnly ? "setup" : "payment",
351
374
  props.retainOnAmountChange ? "retained" : props.amountMinor,
352
375
  props.currency,
353
376
  captureMethod,
@@ -356,10 +379,8 @@ function deferredStripeElementsContract(props) {
356
379
  JSON.stringify(excludedPaymentMethodTypes ?? [])
357
380
  ].join(":");
358
381
  const options = {
359
- mode: "payment",
360
- amount: props.amountMinor,
382
+ ...props.setupOnly ? { mode: "setup" } : { mode: "payment", amount: props.amountMinor, captureMethod },
361
383
  currency: props.currency,
362
- captureMethod,
363
384
  ...props.setupFutureUsage ? { setupFutureUsage: props.setupFutureUsage } : {},
364
385
  ...props.context.paymentMethodConfigurationId ? { paymentMethodConfiguration: props.context.paymentMethodConfigurationId } : {},
365
386
  ...excludedPaymentMethodTypes ? { excludedPaymentMethodTypes } : {},
@@ -369,7 +390,7 @@ function deferredStripeElementsContract(props) {
369
390
  return { elementsContractKey, options };
370
391
  }
371
392
  function PrismDeferredPaymentElement(props) {
372
- 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;
393
+ 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.setupOnly ? props.amountMinor !== 0 : props.amountMinor <= 0) || !/^[a-z]{3}$/.test(props.currency) ? "Prism Stripe checkout context is invalid." : null;
373
394
  const stripe = useMemo(
374
395
  () => validationError ? null : stripePromise(props.context.publishableKey),
375
396
  [props.context.publishableKey, validationError]
@@ -437,6 +458,7 @@ async function dispatchOutcome2(outcome, callbacks) {
437
458
  switch (outcome.status) {
438
459
  case "authorized":
439
460
  case "captured":
461
+ case "setup_completed":
440
462
  await callbacks.onAuthorized(outcome);
441
463
  return;
442
464
  case "pending_authorization":
@@ -470,7 +492,7 @@ function PaymentElementForm(props) {
470
492
  submitted.error.message ?? "Payment details are incomplete."
471
493
  );
472
494
  }
473
- const result = await stripe.confirmPayment({
495
+ const confirmation = {
474
496
  elements,
475
497
  clientSecret: props.session.clientSecret,
476
498
  confirmParams: {
@@ -482,7 +504,15 @@ function PaymentElementForm(props) {
482
504
  } : {}
483
505
  },
484
506
  redirect: "if_required"
485
- });
507
+ };
508
+ if (props.session.setupOnly) {
509
+ const result2 = await stripe.confirmSetup(confirmation);
510
+ if (result2.error) throw new Error(result2.error.message ?? "Your payment method could not be saved.");
511
+ if (!result2.setupIntent) throw new Error("Stripe did not return a setup result.");
512
+ await dispatchOutcome2(normalizeStripeSetupIntent(result2.setupIntent), callbacks);
513
+ return;
514
+ }
515
+ const result = await stripe.confirmPayment(confirmation);
486
516
  if (result.error) {
487
517
  const paymentIntent = result.error.payment_intent;
488
518
  if (paymentIntent) {
@@ -568,7 +598,7 @@ function PaymentElementForm(props) {
568
598
  );
569
599
  }
570
600
  function PrismPaymentElement(props) {
571
- 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;
601
+ const validationError = props.connector !== "stripe" || props.session.connector !== "stripe" ? "Unsupported Prism payment connector." : props.session.providerId !== "pp_hyperswitch-prism_stripe" || !props.session.clientSecret.startsWith(props.session.setupOnly ? "seti_" : "pi_") || !props.session.clientSecret.includes("_secret_") || !/^pk_(test|live)_/.test(props.session.publishableKey) ? "Prism Stripe payment session is invalid." : null;
572
602
  const stripe = useMemo2(
573
603
  () => validationError ? null : stripePromise2(props.session.publishableKey),
574
604
  [props.session.publishableKey, validationError]
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-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';
2
+ import { P as PrismDeferredPaymentElementProps, a as PrismPaymentElementProps } from './types-DhtJlmCT.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-DhtJlmCT.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-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';
2
+ import { P as PrismDeferredPaymentElementProps, a as PrismPaymentElementProps } from './types-DhtJlmCT.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-DhtJlmCT.js';
4
4
  import '@stripe/stripe-js';
5
5
 
6
6
  declare function PrismDeferredPaymentElement(props: PrismDeferredPaymentElementProps): react.JSX.Element;
package/dist/index.js CHANGED
@@ -85,6 +85,14 @@ function stripeAmountUpdater(elements, initial, applied, failed) {
85
85
  }
86
86
 
87
87
  // src/prism/outcome.ts
88
+ function normalizeStripeSetupIntent(intent) {
89
+ if (intent.status === "succeeded") return { connector: "stripe", paymentIntentId: intent.id, status: "setup_completed" };
90
+ if (intent.status === "processing") return { connector: "stripe", paymentIntentId: intent.id, status: "pending_authorization" };
91
+ if (intent.status === "requires_action" || intent.status === "requires_confirmation") {
92
+ return { connector: "stripe", paymentIntentId: intent.id, status: "requires_customer_action" };
93
+ }
94
+ throw new Error(intent.status === "canceled" ? "Saving your payment method was canceled." : "Your payment method could not be saved. Please try another method.");
95
+ }
88
96
  function normalizeStripePaymentIntent(paymentIntent) {
89
97
  switch (paymentIntent.status) {
90
98
  case "requires_capture":
@@ -189,8 +197,8 @@ function paymentElementOptions(displayPolicy) {
189
197
  ...policy.paymentMethodOrder ? { paymentMethodOrder: policy.paymentMethodOrder } : {}
190
198
  };
191
199
  }
192
- function assertPreparedClientSecret(clientSecret) {
193
- if (typeof clientSecret !== "string" || !clientSecret.startsWith("pi_") || !clientSecret.includes("_secret_")) {
200
+ function assertPreparedClientSecret(clientSecret, setupOnly) {
201
+ if (typeof clientSecret !== "string" || !clientSecret.startsWith(setupOnly ? "seti_" : "pi_") || !clientSecret.includes("_secret_")) {
194
202
  throw new Error("Payment preparation did not return a valid Stripe session.");
195
203
  }
196
204
  return clientSecret;
@@ -199,6 +207,7 @@ async function dispatchOutcome(outcome, callbacks) {
199
207
  switch (outcome.status) {
200
208
  case "authorized":
201
209
  case "captured":
210
+ case "setup_completed":
202
211
  await callbacks.onAuthorized(outcome);
203
212
  return;
204
213
  case "pending_authorization":
@@ -219,9 +228,9 @@ function DeferredPaymentElementForm(props) {
219
228
  const latest = (0, import_react.useRef)(props);
220
229
  latest.current = props;
221
230
  const submittingRef = (0, import_react.useRef)(false);
222
- const amountReady = !props.retainOnAmountChange || appliedAmount === props.amountMinor;
231
+ const amountReady = props.setupOnly || !props.retainOnAmountChange || appliedAmount === props.amountMinor;
223
232
  (0, import_react.useEffect)(() => {
224
- if (!elements || !props.retainOnAmountChange) return;
233
+ if (!elements || !props.retainOnAmountChange || props.setupOnly) return;
225
234
  const updater = stripeAmountUpdater(elements, props.initialAmount ?? props.amountMinor, setAppliedAmount, (error) => {
226
235
  latest.current.onError(error, hostErrorContext("details"));
227
236
  });
@@ -231,7 +240,7 @@ function DeferredPaymentElementForm(props) {
231
240
  updater.dispose();
232
241
  amountUpdater.current = null;
233
242
  };
234
- }, [elements, props.retainOnAmountChange, props.initialAmount]);
243
+ }, [elements, props.retainOnAmountChange, props.initialAmount, props.setupOnly]);
235
244
  (0, import_react.useEffect)(() => {
236
245
  amountUpdater.current?.set(props.amountMinor);
237
246
  }, [props.amountMinor]);
@@ -249,7 +258,7 @@ function DeferredPaymentElementForm(props) {
249
258
  async function submit(event) {
250
259
  event.preventDefault();
251
260
  if (!stripe || !elements || !ready || !amountReady || submittingRef.current || props.disabled) return;
252
- if (props.retainOnAmountChange && !amountUpdater.current?.isApplied(latest.current.amountMinor)) return;
261
+ if (!props.setupOnly && props.retainOnAmountChange && !amountUpdater.current?.isApplied(latest.current.amountMinor)) return;
253
262
  submittingRef.current = true;
254
263
  setSubmitting(true);
255
264
  props.onSubmittingChange?.(true);
@@ -268,9 +277,10 @@ function DeferredPaymentElementForm(props) {
268
277
  }
269
278
  phase = "prepare";
270
279
  const prepared = await props.onPrepare();
271
- const clientSecret = assertPreparedClientSecret(prepared?.clientSecret);
280
+ if (Boolean(prepared.setupOnly) !== Boolean(props.setupOnly)) throw new Error("Your checkout total changed. Please review it before continuing.");
281
+ const clientSecret = assertPreparedClientSecret(prepared?.clientSecret, Boolean(props.setupOnly));
272
282
  phase = "confirm";
273
- const result = await stripe.confirmPayment({
283
+ const confirmation = {
274
284
  elements,
275
285
  clientSecret,
276
286
  confirmParams: {
@@ -282,7 +292,19 @@ function DeferredPaymentElementForm(props) {
282
292
  } : {}
283
293
  },
284
294
  redirect: "if_required"
285
- });
295
+ };
296
+ if (props.setupOnly) {
297
+ const result2 = await stripe.confirmSetup(confirmation);
298
+ if (result2.error) {
299
+ reportError(new Error(result2.error.message ?? "Your payment method could not be saved."), stripeErrorContext("confirm", result2.error));
300
+ return;
301
+ }
302
+ if (!result2.setupIntent) throw new Error("Stripe did not return a setup result.");
303
+ phase = "outcome";
304
+ await dispatchOutcome(normalizeStripeSetupIntent(result2.setupIntent), callbacks);
305
+ return;
306
+ }
307
+ const result = await stripe.confirmPayment(confirmation);
286
308
  if (result.error) {
287
309
  reportError(
288
310
  new Error(result.error.message ?? "Stripe could not confirm payment."),
@@ -368,6 +390,7 @@ function deferredStripeElementsContract(props) {
368
390
  const excludedPaymentMethodTypes = props.context.excludedPaymentMethodTypes ? [...props.context.excludedPaymentMethodTypes].sort() : void 0;
369
391
  const elementsContractKey = [
370
392
  props.context.publishableKey,
393
+ props.setupOnly ? "setup" : "payment",
371
394
  props.retainOnAmountChange ? "retained" : props.amountMinor,
372
395
  props.currency,
373
396
  captureMethod,
@@ -376,10 +399,8 @@ function deferredStripeElementsContract(props) {
376
399
  JSON.stringify(excludedPaymentMethodTypes ?? [])
377
400
  ].join(":");
378
401
  const options = {
379
- mode: "payment",
380
- amount: props.amountMinor,
402
+ ...props.setupOnly ? { mode: "setup" } : { mode: "payment", amount: props.amountMinor, captureMethod },
381
403
  currency: props.currency,
382
- captureMethod,
383
404
  ...props.setupFutureUsage ? { setupFutureUsage: props.setupFutureUsage } : {},
384
405
  ...props.context.paymentMethodConfigurationId ? { paymentMethodConfiguration: props.context.paymentMethodConfigurationId } : {},
385
406
  ...excludedPaymentMethodTypes ? { excludedPaymentMethodTypes } : {},
@@ -389,7 +410,7 @@ function deferredStripeElementsContract(props) {
389
410
  return { elementsContractKey, options };
390
411
  }
391
412
  function PrismDeferredPaymentElement(props) {
392
- 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;
413
+ 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.setupOnly ? props.amountMinor !== 0 : props.amountMinor <= 0) || !/^[a-z]{3}$/.test(props.currency) ? "Prism Stripe checkout context is invalid." : null;
393
414
  const stripe = (0, import_react.useMemo)(
394
415
  () => validationError ? null : stripePromise(props.context.publishableKey),
395
416
  [props.context.publishableKey, validationError]
@@ -447,6 +468,7 @@ async function dispatchOutcome2(outcome, callbacks) {
447
468
  switch (outcome.status) {
448
469
  case "authorized":
449
470
  case "captured":
471
+ case "setup_completed":
450
472
  await callbacks.onAuthorized(outcome);
451
473
  return;
452
474
  case "pending_authorization":
@@ -480,7 +502,7 @@ function PaymentElementForm(props) {
480
502
  submitted.error.message ?? "Payment details are incomplete."
481
503
  );
482
504
  }
483
- const result = await stripe.confirmPayment({
505
+ const confirmation = {
484
506
  elements,
485
507
  clientSecret: props.session.clientSecret,
486
508
  confirmParams: {
@@ -492,7 +514,15 @@ function PaymentElementForm(props) {
492
514
  } : {}
493
515
  },
494
516
  redirect: "if_required"
495
- });
517
+ };
518
+ if (props.session.setupOnly) {
519
+ const result2 = await stripe.confirmSetup(confirmation);
520
+ if (result2.error) throw new Error(result2.error.message ?? "Your payment method could not be saved.");
521
+ if (!result2.setupIntent) throw new Error("Stripe did not return a setup result.");
522
+ await dispatchOutcome2(normalizeStripeSetupIntent(result2.setupIntent), callbacks);
523
+ return;
524
+ }
525
+ const result = await stripe.confirmPayment(confirmation);
496
526
  if (result.error) {
497
527
  const paymentIntent = result.error.payment_intent;
498
528
  if (paymentIntent) {
@@ -578,7 +608,7 @@ function PaymentElementForm(props) {
578
608
  );
579
609
  }
580
610
  function PrismPaymentElement(props) {
581
- 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;
611
+ const validationError = props.connector !== "stripe" || props.session.connector !== "stripe" ? "Unsupported Prism payment connector." : props.session.providerId !== "pp_hyperswitch-prism_stripe" || !props.session.clientSecret.startsWith(props.session.setupOnly ? "seti_" : "pi_") || !props.session.clientSecret.includes("_secret_") || !/^pk_(test|live)_/.test(props.session.publishableKey) ? "Prism Stripe payment session is invalid." : null;
582
612
  const stripe = (0, import_react2.useMemo)(
583
613
  () => validationError ? null : stripePromise2(props.session.publishableKey),
584
614
  [props.session.publishableKey, validationError]
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  PrismDeferredPaymentElement,
3
3
  PrismPaymentElement
4
- } from "./chunk-XNEJKU5P.mjs";
4
+ } from "./chunk-EQHBEAD7.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-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';
2
+ import { e as PrismDeferredPaymentSurfaceProps, p as PrismPaymentSurfaceProps } from './types-DhtJlmCT.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-DhtJlmCT.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-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';
2
+ import { e as PrismDeferredPaymentSurfaceProps, p as PrismPaymentSurfaceProps } from './types-DhtJlmCT.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-DhtJlmCT.js';
4
4
  import '@stripe/stripe-js';
5
5
 
6
6
  /**