@tagadapay/plugin-sdk 1.0.2

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.
Files changed (63) hide show
  1. package/README.md +475 -0
  2. package/dist/data/currencies.json +2410 -0
  3. package/dist/index.d.ts +33 -0
  4. package/dist/index.js +37 -0
  5. package/dist/react/components/DebugDrawer.d.ts +7 -0
  6. package/dist/react/components/DebugDrawer.js +368 -0
  7. package/dist/react/components/OffersDemo.d.ts +1 -0
  8. package/dist/react/components/OffersDemo.js +50 -0
  9. package/dist/react/components/index.d.ts +1 -0
  10. package/dist/react/components/index.js +1 -0
  11. package/dist/react/config/environment.d.ts +22 -0
  12. package/dist/react/config/environment.js +132 -0
  13. package/dist/react/config/payment.d.ts +23 -0
  14. package/dist/react/config/payment.js +52 -0
  15. package/dist/react/hooks/useAuth.d.ts +4 -0
  16. package/dist/react/hooks/useAuth.js +12 -0
  17. package/dist/react/hooks/useCheckout.d.ts +262 -0
  18. package/dist/react/hooks/useCheckout.js +325 -0
  19. package/dist/react/hooks/useCurrency.d.ts +4 -0
  20. package/dist/react/hooks/useCurrency.js +640 -0
  21. package/dist/react/hooks/useCustomer.d.ts +7 -0
  22. package/dist/react/hooks/useCustomer.js +14 -0
  23. package/dist/react/hooks/useEnvironment.d.ts +7 -0
  24. package/dist/react/hooks/useEnvironment.js +18 -0
  25. package/dist/react/hooks/useLocale.d.ts +2 -0
  26. package/dist/react/hooks/useLocale.js +43 -0
  27. package/dist/react/hooks/useOffers.d.ts +99 -0
  28. package/dist/react/hooks/useOffers.js +115 -0
  29. package/dist/react/hooks/useOrder.d.ts +44 -0
  30. package/dist/react/hooks/useOrder.js +77 -0
  31. package/dist/react/hooks/usePayment.d.ts +60 -0
  32. package/dist/react/hooks/usePayment.js +343 -0
  33. package/dist/react/hooks/usePaymentPolling.d.ts +45 -0
  34. package/dist/react/hooks/usePaymentPolling.js +146 -0
  35. package/dist/react/hooks/useProducts.d.ts +95 -0
  36. package/dist/react/hooks/useProducts.js +120 -0
  37. package/dist/react/hooks/useSession.d.ts +10 -0
  38. package/dist/react/hooks/useSession.js +17 -0
  39. package/dist/react/hooks/useThreeds.d.ts +38 -0
  40. package/dist/react/hooks/useThreeds.js +162 -0
  41. package/dist/react/hooks/useThreedsModal.d.ts +16 -0
  42. package/dist/react/hooks/useThreedsModal.js +328 -0
  43. package/dist/react/index.d.ts +26 -0
  44. package/dist/react/index.js +27 -0
  45. package/dist/react/providers/TagadaProvider.d.ts +55 -0
  46. package/dist/react/providers/TagadaProvider.js +471 -0
  47. package/dist/react/services/apiService.d.ts +149 -0
  48. package/dist/react/services/apiService.js +168 -0
  49. package/dist/react/types.d.ts +151 -0
  50. package/dist/react/types.js +4 -0
  51. package/dist/react/utils/__tests__/urlUtils.test.d.ts +1 -0
  52. package/dist/react/utils/__tests__/urlUtils.test.js +189 -0
  53. package/dist/react/utils/deviceInfo.d.ts +39 -0
  54. package/dist/react/utils/deviceInfo.js +163 -0
  55. package/dist/react/utils/jwtDecoder.d.ts +14 -0
  56. package/dist/react/utils/jwtDecoder.js +86 -0
  57. package/dist/react/utils/money.d.ts +2273 -0
  58. package/dist/react/utils/money.js +104 -0
  59. package/dist/react/utils/tokenStorage.d.ts +16 -0
  60. package/dist/react/utils/tokenStorage.js +52 -0
  61. package/dist/react/utils/urlUtils.d.ts +239 -0
  62. package/dist/react/utils/urlUtils.js +449 -0
  63. package/package.json +64 -0
