apps-sdk 1.0.40 → 1.0.42

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/index.js CHANGED
@@ -1,4 +1,5 @@
1
- import {NotificationsPush, Networking, Storage, Session, Utils, PayWall} from "./src/libraries";
1
+ import {NotificationsPush, Networking, Storage, Session, Utils, PayWallLogic} from "./src/libraries";
2
+ import PayWall from "./src/components/PayWall";
2
3
 
3
4
  class AppsSDK {
4
5
  initializePushNotifications = async() => {
@@ -17,5 +18,6 @@ export default {
17
18
  storage: Storage,
18
19
  session: Session,
19
20
  utils: Utils,
21
+ paywallLogic: PayWallLogic,
20
22
  paywall: PayWall,
21
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apps-sdk",
3
- "version": "1.0.40",
3
+ "version": "1.0.42",
4
4
  "description": "Apps SDK",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -20,6 +20,7 @@
20
20
  "expo-sharing": "^11.10.0",
21
21
  "react-native": "^0.73.2",
22
22
  "react-native-btr": "^2.2.1",
23
+ "react-native-iap": "^12.12.2",
23
24
  "react-native-webview": "^13.8.1"
24
25
  },
25
26
  "types": "./types/index.d.ts"
@@ -1,10 +1,10 @@
1
1
  import React from 'react';
2
- import { BottomSheet } from 'react-native-btr';
3
- import { WebView } from 'react-native-webview';
4
- import { View } from "react-native";
5
- import Utils from "./Utils";
6
- import Networking from "./Networking";
7
- import Session from "./Session";
2
+ import {BottomSheet} from 'react-native-btr';
3
+ import {WebView} from 'react-native-webview';
4
+ import {View} from "react-native";
5
+ import Utils from "../libraries/Utils";
6
+ import Networking from "../libraries/Networking";
7
+ import PayWallLogic from "../libraries/PayWallLogic";
8
8
  import * as config from "../../config";
9
9
 
10
10
  class PayWall extends React.Component {
@@ -58,6 +58,10 @@ class PayWall extends React.Component {
58
58
 
59
59
  eventClickSubscribe = (data) => {
60
60
  Networking.sendEvent('action', 'cta');
61
+ if (this.paywallData && this.paywallData.products_id && this.paywallData.products_id.length > 0) {
62
+ let subscribed = this.executePurchase(this.paywallData.products_id[0]);
63
+ console.log('subscribed',subscribed)
64
+ }
61
65
  setTimeout(() => {
62
66
  this.hideSpinner();
63
67
  }, 3000);
@@ -150,6 +150,10 @@ class Networking {
150
150
  return storage.getData("ENDPOINTS");
151
151
  }
152
152
 
153
+ createSubscription = async (data) => {
154
+ return this.request(config.ENDPOINTS.SUB_NEW, data);
155
+ }
156
+
153
157
  setEvents(events) {
154
158
  events && (config.EVENTS = events);
155
159
  }
@@ -0,0 +1,100 @@
1
+ import React from 'react';
2
+ import {Platform} from "react-native";
3
+ import Networking from "./Networking";
4
+ import Session from "./Session";
5
+ import {
6
+ finishTransaction,
7
+ flushFailedPurchasesCachedAsPendingAndroid,
8
+ getSubscriptions,
9
+ initConnection,
10
+ purchaseErrorListener,
11
+ purchaseUpdatedListener, requestSubscription,
12
+ } from 'react-native-iap';
13
+
14
+ class PayWallLogic {
15
+ purchaseUpdateSubscription;
16
+ purchaseErrorSubscription;
17
+
18
+ async initializePayWall() {
19
+ initConnection().then(async () => {
20
+
21
+ if(Platform.OS === 'android'){
22
+ flushFailedPurchasesCachedAsPendingAndroid()
23
+ .catch(() => {
24
+ console.log('flushFailedPurchasesCachedAsPendingAndroid')
25
+ })
26
+ .then(async () => {
27
+ this.purchaseUpdateSubscription = purchaseUpdatedListener(
28
+ async (purchase) => {
29
+ console.log('purchaseUpdatedListener', purchase);
30
+ const receipt = purchase.transactionReceipt;
31
+ if (receipt) {
32
+ const subscription = await getSubscriptions({skus: [purchase.productId]})
33
+ const s = subscription[0];
34
+ await finishTransaction({purchase: purchase});
35
+ let data = this.purchaseResp(s,purchase.purchaseToken)
36
+ const purchaseResponse = await Networking.createSubscription(data);
37
+ if(purchaseResponse.success){
38
+ Session.setIsSubscribed(true);
39
+ Session.setSubscriptionData(purchaseResponse.data);
40
+ }
41
+ }
42
+ },
43
+ );
44
+ });
45
+ } else{
46
+ this.purchaseUpdateSubscription = purchaseUpdatedListener(
47
+ async (purchase) => {
48
+ console.log('SDK purchaseUpdatedListener purchase',purchase)
49
+ const subscription = await getSubscriptions({skus: [purchase.productId]})
50
+ const s = subscription[0];
51
+ await finishTransaction({purchase: purchase,isConsumable:true});
52
+ let data = this.purchaseResp(s,(purchase.originalTransactionIdentifierIOS ? purchase.originalTransactionIdentifierIOS : purchase.transactionId))
53
+ const purchaseResponse = await Networking.createSubscription(data);
54
+ if(purchaseResponse.success){
55
+ Session.setIsSubscribed(true);
56
+ Session.setSubscriptionData(purchaseResponse.data);
57
+ } else {
58
+ console.log('SDK checkSubscription fail')
59
+ }
60
+ },
61
+ );
62
+ }
63
+
64
+ this.purchaseErrorSubscription = purchaseErrorListener(
65
+ (error) => {
66
+ console.log('purchaseErrorListener', error);
67
+ },
68
+ );
69
+
70
+ });
71
+ }
72
+
73
+ purchaseResp = (subscription, purchaseToken) => {
74
+ return {
75
+ subs_id: purchaseToken,
76
+ product_id: subscription.productId,
77
+ };
78
+ }
79
+
80
+ executePurchase = async (productID) => {
81
+ let subscribe = false;
82
+ try {
83
+ const subscriptionTemplates = await getSubscriptions({skus: [productID]});
84
+ if (subscriptionTemplates.length > 0) {
85
+ const subscription = subscriptionTemplates[0];
86
+ const sku = subscription.productId;
87
+ const offerToken = Platform.OS === 'android' ? subscription.subscriptionOfferDetails[0].offerToken : false;
88
+ await requestSubscription({
89
+ sku,
90
+ ...(offerToken && {subscriptionOffers: [{sku, offerToken}]}),
91
+ });
92
+ }
93
+ } catch (err) {
94
+ console.warn(err.code, err.message);
95
+ }
96
+ return subscribe;
97
+ };
98
+ }
99
+
100
+ export default new PayWallLogic();
@@ -11,6 +11,7 @@ class Session {
11
11
  sessionID = null;
12
12
  isFirstOpen = false;
13
13
  isSubscribed = false;
14
+ subscriptionData = {};
14
15
  isDevUser = false;
15
16
 
16
17
  init = async () => {
@@ -116,6 +117,10 @@ class Session {
116
117
  this.isSubscribed = isSubscribed;
117
118
  }
118
119
 
120
+ setSubscriptionData = (subscriptionData) => {
121
+ this.subscriptionData = subscriptionData;
122
+ }
123
+
119
124
  setIsDevUser = (isDevUser) => {
120
125
  this.isDevUser = isDevUser;
121
126
  }
@@ -3,4 +3,4 @@ export { default as Networking } from './Networking';
3
3
  export { default as Storage } from './Storage';
4
4
  export { default as Session } from './Session';
5
5
  export { default as Utils } from './Utils';
6
- export { default as PayWall } from './PayWall';
6
+ export { default as PayWallLogic } from './PayWallLogic';
package/types/index.d.ts CHANGED
@@ -45,6 +45,7 @@ declare module 'apps-sdk' {
45
45
  setIsDevUser(isDevUser: boolean): void;
46
46
  setUserID(userID: string): void;
47
47
  getUserID(): string | null;
48
+ setSubscriptionData(subscriptionData: any): void;
48
49
  }
49
50
 
50
51
  export class Networking {
@@ -58,6 +59,7 @@ declare module 'apps-sdk' {
58
59
  getEndpoints(): any;
59
60
  setEvents(events: any): void;
60
61
  sendEvent(eventType: string, eventKeyword:string, eventData?: object): Promise<void>;
62
+ createSubscription()
61
63
  }
62
64
 
63
65
  export class Storage {
@@ -94,6 +96,11 @@ declare module 'apps-sdk' {
94
96
 
95
97
  export class PayWall extends Component<PayWallProps, {}> {}
96
98
 
99
+ export class PayWallLogic {
100
+ initializePayWall(): Promise<void>;
101
+ executePurchase(productID: string): Promise<void>;
102
+ }
103
+
97
104
  export class AppsSDK {
98
105
  initializePushNotifications(): Promise<string>;
99
106
  }