@explorins/pers-sdk-react-native 1.3.2 → 1.5.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 (94) hide show
  1. package/README.md +276 -123
  2. package/dist/hooks/index.d.ts +10 -0
  3. package/dist/hooks/index.d.ts.map +1 -1
  4. package/dist/hooks/index.js +8 -0
  5. package/dist/hooks/useAnalytics.d.ts +96 -0
  6. package/dist/hooks/useAnalytics.d.ts.map +1 -0
  7. package/dist/hooks/useAnalytics.js +73 -0
  8. package/dist/hooks/useAuth.d.ts +52 -7
  9. package/dist/hooks/useAuth.d.ts.map +1 -1
  10. package/dist/hooks/useAuth.js +250 -3
  11. package/dist/hooks/useBusiness.d.ts +45 -3
  12. package/dist/hooks/useBusiness.d.ts.map +1 -1
  13. package/dist/hooks/useBusiness.js +182 -28
  14. package/dist/hooks/useCampaigns.d.ts +11 -4
  15. package/dist/hooks/useCampaigns.d.ts.map +1 -1
  16. package/dist/hooks/useCampaigns.js +101 -39
  17. package/dist/hooks/useDonations.d.ts +33 -0
  18. package/dist/hooks/useDonations.d.ts.map +1 -0
  19. package/dist/hooks/useDonations.js +62 -0
  20. package/dist/hooks/useFiles.d.ts +50 -0
  21. package/dist/hooks/useFiles.d.ts.map +1 -0
  22. package/dist/hooks/useFiles.js +140 -0
  23. package/dist/hooks/usePurchases.d.ts +9 -0
  24. package/dist/hooks/usePurchases.d.ts.map +1 -0
  25. package/dist/hooks/usePurchases.js +56 -0
  26. package/dist/hooks/useRedemptions.d.ts +10 -5
  27. package/dist/hooks/useRedemptions.d.ts.map +1 -1
  28. package/dist/hooks/useRedemptions.js +118 -66
  29. package/dist/hooks/useTenants.d.ts +10 -0
  30. package/dist/hooks/useTenants.d.ts.map +1 -0
  31. package/dist/hooks/useTenants.js +68 -0
  32. package/dist/hooks/useTokens.d.ts +36 -2
  33. package/dist/hooks/useTokens.d.ts.map +1 -1
  34. package/dist/hooks/useTokens.js +180 -19
  35. package/dist/hooks/useTransactionSigner.d.ts +73 -0
  36. package/dist/hooks/useTransactionSigner.d.ts.map +1 -0
  37. package/dist/hooks/useTransactionSigner.js +216 -0
  38. package/dist/hooks/useTransactions.d.ts +45 -3
  39. package/dist/hooks/useTransactions.d.ts.map +1 -1
  40. package/dist/hooks/useTransactions.js +149 -39
  41. package/dist/hooks/useUserStatus.d.ts +9 -0
  42. package/dist/hooks/useUserStatus.d.ts.map +1 -0
  43. package/dist/hooks/useUserStatus.js +57 -0
  44. package/dist/hooks/useUsers.d.ts +17 -0
  45. package/dist/hooks/useUsers.d.ts.map +1 -0
  46. package/dist/hooks/useUsers.js +120 -0
  47. package/dist/hooks/useWeb3.d.ts +69 -6
  48. package/dist/hooks/useWeb3.d.ts.map +1 -1
  49. package/dist/hooks/useWeb3.js +177 -17
  50. package/dist/index.d.ts +6 -4
  51. package/dist/index.d.ts.map +1 -1
  52. package/dist/index.js +31540 -794
  53. package/dist/index.js.map +1 -1
  54. package/dist/polyfills/index.d.ts +1 -1
  55. package/dist/polyfills/index.d.ts.map +1 -1
  56. package/dist/polyfills/index.js +155 -9
  57. package/dist/polyfills/web3-polyfills.d.ts +7 -0
  58. package/dist/polyfills/web3-polyfills.d.ts.map +1 -0
  59. package/dist/polyfills/web3-polyfills.js +85 -0
  60. package/dist/providers/PersSDKProvider.d.ts +28 -18
  61. package/dist/providers/PersSDKProvider.d.ts.map +1 -1
  62. package/dist/providers/PersSDKProvider.js +63 -129
  63. package/dist/providers/react-native-auth-provider.d.ts +19 -64
  64. package/dist/providers/react-native-auth-provider.d.ts.map +1 -1
  65. package/dist/providers/react-native-auth-provider.js +57 -175
  66. package/package.json +177 -157
  67. package/src/hooks/index.ts +13 -1
  68. package/src/hooks/useAnalytics.ts +150 -0
  69. package/src/hooks/useAuth.ts +286 -27
  70. package/src/hooks/useBusiness.ts +193 -30
  71. package/src/hooks/useCampaigns.ts +121 -43
  72. package/src/hooks/useDonations.ts +68 -0
  73. package/src/hooks/useFiles.ts +160 -0
  74. package/src/hooks/usePurchases.ts +69 -0
  75. package/src/hooks/useRedemptions.ts +135 -68
  76. package/src/hooks/useTenants.ts +77 -0
  77. package/src/hooks/useTokens.ts +189 -21
  78. package/src/hooks/useTransactionSigner.ts +287 -0
  79. package/src/hooks/useTransactions.ts +163 -41
  80. package/src/hooks/useUserStatus.ts +65 -0
  81. package/src/hooks/useUsers.ts +133 -0
  82. package/src/hooks/useWeb3.ts +201 -21
  83. package/src/index.ts +22 -17
  84. package/src/polyfills/index.ts +163 -10
  85. package/src/polyfills/web3-polyfills.ts +98 -0
  86. package/src/providers/PersSDKProvider.tsx +114 -178
  87. package/src/providers/react-native-auth-provider.ts +65 -208
  88. package/dist/index.esm.js +0 -1057
  89. package/dist/index.esm.js.map +0 -1
  90. package/dist/polyfills/index.simple.d.ts +0 -2
  91. package/dist/polyfills/index.simple.d.ts.map +0 -1
  92. package/dist/polyfills/index.simple.js +0 -17
  93. package/src/polyfills/index.simple.ts +0 -22
  94. package/src/types/external-modules.d.ts +0 -13
