@tagadapay/plugin-sdk 1.0.23 → 1.0.26
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/dist/react/hooks/useCheckout.d.ts +57 -0
- package/dist/react/hooks/useCheckout.js +22 -3
- package/dist/react/hooks/usePostPurchases.d.ts +109 -0
- package/dist/react/hooks/usePostPurchases.js +104 -0
- package/dist/react/index.d.ts +16 -14
- package/dist/react/index.js +8 -7
- package/package.json +1 -1
|
@@ -135,6 +135,59 @@ export interface CheckoutSession {
|
|
|
135
135
|
adjustments: any;
|
|
136
136
|
}[];
|
|
137
137
|
}
|
|
138
|
+
export interface CheckoutSessionPreviewAdjustment {
|
|
139
|
+
type: 'Promotion' | 'Tax' | 'Shipping';
|
|
140
|
+
description: string;
|
|
141
|
+
level: 'Order' | 'LineItem';
|
|
142
|
+
sourceId: string;
|
|
143
|
+
amount: number;
|
|
144
|
+
currency: string;
|
|
145
|
+
}
|
|
146
|
+
export interface CheckoutSessionPreviewItem {
|
|
147
|
+
id: string;
|
|
148
|
+
productId: string;
|
|
149
|
+
product: {
|
|
150
|
+
name: string;
|
|
151
|
+
description: string;
|
|
152
|
+
};
|
|
153
|
+
variantId: string;
|
|
154
|
+
variant: {
|
|
155
|
+
name: string;
|
|
156
|
+
description: string;
|
|
157
|
+
imageUrl: string;
|
|
158
|
+
grams: number;
|
|
159
|
+
};
|
|
160
|
+
priceId: string;
|
|
161
|
+
sku: string;
|
|
162
|
+
unitAmount: number;
|
|
163
|
+
quantity: number;
|
|
164
|
+
amount: number;
|
|
165
|
+
adjustedAmount: number;
|
|
166
|
+
currency: string;
|
|
167
|
+
adjustments: CheckoutSessionPreviewAdjustment[];
|
|
168
|
+
recurring?: boolean;
|
|
169
|
+
rebillMode?: 'auth' | 'capture';
|
|
170
|
+
rebillStepIntervalMs?: number;
|
|
171
|
+
interval?: 'day' | 'week' | 'month' | 'year';
|
|
172
|
+
intervalCount?: number;
|
|
173
|
+
totalBillingCycles?: number;
|
|
174
|
+
unitAmountAfterFirstCycle?: number;
|
|
175
|
+
subscriptionSettings?: any;
|
|
176
|
+
orderLineItemProduct: {
|
|
177
|
+
name: string | null;
|
|
178
|
+
} | null;
|
|
179
|
+
orderLineItemVariant: {
|
|
180
|
+
name: string | null;
|
|
181
|
+
imageUrl: string | null;
|
|
182
|
+
} | null;
|
|
183
|
+
}
|
|
184
|
+
export interface CheckoutSessionPreview {
|
|
185
|
+
items: CheckoutSessionPreviewItem[];
|
|
186
|
+
adjustments: CheckoutSessionPreviewAdjustment[];
|
|
187
|
+
totalAmount: number;
|
|
188
|
+
totalAdjustedAmount: number;
|
|
189
|
+
totalPromotionAmount: number;
|
|
190
|
+
}
|
|
138
191
|
export interface CheckoutSummary {
|
|
139
192
|
currency: string;
|
|
140
193
|
totalAmount: number;
|
|
@@ -279,6 +332,10 @@ export interface UseCheckoutResult {
|
|
|
279
332
|
currency: string;
|
|
280
333
|
error?: any;
|
|
281
334
|
}>;
|
|
335
|
+
previewCheckoutSession: (lineItems: CheckoutLineItem[], promotionIds?: string[]) => Promise<{
|
|
336
|
+
success: boolean;
|
|
337
|
+
preview: CheckoutSessionPreview;
|
|
338
|
+
}>;
|
|
282
339
|
clear: () => void;
|
|
283
340
|
}
|
|
284
341
|
export declare function useCheckout(options?: UseCheckoutOptions): UseCheckoutResult;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { useCurrency } from '../hooks/useCurrency';
|
|
2
3
|
import { useTagadaContext } from '../providers/TagadaProvider';
|
|
3
4
|
import { getCheckoutToken } from '../utils/urlUtils';
|
|
4
|
-
import { useCurrency } from '../hooks/useCurrency';
|
|
5
5
|
export function useCheckout(options = {}) {
|
|
6
|
-
const { apiService, updateCheckoutDebugData, refreshCoordinator } = useTagadaContext();
|
|
6
|
+
const { apiService, updateCheckoutDebugData, refreshCoordinator, store, currency } = useTagadaContext();
|
|
7
7
|
const { code: currentCurrency } = useCurrency();
|
|
8
8
|
const [checkout, setCheckout] = useState(null);
|
|
9
9
|
const [isLoading, setIsLoading] = useState(false);
|
|
@@ -409,6 +409,24 @@ export function useCheckout(options = {}) {
|
|
|
409
409
|
};
|
|
410
410
|
}
|
|
411
411
|
}, [apiService, checkout?.checkoutSession.id, checkout?.summary?.currency]);
|
|
412
|
+
const previewCheckoutSession = useCallback(async (lineItems, promotionIds) => {
|
|
413
|
+
try {
|
|
414
|
+
const response = await apiService.fetch('/api/v1/checkout-sessions/preview', {
|
|
415
|
+
method: 'POST',
|
|
416
|
+
body: {
|
|
417
|
+
lineItems,
|
|
418
|
+
storeId: store?.id,
|
|
419
|
+
currency: currency.code,
|
|
420
|
+
promotionIds: promotionIds || [],
|
|
421
|
+
},
|
|
422
|
+
});
|
|
423
|
+
return response;
|
|
424
|
+
}
|
|
425
|
+
catch (err) {
|
|
426
|
+
const error = err instanceof Error ? err : new Error('Failed to preview checkout session');
|
|
427
|
+
throw error;
|
|
428
|
+
}
|
|
429
|
+
}, [apiService, checkout?.checkoutSession.storeId, checkout?.summary?.currency]);
|
|
412
430
|
const clear = useCallback(() => {
|
|
413
431
|
setCheckout(null);
|
|
414
432
|
setError(null);
|
|
@@ -465,6 +483,7 @@ export function useCheckout(options = {}) {
|
|
|
465
483
|
updateCustomer,
|
|
466
484
|
updateCustomerAndSessionInfo,
|
|
467
485
|
previewOrderSummary,
|
|
486
|
+
previewCheckoutSession,
|
|
468
487
|
clear,
|
|
469
488
|
};
|
|
470
489
|
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
export interface PostPurchaseOfferItem {
|
|
2
|
+
id: string;
|
|
3
|
+
productId: string;
|
|
4
|
+
variantId: string;
|
|
5
|
+
product: {
|
|
6
|
+
name: string;
|
|
7
|
+
description: string;
|
|
8
|
+
};
|
|
9
|
+
variant: {
|
|
10
|
+
name: string;
|
|
11
|
+
description: string;
|
|
12
|
+
imageUrl: string;
|
|
13
|
+
grams: number | null;
|
|
14
|
+
};
|
|
15
|
+
unitAmount: number;
|
|
16
|
+
quantity: number;
|
|
17
|
+
amount: number;
|
|
18
|
+
adjustedAmount: number;
|
|
19
|
+
}
|
|
20
|
+
export interface PostPurchaseOfferSummary {
|
|
21
|
+
currency: string;
|
|
22
|
+
totalAmount: number;
|
|
23
|
+
totalAdjustedAmount: number;
|
|
24
|
+
items: PostPurchaseOfferItem[];
|
|
25
|
+
}
|
|
26
|
+
export interface PostPurchaseOfferLineItem {
|
|
27
|
+
id: string;
|
|
28
|
+
quantity: number;
|
|
29
|
+
price: {
|
|
30
|
+
variant: {
|
|
31
|
+
id: string;
|
|
32
|
+
name: string;
|
|
33
|
+
description: string | null;
|
|
34
|
+
grams: number | null;
|
|
35
|
+
product: {
|
|
36
|
+
id: string;
|
|
37
|
+
name: string;
|
|
38
|
+
description: string;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export interface PostPurchaseOffer {
|
|
44
|
+
id: string;
|
|
45
|
+
titleTrans: Record<string, string> | null;
|
|
46
|
+
summaries: PostPurchaseOfferSummary[];
|
|
47
|
+
offerLineItems: PostPurchaseOfferLineItem[];
|
|
48
|
+
}
|
|
49
|
+
export interface UsePostPurchasesOptions {
|
|
50
|
+
/**
|
|
51
|
+
* OrderID to fetch post-purchase offers for
|
|
52
|
+
*/
|
|
53
|
+
orderId: string;
|
|
54
|
+
/**
|
|
55
|
+
* Whether to fetch offers automatically on mount
|
|
56
|
+
* @default true
|
|
57
|
+
*/
|
|
58
|
+
enabled?: boolean;
|
|
59
|
+
}
|
|
60
|
+
export interface UsePostPurchasesResult {
|
|
61
|
+
/**
|
|
62
|
+
* Array of fetched post-purchase offers
|
|
63
|
+
*/
|
|
64
|
+
offers: PostPurchaseOffer[];
|
|
65
|
+
/**
|
|
66
|
+
* Loading state
|
|
67
|
+
*/
|
|
68
|
+
isLoading: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Error state
|
|
71
|
+
*/
|
|
72
|
+
error: Error | null;
|
|
73
|
+
/**
|
|
74
|
+
* Refetch post-purchase offers
|
|
75
|
+
*/
|
|
76
|
+
refetch: () => Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Get offer by ID from the loaded offers
|
|
79
|
+
*/
|
|
80
|
+
getOffer: (offerId: string) => PostPurchaseOffer | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* Get total value of all post-purchase offers
|
|
83
|
+
*/
|
|
84
|
+
getTotalValue: () => number;
|
|
85
|
+
/**
|
|
86
|
+
* Get total savings across all post-purchase offers
|
|
87
|
+
*/
|
|
88
|
+
getTotalSavings: () => number;
|
|
89
|
+
/**
|
|
90
|
+
* Initialize a checkout session for a post-purchase offer
|
|
91
|
+
*/
|
|
92
|
+
initCheckoutSession: (offerId: string, orderId: string) => Promise<{
|
|
93
|
+
checkoutSessionId: string;
|
|
94
|
+
}>;
|
|
95
|
+
/**
|
|
96
|
+
* Initialize a checkout session for a post-purchase offer with specific variants
|
|
97
|
+
*/
|
|
98
|
+
initCheckoutSessionWithVariants: (offerId: string, orderId: string, lineItems: Array<{
|
|
99
|
+
variantId: string;
|
|
100
|
+
quantity: number;
|
|
101
|
+
}>) => Promise<{
|
|
102
|
+
checkoutSessionId: string;
|
|
103
|
+
}>;
|
|
104
|
+
/**
|
|
105
|
+
* Pay with a checkout session for a post-purchase offer
|
|
106
|
+
*/
|
|
107
|
+
payWithCheckoutSession: (checkoutSessionId: string, orderId?: string) => Promise<void>;
|
|
108
|
+
}
|
|
109
|
+
export declare function usePostPurchases(options: UsePostPurchasesOptions): UsePostPurchasesResult;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
2
|
+
import { useTagadaContext } from '../providers/TagadaProvider';
|
|
3
|
+
export function usePostPurchases(options) {
|
|
4
|
+
const { apiService } = useTagadaContext();
|
|
5
|
+
const { orderId, enabled = true } = options;
|
|
6
|
+
const [offers, setOffers] = useState([]);
|
|
7
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
8
|
+
const [error, setError] = useState(null);
|
|
9
|
+
const fetchOffers = useCallback(async () => {
|
|
10
|
+
if (!orderId) {
|
|
11
|
+
setOffers([]);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
setIsLoading(true);
|
|
15
|
+
setError(null);
|
|
16
|
+
try {
|
|
17
|
+
const response = await apiService.fetch(`/api/v1/post-purchase/${orderId}/offers`, {
|
|
18
|
+
method: 'GET',
|
|
19
|
+
});
|
|
20
|
+
setOffers(response || []);
|
|
21
|
+
}
|
|
22
|
+
catch (err) {
|
|
23
|
+
const error = err instanceof Error ? err : new Error('Failed to fetch post-purchase offers');
|
|
24
|
+
setError(error);
|
|
25
|
+
console.error('[SDK] Failed to fetch post-purchase offers:', error);
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
setIsLoading(false);
|
|
29
|
+
}
|
|
30
|
+
}, [orderId]);
|
|
31
|
+
const initCheckoutSession = useCallback(async (offerId, orderId) => {
|
|
32
|
+
const response = await apiService.fetch(`/api/v1/checkout/offer/init`, {
|
|
33
|
+
method: 'POST',
|
|
34
|
+
body: JSON.stringify({
|
|
35
|
+
offerId: offerId,
|
|
36
|
+
returnUrl: window.location.href,
|
|
37
|
+
customerId: '',
|
|
38
|
+
orderId,
|
|
39
|
+
}),
|
|
40
|
+
});
|
|
41
|
+
return response;
|
|
42
|
+
}, []);
|
|
43
|
+
const initCheckoutSessionWithVariants = useCallback(async (offerId, orderId, lineItems) => {
|
|
44
|
+
const response = await apiService.fetch(`/api/v1/offers/${offerId}/transform-to-checkout`, {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
body: JSON.stringify({
|
|
47
|
+
offerId: offerId,
|
|
48
|
+
lineItems: lineItems,
|
|
49
|
+
returnUrl: window.location.href,
|
|
50
|
+
mainOrderId: orderId,
|
|
51
|
+
}),
|
|
52
|
+
});
|
|
53
|
+
return response;
|
|
54
|
+
}, []);
|
|
55
|
+
const payWithCheckoutSession = useCallback(async (checkoutSessionId, orderId) => {
|
|
56
|
+
const response = await apiService.fetch(`/api/v1/checkout-sessions/${checkoutSessionId}/pay`, {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
body: JSON.stringify({
|
|
59
|
+
checkoutSessionId,
|
|
60
|
+
metadata: {
|
|
61
|
+
comingFromPostPurchase: true,
|
|
62
|
+
postOrder: orderId,
|
|
63
|
+
},
|
|
64
|
+
}),
|
|
65
|
+
});
|
|
66
|
+
return response;
|
|
67
|
+
}, []);
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
if (enabled && orderId) {
|
|
70
|
+
fetchOffers();
|
|
71
|
+
}
|
|
72
|
+
}, [enabled, orderId, fetchOffers]);
|
|
73
|
+
const getOffer = useCallback((offerId) => {
|
|
74
|
+
return offers.find((offer) => offer.id === offerId);
|
|
75
|
+
}, [offers]);
|
|
76
|
+
const getTotalValue = useCallback(() => {
|
|
77
|
+
return offers.reduce((total, offer) => {
|
|
78
|
+
return (total +
|
|
79
|
+
offer.summaries.reduce((summaryTotal, summary) => {
|
|
80
|
+
return summaryTotal + summary.totalAmount;
|
|
81
|
+
}, 0));
|
|
82
|
+
}, 0);
|
|
83
|
+
}, [offers]);
|
|
84
|
+
const getTotalSavings = useCallback(() => {
|
|
85
|
+
return offers.reduce((total, offer) => {
|
|
86
|
+
return (total +
|
|
87
|
+
offer.summaries.reduce((summaryTotal, summary) => {
|
|
88
|
+
return summaryTotal + (summary.totalAmount - summary.totalAdjustedAmount);
|
|
89
|
+
}, 0));
|
|
90
|
+
}, 0);
|
|
91
|
+
}, [offers]);
|
|
92
|
+
return {
|
|
93
|
+
offers,
|
|
94
|
+
isLoading,
|
|
95
|
+
error,
|
|
96
|
+
refetch: fetchOffers,
|
|
97
|
+
getOffer,
|
|
98
|
+
getTotalValue,
|
|
99
|
+
getTotalSavings,
|
|
100
|
+
initCheckoutSession,
|
|
101
|
+
initCheckoutSessionWithVariants,
|
|
102
|
+
payWithCheckoutSession,
|
|
103
|
+
};
|
|
104
|
+
}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -2,28 +2,30 @@
|
|
|
2
2
|
* React SDK exports
|
|
3
3
|
*/
|
|
4
4
|
export { TagadaProvider } from './providers/TagadaProvider';
|
|
5
|
+
export { useAddress } from './hooks/useAddress';
|
|
6
|
+
export { useAuth } from './hooks/useAuth';
|
|
5
7
|
export { useCheckout } from './hooks/useCheckout';
|
|
6
|
-
export { useProducts } from './hooks/useProducts';
|
|
7
|
-
export { useOffers } from './hooks/useOffers';
|
|
8
|
-
export { useOrderBump } from './hooks/useOrderBump';
|
|
9
|
-
export { useSession } from './hooks/useSession';
|
|
10
8
|
export { useCurrency } from './hooks/useCurrency';
|
|
11
9
|
export { useCustomer } from './hooks/useCustomer';
|
|
12
10
|
export { useEnvironment } from './hooks/useEnvironment';
|
|
13
11
|
export { useLocale } from './hooks/useLocale';
|
|
14
|
-
export {
|
|
15
|
-
export {
|
|
12
|
+
export { useOffers } from './hooks/useOffers';
|
|
13
|
+
export { useOrderBump } from './hooks/useOrderBump';
|
|
14
|
+
export { usePostPurchases } from './hooks/usePostPurchases';
|
|
15
|
+
export { useProducts } from './hooks/useProducts';
|
|
16
|
+
export { useSession } from './hooks/useSession';
|
|
16
17
|
export { useOrder } from './hooks/useOrder';
|
|
17
18
|
export type { UseOrderOptions, UseOrderResult } from './hooks/useOrder';
|
|
18
19
|
export { usePayment } from './hooks/usePayment';
|
|
19
20
|
export { usePaymentPolling } from './hooks/usePaymentPolling';
|
|
20
21
|
export { useThreeds } from './hooks/useThreeds';
|
|
21
22
|
export { useThreedsModal } from './hooks/useThreedsModal';
|
|
22
|
-
export type {
|
|
23
|
-
export type {
|
|
24
|
-
export type { UseOrderBumpOptions, UseOrderBumpResult
|
|
25
|
-
export type {
|
|
26
|
-
export type {
|
|
27
|
-
export type {
|
|
28
|
-
export type {
|
|
29
|
-
export {
|
|
23
|
+
export type { AuthState, Currency, Customer, Environment, EnvironmentConfig, Locale, Order, OrderAddress, OrderItem, OrderSummary, PickupPoint, Session, Store, } from './types';
|
|
24
|
+
export type { CheckoutData, CheckoutInitParams, CheckoutLineItem, CheckoutSession, Promotion, UseCheckoutOptions, UseCheckoutResult, } from './hooks/useCheckout';
|
|
25
|
+
export type { OrderBumpPreview, UseOrderBumpOptions, UseOrderBumpResult } from './hooks/useOrderBump';
|
|
26
|
+
export type { PostPurchaseOffer, PostPurchaseOfferItem, PostPurchaseOfferLineItem, PostPurchaseOfferSummary, UsePostPurchasesOptions, UsePostPurchasesResult, } from './hooks/usePostPurchases';
|
|
27
|
+
export type { AddressData, AddressField, Country, State, UseAddressOptions, UseAddressResult, } from './hooks/useAddress';
|
|
28
|
+
export type { Payment, PaymentPollingHook, PollingOptions } from './hooks/usePaymentPolling';
|
|
29
|
+
export type { PaymentInstrument, ThreedsChallenge, ThreedsHook, ThreedsOptions, ThreedsProvider, ThreedsSession, } from './hooks/useThreeds';
|
|
30
|
+
export type { ApplePayToken, CardPaymentMethod, PaymentHook, PaymentInstrumentResponse, PaymentOptions, PaymentResponse, } from './hooks/usePayment';
|
|
31
|
+
export { convertCurrency, formatMoney, formatMoneyWithoutSymbol, formatSimpleMoney, getCurrencyInfo, minorUnitsToMajorUnits, moneyStringOrNumberToMinorUnits, } from './utils/money';
|
package/dist/react/index.js
CHANGED
|
@@ -5,17 +5,18 @@
|
|
|
5
5
|
// Provider exports
|
|
6
6
|
export { TagadaProvider } from './providers/TagadaProvider';
|
|
7
7
|
// Hook exports
|
|
8
|
+
export { useAddress } from './hooks/useAddress';
|
|
9
|
+
export { useAuth } from './hooks/useAuth';
|
|
8
10
|
export { useCheckout } from './hooks/useCheckout';
|
|
9
|
-
export { useProducts } from './hooks/useProducts';
|
|
10
|
-
export { useOffers } from './hooks/useOffers';
|
|
11
|
-
export { useOrderBump } from './hooks/useOrderBump';
|
|
12
|
-
export { useSession } from './hooks/useSession';
|
|
13
11
|
export { useCurrency } from './hooks/useCurrency';
|
|
14
12
|
export { useCustomer } from './hooks/useCustomer';
|
|
15
13
|
export { useEnvironment } from './hooks/useEnvironment';
|
|
16
14
|
export { useLocale } from './hooks/useLocale';
|
|
17
|
-
export {
|
|
18
|
-
export {
|
|
15
|
+
export { useOffers } from './hooks/useOffers';
|
|
16
|
+
export { useOrderBump } from './hooks/useOrderBump';
|
|
17
|
+
export { usePostPurchases } from './hooks/usePostPurchases';
|
|
18
|
+
export { useProducts } from './hooks/useProducts';
|
|
19
|
+
export { useSession } from './hooks/useSession';
|
|
19
20
|
// Order hook exports
|
|
20
21
|
export { useOrder } from './hooks/useOrder';
|
|
21
22
|
// Payment hooks exports
|
|
@@ -26,4 +27,4 @@ export { useThreedsModal } from './hooks/useThreedsModal';
|
|
|
26
27
|
// Component exports (if any)
|
|
27
28
|
// export { SomeComponent } from './components/SomeComponent';
|
|
28
29
|
// Utility exports
|
|
29
|
-
export { formatMoney, formatMoneyWithoutSymbol, getCurrencyInfo, minorUnitsToMajorUnits, moneyStringOrNumberToMinorUnits,
|
|
30
|
+
export { convertCurrency, formatMoney, formatMoneyWithoutSymbol, formatSimpleMoney, getCurrencyInfo, minorUnitsToMajorUnits, moneyStringOrNumberToMinorUnits, } from './utils/money';
|