21
21
import com .revenuecat .purchases .Purchases ;
22
22
import com .revenuecat .purchases .util .Iso8601Utils ;
23
23
24
+ import org .jetbrains .annotations .NotNull ;
24
25
import org .json .JSONArray ;
25
26
import org .json .JSONException ;
26
27
import org .json .JSONObject ;
@@ -38,7 +39,6 @@ public class RNPurchasesModule extends ReactContextBaseJavaModule implements Pur
38
39
private static final String TRANSACTIONS_RESTORED = "Purchases-RestoredTransactions" ;
39
40
40
41
private final ReactApplicationContext reactContext ;
41
- private Purchases purchases ;
42
42
43
43
public RNPurchasesModule (ReactApplicationContext reactContext ) {
44
44
super (reactContext );
@@ -51,37 +51,43 @@ public String getName() {
51
51
}
52
52
53
53
private void checkPurchases () {
54
- if (purchases == null ) {
54
+ if (Purchases . getSharedInstance () == null ) {
55
55
throw new RuntimeException ("You must call setupPurchases first" );
56
56
}
57
57
}
58
+
58
59
public void onCatalystInstanceDestroy () {
59
- if (purchases != null ) {
60
- purchases .close ();
61
- purchases = null ;
60
+ if (Purchases .getSharedInstance () != null ) {
61
+ Purchases .getSharedInstance ().close ();
62
62
}
63
63
}
64
64
65
65
@ ReactMethod
66
66
public void setupPurchases (String apiKey , String appUserID , final Promise promise ) {
67
- if (purchases != null ) {
68
- purchases .close ();
67
+ if (Purchases . getSharedInstance () != null ) {
68
+ Purchases . getSharedInstance () .close ();
69
69
}
70
- purchases = new Purchases .Builder (reactContext , apiKey , this ).appUserID (appUserID ).build ();
70
+ Purchases purchases = new Purchases .Builder (reactContext , apiKey ).appUserID (appUserID ).build ();
71
+ purchases .setListener (this );
72
+ Purchases .setSharedInstance (purchases );
71
73
promise .resolve (null );
72
74
}
73
75
74
76
@ ReactMethod
75
- public void setIsUsingAnonymousID (boolean isUsingAnonymousID ) {
77
+ public void setAllowSharingStoreAccount (boolean allowSharingStoreAccount ) {
76
78
checkPurchases ();
77
- purchases . setIsUsingAnonymousID ( isUsingAnonymousID );
79
+ Purchases . getSharedInstance (). setAllowSharingPlayStoreAccount ( allowSharingStoreAccount );
78
80
}
79
81
80
82
@ ReactMethod
81
83
public void addAttributionData (ReadableMap data , Integer network ) {
82
84
checkPurchases ();
83
85
try {
84
- purchases .addAttributionData (convertMapToJson (data ), network );
86
+ for (Purchases .AttributionNetwork attributionNetwork : Purchases .AttributionNetwork .values ()) {
87
+ if (attributionNetwork .getServerValue () == network ) {
88
+ Purchases .getSharedInstance ().addAttributionData (convertMapToJson (data ), attributionNetwork );
89
+ }
90
+ }
85
91
} catch (JSONException e ) {
86
92
Log .e ("RNPurchases" , "Error parsing attribution date to JSON: " + e .getLocalizedMessage ());
87
93
}
@@ -114,7 +120,8 @@ private WritableMap mapForSkuDetails(final SkuDetails detail) {
114
120
public void getEntitlements (final Promise promise ) {
115
121
checkPurchases ();
116
122
117
- purchases .getEntitlements (new Purchases .GetEntitlementsHandler () {
123
+ Purchases .getSharedInstance ().getEntitlements (new Purchases .GetEntitlementsHandler () {
124
+
118
125
@ Override
119
126
public void onReceiveEntitlements (Map <String , Entitlement > entitlementMap ) {
120
127
WritableMap response = Arguments .createMap ();
@@ -142,7 +149,7 @@ public void onReceiveEntitlements(Map<String, Entitlement> entitlementMap) {
142
149
}
143
150
144
151
@ Override
145
- public void onReceiveEntitlementsError (int domain , int code , String message ) {
152
+ public void onReceiveEntitlementsError (@ NotNull Purchases . ErrorDomains domain , int code , @ NotNull String message ) {
146
153
promise .reject ("ERROR_FETCHING_ENTITLEMENTS" , message );
147
154
}
148
155
});
@@ -170,9 +177,9 @@ public void onReceiveSkus(List<SkuDetails> skus) {
170
177
};
171
178
172
179
if (type .toLowerCase ().equals ("subs" )) {
173
- purchases .getSubscriptionSkus (productIDList , handler );
180
+ Purchases . getSharedInstance () .getSubscriptionSkus (productIDList , handler );
174
181
} else {
175
- purchases .getNonSubscriptionSkus (productIDList , handler );
182
+ Purchases . getSharedInstance () .getNonSubscriptionSkus (productIDList , handler );
176
183
}
177
184
}
178
185
@@ -185,18 +192,47 @@ public void makePurchase(String productIdentifier, ReadableArray oldSkus, String
185
192
oldSkusList .add ((String )oldSku );
186
193
}
187
194
188
- purchases .makePurchase (getCurrentActivity (), productIdentifier , type , oldSkusList );
195
+ Purchases . getSharedInstance () .makePurchase (getCurrentActivity (), productIdentifier , type , oldSkusList );
189
196
}
190
197
191
198
@ ReactMethod
192
199
public void getAppUserID (final Promise promise ) {
193
- promise .resolve (purchases .getAppUserID ());
200
+ promise .resolve (Purchases . getSharedInstance () .getAppUserID ());
194
201
}
195
202
196
203
@ ReactMethod
197
204
public void restoreTransactions () {
198
205
checkPurchases ();
199
- purchases .restorePurchasesForPlayStoreAccount ();
206
+ Purchases .getSharedInstance ().restorePurchasesForPlayStoreAccount ();
207
+ }
208
+
209
+ @ ReactMethod
210
+ public void reset () {
211
+ checkPurchases ();
212
+ Purchases .getSharedInstance ().reset ();
213
+ }
214
+
215
+ @ ReactMethod
216
+ public void identify (String appUserID ) {
217
+ checkPurchases ();
218
+ Purchases .getSharedInstance ().identify (appUserID );
219
+ }
220
+
221
+ @ ReactMethod
222
+ public void createAlias (String newAppUserID , final Promise promise ) {
223
+ checkPurchases ();
224
+ Purchases .getSharedInstance ().createAlias (newAppUserID , new Purchases .AliasHandler () {
225
+
226
+ @ Override
227
+ public void onSuccess () {
228
+ promise .resolve (null );
229
+ }
230
+
231
+ @ Override
232
+ public void onError (@ NotNull Purchases .ErrorDomains errorDomains , int i , @ NotNull String s ) {
233
+ promise .reject ("ERROR_ALIASING" , s );
234
+ }
235
+ });
200
236
}
201
237
202
238
private void sendEvent (String eventName ,
@@ -267,19 +303,16 @@ public void onCompletedPurchase(String sku, PurchaserInfo purchaserInfo) {
267
303
sendEvent (PURCHASE_COMPLETED_EVENT , map );
268
304
}
269
305
270
- private WritableMap errorMap (int domain , int code , String message ) {
306
+ private WritableMap errorMap (Purchases . ErrorDomains domain , int code , String message ) {
271
307
WritableMap errorMap = Arguments .createMap ();
272
308
String domainString ;
273
309
274
- switch (domain ) {
275
- case Purchases .ErrorDomains .REVENUECAT_BACKEND :
276
- domainString = "RevenueCat Backend" ;
277
- break ;
278
- case Purchases .ErrorDomains .PLAY_BILLING :
279
- domainString = "Play Billing" ;
280
- break ;
281
- default :
282
- domainString = "Unknown" ;
310
+ if (domain == Purchases .ErrorDomains .REVENUECAT_BACKEND ) {
311
+ domainString = "RevenueCat Backend" ;
312
+ } else if (domain == Purchases .ErrorDomains .PLAY_BILLING ) {
313
+ domainString = "Play Billing" ;
314
+ } else {
315
+ domainString = "Unknown" ;
283
316
}
284
317
285
318
errorMap .putString ("message" , message );
@@ -290,7 +323,7 @@ private WritableMap errorMap(int domain, int code, String message) {
290
323
}
291
324
292
325
@ Override
293
- public void onFailedPurchase (int domain , int code , String message ) {
326
+ public void onFailedPurchase (@ NotNull Purchases . ErrorDomains domain , int code , @ org . jetbrains . annotations . Nullable String message ) {
294
327
WritableMap map = Arguments .createMap ();
295
328
296
329
map .putMap ("error" , errorMap (domain , code , message ));
@@ -316,7 +349,7 @@ public void onRestoreTransactions(PurchaserInfo purchaserInfo) {
316
349
}
317
350
318
351
@ Override
319
- public void onRestoreTransactionsFailed (int domain , int code , String reason ) {
352
+ public void onRestoreTransactionsFailed (@ NotNull Purchases . ErrorDomains domain , int code , @ org . jetbrains . annotations . Nullable String reason ) {
320
353
sendEvent (TRANSACTIONS_RESTORED , errorMap (domain , code , reason ));
321
354
}
322
355
@@ -374,5 +407,4 @@ private static JSONArray convertArrayToJson(ReadableArray readableArray) throws
374
407
}
375
408
return array ;
376
409
}
377
-
378
410
}
0 commit comments