apps-sdk 1.0.53 → 1.0.55

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/config.js CHANGED
@@ -21,7 +21,10 @@ export var SPECIAL_EVENTS = ['picture_upload', 'write_prompt', 'picture_generate
21
21
  export var PAYWALL_DATA = {
22
22
  actions: {},
23
23
  scenes: {},
24
- others: {}
24
+ others: {},
25
+ products: {},
26
+ products_metadata: {},
27
+ paywall_info: {},
25
28
  }
26
29
 
27
30
  export const EVENT_TYPES = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apps-sdk",
3
- "version": "1.0.53",
3
+ "version": "1.0.55",
4
4
  "description": "Apps SDK",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,6 +10,7 @@
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
12
  "@react-native-async-storage/async-storage": "^1.21.0",
13
+ "@react-native-community/netinfo": "^11.3.0",
13
14
  "crypto-es": "^2.1.0",
14
15
  "expo-constants": "^15.4.5",
15
16
  "expo-device": "^5.9.3",
@@ -1,10 +1,11 @@
1
- import React from 'react';
1
+ import React, {useEffect} from 'react';
2
2
  import {BottomSheet} from 'react-native-btr';
3
3
  import {WebView} from 'react-native-webview';
4
4
  import {View} from "react-native";
5
5
  import Utils from "../libraries/Utils";
6
6
  import Networking from "../libraries/Networking";
7
7
  import PayWallLogic from "../libraries/PayWallLogic";
8
+ import Session from "../libraries/Session";
8
9
  import * as config from "../../config";
9
10
 
10
11
  class PayWall extends React.Component {
@@ -14,15 +15,23 @@ class PayWall extends React.Component {
14
15
  this.paywallData = {};
15
16
  }
16
17
 
17
- getURL = () => {
18
- // CP de prueba
19
- return "https://backend.ailandsapp.com/payment-card/ZLp_slash_QSy94h9aKdNJWvvvkE8dkHSyeiTGRxsZVQAaiiYveh2jTjeZVJnnIZ0WVKTgiRoAkEoVrB2leXjt_slash_l2g2MtLKRa_slash_30mL6tMu0L9IJxY=?dsn_id=6274";
18
+ initPayWall = async () => {
19
+ await PayWallLogic.initializePayWall();
20
20
  }
21
21
 
22
- getPayWallJS = () => {
23
- // return "window.subInfoInterval = setInterval(() => window.Appquiles && Appquiles.setSubscriptionPlans(" + JSON.stringify(information.subsPlansInfo) + "), 300);" : '';
24
- return "window.subInfoInterval = setInterval(() => window.Appquiles, 300);";
22
+ getURL = (type, keyword) => {
23
+ return config.PAYWALL_DATA[type][keyword].url || '';
24
+ }
25
+
26
+ getPayWallJS = async () => {
27
+ PayWallLogic.requestPaymentInfo().then((payWallInfo) => {
28
+ return "window.subInfoInterval = setInterval(() => window.Appquiles && Appquiles.setSubscriptionPlans(" + JSON.stringify(payWallInfo) + "), 300);";
29
+ });
30
+ }
25
31
 
32
+ getDesignIDFromURL = (url) => {
33
+ const urlObject = new URL(url);
34
+ return urlObject.searchParams.get('dsn_id') || '';
26
35
  }
27
36
 
28
37
  getPayWallData = (type, keyword) => {
@@ -30,7 +39,6 @@ class PayWall extends React.Component {
30
39
  console.log('No se encontraron datos para el paywall.');
31
40
  return;
32
41
  }
33
- console.log('PayWallData: ', JSON.stringify(config.PAYWALL_DATA[type][keyword]));
34
42
  this.paywallData = config.PAYWALL_DATA[type][keyword];
35
43
  }
36
44
 
@@ -44,12 +52,17 @@ class PayWall extends React.Component {
44
52
  }
45
53
  }
46
54
 
47
- componentDidUpdate(prevProps) {
55
+ async componentDidUpdate(prevProps) {
48
56
  if (this.props.visible && !prevProps.visible) {
49
- this.webviewRef.current.injectJavaScript(this.getPayWallJS());
57
+ this.webviewRef.current.injectJavaScript(await this.getPayWallJS());
58
+ Networking.sendEvent('scene', 'payment_card', { user_id: Session.getUserID(), card_id: this.getCardID(), scene: 'payment_card' });
50
59
  }
51
60
  }
52
61
 
62
+ getCardID = () => {
63
+ return this.getDesignIDFromURL(this.paywallData.url);
64
+ }
65
+
53
66
  // --------------------------------------- Eventos ---------------------------------------
54
67
  eventClickClose = (data) => {
55
68
  Networking.sendEvent('action', 'continue_free');
@@ -59,12 +72,13 @@ class PayWall extends React.Component {
59
72
  eventClickSubscribe = (data) => {
60
73
  Networking.sendEvent('action', 'cta');
61
74
  if (this.paywallData && this.paywallData.products_id && this.paywallData.products_id.length > 0) {
62
- let subscribed = PayWallLogic.executePurchase(this.paywallData.products_id[0]);
63
- console.log('subscribed',subscribed)
75
+ let subscribed = PayWallLogic.executePurchase(this.paywallData.products_id[0], this.hideSpinner).then((subscribed) => {
76
+ console.log('subscription result: ',subscribed);
77
+ if (subscribed) {
78
+ this.props.onSubscribe();
79
+ }
80
+ });
64
81
  }
65
- setTimeout(() => {
66
- this.hideSpinner();
67
- }, 3000);
68
82
  }
69
83
 
70
84
  eventClickFreeSubscription = (data) => {
@@ -1,4 +1,5 @@
1
1
  import * as config from '../../config';
2
+ import NetInfo from "@react-native-community/netinfo";
2
3
  import { default as storage } from './Storage';
3
4
  import Session from './Session';
4
5
  import AdJust from './AdJust';
@@ -27,6 +28,7 @@ class Networking {
27
28
  this.setEndpoints(initData.data.domains);
28
29
  this.setEvents(initData.data.attribution);
29
30
  this.setPayWallData(initData);
31
+ this.checkSubscription();
30
32
  }
31
33
  } catch (error) {
32
34
  console.error(error);
@@ -52,13 +54,21 @@ class Networking {
52
54
  try {
53
55
  const installID = await storage.getData('install_id');
54
56
  const result = this.request(config.ENDPOINTS.NOTIFICATION_SET_TOKEN, { expo_id: token });
55
- console.log('setToken result: ', result);
56
57
  } catch (error) {
57
58
  console.error(error);
58
59
  return null;
59
60
  }
60
61
  }
61
62
 
63
+ async checkConnection() {
64
+ try {
65
+ const state = await NetInfo.fetch();
66
+ return state.isConnected;
67
+ } catch (error) {
68
+ console.error(error);
69
+ return false;
70
+ }
71
+ }
62
72
 
63
73
  async request(url, data = {}, encrypt = this.DEFAULT_ENCRYPT_VALUE) {
64
74
  data = { ...Session.sessionData, ...data };
@@ -130,6 +140,16 @@ class Networking {
130
140
  return this.request(config.ENDPOINTS.SUB_NEW, data);
131
141
  }
132
142
 
143
+ checkSubscription = async () => {
144
+ let subsID = await storage.getData('userID');
145
+ let subStatus = await this.request(config.ENDPOINTS.SUB_STATUS, {...Session.sessionData, subs_id: subsID});
146
+ console.log('checkSubscription', subStatus);
147
+ if (subStatus && subStatus.success === 1) {
148
+ Session.setIsSubscribed(subStatus.data.subscription_active);
149
+ Session.setSubscriptionData(subStatus.data);
150
+ }
151
+ }
152
+
133
153
  setEvents(events) {
134
154
  events && (config.EVENTS = events);
135
155
  }
@@ -159,8 +179,11 @@ class Networking {
159
179
  }
160
180
  }
161
181
 
182
+ result.products = data.data.product_ids;
183
+
162
184
  if (result) {
163
185
  config.PAYWALL_DATA = result;
186
+ console.log('PAYWALL_DATA', config.PAYWALL_DATA);
164
187
  }
165
188
  }
166
189
 
@@ -173,10 +196,11 @@ class Networking {
173
196
  try {
174
197
  AdJust.trackEventIfExist(eventKeyword);
175
198
  let eventResponse = await this.request(config.ENDPOINTS.EVENTS_PUSH, {
176
- event_name: eventType,
199
+ event_name: eventKeyword,
177
200
  event_type: eventType,
178
- action: eventKeyword,
179
- data: eventData,
201
+ // action: eventKeyword,
202
+ appevent: eventKeyword,
203
+ payload: eventData,
180
204
  ...Session.sessionData,
181
205
  });
182
206
  if (eventResponse) {
@@ -2,6 +2,8 @@ import React from 'react';
2
2
  import {Platform} from "react-native";
3
3
  import Networking from "./Networking";
4
4
  import Session from "./Session";
5
+ import * as config from "../../config";
6
+
5
7
  import {
6
8
  finishTransaction,
7
9
  flushFailedPurchasesCachedAsPendingAndroid,
@@ -39,6 +41,7 @@ class PayWallLogic {
39
41
  const purchaseResponse = await Networking.createSubscription(data);
40
42
  if(purchaseResponse.success){
41
43
  Session.setIsSubscribed(true);
44
+ Session.setSubscriptionID(data.subs_id);
42
45
  Session.setSubscriptionData(purchaseResponse.data);
43
46
  }
44
47
  }
@@ -59,6 +62,7 @@ class PayWallLogic {
59
62
  const purchaseResponse = await Networking.createSubscription(data);
60
63
  if(purchaseResponse.success){
61
64
  Session.setIsSubscribed(true);
65
+ Session.setSubscriptionID(data.subs_id);
62
66
  Session.setSubscriptionData(purchaseResponse.data);
63
67
  } else {
64
68
  console.log('SDK checkSubscription fail')
@@ -87,8 +91,8 @@ class PayWallLogic {
87
91
  };
88
92
  }
89
93
 
90
- executePurchase = async (productID) => {
91
- let subscribe = false;
94
+ executePurchase = async (productID, hideLoaderCallback) => {
95
+ let subscribed = false;
92
96
  try {
93
97
  const subscriptionTemplates = await getSubscriptions({skus: [productID]});
94
98
  console.log('subscriptionTemplates', subscriptionTemplates);
@@ -96,16 +100,90 @@ class PayWallLogic {
96
100
  const subscription = subscriptionTemplates[0];
97
101
  const sku = subscription.productId;
98
102
  const offerToken = Platform.OS === 'android' ? subscription.subscriptionOfferDetails[0].offerToken : false;
103
+ if (hideLoaderCallback) {
104
+ setTimeout(() => {
105
+ hideLoaderCallback();
106
+ }, 15000);
107
+ }
99
108
  await requestSubscription({
100
109
  sku,
101
110
  ...(offerToken && {subscriptionOffers: [{sku, offerToken}]}),
102
111
  });
112
+ console.log('paso 2');
113
+ subscribed = true;
103
114
  }
104
115
  } catch (err) {
105
- console.warn(err.code, err.message);
116
+ console.log(err.code, err.message);
106
117
  }
107
- return subscribe;
118
+
119
+ return subscribed;
108
120
  };
121
+
122
+ getSubscriptionInfo = async (productIDs) => {
123
+ let subscriptionsData = {};
124
+ try {
125
+ const subscriptionTemplates = await getSubscriptions({skus: productIDs});
126
+ if (subscriptionTemplates.length > 0) {
127
+ subscriptionTemplates.forEach(product => {
128
+ subscriptionsData[product.productId] = product;
129
+ });
130
+ config.PAYWALL_DATA.products_metadata = subscriptionsData;
131
+ console.log('products_metadata', config.PAYWALL_DATA.products_metadata);
132
+ }
133
+ } catch (err) {
134
+ console.log(err.code, err.message);
135
+ return {};
136
+ }
137
+ console.log('subscription Metadata: ', subscriptionsData);
138
+ return subscriptionsData;
139
+ }
140
+
141
+ requestPaymentInfo = async () => {
142
+ await this.getSubscriptionInfo(this.getProductIDs());
143
+ return this.preparePayWallPurchaseInfo();
144
+ }
145
+
146
+ preparePayWallPurchaseInfo = () => {
147
+ let payWallInfo = [];
148
+
149
+ if (config.PAYWALL_DATA && config.PAYWALL_DATA.products_metadata) {
150
+ for (let key in config.PAYWALL_DATA.products_metadata) {
151
+ let product = config.PAYWALL_DATA.products_metadata[key];
152
+ payWallInfo.push({
153
+ productIdentifier: product.productId,
154
+ localizedTitle: product.title,
155
+ description: product.description,
156
+ localizedPrice: product.localizedPrice,
157
+ price: product.price,
158
+ type: product.type,
159
+ platform: product.platform,
160
+ currency: product.currency,
161
+ // duration: product.subscriptionPeriod,
162
+ freePeriod: '',
163
+ subscriptionUnits: product.subscriptionPeriodNumberIOS || product.subscriptionPeriodNumberAndroid || 1,
164
+ subscriptionPeriod: this.getPeriodInt(product.subscriptionPeriodUnitIOS || product.subscriptionPeriodUnitAndroid || 'M'),
165
+ units: product.subscriptionPeriodNumberIOS || product.subscriptionPeriodNumberAndroid || 1,
166
+ period: product.subscriptionPeriodUnitIOS || product.subscriptionPeriodUnitAndroid || 'MONTH',
167
+ });
168
+ }
169
+ config.PAYWALL_DATA.paywall_info = payWallInfo;
170
+ console.log('paywall_info', config.PAYWALL_DATA.paywall_info);
171
+ }
172
+ return payWallInfo;
173
+ }
174
+
175
+ getProductIDs = () => {
176
+ return config.PAYWALL_DATA.products;
177
+ }
178
+
179
+ getPeriodInt(period) {
180
+ return {
181
+ D: 0,
182
+ W: 1,
183
+ M: 2,
184
+ Y: 3,
185
+ }[period.charAt(0)];
186
+ }
109
187
  }
110
188
 
111
189
  export default new PayWallLogic();
@@ -114,13 +114,19 @@ class Session {
114
114
  }
115
115
 
116
116
  setIsSubscribed = (isSubscribed) => {
117
+ Storage.storeData("isSubscribed", isSubscribed);
117
118
  this.isSubscribed = isSubscribed;
118
119
  }
119
120
 
120
121
  setSubscriptionData = (subscriptionData) => {
122
+ Storage.storeData("subscriptionData", subscriptionData);
121
123
  this.subscriptionData = subscriptionData;
122
124
  }
123
125
 
126
+ setSubscriptionID = (subscriptionID) => {
127
+ Storage.storeData("subscriptionID", subscriptionID);
128
+ }
129
+
124
130
  setIsDevUser = (isDevUser) => {
125
131
  this.isDevUser = isDevUser;
126
132
  }
@@ -143,6 +149,28 @@ class Session {
143
149
  return null;
144
150
  }
145
151
  }
152
+
153
+ checkSubscription = async () => {
154
+ config.DEBUG_MODE && console.debug("checkSubscription");
155
+ try {
156
+ let result = Networking.checkSubscription();
157
+ config.DEBUG_MODE && console.debug("checkSubscription - result: ", result);
158
+ } catch (error) {
159
+ console.error(error);
160
+ return null;
161
+ }
162
+ }
163
+
164
+ checkSubscription24h = async () => {
165
+ config.DEBUG_MODE && console.debug("checkSubscription24h");
166
+ let lastCheck = await Storage.getData("lastSubscriptionCheck");
167
+ if (!lastCheck || (new Date().getTime() - lastCheck) > 86400000) {
168
+ await this.checkSubscription();
169
+ await Storage.storeData("lastSubscriptionCheck", new Date().getTime());
170
+ } else {
171
+ config.DEBUG_MODE && console.debug("checkSubscription24h - Not 24h yet");
172
+ }
173
+ }
146
174
  }
147
175
 
148
176
  export default new Session();
package/types/index.d.ts CHANGED
@@ -35,6 +35,7 @@ declare module 'apps-sdk' {
35
35
  storeSessionStructure(): Promise<void>;
36
36
  checkFirstOpen(): Promise<void>;
37
37
  checkUserID(): Promise<void>;
38
+ setSubscriptionID(subscriptionID: string): void;
38
39
  generateSessionID(): string;
39
40
  getSessionData(): SessionData;
40
41
  getSessionID(): string | null;
@@ -46,6 +47,7 @@ declare module 'apps-sdk' {
46
47
  setUserID(userID: string): void;
47
48
  getUserID(): string | null;
48
49
  setSubscriptionData(subscriptionData: any): void;
50
+ checkSubscription24h(): Promise<void>;
49
51
  }
50
52
 
51
53
  export class Networking {
@@ -58,7 +60,8 @@ declare module 'apps-sdk' {
58
60
  getEndpoints(): any;
59
61
  setEvents(events: any): void;
60
62
  sendEvent(eventType: string, eventKeyword:string, eventData?: object): Promise<void>;
61
- createSubscription()
63
+ createSubscription();
64
+ checkConnection(): Promise<boolean>;
62
65
  }
63
66
 
64
67
  export class Storage {