@tagadapay/headless-sdk 1.6.0 → 1.8.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -186
- package/package.json +6 -11
- package/dist/core/client.d.ts +0 -54
- package/dist/core/client.d.ts.map +0 -1
- package/dist/core/client.js +0 -67
- package/dist/core/client.js.map +0 -1
- package/dist/core/errors.d.ts +0 -46
- package/dist/core/errors.d.ts.map +0 -1
- package/dist/core/errors.js +0 -57
- package/dist/core/errors.js.map +0 -1
- package/dist/core/helpers/autocomplete.d.ts +0 -89
- package/dist/core/helpers/autocomplete.d.ts.map +0 -1
- package/dist/core/helpers/autocomplete.js +0 -186
- package/dist/core/helpers/autocomplete.js.map +0 -1
- package/dist/core/helpers/countries.d.ts +0 -45
- package/dist/core/helpers/countries.d.ts.map +0 -1
- package/dist/core/helpers/countries.js +0 -65
- package/dist/core/helpers/countries.js.map +0 -1
- package/dist/core/helpers/currency.d.ts +0 -26
- package/dist/core/helpers/currency.d.ts.map +0 -1
- package/dist/core/helpers/currency.js +0 -72
- package/dist/core/helpers/currency.js.map +0 -1
- package/dist/core/helpers/index.d.ts +0 -5
- package/dist/core/helpers/index.d.ts.map +0 -1
- package/dist/core/helpers/index.js +0 -5
- package/dist/core/helpers/index.js.map +0 -1
- package/dist/core/helpers/iso3166.d.ts +0 -28
- package/dist/core/helpers/iso3166.d.ts.map +0 -1
- package/dist/core/helpers/iso3166.js +0 -84
- package/dist/core/helpers/iso3166.js.map +0 -1
- package/dist/core/http.d.ts +0 -27
- package/dist/core/http.d.ts.map +0 -1
- package/dist/core/http.js +0 -116
- package/dist/core/http.js.map +0 -1
- package/dist/core/index.d.ts +0 -12
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/index.js +0 -11
- package/dist/core/index.js.map +0 -1
- package/dist/core/modules/catalog.d.ts +0 -27
- package/dist/core/modules/catalog.d.ts.map +0 -1
- package/dist/core/modules/catalog.js +0 -38
- package/dist/core/modules/catalog.js.map +0 -1
- package/dist/core/modules/checkout.d.ts +0 -249
- package/dist/core/modules/checkout.d.ts.map +0 -1
- package/dist/core/modules/checkout.js +0 -448
- package/dist/core/modules/checkout.js.map +0 -1
- package/dist/core/modules/customer.d.ts +0 -33
- package/dist/core/modules/customer.d.ts.map +0 -1
- package/dist/core/modules/customer.js +0 -53
- package/dist/core/modules/customer.js.map +0 -1
- package/dist/core/modules/offers.d.ts +0 -137
- package/dist/core/modules/offers.d.ts.map +0 -1
- package/dist/core/modules/offers.js +0 -141
- package/dist/core/modules/offers.js.map +0 -1
- package/dist/core/modules/payment.d.ts +0 -298
- package/dist/core/modules/payment.d.ts.map +0 -1
- package/dist/core/modules/payment.js +0 -701
- package/dist/core/modules/payment.js.map +0 -1
- package/dist/core/types.d.ts +0 -460
- package/dist/core/types.d.ts.map +0 -1
- package/dist/core/types.js +0 -5
- package/dist/core/types.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/react/hooks/useCatalog.d.ts +0 -25
- package/dist/react/hooks/useCatalog.d.ts.map +0 -1
- package/dist/react/hooks/useCatalog.js +0 -58
- package/dist/react/hooks/useCatalog.js.map +0 -1
- package/dist/react/hooks/useCheckout.d.ts +0 -58
- package/dist/react/hooks/useCheckout.d.ts.map +0 -1
- package/dist/react/hooks/useCheckout.js +0 -168
- package/dist/react/hooks/useCheckout.js.map +0 -1
- package/dist/react/hooks/useCustomer.d.ts +0 -28
- package/dist/react/hooks/useCustomer.d.ts.map +0 -1
- package/dist/react/hooks/useCustomer.js +0 -77
- package/dist/react/hooks/useCustomer.js.map +0 -1
- package/dist/react/hooks/useGoogleAutocomplete.d.ts +0 -26
- package/dist/react/hooks/useGoogleAutocomplete.d.ts.map +0 -1
- package/dist/react/hooks/useGoogleAutocomplete.js +0 -70
- package/dist/react/hooks/useGoogleAutocomplete.js.map +0 -1
- package/dist/react/hooks/useISOData.d.ts +0 -23
- package/dist/react/hooks/useISOData.d.ts.map +0 -1
- package/dist/react/hooks/useISOData.js +0 -63
- package/dist/react/hooks/useISOData.js.map +0 -1
- package/dist/react/hooks/useOffers.d.ts +0 -134
- package/dist/react/hooks/useOffers.d.ts.map +0 -1
- package/dist/react/hooks/useOffers.js +0 -171
- package/dist/react/hooks/useOffers.js.map +0 -1
- package/dist/react/hooks/usePayment.d.ts +0 -108
- package/dist/react/hooks/usePayment.d.ts.map +0 -1
- package/dist/react/hooks/usePayment.js +0 -233
- package/dist/react/hooks/usePayment.js.map +0 -1
- package/dist/react/index.d.ts +0 -20
- package/dist/react/index.d.ts.map +0 -1
- package/dist/react/index.js +0 -11
- package/dist/react/index.js.map +0 -1
- package/dist/react/internal/redirectGuard.d.ts +0 -9
- package/dist/react/internal/redirectGuard.d.ts.map +0 -1
- package/dist/react/internal/redirectGuard.js +0 -25
- package/dist/react/internal/redirectGuard.js.map +0 -1
- package/dist/react/provider.d.ts +0 -31
- package/dist/react/provider.d.ts.map +0 -1
- package/dist/react/provider.js +0 -32
- package/dist/react/provider.js.map +0 -1
- package/dist/stripe/StripeExpressButton.d.ts +0 -30
- package/dist/stripe/StripeExpressButton.d.ts.map +0 -1
- package/dist/stripe/StripeExpressButton.js +0 -32
- package/dist/stripe/StripeExpressButton.js.map +0 -1
- package/dist/stripe/StripeExpressCheckout.d.ts +0 -98
- package/dist/stripe/StripeExpressCheckout.d.ts.map +0 -1
- package/dist/stripe/StripeExpressCheckout.js +0 -198
- package/dist/stripe/StripeExpressCheckout.js.map +0 -1
- package/dist/stripe/index.d.ts +0 -8
- package/dist/stripe/index.d.ts.map +0 -1
- package/dist/stripe/index.js +0 -15
- package/dist/stripe/index.js.map +0 -1
- package/dist/stripe/useStripeExpress.d.ts +0 -54
- package/dist/stripe/useStripeExpress.d.ts.map +0 -1
- package/dist/stripe/useStripeExpress.js +0 -126
- package/dist/stripe/useStripeExpress.js.map +0 -1
- package/dist/tagada-headless-react.min.js +0 -8
- package/dist/tagada-headless-react.min.js.map +0 -7
- package/dist/tagada-headless.js +0 -2070
- package/dist/tagada-headless.min.js +0 -8
- package/dist/tagada-headless.min.js.map +0 -7
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import type { Offer, OfferPreview, OfferPayResult, ProcessPaymentResult } from '../../core/types.js';
|
|
2
|
-
export interface UseOffersOptions {
|
|
3
|
-
/**
|
|
4
|
-
* Called when an offer charge succeeds, including after a 3DS redirect
|
|
5
|
-
* return. Use this to navigate to the next funnel step
|
|
6
|
-
* (e.g. `/upsell-2` or `/thank-you`).
|
|
7
|
-
*/
|
|
8
|
-
onOfferAccepted?: (result: ProcessPaymentResult & {
|
|
9
|
-
status: 'succeeded';
|
|
10
|
-
}) => void;
|
|
11
|
-
/**
|
|
12
|
-
* Called when an offer charge fails or is declined, including after a
|
|
13
|
-
* redirect return.
|
|
14
|
-
*/
|
|
15
|
-
onOfferDeclined?: (result: ProcessPaymentResult & {
|
|
16
|
-
status: 'failed';
|
|
17
|
-
}) => void;
|
|
18
|
-
/**
|
|
19
|
-
* Disable the in-hook 3DS-return auto-resume. Set this to `true` if you
|
|
20
|
-
* already mount `usePayment` on the same page — though both hooks use a
|
|
21
|
-
* shared guard so leaving it `false` is also safe.
|
|
22
|
-
*/
|
|
23
|
-
disableAutoResume?: boolean;
|
|
24
|
-
}
|
|
25
|
-
export interface UseOffersReturn {
|
|
26
|
-
/** Current loaded offer. */
|
|
27
|
-
offer: Offer | null;
|
|
28
|
-
/** Current preview (pricing breakdown). */
|
|
29
|
-
preview: OfferPreview | null;
|
|
30
|
-
/** Whether an operation is in progress (including polling after redirect). */
|
|
31
|
-
isLoading: boolean;
|
|
32
|
-
/** Last error. */
|
|
33
|
-
error: Error | null;
|
|
34
|
-
/** Get a single offer by ID. */
|
|
35
|
-
getOffer: (offerId: string) => Promise<Offer>;
|
|
36
|
-
/** List offers for the store, optionally filtered by type or IDs. */
|
|
37
|
-
listOffers: (opts?: {
|
|
38
|
-
type?: 'upsell' | 'orderbump';
|
|
39
|
-
ids?: string[];
|
|
40
|
-
}) => Promise<Offer[]>;
|
|
41
|
-
/** Preview offer pricing (line items with amounts). */
|
|
42
|
-
previewOffer: (opts: {
|
|
43
|
-
offerId: string;
|
|
44
|
-
currency?: string;
|
|
45
|
-
lineItems?: Array<{
|
|
46
|
-
variantId: string;
|
|
47
|
-
quantity: number;
|
|
48
|
-
}>;
|
|
49
|
-
}) => Promise<OfferPreview>;
|
|
50
|
-
/**
|
|
51
|
-
* **Recommended.** Charge a previewed offer and handle the full
|
|
52
|
-
* lifecycle (3DS challenge or redirect, polling, return-URL detection).
|
|
53
|
-
*
|
|
54
|
-
* Resolves with a typed `ProcessPaymentResult`:
|
|
55
|
-
* - `'succeeded'` — charge complete; `onOfferAccepted` callback fired.
|
|
56
|
-
* - `'requires_redirect'` — browser is being redirected to the bank;
|
|
57
|
-
* the hook will auto-resume on return.
|
|
58
|
-
* - `'failed'` — charge declined or error; `onOfferDeclined` fired.
|
|
59
|
-
* - `'pending'` — async charge still in flight (rare).
|
|
60
|
-
*/
|
|
61
|
-
processOfferPayment: (opts: {
|
|
62
|
-
offerId: string;
|
|
63
|
-
mainOrderId: string;
|
|
64
|
-
currency?: string;
|
|
65
|
-
lineItems?: Array<{
|
|
66
|
-
variantId: string;
|
|
67
|
-
quantity: number;
|
|
68
|
-
}>;
|
|
69
|
-
returnUrl?: string;
|
|
70
|
-
paymentInstrumentId?: string;
|
|
71
|
-
}) => Promise<ProcessPaymentResult>;
|
|
72
|
-
/**
|
|
73
|
-
* Low-level pay (returns raw `OfferPayResult`, no automatic 3DS / polling).
|
|
74
|
-
* Most callers should use `processOfferPayment` instead.
|
|
75
|
-
*/
|
|
76
|
-
payPreviewedOffer: (opts: {
|
|
77
|
-
offerId: string;
|
|
78
|
-
mainOrderId: string;
|
|
79
|
-
currency?: string;
|
|
80
|
-
lineItems?: Array<{
|
|
81
|
-
variantId: string;
|
|
82
|
-
quantity: number;
|
|
83
|
-
}>;
|
|
84
|
-
returnUrl?: string;
|
|
85
|
-
paymentInstrumentId?: string;
|
|
86
|
-
}) => Promise<OfferPayResult>;
|
|
87
|
-
/** Toggle an order bump on/off within a checkout session. */
|
|
88
|
-
toggleOrderBump: (opts: {
|
|
89
|
-
checkoutSessionId: string;
|
|
90
|
-
orderBumpOfferId: string;
|
|
91
|
-
selected: boolean;
|
|
92
|
-
}) => Promise<unknown>;
|
|
93
|
-
/** List post-purchase offers for an order. */
|
|
94
|
-
listPostPurchaseOffers: (orderId: string) => Promise<Offer[]>;
|
|
95
|
-
/** Accept a post-purchase offer (only works for offers pre-registered as post-purchase on the order). */
|
|
96
|
-
acceptPostPurchaseOffer: (opts: {
|
|
97
|
-
orderId: string;
|
|
98
|
-
offerId: string;
|
|
99
|
-
items?: Array<{
|
|
100
|
-
variantId: string;
|
|
101
|
-
quantity: number;
|
|
102
|
-
}>;
|
|
103
|
-
}) => Promise<{
|
|
104
|
-
success: boolean;
|
|
105
|
-
error?: string;
|
|
106
|
-
}>;
|
|
107
|
-
/** Decline a post-purchase offer. */
|
|
108
|
-
declinePostPurchaseOffer: (opts: {
|
|
109
|
-
orderId: string;
|
|
110
|
-
offerId: string;
|
|
111
|
-
}) => Promise<{
|
|
112
|
-
success: boolean;
|
|
113
|
-
error?: string;
|
|
114
|
-
}>;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* React hook for offers — upsells, order bumps, and post-purchase offers.
|
|
118
|
-
*
|
|
119
|
-
* @example
|
|
120
|
-
* ```tsx
|
|
121
|
-
* const { processOfferPayment, isLoading, error } = useOffers({
|
|
122
|
-
* onOfferAccepted: () => navigate('/upsell-2'),
|
|
123
|
-
* onOfferDeclined: (res) => toast.error(res.error),
|
|
124
|
-
* });
|
|
125
|
-
*
|
|
126
|
-
* await processOfferPayment({
|
|
127
|
-
* offerId: 'offer_xxx',
|
|
128
|
-
* mainOrderId: order.id,
|
|
129
|
-
* });
|
|
130
|
-
* // 3DS, redirect, return, polling — all handled automatically.
|
|
131
|
-
* ```
|
|
132
|
-
*/
|
|
133
|
-
export declare function useOffers(options?: UseOffersOptions): UseOffersReturn;
|
|
134
|
-
//# sourceMappingURL=useOffers.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useOffers.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useOffers.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,KAAK,EACL,YAAY,EACZ,cAAc,EACd,oBAAoB,EACrB,MAAM,qBAAqB,CAAC;AAM7B,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,eAAe,CAAC,EAAE,CAAC,MAAM,EAAE,oBAAoB,GAAG;QAAE,MAAM,EAAE,WAAW,CAAA;KAAE,KAAK,IAAI,CAAC;IACnF;;;OAGG;IACH,eAAe,CAAC,EAAE,CAAC,MAAM,EAAE,oBAAoB,GAAG;QAAE,MAAM,EAAE,QAAQ,CAAA;KAAE,KAAK,IAAI,CAAC;IAChF;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED,MAAM,WAAW,eAAe;IAC9B,4BAA4B;IAC5B,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,2CAA2C;IAC3C,OAAO,EAAE,YAAY,GAAG,IAAI,CAAC;IAC7B,8EAA8E;IAC9E,SAAS,EAAE,OAAO,CAAC;IACnB,kBAAkB;IAClB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IAEpB,gCAAgC;IAChC,QAAQ,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,KAAK,CAAC,CAAC;IAC9C,qEAAqE;IACrE,UAAU,EAAE,CAAC,IAAI,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,QAAQ,GAAG,WAAW,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,KAAK,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;IAC3F,uDAAuD;IACvD,YAAY,EAAE,CAAC,IAAI,EAAE;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EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;IAC9D,yGAAyG;IACzG,uBAAuB,EAAE,CAAC,IAAI,EAAE;QAC9B,OAAO,EAAE,MAAM,CAAC;QAChB,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,CAAC,EAAE,KAAK,CAAC;YAAE,SAAS,EAAE,MAAM,CAAC;YAAC,QAAQ,EAAE,MAAM,CAAA;SAAE,CAAC,CAAC;KACxD,KAAK,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACpD,qCAAqC;IACrC,wBAAwB,EAAE,CAAC,IAAI,EAAE;QAC/B,OAAO,EAAE,MAAM,CAAC;QAChB,OAAO,EAAE,MAAM,CAAC;KACjB,KAAK,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CACrD;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,SAAS,CAAC,OAAO,CAAC,EAAE,gBAAgB,GAAG,eAAe,CAgNrE"}
|
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import { useState, useCallback, useRef, useEffect } from 'react';
|
|
2
|
-
import { useHeadlessClient } from '../provider.js';
|
|
3
|
-
import { tryClaimResume } from '../internal/redirectGuard.js';
|
|
4
|
-
/**
|
|
5
|
-
* React hook for offers — upsells, order bumps, and post-purchase offers.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```tsx
|
|
9
|
-
* const { processOfferPayment, isLoading, error } = useOffers({
|
|
10
|
-
* onOfferAccepted: () => navigate('/upsell-2'),
|
|
11
|
-
* onOfferDeclined: (res) => toast.error(res.error),
|
|
12
|
-
* });
|
|
13
|
-
*
|
|
14
|
-
* await processOfferPayment({
|
|
15
|
-
* offerId: 'offer_xxx',
|
|
16
|
-
* mainOrderId: order.id,
|
|
17
|
-
* });
|
|
18
|
-
* // 3DS, redirect, return, polling — all handled automatically.
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
export function useOffers(options) {
|
|
22
|
-
const client = useHeadlessClient();
|
|
23
|
-
const [offer, setOffer] = useState(null);
|
|
24
|
-
const [preview, setPreview] = useState(null);
|
|
25
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
26
|
-
const [error, setError] = useState(null);
|
|
27
|
-
const optionsRef = useRef(options);
|
|
28
|
-
optionsRef.current = options;
|
|
29
|
-
const redirectReturnProcessedRef = useRef(false);
|
|
30
|
-
// ---------------------------------------------------------------------------
|
|
31
|
-
// Auto-resume after 3DS / processor redirect (offer-flavored callbacks).
|
|
32
|
-
// Mirrors usePayment; guarded so the two hooks don't both fire.
|
|
33
|
-
// ---------------------------------------------------------------------------
|
|
34
|
-
useEffect(() => {
|
|
35
|
-
if (typeof window === 'undefined')
|
|
36
|
-
return;
|
|
37
|
-
if (optionsRef.current?.disableAutoResume)
|
|
38
|
-
return;
|
|
39
|
-
if (redirectReturnProcessedRef.current)
|
|
40
|
-
return;
|
|
41
|
-
const params = new URLSearchParams(window.location.search);
|
|
42
|
-
const paymentAction = params.get('paymentAction');
|
|
43
|
-
const paymentActionStatus = params.get('paymentActionStatus');
|
|
44
|
-
const paymentId = params.get('paymentId');
|
|
45
|
-
if (paymentAction !== 'requireAction' || paymentActionStatus !== 'completed' || !paymentId) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
redirectReturnProcessedRef.current = true;
|
|
49
|
-
if (!tryClaimResume(paymentId)) {
|
|
50
|
-
// Another hook (likely usePayment) already started the resume.
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
setIsLoading(true);
|
|
54
|
-
setError(null);
|
|
55
|
-
const resume = async () => {
|
|
56
|
-
try {
|
|
57
|
-
const result = await client.payment.resumeAfterRedirect(paymentId);
|
|
58
|
-
const cleanParams = new URLSearchParams(window.location.search);
|
|
59
|
-
['paymentAction', 'paymentActionStatus', 'paymentId', 'payment_intent', 'payment_intent_client_secret', 'source_type', 'redirect_status'].forEach((p) => cleanParams.delete(p));
|
|
60
|
-
const newUrl = cleanParams.toString()
|
|
61
|
-
? `${window.location.pathname}?${cleanParams.toString()}`
|
|
62
|
-
: window.location.pathname;
|
|
63
|
-
window.history.replaceState({}, document.title, newUrl);
|
|
64
|
-
if (result.status === 'succeeded') {
|
|
65
|
-
optionsRef.current?.onOfferAccepted?.(result);
|
|
66
|
-
}
|
|
67
|
-
else if (result.status === 'failed') {
|
|
68
|
-
setError(new Error(result.error));
|
|
69
|
-
optionsRef.current?.onOfferDeclined?.(result);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
catch (err) {
|
|
73
|
-
setError(err instanceof Error ? err : new Error(String(err)));
|
|
74
|
-
}
|
|
75
|
-
finally {
|
|
76
|
-
setIsLoading(false);
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
void resume();
|
|
80
|
-
}, [client]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
81
|
-
const wrap = useCallback(async (fn) => {
|
|
82
|
-
setIsLoading(true);
|
|
83
|
-
setError(null);
|
|
84
|
-
try {
|
|
85
|
-
return await fn();
|
|
86
|
-
}
|
|
87
|
-
catch (err) {
|
|
88
|
-
const e = err instanceof Error ? err : new Error(String(err));
|
|
89
|
-
setError(e);
|
|
90
|
-
throw e;
|
|
91
|
-
}
|
|
92
|
-
finally {
|
|
93
|
-
setIsLoading(false);
|
|
94
|
-
}
|
|
95
|
-
}, []);
|
|
96
|
-
const getOffer = useCallback((offerId) => wrap(async () => {
|
|
97
|
-
const o = await client.offers.getOffer(offerId);
|
|
98
|
-
setOffer(o);
|
|
99
|
-
return o;
|
|
100
|
-
}), [client, wrap]);
|
|
101
|
-
const listOffers = useCallback((opts) => client.offers.listOffers(opts), [client]);
|
|
102
|
-
const previewOffer = useCallback((opts) => wrap(async () => {
|
|
103
|
-
const p = await client.offers.previewOffer(opts);
|
|
104
|
-
setPreview(p);
|
|
105
|
-
return p;
|
|
106
|
-
}), [client, wrap]);
|
|
107
|
-
const payPreviewedOffer = useCallback((opts) => wrap(() => client.offers.payPreviewedOffer(opts)), [client, wrap]);
|
|
108
|
-
const processOfferPayment = useCallback(async (opts) => {
|
|
109
|
-
setIsLoading(true);
|
|
110
|
-
setError(null);
|
|
111
|
-
try {
|
|
112
|
-
const result = await client.offers.processOfferPayment(opts);
|
|
113
|
-
if (result.status === 'requires_redirect') {
|
|
114
|
-
if (result.method === 'POST' && result.postData) {
|
|
115
|
-
const form = document.createElement('form');
|
|
116
|
-
form.method = 'POST';
|
|
117
|
-
form.action = result.redirectUrl;
|
|
118
|
-
for (const [key, value] of Object.entries(result.postData)) {
|
|
119
|
-
const input = document.createElement('input');
|
|
120
|
-
input.type = 'hidden';
|
|
121
|
-
input.name = key;
|
|
122
|
-
input.value = value;
|
|
123
|
-
form.appendChild(input);
|
|
124
|
-
}
|
|
125
|
-
document.body.appendChild(form);
|
|
126
|
-
form.submit();
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
window.location.href = result.redirectUrl;
|
|
130
|
-
}
|
|
131
|
-
return result;
|
|
132
|
-
}
|
|
133
|
-
if (result.status === 'succeeded') {
|
|
134
|
-
optionsRef.current?.onOfferAccepted?.(result);
|
|
135
|
-
}
|
|
136
|
-
else if (result.status === 'failed') {
|
|
137
|
-
setError(new Error(result.error));
|
|
138
|
-
optionsRef.current?.onOfferDeclined?.(result);
|
|
139
|
-
}
|
|
140
|
-
return result;
|
|
141
|
-
}
|
|
142
|
-
catch (err) {
|
|
143
|
-
const e = err instanceof Error ? err : new Error(String(err));
|
|
144
|
-
setError(e);
|
|
145
|
-
return { status: 'failed', error: e.message };
|
|
146
|
-
}
|
|
147
|
-
finally {
|
|
148
|
-
setIsLoading(false);
|
|
149
|
-
}
|
|
150
|
-
}, [client]);
|
|
151
|
-
const toggleOrderBump = useCallback((opts) => client.offers.toggleOrderBump(opts), [client]);
|
|
152
|
-
const listPostPurchaseOffers = useCallback((orderId) => client.offers.listPostPurchaseOffers(orderId), [client]);
|
|
153
|
-
const acceptPostPurchaseOffer = useCallback((opts) => wrap(() => client.offers.acceptPostPurchaseOffer(opts)), [client, wrap]);
|
|
154
|
-
const declinePostPurchaseOffer = useCallback((opts) => wrap(() => client.offers.declinePostPurchaseOffer(opts)), [client, wrap]);
|
|
155
|
-
return {
|
|
156
|
-
offer,
|
|
157
|
-
preview,
|
|
158
|
-
isLoading,
|
|
159
|
-
error,
|
|
160
|
-
getOffer,
|
|
161
|
-
listOffers,
|
|
162
|
-
previewOffer,
|
|
163
|
-
processOfferPayment,
|
|
164
|
-
payPreviewedOffer,
|
|
165
|
-
toggleOrderBump,
|
|
166
|
-
listPostPurchaseOffers,
|
|
167
|
-
acceptPostPurchaseOffer,
|
|
168
|
-
declinePostPurchaseOffer,
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
//# sourceMappingURL=useOffers.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useOffers.js","sourceRoot":"","sources":["../../../src/react/hooks/useOffers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACjE,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AA4G9D;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,SAAS,CAAC,OAA0B;IAClD,MAAM,MAAM,GAAG,iBAAiB,EAAE,CAAC;IACnC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAe,IAAI,CAAC,CAAC;IACvD,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAsB,IAAI,CAAC,CAAC;IAClE,MAAM,CAAC,SAAS,EAAE,YAAY,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AC,IAAI,CAAC,EACrC,CAAC,MAAM,CAAC,CACT,CAAC;IAEF,MAAM,sBAAsB,GAAG,WAAW,CACxC,CAAC,OAAe,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,sBAAsB,CAAC,OAAO,CAAC,EAClE,CAAC,MAAM,CAAC,CACT,CAAC;IAEF,MAAM,uBAAuB,GAAG,WAAW,CACzC,CAAC,IAAkG,EAAE,EAAE,CACrG,IAAI,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,uBAAuB,CAAC,IAAI,CAAC,CAAC,EACzD,CAAC,MAAM,EAAE,IAAI,CAAC,CACf,CAAC;IAEF,MAAM,wBAAwB,GAAG,WAAW,CAC1C,CAAC,IAA0C,EAAE,EAAE,CAC7C,IAAI,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,wBAAwB,CAAC,IAAI,CAAC,CAAC,EAC1D,CAAC,MAAM,EAAE,IAAI,CAAC,CACf,CAAC;IAEF,OAAO;QACL,KAAK;QACL,OAAO;QACP,SAAS;QACT,KAAK;QACL,QAAQ;QACR,UAAU;QACV,YAAY;QACZ,mBAAmB;QACnB,iBAAiB;QACjB,eAAe;QACf,sBAAsB;QACtB,uBAAuB;QACvB,wBAAwB;KACzB,CAAC;AACJ,CAAC"}
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import type { PaymentSetupConfig, ExpressMethods, StoreConfig, PayResult, PayOptions, PaymentStatus, ProcessPaymentOptions, ProcessPaymentResult } from '../../core/types.js';
|
|
2
|
-
export interface UsePaymentOptions {
|
|
3
|
-
/**
|
|
4
|
-
* Called when a payment succeeds (including after 3DS redirect return).
|
|
5
|
-
* Use this to navigate to a confirmation page.
|
|
6
|
-
*/
|
|
7
|
-
onPaymentSuccess?: (result: ProcessPaymentResult & {
|
|
8
|
-
status: 'succeeded';
|
|
9
|
-
}) => void;
|
|
10
|
-
/**
|
|
11
|
-
* Called when a payment fails (including after redirect return).
|
|
12
|
-
*/
|
|
13
|
-
onPaymentFailed?: (result: ProcessPaymentResult & {
|
|
14
|
-
status: 'failed';
|
|
15
|
-
}) => void;
|
|
16
|
-
/**
|
|
17
|
-
* Explicit paymentSetupConfig override.
|
|
18
|
-
* When provided, the SDK skips both __TGD_STEP_CONFIG__ detection and the API call.
|
|
19
|
-
* Useful when a developer fetches the config from their own backend.
|
|
20
|
-
*/
|
|
21
|
-
paymentSetupConfig?: PaymentSetupConfig;
|
|
22
|
-
}
|
|
23
|
-
export interface UsePaymentReturn {
|
|
24
|
-
/** Full payment setup config (all methods with their enabled/disabled state). */
|
|
25
|
-
paymentSetup: PaymentSetupConfig | null;
|
|
26
|
-
/** Express methods (Apple Pay, Google Pay, etc.) with browser availability. */
|
|
27
|
-
expressMethods: ExpressMethods | null;
|
|
28
|
-
/** Store-level config (3DS flags, etc.). */
|
|
29
|
-
storeConfig: StoreConfig | null;
|
|
30
|
-
/** Whether a payment is currently being processed (including polling after redirect). */
|
|
31
|
-
isProcessing: boolean;
|
|
32
|
-
/** Last payment result. */
|
|
33
|
-
result: PayResult | null;
|
|
34
|
-
/** Last error. */
|
|
35
|
-
error: Error | null;
|
|
36
|
-
/**
|
|
37
|
-
* Load payment setup for a checkout session. Call this once when the session is ready.
|
|
38
|
-
* Optionally pass `paymentSetupConfig` to override auto-detection.
|
|
39
|
-
*/
|
|
40
|
-
loadPaymentSetup: (checkoutSessionId: string, opts?: {
|
|
41
|
-
paymentSetupConfig?: PaymentSetupConfig;
|
|
42
|
-
}) => Promise<void>;
|
|
43
|
-
/**
|
|
44
|
-
* **Recommended**. Tokenize + pay + handle 3DS/redirects + poll — all in one call.
|
|
45
|
-
*
|
|
46
|
-
* - If the payment succeeds immediately, resolves with `{ status: 'succeeded' }`.
|
|
47
|
-
* - If 3DS redirect is needed, automatically redirects the browser.
|
|
48
|
-
* When the user comes back, the hook auto-detects the return and polls.
|
|
49
|
-
* - If the payment fails, resolves with `{ status: 'failed', error: '...' }`.
|
|
50
|
-
*/
|
|
51
|
-
processPayment: (opts: ProcessPaymentOptions) => Promise<ProcessPaymentResult>;
|
|
52
|
-
/** Low-level pay (returns raw API response, no automatic action handling). */
|
|
53
|
-
pay: (opts: PayOptions) => Promise<PayResult>;
|
|
54
|
-
/**
|
|
55
|
-
* Tokenize a card (returns tagadaToken). Requires @tagadapay/core-js.
|
|
56
|
-
*
|
|
57
|
-
* PCI note: card data passes through your JS environment.
|
|
58
|
-
* You are responsible for PCI DSS SAQ-A-EP compliance.
|
|
59
|
-
*/
|
|
60
|
-
tokenizeCard: (card: {
|
|
61
|
-
cardNumber: string;
|
|
62
|
-
expiryDate: string;
|
|
63
|
-
cvc: string;
|
|
64
|
-
cardholderName?: string;
|
|
65
|
-
}) => Promise<{
|
|
66
|
-
tagadaToken: string;
|
|
67
|
-
rawToken: Record<string, unknown>;
|
|
68
|
-
}>;
|
|
69
|
-
/** Process Apple Pay. */
|
|
70
|
-
processApplePay: (opts: {
|
|
71
|
-
checkoutSessionId: string;
|
|
72
|
-
applePayToken: unknown;
|
|
73
|
-
}) => Promise<PayResult>;
|
|
74
|
-
/** Process Google Pay. */
|
|
75
|
-
processGooglePay: (opts: {
|
|
76
|
-
checkoutSessionId: string;
|
|
77
|
-
googlePayToken: string;
|
|
78
|
-
}) => Promise<PayResult>;
|
|
79
|
-
/** Process redirect-based APM (Klarna, iDEAL, etc.). */
|
|
80
|
-
processApm: (opts: {
|
|
81
|
-
checkoutSessionId: string;
|
|
82
|
-
paymentMethod: string;
|
|
83
|
-
processorId: string;
|
|
84
|
-
}) => Promise<PayResult>;
|
|
85
|
-
/** Poll a payment until terminal status. */
|
|
86
|
-
pollStatus: (paymentId: string) => Promise<PaymentStatus>;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* React hook for payment processing.
|
|
90
|
-
*
|
|
91
|
-
* Handles the full payment lifecycle including 3DS redirects and return-URL detection.
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
* ```tsx
|
|
95
|
-
* const { processPayment, isProcessing, error } = usePayment({
|
|
96
|
-
* onPaymentSuccess: (result) => router.push('/confirmation'),
|
|
97
|
-
* onPaymentFailed: (result) => toast.error(result.error),
|
|
98
|
-
* });
|
|
99
|
-
*
|
|
100
|
-
* // On submit — one function handles everything
|
|
101
|
-
* const result = await processPayment({
|
|
102
|
-
* checkoutSessionId: session.id,
|
|
103
|
-
* tagadaToken,
|
|
104
|
-
* });
|
|
105
|
-
* ```
|
|
106
|
-
*/
|
|
107
|
-
export declare function usePayment(options?: UsePaymentOptions): UsePaymentReturn;
|
|
108
|
-
//# sourceMappingURL=usePayment.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"usePayment.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/usePayment.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,kBAAkB,EAClB,cAAc,EACd,WAAW,EACX,SAAS,EACT,UAAU,EACV,aAAa,EACb,qBAAqB,EACrB,oBAAoB,EACrB,MAAM,qBAAqB,CAAC;AAM7B,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,gBAAgB,CAAC,EAAE,CAAC,MAAM,EAAE,oBAAoB,GAAG;QAAE,MAAM,EAAE,WAAW,CAAA;KAAE,KAAK,IAAI,CAAC;IACpF;;OAEG;IACH,eAAe,CAAC,EAAE,CAAC,MAAM,EAAE,oBAAoB,GAAG;QAAE,MAAM,EAAE,QAAQ,CAAA;KAAE,KAAK,IAAI,CAAC;IAChF;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;CACzC;AAED,MAAM,WAAW,gBAAgB;IAC/B,iFAAiF;IACjF,YAAY,EAAE,kBAAkB,GAAG,IAAI,CAAC;IACxC,+EAA+E;IAC/E,cAAc,EAAE,cAAc,GAAG,IAAI,CAAC;IACtC,4CAA4C;IAC5C,WAAW,EAAE,WAAW,GAAG,IAAI,CAAC;IAChC,yFAAyF;IACzF,YAAY,EAAE,OAAO,CAAC;IAC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,EAAE,MAAM,CAAC;KACrB,KAAK,OAAO,CAAC,SAAS,CAAC,CAAC;IACzB,4CAA4C;IAC5C,UAAU,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC,aAAa,CAAC,CAAC;CAC3D;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,UAAU,CAAC,OAAO,CAAC,EAAE,iBAAiB,GAAG,gBAAgB,CAsPxE"}
|
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
import { useState, useCallback, useRef, useEffect } from 'react';
|
|
2
|
-
import { useHeadlessClient } from '../provider.js';
|
|
3
|
-
import { tryClaimResume } from '../internal/redirectGuard.js';
|
|
4
|
-
/**
|
|
5
|
-
* React hook for payment processing.
|
|
6
|
-
*
|
|
7
|
-
* Handles the full payment lifecycle including 3DS redirects and return-URL detection.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```tsx
|
|
11
|
-
* const { processPayment, isProcessing, error } = usePayment({
|
|
12
|
-
* onPaymentSuccess: (result) => router.push('/confirmation'),
|
|
13
|
-
* onPaymentFailed: (result) => toast.error(result.error),
|
|
14
|
-
* });
|
|
15
|
-
*
|
|
16
|
-
* // On submit — one function handles everything
|
|
17
|
-
* const result = await processPayment({
|
|
18
|
-
* checkoutSessionId: session.id,
|
|
19
|
-
* tagadaToken,
|
|
20
|
-
* });
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export function usePayment(options) {
|
|
24
|
-
const client = useHeadlessClient();
|
|
25
|
-
const [paymentSetup, setPaymentSetup] = useState(null);
|
|
26
|
-
const [expressMethods, setExpressMethods] = useState(null);
|
|
27
|
-
const [storeConfig, setStoreConfig] = useState(null);
|
|
28
|
-
const [isProcessing, setIsProcessing] = useState(false);
|
|
29
|
-
const [result, setResult] = useState(null);
|
|
30
|
-
const [error, setError] = useState(null);
|
|
31
|
-
const loadedRef = useRef(false);
|
|
32
|
-
const redirectReturnProcessedRef = useRef(false);
|
|
33
|
-
const optionsRef = useRef(options);
|
|
34
|
-
optionsRef.current = options;
|
|
35
|
-
// ---------------------------------------------------------------------------
|
|
36
|
-
// Return-URL detection: auto-resume after 3DS / processor redirect
|
|
37
|
-
// ---------------------------------------------------------------------------
|
|
38
|
-
useEffect(() => {
|
|
39
|
-
if (typeof window === 'undefined')
|
|
40
|
-
return;
|
|
41
|
-
if (redirectReturnProcessedRef.current)
|
|
42
|
-
return;
|
|
43
|
-
const params = new URLSearchParams(window.location.search);
|
|
44
|
-
const paymentAction = params.get('paymentAction');
|
|
45
|
-
const paymentActionStatus = params.get('paymentActionStatus');
|
|
46
|
-
const paymentId = params.get('paymentId');
|
|
47
|
-
if (paymentAction !== 'requireAction' || paymentActionStatus !== 'completed' || !paymentId) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
redirectReturnProcessedRef.current = true;
|
|
51
|
-
if (!tryClaimResume(paymentId)) {
|
|
52
|
-
// Another hook (likely useOffers) already started the resume for this
|
|
53
|
-
// paymentId; don't double-poll or double-fire callbacks.
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
setIsProcessing(true);
|
|
57
|
-
setError(null);
|
|
58
|
-
const resume = async () => {
|
|
59
|
-
try {
|
|
60
|
-
const resumeResult = await client.payment.resumeAfterRedirect(paymentId);
|
|
61
|
-
// Clean payment-related query params from URL
|
|
62
|
-
const cleanParams = new URLSearchParams(window.location.search);
|
|
63
|
-
['paymentAction', 'paymentActionStatus', 'paymentId', 'payment_intent', 'payment_intent_client_secret', 'source_type', 'redirect_status'].forEach((p) => cleanParams.delete(p));
|
|
64
|
-
const newUrl = cleanParams.toString()
|
|
65
|
-
? `${window.location.pathname}?${cleanParams.toString()}`
|
|
66
|
-
: window.location.pathname;
|
|
67
|
-
window.history.replaceState({}, document.title, newUrl);
|
|
68
|
-
if (resumeResult.status === 'succeeded') {
|
|
69
|
-
optionsRef.current?.onPaymentSuccess?.(resumeResult);
|
|
70
|
-
}
|
|
71
|
-
else if (resumeResult.status === 'failed') {
|
|
72
|
-
setError(new Error(resumeResult.error));
|
|
73
|
-
optionsRef.current?.onPaymentFailed?.(resumeResult);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
catch (err) {
|
|
77
|
-
setError(err instanceof Error ? err : new Error(String(err)));
|
|
78
|
-
}
|
|
79
|
-
finally {
|
|
80
|
-
setIsProcessing(false);
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
void resume();
|
|
84
|
-
}, [client]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
85
|
-
// ---------------------------------------------------------------------------
|
|
86
|
-
// Load payment setup
|
|
87
|
-
// ---------------------------------------------------------------------------
|
|
88
|
-
const loadPaymentSetup = useCallback(async (checkoutSessionId, opts) => {
|
|
89
|
-
if (loadedRef.current)
|
|
90
|
-
return;
|
|
91
|
-
loadedRef.current = true;
|
|
92
|
-
const setupOpts = opts?.paymentSetupConfig
|
|
93
|
-
? { paymentSetupConfig: opts.paymentSetupConfig }
|
|
94
|
-
: optionsRef.current?.paymentSetupConfig
|
|
95
|
-
? { paymentSetupConfig: optionsRef.current.paymentSetupConfig }
|
|
96
|
-
: undefined;
|
|
97
|
-
try {
|
|
98
|
-
const [setup, express, config] = await Promise.all([
|
|
99
|
-
client.payment.getPaymentSetup(checkoutSessionId, setupOpts),
|
|
100
|
-
client.payment.getExpressMethods(checkoutSessionId, setupOpts),
|
|
101
|
-
client.payment.getStoreConfig(),
|
|
102
|
-
]);
|
|
103
|
-
setPaymentSetup(setup);
|
|
104
|
-
setExpressMethods(express);
|
|
105
|
-
setStoreConfig(config);
|
|
106
|
-
}
|
|
107
|
-
catch (err) {
|
|
108
|
-
loadedRef.current = false;
|
|
109
|
-
setError(err instanceof Error ? err : new Error(String(err)));
|
|
110
|
-
}
|
|
111
|
-
}, [client]);
|
|
112
|
-
// ---------------------------------------------------------------------------
|
|
113
|
-
// processPayment — high-level, handles everything
|
|
114
|
-
// ---------------------------------------------------------------------------
|
|
115
|
-
const processPayment = useCallback(async (opts) => {
|
|
116
|
-
setIsProcessing(true);
|
|
117
|
-
setError(null);
|
|
118
|
-
try {
|
|
119
|
-
const payResult = await client.payment.processPayment(opts);
|
|
120
|
-
if (payResult.status === 'requires_redirect') {
|
|
121
|
-
// Browser redirect — the hook will resume on return via the useEffect above
|
|
122
|
-
if (payResult.method === 'POST' && payResult.postData) {
|
|
123
|
-
const form = document.createElement('form');
|
|
124
|
-
form.method = 'POST';
|
|
125
|
-
form.action = payResult.redirectUrl;
|
|
126
|
-
for (const [key, value] of Object.entries(payResult.postData)) {
|
|
127
|
-
const input = document.createElement('input');
|
|
128
|
-
input.type = 'hidden';
|
|
129
|
-
input.name = key;
|
|
130
|
-
input.value = value;
|
|
131
|
-
form.appendChild(input);
|
|
132
|
-
}
|
|
133
|
-
document.body.appendChild(form);
|
|
134
|
-
form.submit();
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
window.location.href = payResult.redirectUrl;
|
|
138
|
-
}
|
|
139
|
-
// Don't setIsProcessing(false) — page is navigating away
|
|
140
|
-
return payResult;
|
|
141
|
-
}
|
|
142
|
-
if (payResult.status === 'succeeded') {
|
|
143
|
-
optionsRef.current?.onPaymentSuccess?.(payResult);
|
|
144
|
-
}
|
|
145
|
-
else if (payResult.status === 'failed') {
|
|
146
|
-
setError(new Error(payResult.error));
|
|
147
|
-
optionsRef.current?.onPaymentFailed?.(payResult);
|
|
148
|
-
}
|
|
149
|
-
return payResult;
|
|
150
|
-
}
|
|
151
|
-
catch (err) {
|
|
152
|
-
const e = err instanceof Error ? err : new Error(String(err));
|
|
153
|
-
setError(e);
|
|
154
|
-
return { status: 'failed', error: e.message };
|
|
155
|
-
}
|
|
156
|
-
finally {
|
|
157
|
-
setIsProcessing(false);
|
|
158
|
-
}
|
|
159
|
-
}, [client]);
|
|
160
|
-
// ---------------------------------------------------------------------------
|
|
161
|
-
// Low-level pay (raw API, no action handling)
|
|
162
|
-
// ---------------------------------------------------------------------------
|
|
163
|
-
const wrapAsync = useCallback((fn) => async () => {
|
|
164
|
-
setIsProcessing(true);
|
|
165
|
-
setError(null);
|
|
166
|
-
try {
|
|
167
|
-
const r = await fn();
|
|
168
|
-
return r;
|
|
169
|
-
}
|
|
170
|
-
catch (err) {
|
|
171
|
-
const e = err instanceof Error ? err : new Error(String(err));
|
|
172
|
-
setError(e);
|
|
173
|
-
throw e;
|
|
174
|
-
}
|
|
175
|
-
finally {
|
|
176
|
-
setIsProcessing(false);
|
|
177
|
-
}
|
|
178
|
-
}, []);
|
|
179
|
-
const pay = useCallback(async (opts) => {
|
|
180
|
-
setIsProcessing(true);
|
|
181
|
-
setError(null);
|
|
182
|
-
try {
|
|
183
|
-
const r = await client.payment.pay(opts);
|
|
184
|
-
setResult(r);
|
|
185
|
-
return r;
|
|
186
|
-
}
|
|
187
|
-
catch (err) {
|
|
188
|
-
const e = err instanceof Error ? err : new Error(String(err));
|
|
189
|
-
setError(e);
|
|
190
|
-
throw e;
|
|
191
|
-
}
|
|
192
|
-
finally {
|
|
193
|
-
setIsProcessing(false);
|
|
194
|
-
}
|
|
195
|
-
}, [client]);
|
|
196
|
-
const tokenizeCard = useCallback((card) => client.payment.tokenizeCard(card), [client]);
|
|
197
|
-
const processApplePay = useCallback(async (opts) => {
|
|
198
|
-
const fn = wrapAsync(() => client.payment.processApplePay(opts));
|
|
199
|
-
const r = await fn();
|
|
200
|
-
setResult(r);
|
|
201
|
-
return r;
|
|
202
|
-
}, [client, wrapAsync]);
|
|
203
|
-
const processGooglePay = useCallback(async (opts) => {
|
|
204
|
-
const fn = wrapAsync(() => client.payment.processGooglePay(opts));
|
|
205
|
-
const r = await fn();
|
|
206
|
-
setResult(r);
|
|
207
|
-
return r;
|
|
208
|
-
}, [client, wrapAsync]);
|
|
209
|
-
const processApm = useCallback(async (opts) => {
|
|
210
|
-
const fn = wrapAsync(() => client.payment.processApm(opts));
|
|
211
|
-
const r = await fn();
|
|
212
|
-
setResult(r);
|
|
213
|
-
return r;
|
|
214
|
-
}, [client, wrapAsync]);
|
|
215
|
-
const pollStatus = useCallback((paymentId) => client.payment.pollPaymentStatus(paymentId), [client]);
|
|
216
|
-
return {
|
|
217
|
-
paymentSetup,
|
|
218
|
-
expressMethods,
|
|
219
|
-
storeConfig,
|
|
220
|
-
isProcessing,
|
|
221
|
-
result,
|
|
222
|
-
error,
|
|
223
|
-
loadPaymentSetup,
|
|
224
|
-
processPayment,
|
|
225
|
-
pay,
|
|
226
|
-
tokenizeCard,
|
|
227
|
-
processApplePay,
|
|
228
|
-
processGooglePay,
|
|
229
|
-
processApm,
|
|
230
|
-
pollStatus,
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
//# sourceMappingURL=usePayment.js.map
|