@@ -0,0 +1,104 @@
1
+ import currencyData from '../../data/currencies.json';
2
+ // Universal Currency Formatter with Locale Support Patch
3
+ function formatCurrency(amount, locale, currency) {
4
+ const currencySymbols = {
5
+ AUD: 'A$',
6
+ CAD: 'CA$',
7
+ USD: '$',
8
+ };
9
+ try {
10
+ const formatter = new Intl.NumberFormat(locale, {
11
+ style: 'currency',
12
+ currency,
13
+ });
14
+ let formatted = formatter.format(amount);
15
+ // Force the correct currency symbol based on currency code
16
+ if (currency in currencySymbols) {
17
+ // Get the correct currency symbol
18
+ const correctSymbol = currencySymbols[currency];
19
+ // Replace the currency symbol part while preserving the locale-specific number formatting
20
+ // This regular expression matches any non-digit, non-comma, non-dot, non-space at the beginning
21
+ formatted = formatted.replace(/^[^\d\s.,]+/, correctSymbol);
22
+ }
23
+ return formatted;
24
+ }
25
+ catch (error) {
26
+ console.error('Intl.NumberFormat Error:', error);
27
+ // Fallback if Intl.NumberFormat completely fails
28
+ return `${currency in currencySymbols ? currencySymbols[currency] : ''}${amount.toFixed(2)}`;
29
+ }
30
+ }
31
+ /**
32
+ * Formats the amount in minor units (cents, pence, etc.) to a string based on the provided currency code and locale.
33
+ * @param {number} amountMinorUnits - The amount in minor units (e.g., 12345 for $123.45).
34
+ * @param {string} currencyCode - The ISO 4217 currency code (e.g., "USD", "JPY").
35
+ * @param {string} locale - The locale to format the output (default is "en-US").
36
+ * @returns {string} - The formatted money string (e.g., "$123.45").
37
+ */
38
+ export function formatMoney(amountMinorUnits, currencyCode = 'USD', locale) {
39
+ const safeLocale = locale || 'en-US';
40
+ const currencyInfo = currencyData[currencyCode];
41
+ if (!currencyInfo) {
42
+ throw new Error(`Currency ${currencyCode} not found in currency data.`);
43
+ }
44
+ // Convert minor units to major units
45
+ const value = amountMinorUnits / Math.pow(10, currencyInfo.ISOdigits);
46
+ return formatCurrency(value, safeLocale, currencyCode);
47
+ }
48
+ /**
49
+ * Utility function to get currency information based on the ISO 4217 currency code.
50
+ * Uses the preloaded currency data from the JSON file.
51
+ */
52
+ export function getCurrencyInfo(currencyCode) {
53
+ return currencyData[currencyCode] || null;
54
+ }
55
+ /**
56
+ * Convert a money string or number to minor units
57
+ */
58
+ export function moneyStringOrNumberToMinorUnits(moneyStringOrNumber, currencyCode) {
59
+ const currencyInfo = currencyData[currencyCode];
60
+ if (!currencyInfo) {
61
+ throw new Error(`Currency ${currencyCode} not found in currency data.`);
62
+ }
63
+ const value = typeof moneyStringOrNumber === 'string' ? parseFloat(moneyStringOrNumber) : moneyStringOrNumber;
64
+ return Math.round(value * Math.pow(10, currencyInfo.ISOdigits));
65
+ }
66
+ /**
67
+ * Convert minor units to major units (decimal value)
68
+ */
69
+ export function minorUnitsToMajorUnits(amountMinorUnits, currencyCode) {
70
+ const currencyInfo = currencyData[currencyCode];
71
+ if (!currencyInfo) {
72
+ throw new Error(`Currency ${currencyCode} not found in currency data.`);
73
+ }
74
+ return amountMinorUnits / Math.pow(10, currencyInfo.ISOdigits);
75
+ }
76
+ /**
77
+ * Format money without currency symbol, just the numeric value
78
+ */
79
+ export const formatMoneyWithoutSymbol = ({ amount, currencyCode, fromCents = true, }) => {
80
+ const currencyInfo = currencyData[currencyCode];
81
+ const digits = currencyInfo?.ISOdigits || 2;
82
+ const value = fromCents ? amount / Math.pow(10, digits) : amount;
83
+ return new Intl.NumberFormat('en-US', {
84
+ minimumFractionDigits: digits,
85
+ maximumFractionDigits: digits,
86
+ }).format(value);
87
+ };
88
+ /**
89
+ * Convert currency amounts with exchange rates
90
+ */
91
+ export const convertCurrency = (amount, fromCurrency, toCurrency, rate) => {
92
+ if (fromCurrency === toCurrency)
93
+ return amount;
94
+ return Math.round(amount * rate);
95
+ };
96
+ /**
97
+ * Simple money formatter for quick use (similar to the existing useCurrency format function)
98
+ */
99
+ export function formatSimpleMoney(amount, currencyCode) {
100
+ const currencyInfo = currencyData[currencyCode];
101
+ const symbol = currencyInfo?.symbol || currencyCode;
102
+ const digits = currencyInfo?.ISOdigits || 2;
103
+ return `${symbol}${(amount / Math.pow(10, digits)).toFixed(digits)}`;
104
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Set the CMS token in localStorage
3
+ */
4
+ export declare function setClientToken(token: string): void;
5
+ /**
6
+ * Get the CMS token from localStorage
7
+ */
8
+ export declare function getClientToken(): string | null;
9
+ /**
10
+ * Clear the CMS token from localStorage
11
+ */
12
+ export declare function clearClientToken(): void;
13
+ /**
14
+ * Check if a token exists in localStorage
15
+ */
16
+ export declare function hasClientToken(): boolean;
@@ -0,0 +1,52 @@
1
+ 'use client';
2
+ /**
3
+ * Client-side token storage utilities
4
+ */
5
+ const TOKEN_KEY = 'tagada_cms_token';
6
+ /**
7
+ * Set the CMS token in localStorage
8
+ */
9
+ export function setClientToken(token) {
10
+ if (typeof window !== 'undefined') {
11
+ try {
12
+ localStorage.setItem(TOKEN_KEY, token);
13
+ }
14
+ catch (error) {
15
+ console.error('Failed to save token to localStorage:', error);
16
+ }
17
+ }
18
+ }
19
+ /**
20
+ * Get the CMS token from localStorage
21
+ */
22
+ export function getClientToken() {
23
+ if (typeof window !== 'undefined') {
24
+ try {
25
+ return localStorage.getItem(TOKEN_KEY);
26
+ }
27
+ catch (error) {
28
+ console.error('Failed to get token from localStorage:', error);
29
+ return null;
30
+ }
31
+ }
32
+ return null;
33
+ }
34
+ /**
35
+ * Clear the CMS token from localStorage
36
+ */
37
+ export function clearClientToken() {
38
+ if (typeof window !== 'undefined') {
39
+ try {
40
+ localStorage.removeItem(TOKEN_KEY);
41
+ }
42
+ catch (error) {
43
+ console.error('Failed to clear token from localStorage:', error);
44
+ }
45
+ }
46
+ }
47
+ /**
48
+ * Check if a token exists in localStorage
49
+ */
50
+ export function hasClientToken() {
51
+ return !!getClientToken();
52
+ }
@@ -0,0 +1,239 @@
1
+ /**
2
+ * Extract checkout token from a URL path
3
+ *
4
+ * @param url - The URL to extract the token from
5
+ * @returns The checkout token if found, null otherwise
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // These will all return "0cb592d75aae75e337b7b784f6624dbf"
10
+ * extractCheckoutToken("https://example.com/checkout/0cb592d75aae75e337b7b784f6624dbf")
11
+ * extractCheckoutToken("/checkout/0cb592d75aae75e337b7b784f6624dbf")
12
+ * extractCheckoutToken("/some/path/0cb592d75aae75e337b7b784f6624dbf/other/path")
13
+ * extractCheckoutToken("0cb592d75aae75e337b7b784f6624dbf")
14
+ *
15
+ * // These will return null
16
+ * extractCheckoutToken("https://example.com/checkout")
17
+ * extractCheckoutToken("invalid-token")
18
+ * ```
19
+ */
20
+ export declare function extractCheckoutToken(url: string): string | null;
21
+ /**
22
+ * Extract checkout token from current browser URL
23
+ *
24
+ * @returns The checkout token if found in current URL, null otherwise
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * // If current URL is "https://example.com/checkout/0cb592d75aae75e337b7b784f6624dbf"
29
+ * const token = extractCheckoutTokenFromCurrentUrl();
30
+ * // Returns: "0cb592d75aae75e337b7b784f6624dbf"
31
+ * ```
32
+ */
33
+ export declare function extractCheckoutTokenFromCurrentUrl(): string | null;
34
+ /**
35
+ * Extract checkout token from URL pathname only
36
+ *
37
+ * @param pathname - The pathname to extract the token from
38
+ * @returns The checkout token if found, null otherwise
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * // These will all return "0cb592d75aae75e337b7b784f6624dbf"
43
+ * extractCheckoutTokenFromPath("/checkout/0cb592d75aae75e337b7b784f6624dbf")
44
+ * extractCheckoutTokenFromPath("/some/path/0cb592d75aae75e337b7b784f6624dbf/other/path")
45
+ * extractCheckoutTokenFromPath("0cb592d75aae75e337b7b784f6624dbf")
46
+ * ```
47
+ */
48
+ export declare function extractCheckoutTokenFromPath(pathname: string): string | null;
49
+ /**
50
+ * Validate if a string is a valid checkout token format
51
+ *
52
+ * @param token - The token to validate
53
+ * @returns True if the token is valid, false otherwise
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * isValidCheckoutToken("0cb592d75aae75e337b7b784f6624dbf") // true
58
+ * isValidCheckoutToken("invalid-token") // false
59
+ * isValidCheckoutToken("1234567890abcdef1234567890abcdef") // true
60
+ * isValidCheckoutToken("") // false
61
+ * ```
62
+ */
63
+ export declare function isValidCheckoutToken(token: string): boolean;
64
+ /**
65
+ * Extract checkout token from various URL formats and validate it
66
+ *
67
+ * @param url - The URL to extract and validate the token from
68
+ * @returns The validated checkout token if found and valid, null otherwise
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * // These will all return "0cb592d75aae75e337b7b784f6624dbf"
73
+ * extractAndValidateCheckoutToken("https://example.com/checkout/0cb592d75aae75e337b7b784f6624dbf")
74
+ * extractAndValidateCheckoutToken("/checkout/0cb592d75aae75e337b7b784f6624dbf")
75
+ *
76
+ * // These will return null
77
+ * extractAndValidateCheckoutToken("https://example.com/checkout/invalid-token")
78
+ * extractAndValidateCheckoutToken("https://example.com/checkout")
79
+ * ```
80
+ */
81
+ export declare function extractAndValidateCheckoutToken(url: string): string | null;
82
+ /**
83
+ * Extract checkout token from URL query parameters
84
+ *
85
+ * @param searchParams - URLSearchParams object or search string
86
+ * @returns The checkout token if found in query params, null otherwise
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * // From URLSearchParams
91
+ * const params = new URLSearchParams('?token=0cb592d75aae75e337b7b784f6624dbf');
92
+ * const token = extractCheckoutTokenFromQuery(params);
93
+ *
94
+ * // From search string
95
+ * const token = extractCheckoutTokenFromQuery('?checkoutToken=0cb592d75aae75e337b7b784f6624dbf');
96
+ * ```
97
+ */
98
+ export declare function extractCheckoutTokenFromQuery(searchParams: URLSearchParams | string): string | null;
99
+ /**
100
+ * Extract checkout token from current browser URL query parameters
101
+ *
102
+ * @returns The checkout token if found in current URL query params, null otherwise
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * // If current URL is "https://example.com/checkout?token=0cb592d75aae75e337b7b784f6624dbf"
107
+ * const token = extractCheckoutTokenFromCurrentUrlQuery();
108
+ * // Returns: "0cb592d75aae75e337b7b784f6624dbf"
109
+ * ```
110
+ */
111
+ export declare function extractCheckoutTokenFromCurrentUrlQuery(): string | null;
112
+ /**
113
+ * Get checkout token from multiple possible sources
114
+ *
115
+ * @param options - Options for token extraction
116
+ * @returns The checkout token if found, null otherwise
117
+ *
118
+ * @example
119
+ * ```typescript
120
+ * // Try to get token from current URL (both query params and path), then from a specific URL, then from a fallback
121
+ * const token = getCheckoutToken({
122
+ * currentUrl: true,
123
+ * specificUrl: "https://example.com/checkout/0cb592d75aae75e337b7b784f6624dbf",
124
+ * fallbackToken: "0cb592d75aae75e337b7b784f6624dbf"
125
+ * });
126
+ *
127
+ * // Works with both query parameters and path parameters:
128
+ * // /checkout?token=0cb592d75aae75e337b7b784f6624dbf
129
+ * // /checkout?checkoutToken=0cb592d75aae75e337b7b784f6624dbf
130
+ * // /checkout/0cb592d75aae75e337b7b784f6624dbf
131
+ * ```
132
+ */
133
+ export declare function getCheckoutToken(options?: {
134
+ currentUrl?: boolean;
135
+ specificUrl?: string;
136
+ fallbackToken?: string;
137
+ }): string | null;
138
+ /**
139
+ * Build a checkout URL with a token
140
+ *
141
+ * @param baseUrl - The base URL for the checkout
142
+ * @param token - The checkout token
143
+ * @returns The complete checkout URL
144
+ *
145
+ * @example
146
+ * ```typescript
147
+ * buildCheckoutUrl("https://example.com/checkout", "0cb592d75aae75e337b7b784f6624dbf")
148
+ * // Returns: "https://example.com/checkout/0cb592d75aae75e337b7b784f6624dbf"
149
+ * ```
150
+ */
151
+ export declare function buildCheckoutUrl(baseUrl: string, token: string): string;
152
+ /**
153
+ * Check if a URL contains a checkout token (in path or query parameters)
154
+ *
155
+ * @param url - The URL to check
156
+ * @returns True if the URL contains a valid checkout token, false otherwise
157
+ *
158
+ * @example
159
+ * ```typescript
160
+ * hasCheckoutToken("https://example.com/checkout/0cb592d75aae75e337b7b784f6624dbf") // true
161
+ * hasCheckoutToken("https://example.com/checkout?token=0cb592d75aae75e337b7b784f6624dbf") // true
162
+ * hasCheckoutToken("https://example.com/checkout?checkoutToken=0cb592d75aae75e337b7b784f6624dbf") // true
163
+ * hasCheckoutToken("https://example.com/checkout") // false
164
+ * hasCheckoutToken("https://example.com/checkout/invalid-token") // false
165
+ * ```
166
+ */
167
+ export declare function hasCheckoutToken(url: string): boolean;
168
+ /**
169
+ * Extract checkout initialization data from URL search parameters
170
+ *
171
+ * @param searchParams - URLSearchParams object or search string
172
+ * @returns Checkout initialization data if found, null otherwise
173
+ *
174
+ * @example
175
+ * ```typescript
176
+ * // From URLSearchParams
177
+ * const params = new URLSearchParams('?items=[{"id":"1","quantity":2}]&cartToken=abc&storeId=123');
178
+ * const data = extractCheckoutInitData(params);
179
+ *
180
+ * // From search string
181
+ * const data = extractCheckoutInitData('?items=[{"id":"1","quantity":2}]&cartToken=abc&storeId=123');
182
+ *
183
+ * // From base64 encoded data
184
+ * const data = extractCheckoutInitData('?data=eyJpdGVtcyI6W3siaWQiOiIxIiwicXVhbnRpdHkiOjJ9XX0=');
185
+ * ```
186
+ */
187
+ export declare function extractCheckoutInitData(searchParams: URLSearchParams | string): {
188
+ cartToken?: string;
189
+ lineItems: {
190
+ id: string;
191
+ quantity: number;
192
+ price?: number;
193
+ }[];
194
+ customer?: {
195
+ currency?: string;
196
+ locale?: string;
197
+ };
198
+ returnUrl?: string;
199
+ storeId?: string;
200
+ } | null;
201
+ /**
202
+ * Extract checkout initialization data from current browser URL
203
+ *
204
+ * @returns Checkout initialization data if found, null otherwise
205
+ *
206
+ * @example
207
+ * ```typescript
208
+ * // If current URL is "https://example.com/checkout?items=[...]&cartToken=abc"
209
+ * const data = extractCheckoutInitDataFromCurrentUrl();
210
+ * ```
211
+ */
212
+ export declare function extractCheckoutInitDataFromCurrentUrl(): ReturnType<typeof extractCheckoutInitData>;
213
+ /**
214
+ * Check if URL contains checkout initialization data
215
+ *
216
+ * @param searchParams - URLSearchParams object or search string
217
+ * @returns True if URL contains checkout init data, false otherwise
218
+ *
219
+ * @example
220
+ * ```typescript
221
+ * hasCheckoutInitData('?items=[{"id":"1","quantity":2}]') // true
222
+ * hasCheckoutInitData('?other=param') // false
223
+ * ```
224
+ */
225
+ export declare function hasCheckoutInitData(searchParams: URLSearchParams | string): boolean;
226
+ /**
227
+ * Check if current URL contains checkout initialization data
228
+ *
229
+ * @returns True if current URL contains checkout init data, false otherwise
230
+ *
231
+ * @example
232
+ * ```typescript
233
+ * // If current URL has checkout data
234
+ * if (hasCheckoutInitDataInCurrentUrl()) {
235
+ * // Auto-initialize checkout
236
+ * }
237
+ * ```
238
+ */
239
+ export declare function hasCheckoutInitDataInCurrentUrl(): boolean;