insert-affiliate-react-native-sdk 1.0.5 → 1.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "insert-affiliate-react-native-sdk",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "A package that will give context having implementation of react-native-branch and react-native-iap and iaptic validate api.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/readme.md CHANGED
@@ -2,14 +2,13 @@
2
2
 
3
3
  ## Overview
4
4
 
5
- The **InsertAffiliateReactNative SDK** is designed for React Native applications, providing seamless integration with the [Insert Affiliate platform](https://insertaffiliate.com). This SDK enables functionalities such as managing affiliate links, handling in-app purchases (IAP), and utilising deep links. For more details and to access the Insert Affiliate dashboard, visit [app.insertaffiliate.com](https://app.insertaffiliate.com).
5
+ The **InsertAffiliateReactNative SDK** is designed for React Native applications, providing seamless integration with the [Insert Affiliate platform](https://insertaffiliate.com). For more details and to access the Insert Affiliate dashboard, visit [app.insertaffiliate.com](https://app.insertaffiliate.com).
6
6
 
7
7
  ## Features
8
8
 
9
9
  - **Unique Device Identification**: Generates and stores a short unique device ID to identify users effectively.
10
10
  - **Affiliate Identifier Management**: Set and retrieve the affiliate identifier based on user-specific links.
11
11
  - **In-App Purchase (IAP) Initialisation**: Easily reinitialise in-app purchases with validation options using the affiliate identifier.
12
- - **Offer Code Handling**: Fetch offer codes from the Insert Affiliate API and open redeem URLs directly in the App Store.
13
12
 
14
13
  ## Peer Dependencies
15
14
 
@@ -9,6 +9,7 @@ import {
9
9
  } from "react-native-iap";
10
10
  import { isPlay } from "react-native-iap/src/internal";
11
11
  import branch from "react-native-branch";
12
+ import { Platform } from "react-native";
12
13
  import axios from "axios";
13
14
  import AsyncStorage from "@react-native-async-storage/async-storage";
14
15
 
@@ -30,6 +31,23 @@ type T_DEEPLINK_IAP_CONTEXT = {
30
31
  userId: string;
31
32
  isIapticValidated: boolean | undefined;
32
33
  handleBuySubscription: (productId: string, offerToken?: string) => void;
34
+ trackEvent: (eventName: string) => Promise<void>;
35
+ };
36
+
37
+ type RequestBody = {
38
+ id: string;
39
+ type: string;
40
+ transaction?: {
41
+ id: string;
42
+ type: string;
43
+ appStoreReceipt?: string; // iOS-specific
44
+ purchaseToken?: string; // Android-specific
45
+ receipt?: string; // Android-specific
46
+ signature?: string; // Android-specific
47
+ };
48
+ additionalData?: {
49
+ applicationUsername: string;
50
+ };
33
51
  };
34
52
 
35
53
  const ASYNC_KEYS = {
@@ -48,6 +66,7 @@ export const DeepLinkIapContext = createContext<T_DEEPLINK_IAP_CONTEXT>({
48
66
  referrerLink: "",
49
67
  userId: "",
50
68
  handleBuySubscription: (productId: string, offerToken?: string) => {},
69
+ trackEvent: async (eventName: string) => {},
51
70
  });
52
71
 
53
72
  const DeepLinkIapProvider: React.FC<T_DEEPLINK_IAP_PROVIDER> = ({
@@ -221,55 +240,66 @@ const DeepLinkIapProvider: React.FC<T_DEEPLINK_IAP_PROVIDER> = ({
221
240
 
222
241
  const handlePurchaseValidation = async (jsonIapPurchase: Purchase) => {
223
242
  try {
224
- if (!userId || !referrerLink) {
225
- errorLog(
226
- `WANR ~ handlePurchaseValidation: No Referrer Link or User ID for validation`
227
- );
228
-
229
- await axios({
230
- url: `https://validator.iaptic.com/v1/validate`,
231
- method: "POST",
232
- headers: {
233
- Authorization: `Basic ${btoa(
234
- iapticAppName + ":" + iapticPublicKey
235
- )}`,
236
- },
237
- data: {
238
- id: iapticAppId,
239
- type: "application",
240
- transaction: {
241
- id: iapticAppId,
242
- type: "ios-appstore",
243
- appStoreReceipt: jsonIapPurchase.transactionReceipt,
244
- },
245
- },
246
- });
243
+ const baseRequestBody: RequestBody = {
244
+ id: iapticAppId,
245
+ type: "application",
246
+ };
247
+
248
+ let transaction;
249
+
250
+ if (Platform.OS === "ios") {
251
+ transaction = {
252
+ id: iapticAppId,
253
+ type: "ios-appstore",
254
+ appStoreReceipt: jsonIapPurchase.transactionReceipt,
255
+ };
256
+ } else {
257
+ const receiptJson = JSON.parse(atob(jsonIapPurchase.transactionReceipt || ""));
258
+ transaction = {
259
+ id: receiptJson.orderId, // Extracted orderId
260
+ type: "android-playstore",
261
+ purchaseToken: receiptJson.purchaseToken, // Extracted purchase token
262
+ receipt: jsonIapPurchase.transactionReceipt, // Full receipt (Base64)
263
+ signature: receiptJson.signature, // Receipt signature
264
+ };
265
+ }
266
+
267
+ const requestBody = {
268
+ ...baseRequestBody,
269
+ transaction,
270
+ };
271
+
272
+ if (userId && referrerLink) {
273
+ requestBody.additionalData = {
274
+ applicationUsername: `${referrerLink}/${userId}`,
275
+ };
276
+ }
277
+
278
+ // Send validation request to server
279
+ const response = await axios({
280
+ url: `https://validator.iaptic.com/v1/validate`,
281
+ method: "POST",
282
+ headers: {
283
+ Authorization: `Basic ${btoa(`${iapticAppName}:${iapticPublicKey}`)}`,
284
+ "Content-Type": "application/json",
285
+ },
286
+ data: requestBody,
287
+ });
288
+
289
+ if (response.status === 200) {
290
+ console.log("Validation successful:", response.data);
247
291
  setIapticValidated(true);
248
292
  } else {
249
- await axios({
250
- url: `https://validator.iaptic.com/v1/validate`,
251
- method: "POST",
252
- headers: {
253
- Authorization: `Basic ${btoa(
254
- iapticAppName + ":" + iapticPublicKey
255
- )}`,
256
- },
257
- data: {
258
- id: iapticAppId,
259
- type: "application",
260
- transaction: {
261
- id: iapticAppId,
262
- type: "ios-appstore",
263
- appStoreReceipt: jsonIapPurchase.transactionReceipt,
264
- },
265
- additionalData: {
266
- applicationUsername: `${referrerLink}/${userId}`,
267
- },
268
- },
269
- });
293
+ console.error("Validation failed:", response.data);
294
+ setIapticValidated(false);
270
295
  }
271
296
  } catch (error) {
272
- errorLog(`handlePurchaseValidation: ${error}`, "error");
297
+ if (error instanceof Error) {
298
+ console.error(`handlePurchaseValidation Error: ${error.message}`);
299
+ } else {
300
+ console.error(`handlePurchaseValidation Unknown Error: ${JSON.stringify(error)}`);
301
+ }
302
+
273
303
  setIapticValidated(false);
274
304
  }
275
305
  };
@@ -344,6 +374,40 @@ const DeepLinkIapProvider: React.FC<T_DEEPLINK_IAP_PROVIDER> = ({
344
374
  }
345
375
  };
346
376
 
377
+ const trackEvent = async (eventName: string): Promise<void> => {
378
+ try {
379
+ if (!referrerLink || !userId) {
380
+ console.warn(
381
+ "[Insert Affiliate] No affiliate identifier found. Please set one before tracking events."
382
+ );
383
+ return;
384
+ }
385
+
386
+ const payload = {
387
+ eventName,
388
+ deepLinkParam: `${referrerLink}/${userId}`, // Similar to Swift SDK
389
+ };
390
+
391
+ const response = await axios.post(
392
+ "https://api.insertaffiliate.com/v1/trackEvent",
393
+ payload,
394
+ {
395
+ headers: { "Content-Type": "application/json" },
396
+ }
397
+ );
398
+
399
+ if (response.status === 200) {
400
+ console.log("[Insert Affiliate] Event tracked successfully");
401
+ } else {
402
+ console.error(
403
+ `[Insert Affiliate] Failed to track event with status code: ${response.status}`
404
+ );
405
+ }
406
+ } catch (error) {
407
+ console.error("[Insert Affiliate] Error tracking event:", error);
408
+ }
409
+ };
410
+
347
411
  useEffect(() => {
348
412
  return () => {
349
413
  endConnection();
@@ -361,6 +425,7 @@ const DeepLinkIapProvider: React.FC<T_DEEPLINK_IAP_PROVIDER> = ({
361
425
  referrerLink,
362
426
  userId,
363
427
  handleBuySubscription,
428
+ trackEvent,
364
429
  }}
365
430
  >
366
431
  {children}
@@ -0,0 +1,6 @@
1
+ 1. Update version number in package.json...
2
+ 2. Make changes, push to main
3
+ git tag v1.0...
4
+ git push origin --tags
5
+ 3. Release version update on github
6
+ 4. Run npm publish to update - https://www.npmjs.com/package/insert-affiliate-react-native-sdk