Skip to content

Commit bbc820b

Browse files
authored
Merge pull request #31 from RevenueCat/updates_sdk
Updates sdk to 1.5.0
2 parents 63978f1 + d941d8a commit bbc820b

File tree

7 files changed

+134
-53
lines changed

7 files changed

+134
-53
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 1.5.0
2+
- Adds create alias, identify and reset
3+
14
## 1.4.4
25
- Fixes download script issue
36

RNPurchases.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ Pod::Spec.new do |spec|
1919
spec.exclude_files = "ios/Purchases.framework"
2020

2121
spec.dependency "React"
22-
spec.dependency "Purchases"
22+
spec.dependency "Purchases", "~> 1.2.0"
2323
end

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ repositories {
2929

3030
dependencies {
3131
provided 'com.facebook.react:react-native:+'
32-
compile 'com.revenuecat.purchases:purchases:1.3.7'
32+
compile 'com.revenuecat.purchases:purchases:1.4.0'
3333
compile 'com.android.billingclient:billing:1.1'
3434
}

android/src/main/java/com/reactlibrary/RNPurchasesModule.java

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.revenuecat.purchases.Purchases;
2222
import com.revenuecat.purchases.util.Iso8601Utils;
2323

24+
import org.jetbrains.annotations.NotNull;
2425
import org.json.JSONArray;
2526
import org.json.JSONException;
2627
import org.json.JSONObject;
@@ -38,7 +39,6 @@ public class RNPurchasesModule extends ReactContextBaseJavaModule implements Pur
3839
private static final String TRANSACTIONS_RESTORED = "Purchases-RestoredTransactions";
3940

4041
private final ReactApplicationContext reactContext;
41-
private Purchases purchases;
4242

4343
public RNPurchasesModule(ReactApplicationContext reactContext) {
4444
super(reactContext);
@@ -51,37 +51,43 @@ public String getName() {
5151
}
5252

5353
private void checkPurchases() {
54-
if (purchases == null) {
54+
if (Purchases.getSharedInstance() == null) {
5555
throw new RuntimeException("You must call setupPurchases first");
5656
}
5757
}
58+
5859
public void onCatalystInstanceDestroy() {
59-
if (purchases != null) {
60-
purchases.close();
61-
purchases = null;
60+
if (Purchases.getSharedInstance() != null) {
61+
Purchases.getSharedInstance().close();
6262
}
6363
}
6464

6565
@ReactMethod
6666
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();
6969
}
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);
7173
promise.resolve(null);
7274
}
7375

7476
@ReactMethod
75-
public void setIsUsingAnonymousID(boolean isUsingAnonymousID) {
77+
public void setAllowSharingStoreAccount(boolean allowSharingStoreAccount) {
7678
checkPurchases();
77-
purchases.setIsUsingAnonymousID(isUsingAnonymousID);
79+
Purchases.getSharedInstance().setAllowSharingPlayStoreAccount(allowSharingStoreAccount);
7880
}
7981

8082
@ReactMethod
8183
public void addAttributionData(ReadableMap data, Integer network) {
8284
checkPurchases();
8385
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+
}
8591
} catch (JSONException e) {
8692
Log.e("RNPurchases", "Error parsing attribution date to JSON: " + e.getLocalizedMessage());
8793
}
@@ -114,7 +120,8 @@ private WritableMap mapForSkuDetails(final SkuDetails detail) {
114120
public void getEntitlements(final Promise promise) {
115121
checkPurchases();
116122

117-
purchases.getEntitlements(new Purchases.GetEntitlementsHandler() {
123+
Purchases.getSharedInstance().getEntitlements(new Purchases.GetEntitlementsHandler() {
124+
118125
@Override
119126
public void onReceiveEntitlements(Map<String, Entitlement> entitlementMap) {
120127
WritableMap response = Arguments.createMap();
@@ -142,7 +149,7 @@ public void onReceiveEntitlements(Map<String, Entitlement> entitlementMap) {
142149
}
143150

144151
@Override
145-
public void onReceiveEntitlementsError(int domain, int code, String message) {
152+
public void onReceiveEntitlementsError(@NotNull Purchases.ErrorDomains domain, int code, @NotNull String message) {
146153
promise.reject("ERROR_FETCHING_ENTITLEMENTS", message);
147154
}
148155
});
@@ -170,9 +177,9 @@ public void onReceiveSkus(List<SkuDetails> skus) {
170177
};
171178

172179
if (type.toLowerCase().equals("subs")) {
173-
purchases.getSubscriptionSkus(productIDList, handler);
180+
Purchases.getSharedInstance().getSubscriptionSkus(productIDList, handler);
174181
} else {
175-
purchases.getNonSubscriptionSkus(productIDList, handler);
182+
Purchases.getSharedInstance().getNonSubscriptionSkus(productIDList, handler);
176183
}
177184
}
178185

@@ -185,18 +192,47 @@ public void makePurchase(String productIdentifier, ReadableArray oldSkus, String
185192
oldSkusList.add((String)oldSku);
186193
}
187194

188-
purchases.makePurchase(getCurrentActivity(), productIdentifier, type, oldSkusList);
195+
Purchases.getSharedInstance().makePurchase(getCurrentActivity(), productIdentifier, type, oldSkusList);
189196
}
190197

191198
@ReactMethod
192199
public void getAppUserID(final Promise promise) {
193-
promise.resolve(purchases.getAppUserID());
200+
promise.resolve(Purchases.getSharedInstance().getAppUserID());
194201
}
195202

196203
@ReactMethod
197204
public void restoreTransactions() {
198205
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+
});
200236
}
201237

202238
private void sendEvent(String eventName,
@@ -267,19 +303,16 @@ public void onCompletedPurchase(String sku, PurchaserInfo purchaserInfo) {
267303
sendEvent(PURCHASE_COMPLETED_EVENT, map);
268304
}
269305

270-
private WritableMap errorMap(int domain, int code, String message) {
306+
private WritableMap errorMap(Purchases.ErrorDomains domain, int code, String message) {
271307
WritableMap errorMap = Arguments.createMap();
272308
String domainString;
273309

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";
283316
}
284317

285318
errorMap.putString("message", message);
@@ -290,7 +323,7 @@ private WritableMap errorMap(int domain, int code, String message) {
290323
}
291324

292325
@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) {
294327
WritableMap map = Arguments.createMap();
295328

296329
map.putMap("error", errorMap(domain, code, message));
@@ -316,7 +349,7 @@ public void onRestoreTransactions(PurchaserInfo purchaserInfo) {
316349
}
317350

318351
@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) {
320353
sendEvent(TRANSACTIONS_RESTORED, errorMap(domain, code, reason));
321354
}
322355

@@ -374,5 +407,4 @@ private static JSONArray convertArrayToJson(ReadableArray readableArray) throws
374407
}
375408
return array;
376409
}
377-
378410
}

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,27 @@ export default class Purchases {
158158
static getAppUserID() {
159159
return RNPurchases.getAppUserID();
160160
}
161+
162+
/** This function will alias two appUserIDs together.
163+
* @param alias The new appUserID that should be linked to the currently identified appUserID
164+
* */
165+
static createAlias(newAppUserID) {
166+
return RNPurchases.createAlias(newAppUserID);
167+
}
168+
169+
/**
170+
* This function will identify the current user with an appUserID. Typically this would be used after a logout to identify a new user without calling configure
171+
* @param appUserID The appUserID that should be linked to the currently user
172+
*/
173+
static identify(newAppUserID) {
174+
return RNPurchases.identify(newAppUserID);
175+
}
176+
177+
/**
178+
* Resets the Purchases client clearing the saved appUserID. This will generate a random user id and save it in the cache.
179+
*/
180+
static reset() {
181+
return RNPurchases.reset();
182+
}
183+
161184
};

ios/RNPurchases.m

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
@interface RNPurchases () <RCPurchasesDelegate>
1111

12-
@property (nonatomic, retain) RCPurchases *purchases;
1312
@property (nonatomic, retain) NSMutableDictionary *products;
1413

1514
@end
@@ -38,33 +37,33 @@ - (dispatch_queue_t)methodQueue
3837
appUserID:(NSString *)appUserID
3938
resolve:(RCTPromiseResolveBlock)resolve
4039
reject:(RCTPromiseRejectBlock)reject)
41-
{
42-
self.purchases.delegate = nil;
40+
{
41+
RCPurchases.sharedPurchases.delegate = nil;
4342
self.products = [NSMutableDictionary new];
44-
self.purchases = [[RCPurchases alloc] initWithAPIKey:apiKey appUserID:appUserID];
45-
self.purchases.delegate = self;
43+
[RCPurchases configureWithAPIKey:apiKey appUserID:appUserID];
44+
RCPurchases.sharedPurchases.delegate = self;
4645
resolve(nil);
4746
}
4847

49-
RCT_EXPORT_METHOD(setIsUsingAnonymousID:(BOOL)isUsingAnonymousID)
48+
RCT_EXPORT_METHOD(setAllowSharingStoreAccount:(BOOL)allowSharingStoreAccount)
5049
{
51-
self.purchases.isUsingAnonymousID = isUsingAnonymousID;
50+
RCPurchases.sharedPurchases.allowSharingAppStoreAccount = allowSharingStoreAccount;
5251
}
5352

5453
RCT_REMAP_METHOD(addAttributionData,
5554
addAttributionData:(NSDictionary *)data
5655
forNetwork:(NSInteger)network)
5756
{
58-
NSAssert(self.purchases, @"You must call setup first.");
59-
[self.purchases addAttributionData:data fromNetwork:(RCAttributionNetwork)network];
57+
NSAssert(RCPurchases.sharedPurchases, @"You must call setup first.");
58+
[RCPurchases.sharedPurchases addAttributionData:data fromNetwork:(RCAttributionNetwork)network];
6059
}
6160

6261
RCT_REMAP_METHOD(getEntitlements,
6362
getEntitlementsWithResolve:(RCTPromiseResolveBlock)resolve
6463
reject:(RCTPromiseRejectBlock)reject)
6564
{
66-
NSAssert(self.purchases, @"You must call setup first.");
67-
[self.purchases entitlements:^(NSDictionary<NSString *,RCEntitlement *> * _Nonnull entitlements) {
65+
NSAssert(RCPurchases.sharedPurchases, @"You must call setup first.");
66+
[RCPurchases.sharedPurchases entitlements:^(NSDictionary<NSString *,RCEntitlement *> * _Nonnull entitlements) {
6867
NSMutableDictionary *result = [NSMutableDictionary new];
6968
for (NSString *entId in entitlements) {
7069
RCEntitlement *entitlement = entitlements[entId];
@@ -90,9 +89,9 @@ - (dispatch_queue_t)methodQueue
9089
resolve:(RCTPromiseResolveBlock)resolve
9190
reject:(RCTPromiseRejectBlock)reject)
9291
{
93-
NSAssert(self.purchases, @"You must call setup first.");
92+
NSAssert(RCPurchases.sharedPurchases, @"You must call setup first.");
9493

95-
[self.purchases productsWithIdentifiers:products completion:^(NSArray<SKProduct *> * _Nonnull products) {
94+
[RCPurchases.sharedPurchases productsWithIdentifiers:products completion:^(NSArray<SKProduct *> * _Nonnull products) {
9695
NSMutableArray *productObjects = [NSMutableArray new];
9796
for (SKProduct *p in products) {
9897
self.products[p.productIdentifier] = p;
@@ -107,26 +106,50 @@ - (dispatch_queue_t)methodQueue
107106
oldSkus:(NSArray *)oldSkus
108107
type:(NSString *)type)
109108
{
110-
NSAssert(self.purchases, @"You must call setup first.");
109+
NSAssert(RCPurchases.sharedPurchases, @"You must call setup first.");
111110

112111
if (self.products[productIdentifier] == nil) {
113112
NSLog(@"Purchases cannot find product. Did you call getProductInfo first?");
114113
return;
115114
}
116115

117-
[self.purchases makePurchase:self.products[productIdentifier]];
116+
[RCPurchases.sharedPurchases makePurchase:self.products[productIdentifier]];
118117
}
119118

120119
RCT_EXPORT_METHOD(restoreTransactions) {
121-
NSAssert(self.purchases, @"You must call setup first.");
122-
[self.purchases restoreTransactionsForAppStoreAccount];
120+
NSAssert(RCPurchases.sharedPurchases, @"You must call setup first.");
121+
[RCPurchases.sharedPurchases restoreTransactionsForAppStoreAccount];
123122
}
124123

125124
RCT_REMAP_METHOD(getAppUserID,
126125
getAppUserIDWithResolve:(RCTPromiseResolveBlock)resolve
127126
reject:(RCTPromiseRejectBlock)reject)
128127
{
129-
resolve(self.purchases.appUserID);
128+
resolve(RCPurchases.sharedPurchases.appUserID);
129+
}
130+
131+
RCT_EXPORT_METHOD(createAlias:(NSString * _Nullable)newAppUserID
132+
resolve:(RCTPromiseResolveBlock)resolve
133+
reject:(RCTPromiseRejectBlock)reject)
134+
{
135+
NSAssert(RCPurchases.sharedPurchases, @"You must call setup first.");
136+
[RCPurchases.sharedPurchases createAlias:newAppUserID completion:^(NSError * _Nullable error) {
137+
if (error) {
138+
reject("ERROR_ALIASING");
139+
} else {
140+
resolve(nil);
141+
}
142+
}];
143+
}
144+
145+
RCT_EXPORT_METHOD(identify:(NSString * _Nullable)appUserID) {
146+
NSAssert(RCPurchases.sharedPurchases, @"You must call setup first.");
147+
[RCPurchases.sharedPurchases identify:appUserID];
148+
}
149+
150+
RCT_EXPORT_METHOD(reset) {
151+
NSAssert(RCPurchases.sharedPurchases, @"You must call setup first.");
152+
[RCPurchases.sharedPurchases reset];
130153
}
131154

132155
#pragma mark -

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "react-native-purchases",
3-
"version": "1.4.4",
3+
"version": "1.5.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"fetch:ios:sdk": "./scripts/download-purchases-framework.sh 1.1.5",
7+
"fetch:ios:sdk": "./scripts/download-purchases-framework.sh 1.2.0",
88
"preinstall": "npm run fetch:ios:sdk",
99
"test": "echo \"Error: no test specified\" && exit 1"
1010
},

0 commit comments

Comments
 (0)