@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.
- package/README.md +475 -0
- package/dist/data/currencies.json +2410 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +37 -0
- package/dist/react/components/DebugDrawer.d.ts +7 -0
- package/dist/react/components/DebugDrawer.js +368 -0
- package/dist/react/components/OffersDemo.d.ts +1 -0
- package/dist/react/components/OffersDemo.js +50 -0
- package/dist/react/components/index.d.ts +1 -0
- package/dist/react/components/index.js +1 -0
- package/dist/react/config/environment.d.ts +22 -0
- package/dist/react/config/environment.js +132 -0
- package/dist/react/config/payment.d.ts +23 -0
- package/dist/react/config/payment.js +52 -0
- package/dist/react/hooks/useAuth.d.ts +4 -0
- package/dist/react/hooks/useAuth.js +12 -0
- package/dist/react/hooks/useCheckout.d.ts +262 -0
- package/dist/react/hooks/useCheckout.js +325 -0
- package/dist/react/hooks/useCurrency.d.ts +4 -0
- package/dist/react/hooks/useCurrency.js +640 -0
- package/dist/react/hooks/useCustomer.d.ts +7 -0
- package/dist/react/hooks/useCustomer.js +14 -0
- package/dist/react/hooks/useEnvironment.d.ts +7 -0
- package/dist/react/hooks/useEnvironment.js +18 -0
- package/dist/react/hooks/useLocale.d.ts +2 -0
- package/dist/react/hooks/useLocale.js +43 -0
- package/dist/react/hooks/useOffers.d.ts +99 -0
- package/dist/react/hooks/useOffers.js +115 -0
- package/dist/react/hooks/useOrder.d.ts +44 -0
- package/dist/react/hooks/useOrder.js +77 -0
- package/dist/react/hooks/usePayment.d.ts +60 -0
- package/dist/react/hooks/usePayment.js +343 -0
- package/dist/react/hooks/usePaymentPolling.d.ts +45 -0
- package/dist/react/hooks/usePaymentPolling.js +146 -0
- package/dist/react/hooks/useProducts.d.ts +95 -0
- package/dist/react/hooks/useProducts.js +120 -0
- package/dist/react/hooks/useSession.d.ts +10 -0
- package/dist/react/hooks/useSession.js +17 -0
- package/dist/react/hooks/useThreeds.d.ts +38 -0
- package/dist/react/hooks/useThreeds.js +162 -0
- package/dist/react/hooks/useThreedsModal.d.ts +16 -0
- package/dist/react/hooks/useThreedsModal.js +328 -0
- package/dist/react/index.d.ts +26 -0
- package/dist/react/index.js +27 -0
- package/dist/react/providers/TagadaProvider.d.ts +55 -0
- package/dist/react/providers/TagadaProvider.js +471 -0
- package/dist/react/services/apiService.d.ts +149 -0
- package/dist/react/services/apiService.js +168 -0
- package/dist/react/types.d.ts +151 -0
- package/dist/react/types.js +4 -0
- package/dist/react/utils/__tests__/urlUtils.test.d.ts +1 -0
- package/dist/react/utils/__tests__/urlUtils.test.js +189 -0
- package/dist/react/utils/deviceInfo.d.ts +39 -0
- package/dist/react/utils/deviceInfo.js +163 -0
- package/dist/react/utils/jwtDecoder.d.ts +14 -0
- package/dist/react/utils/jwtDecoder.js +86 -0
- package/dist/react/utils/money.d.ts +2273 -0
- package/dist/react/utils/money.js +104 -0
- package/dist/react/utils/tokenStorage.d.ts +16 -0
- package/dist/react/utils/tokenStorage.js +52 -0
- package/dist/react/utils/urlUtils.d.ts +239 -0
- package/dist/react/utils/urlUtils.js +449 -0
- package/package.json +64 -0
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* Checkout token format: 32 character hexadecimal string
|
|
4
|
+
* Example: 0cb592d75aae75e337b7b784f6624dbf
|
|
5
|
+
*/
|
|
6
|
+
const CHECKOUT_TOKEN_REGEX = /[a-f0-9]{32}/i;
|
|
7
|
+
/**
|
|
8
|
+
* Extract checkout token from a URL path
|
|
9
|
+
*
|
|
10
|
+
* @param url - The URL to extract the token from
|
|
11
|
+
* @returns The checkout token if found, null otherwise
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // These will all return "0cb592d75aae75e337b7b784f6624dbf"
|
|
16
|
+
* extractCheckoutToken("https://example.com/checkout/0cb592d75aae75e337b7b784f6624dbf")
|
|
17
|
+
* extractCheckoutToken("/checkout/0cb592d75aae75e337b7b784f6624dbf")
|
|
18
|
+
* extractCheckoutToken("/some/path/0cb592d75aae75e337b7b784f6624dbf/other/path")
|
|
19
|
+
* extractCheckoutToken("0cb592d75aae75e337b7b784f6624dbf")
|
|
20
|
+
*
|
|
21
|
+
* // These will return null
|
|
22
|
+
* extractCheckoutToken("https://example.com/checkout")
|
|
23
|
+
* extractCheckoutToken("invalid-token")
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function extractCheckoutToken(url) {
|
|
27
|
+
if (!url || typeof url !== 'string') {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
// Try to match the checkout token pattern
|
|
31
|
+
const match = CHECKOUT_TOKEN_REGEX.exec(url);
|
|
32
|
+
if (match) {
|
|
33
|
+
return match[0].toLowerCase(); // Normalize to lowercase
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Extract checkout token from current browser URL
|
|
39
|
+
*
|
|
40
|
+
* @returns The checkout token if found in current URL, null otherwise
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* // If current URL is "https://example.com/checkout/0cb592d75aae75e337b7b784f6624dbf"
|
|
45
|
+
* const token = extractCheckoutTokenFromCurrentUrl();
|
|
46
|
+
* // Returns: "0cb592d75aae75e337b7b784f6624dbf"
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export function extractCheckoutTokenFromCurrentUrl() {
|
|
50
|
+
if (typeof window === 'undefined') {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
return extractCheckoutToken(window.location.href);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Extract checkout token from URL pathname only
|
|
57
|
+
*
|
|
58
|
+
* @param pathname - The pathname to extract the token from
|
|
59
|
+
* @returns The checkout token if found, null otherwise
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* // These will all return "0cb592d75aae75e337b7b784f6624dbf"
|
|
64
|
+
* extractCheckoutTokenFromPath("/checkout/0cb592d75aae75e337b7b784f6624dbf")
|
|
65
|
+
* extractCheckoutTokenFromPath("/some/path/0cb592d75aae75e337b7b784f6624dbf/other/path")
|
|
66
|
+
* extractCheckoutTokenFromPath("0cb592d75aae75e337b7b784f6624dbf")
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export function extractCheckoutTokenFromPath(pathname) {
|
|
70
|
+
if (!pathname || typeof pathname !== 'string') {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
return extractCheckoutToken(pathname);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Validate if a string is a valid checkout token format
|
|
77
|
+
*
|
|
78
|
+
* @param token - The token to validate
|
|
79
|
+
* @returns True if the token is valid, false otherwise
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* isValidCheckoutToken("0cb592d75aae75e337b7b784f6624dbf") // true
|
|
84
|
+
* isValidCheckoutToken("invalid-token") // false
|
|
85
|
+
* isValidCheckoutToken("1234567890abcdef1234567890abcdef") // true
|
|
86
|
+
* isValidCheckoutToken("") // false
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export function isValidCheckoutToken(token) {
|
|
90
|
+
if (!token || typeof token !== 'string') {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return CHECKOUT_TOKEN_REGEX.test(token);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Extract checkout token from various URL formats and validate it
|
|
97
|
+
*
|
|
98
|
+
* @param url - The URL to extract and validate the token from
|
|
99
|
+
* @returns The validated checkout token if found and valid, null otherwise
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* // These will all return "0cb592d75aae75e337b7b784f6624dbf"
|
|
104
|
+
* extractAndValidateCheckoutToken("https://example.com/checkout/0cb592d75aae75e337b7b784f6624dbf")
|
|
105
|
+
* extractAndValidateCheckoutToken("/checkout/0cb592d75aae75e337b7b784f6624dbf")
|
|
106
|
+
*
|
|
107
|
+
* // These will return null
|
|
108
|
+
* extractAndValidateCheckoutToken("https://example.com/checkout/invalid-token")
|
|
109
|
+
* extractAndValidateCheckoutToken("https://example.com/checkout")
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export function extractAndValidateCheckoutToken(url) {
|
|
113
|
+
const token = extractCheckoutToken(url);
|
|
114
|
+
if (token && isValidCheckoutToken(token)) {
|
|
115
|
+
return token;
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Extract checkout token from URL query parameters
|
|
121
|
+
*
|
|
122
|
+
* @param searchParams - URLSearchParams object or search string
|
|
123
|
+
* @returns The checkout token if found in query params, null otherwise
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* // From URLSearchParams
|
|
128
|
+
* const params = new URLSearchParams('?token=0cb592d75aae75e337b7b784f6624dbf');
|
|
129
|
+
* const token = extractCheckoutTokenFromQuery(params);
|
|
130
|
+
*
|
|
131
|
+
* // From search string
|
|
132
|
+
* const token = extractCheckoutTokenFromQuery('?checkoutToken=0cb592d75aae75e337b7b784f6624dbf');
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export function extractCheckoutTokenFromQuery(searchParams) {
|
|
136
|
+
try {
|
|
137
|
+
let params;
|
|
138
|
+
if (typeof searchParams === 'string') {
|
|
139
|
+
params = new URLSearchParams(searchParams);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
params = searchParams;
|
|
143
|
+
}
|
|
144
|
+
// Check for 'token' parameter first
|
|
145
|
+
const token = params.get('token');
|
|
146
|
+
if (token && isValidCheckoutToken(token)) {
|
|
147
|
+
return token.toLowerCase();
|
|
148
|
+
}
|
|
149
|
+
// Check for 'checkoutToken' parameter
|
|
150
|
+
const checkoutToken = params.get('checkoutToken');
|
|
151
|
+
if (checkoutToken && isValidCheckoutToken(checkoutToken)) {
|
|
152
|
+
return checkoutToken.toLowerCase();
|
|
153
|
+
}
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
console.error('Failed to extract checkout token from query params:', error);
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Extract checkout token from current browser URL query parameters
|
|
163
|
+
*
|
|
164
|
+
* @returns The checkout token if found in current URL query params, null otherwise
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* // If current URL is "https://example.com/checkout?token=0cb592d75aae75e337b7b784f6624dbf"
|
|
169
|
+
* const token = extractCheckoutTokenFromCurrentUrlQuery();
|
|
170
|
+
* // Returns: "0cb592d75aae75e337b7b784f6624dbf"
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
export function extractCheckoutTokenFromCurrentUrlQuery() {
|
|
174
|
+
if (typeof window === 'undefined') {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
return extractCheckoutTokenFromQuery(window.location.search);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Get checkout token from multiple possible sources
|
|
181
|
+
*
|
|
182
|
+
* @param options - Options for token extraction
|
|
183
|
+
* @returns The checkout token if found, null otherwise
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* // Try to get token from current URL (both query params and path), then from a specific URL, then from a fallback
|
|
188
|
+
* const token = getCheckoutToken({
|
|
189
|
+
* currentUrl: true,
|
|
190
|
+
* specificUrl: "https://example.com/checkout/0cb592d75aae75e337b7b784f6624dbf",
|
|
191
|
+
* fallbackToken: "0cb592d75aae75e337b7b784f6624dbf"
|
|
192
|
+
* });
|
|
193
|
+
*
|
|
194
|
+
* // Works with both query parameters and path parameters:
|
|
195
|
+
* // /checkout?token=0cb592d75aae75e337b7b784f6624dbf
|
|
196
|
+
* // /checkout?checkoutToken=0cb592d75aae75e337b7b784f6624dbf
|
|
197
|
+
* // /checkout/0cb592d75aae75e337b7b784f6624dbf
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
export function getCheckoutToken(options = {}) {
|
|
201
|
+
const { currentUrl = true, specificUrl, fallbackToken } = options;
|
|
202
|
+
// Try current URL first (both path and query params)
|
|
203
|
+
if (currentUrl) {
|
|
204
|
+
// Try query parameters first (more common for SPA)
|
|
205
|
+
const currentQueryToken = extractCheckoutTokenFromCurrentUrlQuery();
|
|
206
|
+
if (currentQueryToken) {
|
|
207
|
+
return currentQueryToken;
|
|
208
|
+
}
|
|
209
|
+
// Then try path-based token
|
|
210
|
+
const currentPathToken = extractCheckoutTokenFromCurrentUrl();
|
|
211
|
+
if (currentPathToken) {
|
|
212
|
+
return currentPathToken;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
// Try specific URL (both path and query params)
|
|
216
|
+
if (specificUrl) {
|
|
217
|
+
// Try query parameters first
|
|
218
|
+
try {
|
|
219
|
+
const url = new URL(specificUrl, window?.location?.origin || 'https://example.com');
|
|
220
|
+
const queryToken = extractCheckoutTokenFromQuery(url.search);
|
|
221
|
+
if (queryToken) {
|
|
222
|
+
return queryToken;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
// If URL parsing fails, continue to path extraction
|
|
227
|
+
}
|
|
228
|
+
// Then try path-based token
|
|
229
|
+
const specificToken = extractCheckoutToken(specificUrl);
|
|
230
|
+
if (specificToken) {
|
|
231
|
+
return specificToken;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// Try fallback token
|
|
235
|
+
if (fallbackToken && isValidCheckoutToken(fallbackToken)) {
|
|
236
|
+
return fallbackToken;
|
|
237
|
+
}
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Build a checkout URL with a token
|
|
242
|
+
*
|
|
243
|
+
* @param baseUrl - The base URL for the checkout
|
|
244
|
+
* @param token - The checkout token
|
|
245
|
+
* @returns The complete checkout URL
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* buildCheckoutUrl("https://example.com/checkout", "0cb592d75aae75e337b7b784f6624dbf")
|
|
250
|
+
* // Returns: "https://example.com/checkout/0cb592d75aae75e337b7b784f6624dbf"
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
export function buildCheckoutUrl(baseUrl, token) {
|
|
254
|
+
if (!baseUrl || !token || !isValidCheckoutToken(token)) {
|
|
255
|
+
throw new Error('Invalid base URL or checkout token');
|
|
256
|
+
}
|
|
257
|
+
// Remove trailing slash from base URL if present
|
|
258
|
+
const cleanBaseUrl = baseUrl.replace(/\/$/, '');
|
|
259
|
+
return `${cleanBaseUrl}/${token}`;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Check if a URL contains a checkout token (in path or query parameters)
|
|
263
|
+
*
|
|
264
|
+
* @param url - The URL to check
|
|
265
|
+
* @returns True if the URL contains a valid checkout token, false otherwise
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```typescript
|
|
269
|
+
* hasCheckoutToken("https://example.com/checkout/0cb592d75aae75e337b7b784f6624dbf") // true
|
|
270
|
+
* hasCheckoutToken("https://example.com/checkout?token=0cb592d75aae75e337b7b784f6624dbf") // true
|
|
271
|
+
* hasCheckoutToken("https://example.com/checkout?checkoutToken=0cb592d75aae75e337b7b784f6624dbf") // true
|
|
272
|
+
* hasCheckoutToken("https://example.com/checkout") // false
|
|
273
|
+
* hasCheckoutToken("https://example.com/checkout/invalid-token") // false
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
276
|
+
export function hasCheckoutToken(url) {
|
|
277
|
+
// Check path first
|
|
278
|
+
if (extractCheckoutToken(url) !== null) {
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
// Check query parameters
|
|
282
|
+
try {
|
|
283
|
+
const urlObj = new URL(url, window?.location?.origin || 'https://example.com');
|
|
284
|
+
return extractCheckoutTokenFromQuery(urlObj.search) !== null;
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Extract checkout initialization data from URL search parameters
|
|
292
|
+
*
|
|
293
|
+
* @param searchParams - URLSearchParams object or search string
|
|
294
|
+
* @returns Checkout initialization data if found, null otherwise
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* ```typescript
|
|
298
|
+
* // From URLSearchParams
|
|
299
|
+
* const params = new URLSearchParams('?items=[{"id":"1","quantity":2}]&cartToken=abc&storeId=123');
|
|
300
|
+
* const data = extractCheckoutInitData(params);
|
|
301
|
+
*
|
|
302
|
+
* // From search string
|
|
303
|
+
* const data = extractCheckoutInitData('?items=[{"id":"1","quantity":2}]&cartToken=abc&storeId=123');
|
|
304
|
+
*
|
|
305
|
+
* // From base64 encoded data
|
|
306
|
+
* const data = extractCheckoutInitData('?data=eyJpdGVtcyI6W3siaWQiOiIxIiwicXVhbnRpdHkiOjJ9XX0=');
|
|
307
|
+
* ```
|
|
308
|
+
*/
|
|
309
|
+
export function extractCheckoutInitData(searchParams) {
|
|
310
|
+
try {
|
|
311
|
+
let params;
|
|
312
|
+
if (typeof searchParams === 'string') {
|
|
313
|
+
params = new URLSearchParams(searchParams);
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
params = searchParams;
|
|
317
|
+
}
|
|
318
|
+
console.log('[extractCheckoutInitData] Processing search params:', Object.fromEntries(params.entries()));
|
|
319
|
+
// Try to get base64 encoded data first
|
|
320
|
+
const encodedData = params.get('data');
|
|
321
|
+
if (encodedData) {
|
|
322
|
+
console.log('[extractCheckoutInitData] Found base64 encoded data');
|
|
323
|
+
const decodedData = atob(encodedData);
|
|
324
|
+
const parsedData = JSON.parse(decodedData);
|
|
325
|
+
// Validate required fields
|
|
326
|
+
if (!parsedData.lineItems || !Array.isArray(parsedData.lineItems)) {
|
|
327
|
+
console.log('[extractCheckoutInitData] Invalid base64 data structure');
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
return {
|
|
331
|
+
cartToken: parsedData.cartToken,
|
|
332
|
+
lineItems: parsedData.lineItems,
|
|
333
|
+
customer: parsedData.customer,
|
|
334
|
+
returnUrl: parsedData.returnUrl,
|
|
335
|
+
storeId: parsedData.storeId,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
// Try to extract lineItems from array format (lineItems[0][variantId], etc.)
|
|
339
|
+
const lineItems = [];
|
|
340
|
+
let index = 0;
|
|
341
|
+
while (true) {
|
|
342
|
+
const variantId = params.get(`lineItems[${index}][variantId]`);
|
|
343
|
+
const quantity = params.get(`lineItems[${index}][quantity]`);
|
|
344
|
+
if (!variantId || !quantity) {
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
lineItems.push({
|
|
348
|
+
id: variantId,
|
|
349
|
+
quantity: parseInt(quantity, 10),
|
|
350
|
+
});
|
|
351
|
+
index++;
|
|
352
|
+
}
|
|
353
|
+
if (lineItems.length > 0) {
|
|
354
|
+
console.log('[extractCheckoutInitData] Found lineItems in array format:', lineItems);
|
|
355
|
+
return {
|
|
356
|
+
cartToken: params.get('cartToken') || undefined,
|
|
357
|
+
lineItems,
|
|
358
|
+
customer: {
|
|
359
|
+
currency: params.get('currency') || undefined,
|
|
360
|
+
locale: params.get('locale') || undefined,
|
|
361
|
+
},
|
|
362
|
+
returnUrl: params.get('returnUrl') || undefined,
|
|
363
|
+
storeId: params.get('storeId') || undefined,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
// Fall back to individual query parameters
|
|
367
|
+
const items = params.get('items');
|
|
368
|
+
if (items) {
|
|
369
|
+
console.log('[extractCheckoutInitData] Found items parameter:', items);
|
|
370
|
+
const parsedItems = JSON.parse(items);
|
|
371
|
+
if (!Array.isArray(parsedItems)) {
|
|
372
|
+
console.log('[extractCheckoutInitData] Invalid items structure');
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
375
|
+
// Preserve original field names from the URL
|
|
376
|
+
const lineItems = parsedItems.map((item) => ({
|
|
377
|
+
...item, // Keep all original fields (variantId, quantity, etc.)
|
|
378
|
+
}));
|
|
379
|
+
console.log('[extractCheckoutInitData] Converted lineItems:', lineItems);
|
|
380
|
+
return {
|
|
381
|
+
cartToken: params.get('cartToken') || undefined,
|
|
382
|
+
lineItems,
|
|
383
|
+
customer: {
|
|
384
|
+
currency: params.get('currency') || undefined,
|
|
385
|
+
locale: params.get('locale') || undefined,
|
|
386
|
+
},
|
|
387
|
+
returnUrl: params.get('returnUrl') || undefined,
|
|
388
|
+
storeId: params.get('storeId') || undefined,
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
console.log('[extractCheckoutInitData] No checkout init data found');
|
|
392
|
+
return null;
|
|
393
|
+
}
|
|
394
|
+
catch (error) {
|
|
395
|
+
console.error('Failed to extract checkout init data:', error);
|
|
396
|
+
return null;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Extract checkout initialization data from current browser URL
|
|
401
|
+
*
|
|
402
|
+
* @returns Checkout initialization data if found, null otherwise
|
|
403
|
+
*
|
|
404
|
+
* @example
|
|
405
|
+
* ```typescript
|
|
406
|
+
* // If current URL is "https://example.com/checkout?items=[...]&cartToken=abc"
|
|
407
|
+
* const data = extractCheckoutInitDataFromCurrentUrl();
|
|
408
|
+
* ```
|
|
409
|
+
*/
|
|
410
|
+
export function extractCheckoutInitDataFromCurrentUrl() {
|
|
411
|
+
if (typeof window === 'undefined') {
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
return extractCheckoutInitData(window.location.search);
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Check if URL contains checkout initialization data
|
|
418
|
+
*
|
|
419
|
+
* @param searchParams - URLSearchParams object or search string
|
|
420
|
+
* @returns True if URL contains checkout init data, false otherwise
|
|
421
|
+
*
|
|
422
|
+
* @example
|
|
423
|
+
* ```typescript
|
|
424
|
+
* hasCheckoutInitData('?items=[{"id":"1","quantity":2}]') // true
|
|
425
|
+
* hasCheckoutInitData('?other=param') // false
|
|
426
|
+
* ```
|
|
427
|
+
*/
|
|
428
|
+
export function hasCheckoutInitData(searchParams) {
|
|
429
|
+
return extractCheckoutInitData(searchParams) !== null;
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Check if current URL contains checkout initialization data
|
|
433
|
+
*
|
|
434
|
+
* @returns True if current URL contains checkout init data, false otherwise
|
|
435
|
+
*
|
|
436
|
+
* @example
|
|
437
|
+
* ```typescript
|
|
438
|
+
* // If current URL has checkout data
|
|
439
|
+
* if (hasCheckoutInitDataInCurrentUrl()) {
|
|
440
|
+
* // Auto-initialize checkout
|
|
441
|
+
* }
|
|
442
|
+
* ```
|
|
443
|
+
*/
|
|
444
|
+
export function hasCheckoutInitDataInCurrentUrl() {
|
|
445
|
+
if (typeof window === 'undefined') {
|
|
446
|
+
return false;
|
|
447
|
+
}
|
|
448
|
+
return hasCheckoutInitData(window.location.search);
|
|
449
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tagadapay/plugin-sdk",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Modern React SDK for building Tagada Pay plugins",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"clean": "rm -rf dist",
|
|
10
|
+
"lint": "echo \"No linting configured\"",
|
|
11
|
+
"test": "echo \"No tests yet\" && exit 0",
|
|
12
|
+
"dev": "tsc --watch",
|
|
13
|
+
"prepublishOnly": "npm run clean && npm run build",
|
|
14
|
+
"publish:patch": "npm version patch && npm publish",
|
|
15
|
+
"publish:minor": "npm version minor && npm publish",
|
|
16
|
+
"publish:major": "npm version major && npm publish",
|
|
17
|
+
"publish:beta": "npm version prerelease --preid=beta && npm publish --tag beta",
|
|
18
|
+
"publish:alpha": "npm version prerelease --preid=alpha && npm publish --tag alpha",
|
|
19
|
+
"version:patch": "npm version patch",
|
|
20
|
+
"version:minor": "npm version minor",
|
|
21
|
+
"version:major": "npm version major",
|
|
22
|
+
"version:beta": "npm version prerelease --preid=beta",
|
|
23
|
+
"version:alpha": "npm version prerelease --preid=alpha"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"tagadapay",
|
|
27
|
+
"cms",
|
|
28
|
+
"plugin",
|
|
29
|
+
"sdk",
|
|
30
|
+
"react",
|
|
31
|
+
"typescript"
|
|
32
|
+
],
|
|
33
|
+
"author": "Tagada Pay",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"axios": "^1.6.0",
|
|
37
|
+
"react-intl": "^7.1.11"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^18.0.0",
|
|
41
|
+
"@types/react": "^19",
|
|
42
|
+
"@types/react-dom": "^19",
|
|
43
|
+
"typescript": "^5.0.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
47
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
48
|
+
},
|
|
49
|
+
"files": [
|
|
50
|
+
"dist/**/*",
|
|
51
|
+
"README.md"
|
|
52
|
+
],
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
},
|
|
56
|
+
"repository": {
|
|
57
|
+
"type": "git",
|
|
58
|
+
"url": "git+https://github.com/tagadapay/plugin-sdk.git"
|
|
59
|
+
},
|
|
60
|
+
"bugs": {
|
|
61
|
+
"url": "https://github.com/tagadapay/plugin-sdk/issues"
|
|
62
|
+
},
|
|
63
|
+
"homepage": "https://github.com/tagadapay/plugin-sdk#readme"
|
|
64
|
+
}
|