@tagadapay/plugin-sdk 2.4.29 → 2.4.31
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/useCustomerInfos.d.ts +15 -0
- package/dist/react/hooks/useCustomerInfos.js +54 -0
- package/dist/react/hooks/useCustomerSubscriptions.d.ts +56 -0
- package/dist/react/hooks/useCustomerSubscriptions.js +77 -0
- package/dist/react/hooks/useLogin.js +0 -1
- package/dist/react/index.d.ts +3 -1
- package/dist/react/index.js +2 -0
- package/dist/react/types.d.ts +66 -0
- package/dist/react/utils/tokenStorage.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CustomerInfos } from '../types';
|
|
2
|
+
export interface UseCustomerInfosOptions {
|
|
3
|
+
customerId?: string | null;
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export interface UseCustomerInfosResult {
|
|
7
|
+
data: CustomerInfos | null;
|
|
8
|
+
isLoading: boolean;
|
|
9
|
+
error: Error | null;
|
|
10
|
+
refetch: () => Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* useCustomerInfos - Fetches customer infos from `/api/v1/customers/{customerId}` with `storeId` param
|
|
14
|
+
*/
|
|
15
|
+
export declare function useCustomerInfos(options: UseCustomerInfosOptions): UseCustomerInfosResult;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
3
|
+
import { useTagadaContext } from '../providers/TagadaProvider';
|
|
4
|
+
import { usePluginConfig } from './usePluginConfig';
|
|
5
|
+
/**
|
|
6
|
+
* useCustomerInfos - Fetches customer infos from `/api/v1/customers/{customerId}` with `storeId` param
|
|
7
|
+
*/
|
|
8
|
+
export function useCustomerInfos(options) {
|
|
9
|
+
const { apiService } = useTagadaContext();
|
|
10
|
+
const { storeId } = usePluginConfig();
|
|
11
|
+
const stableOptions = useMemo(() => {
|
|
12
|
+
return {
|
|
13
|
+
customerId: options.customerId ?? null,
|
|
14
|
+
enabled: options.enabled ?? true,
|
|
15
|
+
};
|
|
16
|
+
}, [options.customerId, options.enabled]);
|
|
17
|
+
const isEnabled = useMemo(() => {
|
|
18
|
+
return Boolean(stableOptions.enabled && stableOptions.customerId && storeId);
|
|
19
|
+
}, [stableOptions.enabled, stableOptions.customerId, storeId]);
|
|
20
|
+
const [data, setData] = useState(null);
|
|
21
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
22
|
+
const [error, setError] = useState(null);
|
|
23
|
+
const fetchCustomerInfos = useCallback(async () => {
|
|
24
|
+
if (!isEnabled)
|
|
25
|
+
return;
|
|
26
|
+
if (!stableOptions.customerId || !storeId)
|
|
27
|
+
return;
|
|
28
|
+
setIsLoading(true);
|
|
29
|
+
setError(null);
|
|
30
|
+
try {
|
|
31
|
+
const response = await apiService.fetch(`/api/v1/customers/${stableOptions.customerId}`, {
|
|
32
|
+
method: 'GET',
|
|
33
|
+
params: { storeId },
|
|
34
|
+
});
|
|
35
|
+
setData(response ?? null);
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
const safeError = err instanceof Error ? err : new Error('Failed to fetch customer infos');
|
|
39
|
+
setError(safeError);
|
|
40
|
+
}
|
|
41
|
+
finally {
|
|
42
|
+
setIsLoading(false);
|
|
43
|
+
}
|
|
44
|
+
}, [apiService, isEnabled, stableOptions.customerId, storeId]);
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
void fetchCustomerInfos();
|
|
47
|
+
}, [fetchCustomerInfos]);
|
|
48
|
+
return {
|
|
49
|
+
data,
|
|
50
|
+
isLoading,
|
|
51
|
+
error,
|
|
52
|
+
refetch: fetchCustomerInfos,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export interface Subscription {
|
|
2
|
+
id: string;
|
|
3
|
+
status: string;
|
|
4
|
+
createdAt: string;
|
|
5
|
+
currency: string;
|
|
6
|
+
cancelAtPeriodEnd: boolean;
|
|
7
|
+
currentPeriodEnd: string | null;
|
|
8
|
+
currentPeriodStart: string | null;
|
|
9
|
+
quantity: number;
|
|
10
|
+
trialEnd: string | null;
|
|
11
|
+
customerId: string;
|
|
12
|
+
customerEmail: string;
|
|
13
|
+
customerName: string;
|
|
14
|
+
priceCurrencyOptions: Record<string, {
|
|
15
|
+
rate: number;
|
|
16
|
+
amount: number;
|
|
17
|
+
lock: boolean;
|
|
18
|
+
date: string;
|
|
19
|
+
}>;
|
|
20
|
+
priceInterval: string;
|
|
21
|
+
priceIntervalCount: number;
|
|
22
|
+
priceRecurring: boolean;
|
|
23
|
+
productId: string;
|
|
24
|
+
priceId: string;
|
|
25
|
+
productTitle: string;
|
|
26
|
+
}
|
|
27
|
+
export interface SubscriptionsResponse {
|
|
28
|
+
items: Subscription[];
|
|
29
|
+
pagination: {
|
|
30
|
+
page: number;
|
|
31
|
+
pageSize: number;
|
|
32
|
+
hasNext: boolean;
|
|
33
|
+
nextPage: number | null;
|
|
34
|
+
previousPage: number | null;
|
|
35
|
+
totalItems: number;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export interface UseCustomerSubscriptionsOptions {
|
|
39
|
+
customerId?: string | null;
|
|
40
|
+
enabled?: boolean;
|
|
41
|
+
}
|
|
42
|
+
export interface UseCustomerSubscriptionsResult {
|
|
43
|
+
data: SubscriptionsResponse | null;
|
|
44
|
+
isLoading: boolean;
|
|
45
|
+
error: Error | null;
|
|
46
|
+
refetch: () => Promise<void>;
|
|
47
|
+
resumeSubscription: (subscriptionId: string) => Promise<{
|
|
48
|
+
success: boolean;
|
|
49
|
+
error?: string;
|
|
50
|
+
}>;
|
|
51
|
+
cancelSubscription: (subscriptionId: string) => Promise<{
|
|
52
|
+
success: boolean;
|
|
53
|
+
error?: string;
|
|
54
|
+
}>;
|
|
55
|
+
}
|
|
56
|
+
export declare function useCustomerSubscriptions(options: UseCustomerSubscriptionsOptions): UseCustomerSubscriptionsResult;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
3
|
+
import { useTagadaContext } from '../providers/TagadaProvider';
|
|
4
|
+
export function useCustomerSubscriptions(options) {
|
|
5
|
+
const { apiService } = useTagadaContext();
|
|
6
|
+
const stableOptions = useMemo(() => {
|
|
7
|
+
return {
|
|
8
|
+
customerId: options.customerId ?? null,
|
|
9
|
+
enabled: options.enabled ?? true,
|
|
10
|
+
};
|
|
11
|
+
}, [options.customerId, options.enabled]);
|
|
12
|
+
const isEnabled = useMemo(() => {
|
|
13
|
+
return Boolean(stableOptions.enabled && stableOptions.customerId);
|
|
14
|
+
}, [stableOptions.enabled, stableOptions.customerId]);
|
|
15
|
+
const [data, setData] = useState(null);
|
|
16
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
17
|
+
const [error, setError] = useState(null);
|
|
18
|
+
const fetchSubscriptions = useCallback(async () => {
|
|
19
|
+
if (!isEnabled)
|
|
20
|
+
return;
|
|
21
|
+
setIsLoading(true);
|
|
22
|
+
setError(null);
|
|
23
|
+
try {
|
|
24
|
+
// Token-authenticated request; backend infers customer from token
|
|
25
|
+
const response = await apiService.fetch(`/api/v1/subscriptions`, {
|
|
26
|
+
method: 'GET',
|
|
27
|
+
});
|
|
28
|
+
setData(response ?? null);
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
const safeError = err instanceof Error ? err : new Error('Failed to fetch subscriptions');
|
|
32
|
+
setError(safeError);
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
setIsLoading(false);
|
|
36
|
+
}
|
|
37
|
+
}, [apiService, isEnabled]);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
void fetchSubscriptions();
|
|
40
|
+
}, [fetchSubscriptions]);
|
|
41
|
+
const resumeSubscription = useCallback(async (subscriptionId) => {
|
|
42
|
+
try {
|
|
43
|
+
await apiService.fetch(`/api/v1/subscriptions/resume`, {
|
|
44
|
+
method: 'POST',
|
|
45
|
+
body: { subscriptionId },
|
|
46
|
+
});
|
|
47
|
+
await fetchSubscriptions();
|
|
48
|
+
return { success: true };
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
const errorMessage = err instanceof Error ? err.message : 'Failed to resume subscription';
|
|
52
|
+
return { success: false, error: errorMessage };
|
|
53
|
+
}
|
|
54
|
+
}, [apiService, fetchSubscriptions]);
|
|
55
|
+
const cancelSubscription = useCallback(async (subscriptionId) => {
|
|
56
|
+
try {
|
|
57
|
+
await apiService.fetch(`/api/v1/subscriptions/cancel`, {
|
|
58
|
+
method: 'POST',
|
|
59
|
+
body: { subscriptionId },
|
|
60
|
+
});
|
|
61
|
+
await fetchSubscriptions();
|
|
62
|
+
return { success: true };
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
const errorMessage = err instanceof Error ? err.message : 'Failed to cancel subscription';
|
|
66
|
+
return { success: false, error: errorMessage };
|
|
67
|
+
}
|
|
68
|
+
}, [apiService, fetchSubscriptions]);
|
|
69
|
+
return {
|
|
70
|
+
data,
|
|
71
|
+
isLoading,
|
|
72
|
+
error,
|
|
73
|
+
refetch: fetchSubscriptions,
|
|
74
|
+
resumeSubscription,
|
|
75
|
+
cancelSubscription,
|
|
76
|
+
};
|
|
77
|
+
}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ export { useCheckout } from './hooks/useCheckout';
|
|
|
7
7
|
export { useClubOffers } from './hooks/useClubOffers';
|
|
8
8
|
export { useCurrency } from './hooks/useCurrency';
|
|
9
9
|
export { useCustomer } from './hooks/useCustomer';
|
|
10
|
+
export { useCustomerInfos } from './hooks/useCustomerInfos';
|
|
11
|
+
export { useCustomerSubscriptions } from './hooks/useCustomerSubscriptions';
|
|
10
12
|
export { useDiscounts } from './hooks/useDiscounts';
|
|
11
13
|
export { useEnvironment } from './hooks/useEnvironment';
|
|
12
14
|
export { useGeoLocation } from './hooks/useGeoLocation';
|
|
@@ -37,7 +39,7 @@ export { useThreeds } from './hooks/useThreeds';
|
|
|
37
39
|
export { useThreedsModal } from './hooks/useThreedsModal';
|
|
38
40
|
export { useApplePay } from './hooks/useApplePay';
|
|
39
41
|
export { ExpressPaymentProvider, useExpressPayment } from './hooks/useExpressPayment';
|
|
40
|
-
export type { AuthState, Currency, Customer, Environment, EnvironmentConfig, Locale, Order, OrderAddress, OrderItem, OrderSummary, PickupPoint, Session, Store } from './types';
|
|
42
|
+
export type { AuthState, Currency, Customer, CustomerInfos, Environment, EnvironmentConfig, Locale, Order, OrderAddress, OrderItem, OrderSummary, PickupPoint, Session, Store } from './types';
|
|
41
43
|
export type { CheckoutData, CheckoutInitParams, CheckoutLineItem, CheckoutSession, CheckoutSessionPreview, Promotion, UseCheckoutOptions, UseCheckoutResult } from './hooks/useCheckout';
|
|
42
44
|
export type { Discount, DiscountCodeValidation, UseDiscountsOptions, UseDiscountsResult } from './hooks/useDiscounts';
|
|
43
45
|
export type { OrderBumpPreview, UseOrderBumpOptions, UseOrderBumpResult } from './hooks/useOrderBump';
|
package/dist/react/index.js
CHANGED
|
@@ -10,6 +10,8 @@ export { useCheckout } from './hooks/useCheckout';
|
|
|
10
10
|
export { useClubOffers } from './hooks/useClubOffers';
|
|
11
11
|
export { useCurrency } from './hooks/useCurrency';
|
|
12
12
|
export { useCustomer } from './hooks/useCustomer';
|
|
13
|
+
export { useCustomerInfos } from './hooks/useCustomerInfos';
|
|
14
|
+
export { useCustomerSubscriptions } from './hooks/useCustomerSubscriptions';
|
|
13
15
|
export { useDiscounts } from './hooks/useDiscounts';
|
|
14
16
|
export { useEnvironment } from './hooks/useEnvironment';
|
|
15
17
|
export { useGeoLocation } from './hooks/useGeoLocation';
|
package/dist/react/types.d.ts
CHANGED
|
@@ -150,3 +150,69 @@ export interface Order {
|
|
|
150
150
|
};
|
|
151
151
|
relatedOrders?: Order[];
|
|
152
152
|
}
|
|
153
|
+
export interface CustomerAddress {
|
|
154
|
+
company?: string;
|
|
155
|
+
firstName: string;
|
|
156
|
+
lastName: string;
|
|
157
|
+
address1: string;
|
|
158
|
+
city: string;
|
|
159
|
+
country: string;
|
|
160
|
+
state: string;
|
|
161
|
+
postal: string;
|
|
162
|
+
phone?: string;
|
|
163
|
+
email?: string;
|
|
164
|
+
}
|
|
165
|
+
export interface CustomerOrderSummary {
|
|
166
|
+
id: string;
|
|
167
|
+
storeId: string;
|
|
168
|
+
accountId: string;
|
|
169
|
+
createdAt: string;
|
|
170
|
+
updatedAt: string;
|
|
171
|
+
status: string;
|
|
172
|
+
cancelledAt: string | null;
|
|
173
|
+
cancelledReason: string | null;
|
|
174
|
+
paidAt: string | null;
|
|
175
|
+
paidAmount: number | null;
|
|
176
|
+
openAt: string | null;
|
|
177
|
+
abandonedAt: string | null;
|
|
178
|
+
currency: string;
|
|
179
|
+
externalCustomerType: string | null;
|
|
180
|
+
externalCustomerId: string | null;
|
|
181
|
+
externalOrderId: string | null;
|
|
182
|
+
billingAddress: CustomerAddress;
|
|
183
|
+
shippingAddress: Omit<CustomerAddress, 'email'>;
|
|
184
|
+
pickupAddress: any | null;
|
|
185
|
+
taxesIncluded: boolean;
|
|
186
|
+
draft: boolean;
|
|
187
|
+
checkoutSessionId: string | null;
|
|
188
|
+
sessionHash: string | null;
|
|
189
|
+
customerId: string;
|
|
190
|
+
createdFrom: string | null;
|
|
191
|
+
paymentInstrumentId: string | null;
|
|
192
|
+
refundedAt: string | null;
|
|
193
|
+
refundedAmount: number | null;
|
|
194
|
+
metadata?: Record<string, any>;
|
|
195
|
+
}
|
|
196
|
+
export interface CustomerInfos {
|
|
197
|
+
id: string;
|
|
198
|
+
email: string | null;
|
|
199
|
+
firstName: string | null;
|
|
200
|
+
lastName: string | null;
|
|
201
|
+
externalCustomerId: string | null;
|
|
202
|
+
lastOrderId: string | null;
|
|
203
|
+
accountId: string;
|
|
204
|
+
storeId: string;
|
|
205
|
+
billingAddress: CustomerAddress | null;
|
|
206
|
+
shippingAddress: Omit<CustomerAddress, 'email'> | null;
|
|
207
|
+
currency: string | null;
|
|
208
|
+
locale: string | null;
|
|
209
|
+
draft: boolean;
|
|
210
|
+
acceptsMarketing: boolean;
|
|
211
|
+
createdAt: string;
|
|
212
|
+
updatedAt: string;
|
|
213
|
+
metadata: Record<string, any>;
|
|
214
|
+
device: any | null;
|
|
215
|
+
orders: CustomerOrderSummary[];
|
|
216
|
+
subscriptions: any[];
|
|
217
|
+
promotionCodes: any[];
|
|
218
|
+
}
|
|
@@ -10,6 +10,7 @@ export function setClientToken(token) {
|
|
|
10
10
|
if (typeof window !== 'undefined') {
|
|
11
11
|
try {
|
|
12
12
|
localStorage.setItem(TOKEN_KEY, token);
|
|
13
|
+
window.dispatchEvent(new Event('storage'));
|
|
13
14
|
}
|
|
14
15
|
catch (error) {
|
|
15
16
|
console.error('Failed to save token to localStorage:', error);
|