@tagadapay/plugin-sdk 1.0.21 → 1.0.23
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.
|
@@ -95,5 +95,15 @@ export interface UseOffersResult {
|
|
|
95
95
|
* Get total savings across all offers
|
|
96
96
|
*/
|
|
97
97
|
getTotalSavings: () => number;
|
|
98
|
+
/**
|
|
99
|
+
* Pay for an offer with a checkout session
|
|
100
|
+
*/
|
|
101
|
+
payWithCheckoutSession: (checkoutSessionId: string, orderId?: string) => Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* Initialize a checkout session
|
|
104
|
+
*/
|
|
105
|
+
initCheckoutSession: (offerId: string, orderId: string) => Promise<{
|
|
106
|
+
checkoutSessionId: string;
|
|
107
|
+
}>;
|
|
98
108
|
}
|
|
99
109
|
export declare function useOffers(options: UseOffersOptions): UseOffersResult;
|
|
@@ -12,17 +12,16 @@ export function useOffers(options) {
|
|
|
12
12
|
setIsLoading(true);
|
|
13
13
|
setError(null);
|
|
14
14
|
try {
|
|
15
|
-
const
|
|
15
|
+
const responseData = await apiService.fetch(`/api/v1/stores/${apiService.getStoredStoreId()}/offers`, {
|
|
16
16
|
method: 'GET',
|
|
17
17
|
headers: {
|
|
18
18
|
'Content-Type': 'application/json',
|
|
19
19
|
},
|
|
20
20
|
});
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
setOffers(fetchedOffers);
|
|
21
|
+
const allOffers = responseData.offers || [];
|
|
22
|
+
// Filter offers by the provided offerIds if any are specified
|
|
23
|
+
const filteredOffers = offerIds.length > 0 ? allOffers.filter((offer) => offerIds.includes(offer.id)) : allOffers;
|
|
24
|
+
setOffers(filteredOffers);
|
|
26
25
|
}
|
|
27
26
|
catch (err) {
|
|
28
27
|
const error = err instanceof Error ? err : new Error('Failed to fetch offers');
|
|
@@ -32,9 +31,21 @@ export function useOffers(options) {
|
|
|
32
31
|
finally {
|
|
33
32
|
setIsLoading(false);
|
|
34
33
|
}
|
|
35
|
-
}, [environment.apiConfig.baseUrl, offerIds, enabled]);
|
|
34
|
+
}, [apiService, environment.apiConfig.baseUrl, offerIds, enabled]);
|
|
35
|
+
const initCheckoutSession = useCallback(async (offerId, orderId) => {
|
|
36
|
+
const response = await apiService.fetch(`/api/v1/checkout/offer/init`, {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
body: JSON.stringify({
|
|
39
|
+
offerId: offerId,
|
|
40
|
+
returnUrl: window.location.href,
|
|
41
|
+
customerId: '',
|
|
42
|
+
orderId,
|
|
43
|
+
}),
|
|
44
|
+
});
|
|
45
|
+
return response;
|
|
46
|
+
}, [apiService]);
|
|
36
47
|
const createCheckoutSession = useCallback(async (offerId, sessionOptions) => {
|
|
37
|
-
const response = await fetch(`${environment.apiConfig.baseUrl}/api/v1/offers/${offerId}/checkout`, {
|
|
48
|
+
const response = await apiService.fetch(`${environment.apiConfig.baseUrl}/api/v1/offers/${offerId}/checkout`, {
|
|
38
49
|
method: 'POST',
|
|
39
50
|
headers: {
|
|
40
51
|
'Content-Type': 'application/json',
|
|
@@ -43,13 +54,29 @@ export function useOffers(options) {
|
|
|
43
54
|
returnUrl: sessionOptions?.returnUrl || returnUrl || window.location.origin,
|
|
44
55
|
}),
|
|
45
56
|
});
|
|
46
|
-
if (!response.ok) {
|
|
47
|
-
throw new Error(`Failed to create checkout session: ${response.statusText}`);
|
|
48
|
-
}
|
|
49
57
|
return response.json();
|
|
50
58
|
}, [apiService, environment.apiConfig.baseUrl, returnUrl]);
|
|
51
|
-
const
|
|
52
|
-
const response = await fetch(
|
|
59
|
+
const payWithCheckoutSession = useCallback(async (checkoutSessionId, orderId) => {
|
|
60
|
+
const response = await apiService.fetch(`/api/v1/checkout-sessions/${checkoutSessionId}/pay`, {
|
|
61
|
+
method: 'POST',
|
|
62
|
+
body: JSON.stringify({
|
|
63
|
+
checkoutSessionId,
|
|
64
|
+
metadata: {
|
|
65
|
+
comingFromPostPurchase: true,
|
|
66
|
+
postOrder: orderId,
|
|
67
|
+
},
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
}, [apiService]);
|
|
71
|
+
const payOffer = useCallback(async (offerId, orderId) => {
|
|
72
|
+
const response = await apiService.fetch(`/api/v1/offers/${offerId}/pay`, {
|
|
73
|
+
body: {
|
|
74
|
+
offerId,
|
|
75
|
+
metadata: orderId && {
|
|
76
|
+
comingFromPostPurchase: true,
|
|
77
|
+
postOrder: orderId,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
53
80
|
method: 'POST',
|
|
54
81
|
headers: {
|
|
55
82
|
'Content-Type': 'application/json',
|
|
@@ -104,5 +131,7 @@ export function useOffers(options) {
|
|
|
104
131
|
getOffer,
|
|
105
132
|
getTotalValue,
|
|
106
133
|
getTotalSavings,
|
|
134
|
+
payWithCheckoutSession,
|
|
135
|
+
initCheckoutSession,
|
|
107
136
|
};
|
|
108
137
|
}
|