@purchasely/cordova-plugin-purchasely 2.3.1 → 4.1.0
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/example/android.sh +8 -0
- package/example/config.xml +1 -1
- package/example/ios.sh +16 -0
- package/example/package-lock.json +410 -482
- package/example/package.json +5 -4
- package/example/www/index.html +12 -0
- package/example/www/js/index.js +113 -9
- package/package.json +9 -1
- package/plugin.xml +16 -5
- package/purchasely.iml +11 -0
- package/src/android/PLYProductActivity.kt +171 -0
- package/src/android/PLYSubscriptionsActivity.java +1 -1
- package/src/android/PurchaselyPlugin.kt +916 -0
- package/src/android/activity_ply_product_activity.xml +2 -2
- package/src/android/activity_ply_subscriptions_activity.xml +6 -0
- package/src/ios/CDVPurchasely.h +23 -6
- package/src/ios/CDVPurchasely.m +432 -35
- package/src/ios/Hybrid/PLYPlan+Hybrid.m +0 -1
- package/src/ios/Hybrid/PLYPresentationPlan+Hybrid.h +20 -0
- package/src/ios/Hybrid/PLYPresentationPlan+Hybrid.m +31 -0
- package/src/ios/Hybrid/Purchasely_Hybrid.h +1 -0
- package/src/ios/Hybrid/UIColor+PLYHelper.h +19 -0
- package/src/ios/Hybrid/UIColor+PLYHelper.m +66 -0
- package/www/Purchasely.js +90 -28
- package/src/android/PLYProductActivity.java +0 -87
- package/src/android/PurchaselyPlugin.java +0 -739
|
@@ -1,739 +0,0 @@
|
|
|
1
|
-
package cordova.plugin.purchasely;
|
|
2
|
-
|
|
3
|
-
import android.app.Activity;
|
|
4
|
-
import android.content.Intent;
|
|
5
|
-
import android.net.Uri;
|
|
6
|
-
import android.util.Log;
|
|
7
|
-
|
|
8
|
-
import androidx.annotation.NonNull;
|
|
9
|
-
import androidx.fragment.app.FragmentActivity;
|
|
10
|
-
|
|
11
|
-
import org.apache.cordova.CallbackContext;
|
|
12
|
-
import org.apache.cordova.CordovaInterface;
|
|
13
|
-
import org.apache.cordova.CordovaPlugin;
|
|
14
|
-
import org.apache.cordova.PluginResult;
|
|
15
|
-
import org.jetbrains.annotations.NotNull;
|
|
16
|
-
import org.jetbrains.annotations.Nullable;
|
|
17
|
-
import org.json.JSONArray;
|
|
18
|
-
import org.json.JSONException;
|
|
19
|
-
import org.json.JSONObject;
|
|
20
|
-
import org.w3c.dom.Attr;
|
|
21
|
-
|
|
22
|
-
import java.lang.ref.WeakReference;
|
|
23
|
-
import java.util.ArrayList;
|
|
24
|
-
import java.util.HashMap;
|
|
25
|
-
import java.util.Iterator;
|
|
26
|
-
import java.util.List;
|
|
27
|
-
import java.util.Locale;
|
|
28
|
-
import java.util.Map;
|
|
29
|
-
import java.util.function.BiConsumer;
|
|
30
|
-
|
|
31
|
-
import io.purchasely.billing.Store;
|
|
32
|
-
import io.purchasely.ext.Attribute;
|
|
33
|
-
import io.purchasely.ext.DistributionType;
|
|
34
|
-
import io.purchasely.ext.LogLevel;
|
|
35
|
-
import io.purchasely.ext.PLYAppTechnology;
|
|
36
|
-
import io.purchasely.ext.PLYPaywallActionListener;
|
|
37
|
-
import io.purchasely.ext.PLYPresentationAction;
|
|
38
|
-
import io.purchasely.ext.PLYProcessActionListener;
|
|
39
|
-
import io.purchasely.ext.PLYProductViewResult;
|
|
40
|
-
import io.purchasely.ext.PLYRunningMode;
|
|
41
|
-
import io.purchasely.ext.PlanListener;
|
|
42
|
-
import io.purchasely.ext.ProductListener;
|
|
43
|
-
import io.purchasely.ext.ProductsListener;
|
|
44
|
-
import io.purchasely.ext.Purchasely;
|
|
45
|
-
import io.purchasely.ext.State;
|
|
46
|
-
import io.purchasely.ext.StoreType;
|
|
47
|
-
import io.purchasely.ext.SubscriptionsListener;
|
|
48
|
-
import io.purchasely.models.PLYPlan;
|
|
49
|
-
import io.purchasely.models.PLYProduct;
|
|
50
|
-
import io.purchasely.models.PLYSubscriptionData;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* This class echoes a string called from JavaScript.
|
|
54
|
-
*/
|
|
55
|
-
public class PurchaselyPlugin extends CordovaPlugin {
|
|
56
|
-
|
|
57
|
-
static CallbackContext defaultCallback = null;
|
|
58
|
-
static CallbackContext presentationCallback = null;
|
|
59
|
-
static CallbackContext eventsCallback = null;
|
|
60
|
-
static ProductActivity productActivity = null;
|
|
61
|
-
|
|
62
|
-
@Nullable private PLYProcessActionListener processActionListener;
|
|
63
|
-
@Nullable private PLYPresentationAction paywallAction;
|
|
64
|
-
|
|
65
|
-
private static int runningModeTransactionOnly = 0;
|
|
66
|
-
private static int runningModeObserver = 1;
|
|
67
|
-
private static int runningModePaywallOnly = 2;
|
|
68
|
-
private static int runningModePaywallObserver = 3;
|
|
69
|
-
private static int runningModeFull = 4;
|
|
70
|
-
|
|
71
|
-
@Override
|
|
72
|
-
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
|
|
73
|
-
try {
|
|
74
|
-
switch (action) {
|
|
75
|
-
case "startWithAPIKey":
|
|
76
|
-
startWithAPIKey(
|
|
77
|
-
getStringFromJson(args.getString(0)),
|
|
78
|
-
args.getJSONArray(1),
|
|
79
|
-
getStringFromJson(args.getString(2)),
|
|
80
|
-
args.getInt(3),
|
|
81
|
-
args.getInt(4),
|
|
82
|
-
getStringFromJson(args.getString(5)),
|
|
83
|
-
callbackContext);
|
|
84
|
-
break;
|
|
85
|
-
case "close":
|
|
86
|
-
close();
|
|
87
|
-
break;
|
|
88
|
-
case "addEventsListener":
|
|
89
|
-
addEventsListener(callbackContext);
|
|
90
|
-
break;
|
|
91
|
-
case "removeEventsListener":
|
|
92
|
-
removeEventsListener();
|
|
93
|
-
break;
|
|
94
|
-
case "getAnonymousUserId":
|
|
95
|
-
getAnonymousUserId(callbackContext);
|
|
96
|
-
break;
|
|
97
|
-
case "userLogin":
|
|
98
|
-
userLogin(getStringFromJson(args.getString(0)), callbackContext);
|
|
99
|
-
break;
|
|
100
|
-
case "userLogout":
|
|
101
|
-
userLogout();
|
|
102
|
-
break;
|
|
103
|
-
case "setLanguage":
|
|
104
|
-
setLanguage(getStringFromJson(args.getString(0)));
|
|
105
|
-
break;
|
|
106
|
-
case "setLogLevel":
|
|
107
|
-
setLogLevel(args.getInt(0));
|
|
108
|
-
break;
|
|
109
|
-
case "setAttribute":
|
|
110
|
-
setAttribute(args.getInt(0), getStringFromJson(args.getString(1)));
|
|
111
|
-
break;
|
|
112
|
-
case "setDefaultPresentationResultHandler":
|
|
113
|
-
setDefaultPresentationResultHandler(callbackContext);
|
|
114
|
-
break;
|
|
115
|
-
case "purchasedSubscription":
|
|
116
|
-
purchasedSubscription(callbackContext);
|
|
117
|
-
break;
|
|
118
|
-
case "isReadyToPurchase":
|
|
119
|
-
isReadyToPurchase(args.getBoolean(0));
|
|
120
|
-
break;
|
|
121
|
-
case "synchronize":
|
|
122
|
-
synchronize();
|
|
123
|
-
break;
|
|
124
|
-
case "presentPresentationWithIdentifier":
|
|
125
|
-
presentPresentationWithIdentifier(
|
|
126
|
-
getStringFromJson(args.getString(0)),
|
|
127
|
-
getStringFromJson(args.getString(1)),
|
|
128
|
-
args.getBoolean(2),
|
|
129
|
-
callbackContext
|
|
130
|
-
);
|
|
131
|
-
break;
|
|
132
|
-
case "presentPresentationForPlacement":
|
|
133
|
-
presentPresentationForPlacement(
|
|
134
|
-
getStringFromJson(args.getString(0)),
|
|
135
|
-
getStringFromJson(args.getString(1)),
|
|
136
|
-
args.getBoolean(2),
|
|
137
|
-
callbackContext
|
|
138
|
-
);
|
|
139
|
-
break;
|
|
140
|
-
case "presentProductWithIdentifier":
|
|
141
|
-
presentProductWithIdentifier(
|
|
142
|
-
getStringFromJson(args.getString(0)),
|
|
143
|
-
getStringFromJson(args.getString(1)),
|
|
144
|
-
getStringFromJson(args.getString(2)),
|
|
145
|
-
args.getBoolean(3),
|
|
146
|
-
callbackContext
|
|
147
|
-
);
|
|
148
|
-
break;
|
|
149
|
-
case "presentPlanWithIdentifier":
|
|
150
|
-
presentPlanWithIdentifier(
|
|
151
|
-
getStringFromJson(args.getString(0)),
|
|
152
|
-
getStringFromJson(args.getString(1)),
|
|
153
|
-
getStringFromJson(args.getString(2)),
|
|
154
|
-
args.getBoolean(3),
|
|
155
|
-
callbackContext
|
|
156
|
-
);
|
|
157
|
-
break;
|
|
158
|
-
case "presentSubscriptions":
|
|
159
|
-
presentSubscriptions();
|
|
160
|
-
break;
|
|
161
|
-
case "restoreAllProducts":
|
|
162
|
-
restoreAllProducts(callbackContext);
|
|
163
|
-
break;
|
|
164
|
-
case "silentRestoreAllProducts":
|
|
165
|
-
restoreAllProducts(callbackContext);
|
|
166
|
-
break;
|
|
167
|
-
case "userSubscriptions":
|
|
168
|
-
userSubscriptions(callbackContext);
|
|
169
|
-
break;
|
|
170
|
-
case "handle":
|
|
171
|
-
handle(getStringFromJson(args.getString(0)), callbackContext);
|
|
172
|
-
break;
|
|
173
|
-
case "allProducts":
|
|
174
|
-
allProducts(callbackContext);
|
|
175
|
-
break;
|
|
176
|
-
case "productWithIdentifier":
|
|
177
|
-
productWithIdentifier(getStringFromJson(args.getString(0)), callbackContext);
|
|
178
|
-
break;
|
|
179
|
-
case "planWithIdentifier":
|
|
180
|
-
planWithIdentifier(getStringFromJson(args.getString(0)), callbackContext);
|
|
181
|
-
break;
|
|
182
|
-
case "purchaseWithPlanVendorId":
|
|
183
|
-
purchaseWithPlanVendorId(
|
|
184
|
-
getStringFromJson(args.getString(0)),
|
|
185
|
-
getStringFromJson(args.getString(1)),
|
|
186
|
-
callbackContext
|
|
187
|
-
);
|
|
188
|
-
break;
|
|
189
|
-
case "setPaywallActionInterceptor":
|
|
190
|
-
setPaywallActionInterceptor(callbackContext);
|
|
191
|
-
break;
|
|
192
|
-
case "onProcessAction":
|
|
193
|
-
onProcessAction(args.getBoolean(0));
|
|
194
|
-
break;
|
|
195
|
-
case "closePaywall":
|
|
196
|
-
closePaywall(callbackContext);
|
|
197
|
-
break;
|
|
198
|
-
case "userDidConsumeSubscriptionContent":
|
|
199
|
-
userDidConsumeSubscriptionContent();
|
|
200
|
-
break;
|
|
201
|
-
default:
|
|
202
|
-
return false;
|
|
203
|
-
}
|
|
204
|
-
} catch (JSONException e) {
|
|
205
|
-
Log.e("Purchasely", String.format("Error executing action %s", action), e);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return true;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
private String getStringFromJson(String value) {
|
|
212
|
-
if(value == null || value.equals("null") || value.isEmpty()) {
|
|
213
|
-
return null;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
return value;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
public static void sendPurchaseResult(PLYProductViewResult result, PLYPlan plan) {
|
|
220
|
-
int productViewResult = 0;
|
|
221
|
-
if(result == PLYProductViewResult.PURCHASED) {
|
|
222
|
-
productViewResult = PLYProductViewResult.PURCHASED.ordinal();
|
|
223
|
-
} else if(result == PLYProductViewResult.CANCELLED) {
|
|
224
|
-
productViewResult = PLYProductViewResult.CANCELLED.ordinal();
|
|
225
|
-
} else if(result == PLYProductViewResult.RESTORED) {
|
|
226
|
-
productViewResult = PLYProductViewResult.RESTORED.ordinal();
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
HashMap<String, Object> map = new HashMap<>();
|
|
230
|
-
map.put("result", productViewResult);
|
|
231
|
-
map.put("plan", transformPlanToMap(plan));
|
|
232
|
-
|
|
233
|
-
if(presentationCallback != null) {
|
|
234
|
-
presentationCallback.success(new JSONObject(map));
|
|
235
|
-
presentationCallback = null;
|
|
236
|
-
} else if(defaultCallback != null) {
|
|
237
|
-
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, new JSONObject(map));
|
|
238
|
-
pluginResult.setKeepCallback(true);
|
|
239
|
-
defaultCallback.sendPluginResult(pluginResult);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
private void startWithAPIKey(String apiKey,
|
|
244
|
-
JSONArray stores,
|
|
245
|
-
String userId,
|
|
246
|
-
int logLevel,
|
|
247
|
-
int runningMode,
|
|
248
|
-
String cordovaSdkVersion,
|
|
249
|
-
CallbackContext callbackContext) {
|
|
250
|
-
ArrayList<String> list = new ArrayList<>();
|
|
251
|
-
for (int i=0; i< stores.length(); i++) {
|
|
252
|
-
try {
|
|
253
|
-
list.add(stores.getString(i) );
|
|
254
|
-
} catch (JSONException e) {
|
|
255
|
-
Log.e("Purchasely", "Error in store array" + e.getMessage(), e);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
ArrayList<Store> storesInstances = getStoresInstances(list);
|
|
259
|
-
|
|
260
|
-
PLYRunningMode plyRunningMode = PLYRunningMode.Full.INSTANCE;
|
|
261
|
-
if(runningMode == runningModeTransactionOnly) plyRunningMode = PLYRunningMode.TransactionOnly.INSTANCE;
|
|
262
|
-
else if(runningMode == runningModeObserver) plyRunningMode = PLYRunningMode.Observer.INSTANCE;
|
|
263
|
-
else if(runningMode == runningModePaywallOnly) plyRunningMode = PLYRunningMode.PaywallOnly.INSTANCE;
|
|
264
|
-
else if(runningMode == runningModePaywallObserver) plyRunningMode = PLYRunningMode.PaywallObserver.INSTANCE;
|
|
265
|
-
|
|
266
|
-
new Purchasely.Builder(cordova.getContext())
|
|
267
|
-
.apiKey(apiKey)
|
|
268
|
-
.stores(storesInstances)
|
|
269
|
-
.userId(userId)
|
|
270
|
-
.runningMode(plyRunningMode)
|
|
271
|
-
.logLevel(LogLevel.values()[logLevel])
|
|
272
|
-
.build();
|
|
273
|
-
|
|
274
|
-
Purchasely.setSdkBridgeVersion(cordovaSdkVersion);
|
|
275
|
-
Purchasely.setAppTechnology(PLYAppTechnology.CORDOVA);
|
|
276
|
-
|
|
277
|
-
Purchasely.start((isConfigured, error) -> {
|
|
278
|
-
if(isConfigured) {
|
|
279
|
-
callbackContext.success();
|
|
280
|
-
} else {
|
|
281
|
-
callbackContext.error(
|
|
282
|
-
error != null ? error.getMessage() : "Purchasely SDK not configured"
|
|
283
|
-
);
|
|
284
|
-
}
|
|
285
|
-
return null;
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
private ArrayList<Store> getStoresInstances(List<String> stores) {
|
|
290
|
-
ArrayList<Store> result = new ArrayList<>();
|
|
291
|
-
if (stores.contains("Google") && Package.getPackage("io.purchasely.google") != null) {
|
|
292
|
-
try {
|
|
293
|
-
result.add((Store) Class.forName("io.purchasely.google.GoogleStore").newInstance());
|
|
294
|
-
} catch (Exception e) {
|
|
295
|
-
Log.e("Purchasely", "Google Store not found :" + e.getMessage(), e);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
if (stores.contains("Huawei") && Package.getPackage("io.purchasely.huawei") != null) {
|
|
299
|
-
try {
|
|
300
|
-
result.add((Store) Class.forName("io.purchasely.huawei.HuaweiStore").newInstance());
|
|
301
|
-
} catch (Exception e) {
|
|
302
|
-
Log.e("Purchasely", "Huawei Store not found :" + e.getMessage(), e);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
if (stores.contains("Amazon") && Package.getPackage("io.purchasely.amazon") != null) {
|
|
306
|
-
try {
|
|
307
|
-
result.add((Store) Class.forName("io.purchasely.amazon.AmazonStore").newInstance());
|
|
308
|
-
} catch (Exception e) {
|
|
309
|
-
Log.e("Purchasely", "Amazon Store not found :" + e.getMessage(), e);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
return result;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
private void close() {
|
|
317
|
-
defaultCallback = null;
|
|
318
|
-
presentationCallback = null;
|
|
319
|
-
processActionListener = null;
|
|
320
|
-
productActivity = null;
|
|
321
|
-
Purchasely.close();
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
private void addEventsListener(CallbackContext callbackContext) {
|
|
325
|
-
eventsCallback = callbackContext;
|
|
326
|
-
|
|
327
|
-
Purchasely.setEventListener(plyEvent -> {
|
|
328
|
-
if(plyEvent.getProperties() != null) {
|
|
329
|
-
HashMap<String, Object> map = new HashMap<>();
|
|
330
|
-
map.put("name", plyEvent.getName());
|
|
331
|
-
map.put("properties", plyEvent.getProperties().toMap());
|
|
332
|
-
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, new JSONObject(map));
|
|
333
|
-
pluginResult.setKeepCallback(true);
|
|
334
|
-
if(eventsCallback != null) eventsCallback.sendPluginResult(pluginResult);
|
|
335
|
-
} else {
|
|
336
|
-
HashMap<String, String> map = new HashMap<>();
|
|
337
|
-
map.put("name", plyEvent.getName());
|
|
338
|
-
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, new JSONObject(map));
|
|
339
|
-
pluginResult.setKeepCallback(true);
|
|
340
|
-
if(eventsCallback != null) eventsCallback.sendPluginResult(pluginResult);
|
|
341
|
-
}
|
|
342
|
-
});
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
private void removeEventsListener() {
|
|
346
|
-
eventsCallback = null;
|
|
347
|
-
Purchasely.setEventListener(null);
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
private void getAnonymousUserId(CallbackContext callbackContext) {
|
|
351
|
-
callbackContext.success(Purchasely.getAnonymousUserId());
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
private void userLogin(String userId, CallbackContext callbackContext) {
|
|
355
|
-
Purchasely.userLogin(userId, refresh -> {
|
|
356
|
-
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, refresh));
|
|
357
|
-
return null;
|
|
358
|
-
});
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
private void userLogout() {
|
|
362
|
-
Purchasely.userLogout();
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
private void setLogLevel(int logLevel) {
|
|
366
|
-
Purchasely.setLogLevel(LogLevel.values()[logLevel]);
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
private void setLanguage(String language) {
|
|
370
|
-
try {
|
|
371
|
-
Purchasely.setLanguage(new Locale(language));
|
|
372
|
-
} catch (Exception e) {
|
|
373
|
-
Purchasely.setLanguage(Locale.getDefault());
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
private void isReadyToPurchase(boolean isReadyToPurchase) {
|
|
378
|
-
Purchasely.setReadyToPurchase(isReadyToPurchase);
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
private void setAttribute(int attribute, String value) {
|
|
382
|
-
Purchasely.setAttribute(Attribute.values()[attribute], value);
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
private void setDefaultPresentationResultHandler(CallbackContext callbackContext) {
|
|
386
|
-
defaultCallback = callbackContext;
|
|
387
|
-
Purchasely.setDefaultPresentationResultHandler(PurchaselyPlugin::sendPurchaseResult);
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
private void purchasedSubscription(CallbackContext callbackContext) {
|
|
391
|
-
Purchasely.setPurchaseListener(state -> {
|
|
392
|
-
if(state instanceof State.PurchaseComplete || state instanceof State.RestorationComplete) {
|
|
393
|
-
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, "");
|
|
394
|
-
pluginResult.setKeepCallback(true);
|
|
395
|
-
callbackContext.sendPluginResult(pluginResult);
|
|
396
|
-
}
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
private void synchronize() {
|
|
401
|
-
Purchasely.synchronize();
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
private void userDidConsumeSubscriptionContent() {
|
|
405
|
-
Purchasely.userDidConsumeSubscriptionContent();
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
private void presentPresentationWithIdentifier(String presentationVendorId,
|
|
409
|
-
String contentId,
|
|
410
|
-
boolean isFullScreen,
|
|
411
|
-
CallbackContext callbackContext) {
|
|
412
|
-
presentationCallback = callbackContext;
|
|
413
|
-
Intent intent = PLYProductActivity.newIntent(cordova.getActivity());
|
|
414
|
-
intent.putExtra("presentationId", presentationVendorId);
|
|
415
|
-
intent.putExtra("contentId", contentId);
|
|
416
|
-
intent.putExtra("isFullScreen", isFullScreen);
|
|
417
|
-
cordova.getActivity().startActivity(intent);
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
private void presentPresentationForPlacement(String placementVendorId,
|
|
421
|
-
String contentId,
|
|
422
|
-
boolean isFullScreen,
|
|
423
|
-
CallbackContext callbackContext) {
|
|
424
|
-
presentationCallback = callbackContext;
|
|
425
|
-
Intent intent = PLYProductActivity.newIntent(cordova.getActivity());
|
|
426
|
-
intent.putExtra("placementId", placementVendorId);
|
|
427
|
-
intent.putExtra("contentId", contentId);
|
|
428
|
-
intent.putExtra("isFullScreen", isFullScreen);
|
|
429
|
-
cordova.getActivity().startActivity(intent);
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
private void presentProductWithIdentifier(String productVendorId,
|
|
433
|
-
String presentationVendorId,
|
|
434
|
-
String contentId,
|
|
435
|
-
boolean isFullScreen,
|
|
436
|
-
CallbackContext callbackContext) {
|
|
437
|
-
presentationCallback = callbackContext;
|
|
438
|
-
Intent intent = PLYProductActivity.newIntent(cordova.getActivity());
|
|
439
|
-
intent.putExtra("presentationId", presentationVendorId);
|
|
440
|
-
intent.putExtra("productId", productVendorId);
|
|
441
|
-
intent.putExtra("contentId", contentId);
|
|
442
|
-
intent.putExtra("isFullScreen", isFullScreen);
|
|
443
|
-
cordova.getActivity().startActivity(intent);
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
private void presentPlanWithIdentifier(String planVendorId,
|
|
447
|
-
String presentationVendorId,
|
|
448
|
-
String contentId,
|
|
449
|
-
boolean isFullScreen,
|
|
450
|
-
CallbackContext callbackContext) {
|
|
451
|
-
presentationCallback = callbackContext;
|
|
452
|
-
Intent intent = PLYProductActivity.newIntent(cordova.getActivity());
|
|
453
|
-
intent.putExtra("presentationId", presentationVendorId);
|
|
454
|
-
intent.putExtra("planId", planVendorId);
|
|
455
|
-
intent.putExtra("contentId", contentId);
|
|
456
|
-
intent.putExtra("isFullScreen", isFullScreen);
|
|
457
|
-
cordova.getActivity().startActivity(intent);
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
private void presentSubscriptions() {
|
|
461
|
-
Intent intent = new Intent(cordova.getContext(), cordova.plugin.purchasely.PLYSubscriptionsActivity.class);
|
|
462
|
-
cordova.getActivity().startActivity(intent);
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
private void restoreAllProducts(CallbackContext callbackContext) {
|
|
466
|
-
Purchasely.restoreAllProducts(plyPlan -> {
|
|
467
|
-
callbackContext.success(new JSONObject(transformPlanToMap(plyPlan)));
|
|
468
|
-
return null;
|
|
469
|
-
}, plyError -> {
|
|
470
|
-
callbackContext.error(plyError.getMessage());
|
|
471
|
-
return null;
|
|
472
|
-
});
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
private void userSubscriptions(CallbackContext callbackContext) {
|
|
476
|
-
Purchasely.userSubscriptions(new SubscriptionsListener() {
|
|
477
|
-
@Override
|
|
478
|
-
public void onSuccess(@NotNull List<PLYSubscriptionData> list) {
|
|
479
|
-
JSONArray result = new JSONArray();
|
|
480
|
-
for (int i = 0; i < list.size(); i++) {
|
|
481
|
-
PLYSubscriptionData data = list.get(i);
|
|
482
|
-
HashMap<String, Object> map = new HashMap<>(data.toMap());
|
|
483
|
-
map.put("plan", transformPlanToMap(data.getPlan()));
|
|
484
|
-
map.put("product", data.getProduct().toMap());
|
|
485
|
-
if(data.getData().getStoreType() == StoreType.GOOGLE_PLAY_STORE) {
|
|
486
|
-
map.put("subscriptionSource", StoreType.GOOGLE_PLAY_STORE.ordinal());
|
|
487
|
-
} else if(data.getData().getStoreType() == StoreType.AMAZON_APP_STORE) {
|
|
488
|
-
map.put("subscriptionSource", StoreType.AMAZON_APP_STORE.ordinal());
|
|
489
|
-
} else if(data.getData().getStoreType() == StoreType.HUAWEI_APP_GALLERY) {
|
|
490
|
-
map.put("subscriptionSource", StoreType.HUAWEI_APP_GALLERY.ordinal());
|
|
491
|
-
} else if(data.getData().getStoreType() == StoreType.APPLE_APP_STORE) {
|
|
492
|
-
map.put("subscriptionSource", StoreType.APPLE_APP_STORE.ordinal());
|
|
493
|
-
}
|
|
494
|
-
result.put(new JSONObject(map));
|
|
495
|
-
}
|
|
496
|
-
callbackContext.success(result);
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
@Override
|
|
500
|
-
public void onFailure(@NotNull Throwable throwable) {
|
|
501
|
-
callbackContext.error(throwable.getMessage());
|
|
502
|
-
}
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
private void handle(String deeplink, CallbackContext callbackContext) {
|
|
507
|
-
if (deeplink == null) {
|
|
508
|
-
callbackContext.error("Deeplink must not be null");
|
|
509
|
-
return;
|
|
510
|
-
}
|
|
511
|
-
Uri uri = Uri.parse(deeplink);
|
|
512
|
-
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, Purchasely.handle(uri)));
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
private void allProducts(CallbackContext callbackContext) {
|
|
516
|
-
Purchasely.allProducts(new ProductsListener() {
|
|
517
|
-
@Override
|
|
518
|
-
public void onSuccess(@NotNull List<PLYProduct> list) {
|
|
519
|
-
JSONArray result = new JSONArray();
|
|
520
|
-
for (int i = 0; i < list.size(); i++) {
|
|
521
|
-
result.put(new JSONObject(list.get(i).toMap()));
|
|
522
|
-
}
|
|
523
|
-
callbackContext.success(result);
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
@Override
|
|
527
|
-
public void onFailure(@NotNull Throwable throwable) {
|
|
528
|
-
callbackContext.error(throwable.getMessage());
|
|
529
|
-
}
|
|
530
|
-
});
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
private void productWithIdentifier(String vendorId, CallbackContext callbackContext) {
|
|
534
|
-
Purchasely.product(vendorId, new ProductListener() {
|
|
535
|
-
@Override
|
|
536
|
-
public void onSuccess(@Nullable PLYProduct plyProduct) {
|
|
537
|
-
if(plyProduct != null) {
|
|
538
|
-
callbackContext.success(new JSONObject(plyProduct.toMap()));
|
|
539
|
-
} else {
|
|
540
|
-
callbackContext.error("No product found with " + vendorId);
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
@Override
|
|
545
|
-
public void onFailure(@NotNull Throwable throwable) {
|
|
546
|
-
callbackContext.error(throwable.getMessage());
|
|
547
|
-
}
|
|
548
|
-
});
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
private void planWithIdentifier(String vendorId, CallbackContext callbackContext) {
|
|
552
|
-
Purchasely.plan(vendorId, new PlanListener() {
|
|
553
|
-
@Override
|
|
554
|
-
public void onSuccess(@Nullable PLYPlan plyPlan) {
|
|
555
|
-
if(plyPlan != null) {
|
|
556
|
-
callbackContext.success(new JSONObject(transformPlanToMap(plyPlan)));
|
|
557
|
-
} else {
|
|
558
|
-
callbackContext.error("No plan found with " + vendorId);
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
@Override
|
|
563
|
-
public void onFailure(@NotNull Throwable throwable) {
|
|
564
|
-
callbackContext.error(throwable.getMessage());
|
|
565
|
-
}
|
|
566
|
-
});
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
private void purchaseWithPlanVendorId(String planVendorId, String contentId, CallbackContext callbackContext) {
|
|
570
|
-
Purchasely.plan(planVendorId, new PlanListener() {
|
|
571
|
-
@Override
|
|
572
|
-
public void onSuccess(@Nullable PLYPlan plyPlan) {
|
|
573
|
-
if(plyPlan != null) {
|
|
574
|
-
Purchasely.purchase(cordova.getActivity(), plyPlan, contentId, plyPlan1 -> {
|
|
575
|
-
callbackContext.success(new JSONObject(transformPlanToMap(plyPlan1)));
|
|
576
|
-
return null;
|
|
577
|
-
}, plyError -> {
|
|
578
|
-
callbackContext.error(plyError.getMessage());
|
|
579
|
-
return null;
|
|
580
|
-
});
|
|
581
|
-
} else {
|
|
582
|
-
callbackContext.error("No plan found with " + planVendorId);
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
@Override
|
|
587
|
-
public void onFailure(@NotNull Throwable throwable) {
|
|
588
|
-
callbackContext.error(throwable.getMessage());
|
|
589
|
-
}
|
|
590
|
-
});
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
private void setPaywallActionInterceptor(CallbackContext callbackContext) {
|
|
594
|
-
Purchasely.setPaywallActionsInterceptor(
|
|
595
|
-
(info, plyPresentationAction, map, plyProcessActionListener) -> {
|
|
596
|
-
processActionListener = plyProcessActionListener;
|
|
597
|
-
paywallAction = plyPresentationAction;
|
|
598
|
-
|
|
599
|
-
String url = null;
|
|
600
|
-
if(map.getUrl() != null) {
|
|
601
|
-
url = map.getUrl().toString();
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
HashMap<String, Object> parametersForCordova = new HashMap<>();
|
|
605
|
-
parametersForCordova.put("title", map.getTitle());
|
|
606
|
-
parametersForCordova.put("url", url);
|
|
607
|
-
parametersForCordova.put("plan", transformPlanToMap(map.getPlan()));
|
|
608
|
-
parametersForCordova.put("presentation", map.getPresentation());
|
|
609
|
-
|
|
610
|
-
HashMap<String, Object> infoMap = new HashMap<>();
|
|
611
|
-
if(info.getContentId() != null) infoMap.put("contentId", info.getContentId());
|
|
612
|
-
if(info.getPresentationId() != null) infoMap.put("presentationId", info.getPresentationId());
|
|
613
|
-
if(info.getPlacementId() != null) infoMap.put("placementId", info.getPlacementId());
|
|
614
|
-
if(info.getAbTestId() != null) infoMap.put("abTestId", info.getAbTestId());
|
|
615
|
-
if(info.getAbTestVariantId() != null) infoMap.put("abTestVariantId", info.getAbTestVariantId());
|
|
616
|
-
|
|
617
|
-
HashMap<String, Object> resultForCordova = new HashMap<>();
|
|
618
|
-
resultForCordova.put("info", infoMap);
|
|
619
|
-
resultForCordova.put("action", plyPresentationAction.getValue());
|
|
620
|
-
resultForCordova.put("parameters", parametersForCordova);
|
|
621
|
-
|
|
622
|
-
PluginResult result = new PluginResult(
|
|
623
|
-
PluginResult.Status.OK,
|
|
624
|
-
new JSONObject(resultForCordova)
|
|
625
|
-
);
|
|
626
|
-
result.setKeepCallback(true);
|
|
627
|
-
callbackContext.sendPluginResult(result);
|
|
628
|
-
});
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
private void closePaywall(CallbackContext callbackContext) {
|
|
632
|
-
Activity purchaselyActivity = null;
|
|
633
|
-
if(productActivity != null && productActivity.activity != null) {
|
|
634
|
-
purchaselyActivity = productActivity.activity.get();
|
|
635
|
-
}
|
|
636
|
-
Activity activity = purchaselyActivity != null ? purchaselyActivity : cordova.getActivity();
|
|
637
|
-
Intent intent = new Intent(activity, cordova.getActivity().getClass());
|
|
638
|
-
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
|
|
639
|
-
cordova.getActivity().startActivity(intent);
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
private void onProcessAction(boolean processAction) {
|
|
643
|
-
if(processActionListener == null) return;
|
|
644
|
-
|
|
645
|
-
PLYPresentationAction action = PLYPresentationAction.CLOSE;
|
|
646
|
-
if(paywallAction != null) action = paywallAction;
|
|
647
|
-
|
|
648
|
-
switch (action) {
|
|
649
|
-
case PROMO_CODE:
|
|
650
|
-
case RESTORE:
|
|
651
|
-
case PURCHASE:
|
|
652
|
-
case LOGIN:
|
|
653
|
-
case OPEN_PRESENTATION:
|
|
654
|
-
processActionWithPaywallActivity(processAction);
|
|
655
|
-
break;
|
|
656
|
-
default:
|
|
657
|
-
cordova.getActivity().runOnUiThread(() -> processActionListener.processAction(processAction));
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
private void processActionWithPaywallActivity(boolean processAction) {
|
|
662
|
-
if(processActionListener == null) return;
|
|
663
|
-
|
|
664
|
-
boolean softRelaunched = productActivity != null && productActivity.relaunch(cordova);
|
|
665
|
-
if(softRelaunched) {
|
|
666
|
-
cordova.getActivity().runOnUiThread(() -> processActionListener.processAction(processAction));
|
|
667
|
-
} else {
|
|
668
|
-
new Thread(() -> {
|
|
669
|
-
try {
|
|
670
|
-
Thread.sleep(500);
|
|
671
|
-
} catch (InterruptedException e) {
|
|
672
|
-
Log.e("Purchasely", "process action error", e);
|
|
673
|
-
} finally {
|
|
674
|
-
cordova.getActivity().runOnUiThread(() -> processActionListener.processAction(processAction));
|
|
675
|
-
}
|
|
676
|
-
}).start();
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
private static Map<String, Object> transformPlanToMap(PLYPlan plan) {
|
|
681
|
-
if(plan == null) return new HashMap<>();
|
|
682
|
-
|
|
683
|
-
HashMap<String, Object> map = new HashMap<>(plan.toMap());
|
|
684
|
-
if(plan.getType() == DistributionType.CONSUMABLE) {
|
|
685
|
-
map.put("type", DistributionType.CONSUMABLE.ordinal());
|
|
686
|
-
} else if(plan.getType() == DistributionType.CONSUMABLE) {
|
|
687
|
-
map.put("type", DistributionType.NON_CONSUMABLE.ordinal());
|
|
688
|
-
} else if(plan.getType() == DistributionType.NON_CONSUMABLE) {
|
|
689
|
-
map.put("type", DistributionType.RENEWING_SUBSCRIPTION.ordinal());
|
|
690
|
-
} else if(plan.getType() == DistributionType.NON_RENEWING_SUBSCRIPTION) {
|
|
691
|
-
map.put("type", DistributionType.NON_RENEWING_SUBSCRIPTION.ordinal());
|
|
692
|
-
} else if(plan.getType() == DistributionType.UNKNOWN) {
|
|
693
|
-
map.put("type", DistributionType.UNKNOWN.ordinal());
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
map.put("isEligibleForIntroOffer", plan.isEligibleToIntroOffer());
|
|
697
|
-
return map;
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
public static class ProductActivity {
|
|
701
|
-
String presentationId = null;
|
|
702
|
-
String placementId = null;
|
|
703
|
-
String productId = null;
|
|
704
|
-
String planId = null;
|
|
705
|
-
String contentId = null;
|
|
706
|
-
@Nullable WeakReference<PLYProductActivity> activity = null;
|
|
707
|
-
|
|
708
|
-
public boolean relaunch(CordovaInterface cordova) {
|
|
709
|
-
PLYProductActivity backgroundActivity = null;
|
|
710
|
-
|
|
711
|
-
if(activity != null) {
|
|
712
|
-
backgroundActivity = activity.get();
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
if(backgroundActivity != null
|
|
716
|
-
&& !backgroundActivity.isFinishing()
|
|
717
|
-
&& !backgroundActivity.isDestroyed()) {
|
|
718
|
-
Intent intent = new Intent(cordova.getActivity(), PLYProductActivity.class);
|
|
719
|
-
intent.putExtra("presentationId", presentationId);
|
|
720
|
-
intent.putExtra("placementId", placementId);
|
|
721
|
-
intent.putExtra("productId", productId);
|
|
722
|
-
intent.putExtra("planId", planId);
|
|
723
|
-
intent.putExtra("contentId", contentId);
|
|
724
|
-
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
|
|
725
|
-
cordova.getActivity().startActivity(intent);
|
|
726
|
-
return true;
|
|
727
|
-
} else {
|
|
728
|
-
Intent intent = PLYProductActivity.newIntent(cordova.getActivity());
|
|
729
|
-
intent.putExtra("presentationId", presentationId);
|
|
730
|
-
intent.putExtra("placementId", placementId);
|
|
731
|
-
intent.putExtra("productId", productId);
|
|
732
|
-
intent.putExtra("planId", planId);
|
|
733
|
-
intent.putExtra("contentId", contentId);
|
|
734
|
-
cordova.getActivity().startActivity(intent);
|
|
735
|
-
return false;
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
}
|