@@ -1,96 +1,174 @@
1
1
  import { useCallback } from 'react';
2
2
  import { usePersSDK } from '../providers/PersSDKProvider';
3
+ import type {
4
+ CampaignClaimRequestDTO,
5
+ CampaignDTO,
6
+ CampaignClaimDTO,
7
+ CampaignTriggerDTO,
8
+ CampaignCreateRequestDTO,
9
+ TokenUnitCreateRequestDTO,
10
+ CampaignBusinessEngagementCreateRequestDTO
11
+ } from '@explorins/pers-shared';
3
12
 
4
13
  export const useCampaigns = () => {
5
- const { campaigns, isInitialized, isAuthenticated } = usePersSDK();
14
+ const { sdk, isInitialized, isAuthenticated } = usePersSDK();
6
15
 
7
- const getActiveCampaigns = useCallback(async () => {
8
- if (!isInitialized) {
16
+ const getActiveCampaigns = useCallback(async (): Promise<CampaignDTO[]> => {
17
+ if (!isInitialized || !sdk) {
9
18
  throw new Error('SDK not initialized. Call initialize() first.');
10
19
  }
11
- if (!campaigns?.getActiveCampaigns) {
12
- console.warn('getActiveCampaigns method not available');
13
- return [];
14
- }
15
20
 
16
21
  try {
17
- const result = await campaigns.getActiveCampaigns();
18
- console.log('Active campaigns fetched successfully:', result);
22
+ const result = await sdk.campaigns.getActiveCampaigns();
23
+ console.log('Active campaigns fetched successfully:', result);
19
24
  return result;
20
25
  } catch (error) {
21
- console.error('Failed to fetch active campaigns:', error);
26
+ console.error('Failed to fetch active campaigns:', error);
22
27
  throw error;
23
28
  }
24
- }, [campaigns]);
29
+ }, [sdk, isInitialized]);
25
30
 
26
- const getCampaignById = useCallback(async (campaignId: string) => {
27
- if (!isInitialized) {
31
+ const getCampaignById = useCallback(async (campaignId: string): Promise<CampaignDTO | null> => {
32
+ if (!isInitialized || !sdk) {
28
33
  throw new Error('SDK not initialized. Call initialize() first.');
29
34
  }
30
- if (!campaigns?.getCampaignById) {
31
- throw new Error('getCampaignById method not available');
32
- }
33
35
 
34
36
  try {
35
- const result = await campaigns.getCampaignById(campaignId);
36
- console.log('Campaign fetched successfully:', result);
37
+ const result = await sdk.campaigns.getCampaignById(campaignId);
38
+ console.log('Campaign fetched successfully:', result);
37
39
  return result;
38
40
  } catch (error) {
39
- console.error('Failed to fetch campaign:', error);
41
+ console.error('Failed to fetch campaign:', error);
40
42
  throw error;
41
43
  }
42
- }, [campaigns]);
44
+ }, [sdk, isInitialized]);
43
45
 
44
- const claimCampaign = useCallback(async (request: any) => {
45
- if (!isInitialized) {
46
+ const claimCampaign = useCallback(async (request: CampaignClaimRequestDTO): Promise<CampaignClaimDTO | null> => {
47
+ if (!isInitialized || !sdk) {
46
48
  throw new Error('SDK not initialized. Call initialize() first.');
47
49
  }
48
50
  if (!isAuthenticated) {
49
51
  throw new Error('SDK not authenticated. claimCampaign requires authentication.');
50
52
  }
51
- if (!campaigns?.claimCampaign) {
52
- throw new Error('claimCampaign method not available');
53
- }
54
53
 
55
54
  try {
56
- console.log('🔄 Claiming campaign with request:', request);
57
- const result = await campaigns.claimCampaign(request);
58
- console.log('Campaign claimed successfully:', result);
55
+ console.log('Claiming campaign with request:', request);
56
+ const result = await sdk.campaigns.claimCampaign(request);
57
+ console.log('Campaign claimed successfully:', result);
59
58
  return result;
60
59
  } catch (error) {
61
- console.error('Failed to claim campaign:', error);
60
+ console.error('Failed to claim campaign:', error);
62
61
  throw error;
63
62
  }
64
- }, [isInitialized, isAuthenticated, campaigns]);
63
+ }, [sdk, isInitialized, isAuthenticated]);
65
64
 
66
- const getClaimsForLoggedUser = useCallback(async () => {
67
- if (!isInitialized) {
65
+ const getUserClaims = useCallback(async (): Promise<CampaignClaimDTO[]> => {
66
+ if (!isInitialized || !sdk) {
68
67
  throw new Error('SDK not initialized. Call initialize() first.');
69
68
  }
70
69
  if (!isAuthenticated) {
71
- console.warn('SDK not authenticated. getClaimsForLoggedUser requires authentication.');
70
+ console.warn('SDK not authenticated. getUserClaims requires authentication.');
72
71
  return [];
73
72
  }
74
- if (!campaigns?.getClaimsForLoggedUser) {
75
- console.warn('getClaimsForLoggedUser method not available');
76
- return [];
73
+
74
+ try {
75
+ const result = await sdk.campaigns.getUserClaims();
76
+ console.log('User claims fetched successfully:', result);
77
+ return result;
78
+ } catch (error) {
79
+ console.error('Failed to fetch user claims:', error);
80
+ throw error;
81
+ }
82
+ }, [sdk, isInitialized, isAuthenticated]);
83
+
84
+ const getCampaignTriggers = useCallback(async (): Promise<CampaignTriggerDTO[]> => {
85
+ if (!isInitialized || !sdk) {
86
+ throw new Error('SDK not initialized. Call initialize() first.');
87
+ }
88
+
89
+ try {
90
+ const result = await sdk.campaigns.getCampaignTriggers();
91
+ console.log('Campaign triggers fetched successfully:', result);
92
+ return result;
93
+ } catch (error) {
94
+ console.error('Failed to fetch campaign triggers:', error);
95
+ throw error;
96
+ }
97
+ }, [sdk, isInitialized]);
98
+
99
+ // Admin methods
100
+ const getAllCampaigns = useCallback(async (active?: boolean): Promise<CampaignDTO[]> => {
101
+ if (!isInitialized || !sdk) {
102
+ throw new Error('SDK not initialized. Call initialize() first.');
103
+ }
104
+
105
+ try {
106
+ const result = await sdk.campaigns.getAllCampaigns(active);
107
+ console.log('All campaigns fetched successfully:', result);
108
+ return result;
109
+ } catch (error) {
110
+ console.error('Failed to fetch all campaigns:', error);
111
+ throw error;
112
+ }
113
+ }, [sdk, isInitialized]);
114
+
115
+ const getCampaignClaims = useCallback(async (): Promise<CampaignClaimDTO[]> => {
116
+ if (!isInitialized || !sdk) {
117
+ throw new Error('SDK not initialized. Call initialize() first.');
77
118
  }
78
119
 
79
120
  try {
80
- const result = await campaigns.getClaimsForLoggedUser();
81
- console.log(' User claims fetched successfully:', result);
121
+ const result = await sdk.campaigns.getCampaignClaims();
122
+ console.log('Campaign claims fetched successfully:', result);
82
123
  return result;
83
124
  } catch (error) {
84
- console.error('Failed to fetch user claims:', error);
125
+ console.error('Failed to fetch campaign claims:', error);
85
126
  throw error;
86
127
  }
87
- }, [isInitialized, isAuthenticated, campaigns]);
128
+ }, [sdk, isInitialized]);
129
+
130
+ const getCampaignClaimsByUserId = useCallback(async (userId: string): Promise<CampaignClaimDTO[]> => {
131
+ if (!isInitialized || !sdk) {
132
+ throw new Error('SDK not initialized. Call initialize() first.');
133
+ }
134
+
135
+ try {
136
+ const result = await sdk.campaigns.getCampaignClaimsByUserId(userId);
137
+ console.log('Campaign claims by user ID fetched successfully:', result);
138
+ return result;
139
+ } catch (error) {
140
+ console.error('Failed to fetch campaign claims by user ID:', error);
141
+ throw error;
142
+ }
143
+ }, [sdk, isInitialized]);
144
+
145
+ const getCampaignClaimsByBusinessId = useCallback(async (businessId: string): Promise<CampaignClaimDTO[]> => {
146
+ if (!isInitialized || !sdk) {
147
+ throw new Error('SDK not initialized. Call initialize() first.');
148
+ }
149
+
150
+ try {
151
+ const result = await sdk.campaigns.getCampaignClaimsByBusinessId(businessId);
152
+ console.log('Campaign claims by business ID fetched successfully:', result);
153
+ return result;
154
+ } catch (error) {
155
+ console.error('Failed to fetch campaign claims by business ID:', error);
156
+ throw error;
157
+ }
158
+ }, [sdk, isInitialized]);
88
159
 
89
160
  return {
90
161
  getActiveCampaigns,
91
162
  getCampaignById,
92
163
  claimCampaign,
93
- getClaimsForLoggedUser,
94
- isAvailable: isInitialized && !!campaigns,
164
+ getUserClaims,
165
+ getCampaignTriggers,
166
+ getAllCampaigns,
167
+ getCampaignClaims,
168
+ getCampaignClaimsByUserId,
169
+ getCampaignClaimsByBusinessId,
170
+ isAvailable: isInitialized && !!sdk?.campaigns,
95
171
  };
96
- };
172
+ };
173
+
174
+ export type CampaignHook = ReturnType<typeof useCampaigns>;
@@ -0,0 +1,68 @@
1
+ import { useCallback } from 'react';
2
+ import { usePersSDK } from '../providers/PersSDKProvider';
3
+ import type { DonationTypeDTO } from '@explorins/pers-shared';
4
+
5
+ /**
6
+ * React hook for donation operations in the PERS SDK
7
+ *
8
+ * Provides methods for managing donations and donation types within the platform.
9
+ * Supports retrieving available donation categories and types.
10
+ *
11
+ * @returns Donation hook with methods for donation management
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * function DonationsComponent() {
16
+ * const { getDonationTypes } = useDonations();
17
+ *
18
+ * const loadDonationTypes = async () => {
19
+ * try {
20
+ * const types = await getDonationTypes();
21
+ * console.log('Available donation types:', types);
22
+ * } catch (error) {
23
+ * console.error('Failed to load donation types:', error);
24
+ * }
25
+ * };
26
+ *
27
+ * return <button onClick={loadDonationTypes}>Load Donation Types</button>;
28
+ * }
29
+ * ```
30
+ */
31
+ export const useDonations = () => {
32
+ const { sdk, isInitialized } = usePersSDK();
33
+
34
+ /**
35
+ * Retrieves all available donation types in the system
36
+ *
37
+ * @returns Promise resolving to array of donation types
38
+ * @throws Error if SDK is not initialized
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const { getDonationTypes } = useDonations();
43
+ * const types = await getDonationTypes();
44
+ * console.log('Donation types:', types.map(t => t.name));
45
+ * ```
46
+ */
47
+ const getDonationTypes = useCallback(async (): Promise<DonationTypeDTO[]> => {
48
+ if (!isInitialized || !sdk) {
49
+ throw new Error('SDK not initialized. Call initialize() first.');
50
+ }
51
+
52
+ try {
53
+ const result = await sdk.donations.getDonationTypes();
54
+ console.log('Donation types fetched successfully:', result);
55
+ return result;
56
+ } catch (error) {
57
+ console.error('Failed to fetch donation types:', error);
58
+ throw error;
59
+ }
60
+ }, [sdk, isInitialized]);
61
+
62
+ return {
63
+ getDonationTypes,
64
+ isAvailable: isInitialized && !!sdk?.donations,
65
+ };
66
+ };
67
+
68
+ export type DonationHook = ReturnType<typeof useDonations>;
@@ -0,0 +1,160 @@
1
+ import { useCallback } from 'react';
2
+ import { usePersSDK } from '../providers/PersSDKProvider';
3
+ import type {
4
+ SignedUrlRequest,
5
+ FileUploadEntityType
6
+ } from '@explorins/pers-sdk';
7
+
8
+ /**
9
+ * React hook for file operations in the PERS SDK
10
+ *
11
+ * Provides methods for generating signed URLs for file uploads and downloads,
12
+ * media optimization, and secure file access. Supports various entity types
13
+ * and customizable expiration times.
14
+ *
15
+ * @returns File hook with methods for file management
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * function FileUploadComponent() {
20
+ * const { getSignedPutUrl, getSignedGetUrl, optimizeMedia } = useFiles();
21
+ *
22
+ * const uploadFile = async (entityId: string, file: File) => {
23
+ * try {
24
+ * const uploadUrl = await getSignedPutUrl(
25
+ * entityId,
26
+ * 'profile',
27
+ * 'jpg'
28
+ * );
29
+ *
30
+ * // Upload file to the signed URL
31
+ * await fetch(uploadUrl, {
32
+ * method: 'PUT',
33
+ * body: file
34
+ * });
35
+ *
36
+ * // Get download URL
37
+ * const downloadUrl = await getSignedGetUrl(entityId, 'profile');
38
+ * console.log('File uploaded, download URL:', downloadUrl);
39
+ * } catch (error) {
40
+ * console.error('File upload failed:', error);
41
+ * }
42
+ * };
43
+ *
44
+ * return <input type="file" onChange={(e) => uploadFile('user-123', e.target.files[0])} />;
45
+ * }
46
+ * ```
47
+ */
48
+ export const useFiles = () => {
49
+ const { sdk, isInitialized } = usePersSDK();
50
+
51
+ /**
52
+ * Generates a signed URL for uploading files to cloud storage
53
+ *
54
+ * @param entityId - Unique identifier of the entity the file belongs to
55
+ * @param entityType - Type of entity ('profile', 'business', 'campaign', etc.)
56
+ * @param fileExtension - File extension without the dot (e.g., 'jpg', 'png')
57
+ * @returns Promise resolving to signed upload URL
58
+ * @throws Error if SDK is not initialized
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * const { getSignedPutUrl } = useFiles();
63
+ * const uploadUrl = await getSignedPutUrl('user-123', 'profile', 'jpg');
64
+ * // Use uploadUrl with PUT request to upload file
65
+ * ```
66
+ */
67
+ const getSignedPutUrl = useCallback(async (
68
+ entityId: string,
69
+ entityType: FileUploadEntityType,
70
+ fileExtension: string
71
+ ): Promise<string> => {
72
+ if (!isInitialized || !sdk) {
73
+ throw new Error('SDK not initialized. Call initialize() first.');
74
+ }
75
+
76
+ try {
77
+ const result = await sdk.files.getSignedPutUrl(entityId, entityType, fileExtension);
78
+ console.log('Signed put URL generated successfully:', result);
79
+ return result;
80
+ } catch (error) {
81
+ console.error('Failed to generate signed put URL:', error);
82
+ throw error;
83
+ }
84
+ }, [sdk, isInitialized]);
85
+
86
+ /**
87
+ * Generates a signed URL for downloading files from cloud storage
88
+ *
89
+ * @param entityId - Unique identifier of the entity the file belongs to
90
+ * @param entityType - Type of entity ('profile', 'business', 'campaign', etc.)
91
+ * @param expireSeconds - Optional expiration time in seconds (default varies by implementation)
92
+ * @returns Promise resolving to signed download URL
93
+ * @throws Error if SDK is not initialized
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * const { getSignedGetUrl } = useFiles();
98
+ * const downloadUrl = await getSignedGetUrl('user-123', 'profile', 3600); // 1 hour expiry
99
+ * // Use downloadUrl to display or download the file
100
+ * ```
101
+ */
102
+ const getSignedGetUrl = useCallback(async (
103
+ entityId: string,
104
+ entityType: FileUploadEntityType,
105
+ expireSeconds?: number
106
+ ): Promise<string> => {
107
+ if (!isInitialized || !sdk) {
108
+ throw new Error('SDK not initialized. Call initialize() first.');
109
+ }
110
+
111
+ try {
112
+ const result = await sdk.files.getSignedGetUrl(entityId, entityType, expireSeconds);
113
+ console.log('Signed get URL generated successfully:', result);
114
+ return result;
115
+ } catch (error) {
116
+ console.error('Failed to generate signed get URL:', error);
117
+ throw error;
118
+ }
119
+ }, [sdk, isInitialized]);
120
+
121
+ const getSignedUrl = useCallback(async (request: SignedUrlRequest): Promise<string> => {
122
+ if (!isInitialized || !sdk) {
123
+ throw new Error('SDK not initialized. Call initialize() first.');
124
+ }
125
+
126
+ try {
127
+ const result = await sdk.files.getSignedUrl(request);
128
+ console.log('Signed URL generated successfully:', result);
129
+ return result;
130
+ } catch (error) {
131
+ console.error('Failed to generate signed URL:', error);
132
+ throw error;
133
+ }
134
+ }, [sdk, isInitialized]);
135
+
136
+ const optimizeMedia = useCallback(async (url: string, width?: number, height?: number): Promise<string> => {
137
+ if (!isInitialized || !sdk) {
138
+ throw new Error('SDK not initialized. Call initialize() first.');
139
+ }
140
+
141
+ try {
142
+ const result = await sdk.files.optimizeMedia(url, width, height);
143
+ console.log('Media optimized successfully:', result);
144
+ return result;
145
+ } catch (error) {
146
+ console.error('Failed to optimize media:', error);
147
+ throw error;
148
+ }
149
+ }, [sdk, isInitialized]);
150
+
151
+ return {
152
+ getSignedPutUrl,
153
+ getSignedGetUrl,
154
+ getSignedUrl,
155
+ optimizeMedia,
156
+ isAvailable: isInitialized && !!sdk?.files,
157
+ };
158
+ };
159
+
160
+ export type FileHook = ReturnType<typeof useFiles>;
@@ -0,0 +1,69 @@
1
+ import { useCallback } from 'react';
2
+ import { usePersSDK } from '../providers/PersSDKProvider';
3
+ import type { PurchaseTokenDTO, PurchaseDTO } from '@explorins/pers-shared';
4
+
5
+ export const usePurchases = () => {
6
+ const { sdk, isInitialized, isAuthenticated } = usePersSDK();
7
+
8
+ const createPaymentIntent = useCallback(async (
9
+ amount: number,
10
+ currency: string,
11
+ receiptEmail: string,
12
+ description: string
13
+ ) => {
14
+ if (!isInitialized || !sdk) {
15
+ throw new Error('SDK not initialized. Call initialize() first.');
16
+ }
17
+
18
+ try {
19
+ const result = await sdk.purchases.createPaymentIntent(amount, currency, receiptEmail, description);
20
+ console.log('Payment intent created successfully:', result);
21
+ return result;
22
+ } catch (error) {
23
+ console.error('Failed to create payment intent:', error);
24
+ throw error;
25
+ }
26
+ }, [sdk, isInitialized]);
27
+
28
+ const getActivePurchaseTokens = useCallback(async (): Promise<PurchaseTokenDTO[]> => {
29
+ if (!isInitialized || !sdk) {
30
+ throw new Error('SDK not initialized. Call initialize() first.');
31
+ }
32
+
33
+ try {
34
+ const result = await sdk.purchases.getActivePurchaseTokens();
35
+ console.log('Active purchase tokens fetched successfully:', result);
36
+ return result;
37
+ } catch (error) {
38
+ console.error('Failed to fetch active purchase tokens:', error);
39
+ throw error;
40
+ }
41
+ }, [sdk, isInitialized]);
42
+
43
+ const getAllUserPurchases = useCallback(async (): Promise<PurchaseDTO[]> => {
44
+ if (!isInitialized || !sdk) {
45
+ throw new Error('SDK not initialized. Call initialize() first.');
46
+ }
47
+ if (!isAuthenticated) {
48
+ throw new Error('SDK not authenticated. getAllUserPurchases requires authentication.');
49
+ }
50
+
51
+ try {
52
+ const result = await sdk.purchases.getAllUserPurchases();
53
+ console.log('User purchases fetched successfully:', result);
54
+ return result;
55
+ } catch (error) {
56
+ console.error('Failed to fetch user purchases:', error);
57
+ throw error;
58
+ }
59
+ }, [sdk, isInitialized, isAuthenticated]);
60
+
61
+ return {
62
+ createPaymentIntent,
63
+ getActivePurchaseTokens,
64
+ getAllUserPurchases,
65
+ isAvailable: isInitialized && !!sdk?.purchases,
66
+ };
67
+ };
68
+
69
+ export type PurchaseHook = ReturnType<typeof usePurchases>;