Skip to content

Commit

Permalink
Fix purchaseSubscriptionOption not accepting oldProductIdentifier (#764)
Browse files Browse the repository at this point in the history
As reported in #763, we were using the `oldSKU` for google upgrades but
the typescript interface only accepts `oldProductIdentifier` in the
`purchaseSubscriptionOption` method. This makes sure all purchase
methods have the same logic. Note that `purchaseSubscriptionOption`
didn't exist before V5, but since some developers might have worked
around the issue by using `oldSKU`, I think it's better to support that
and avoid breaking their fixes.
  • Loading branch information
tonidero authored Nov 2, 2023
1 parent 93ef699 commit 02e9b02
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.revenuecat.purchases.react;

class GoogleUpgradeInfo {
private final String oldProductIdentifier;
private final Integer prorationMode;

public GoogleUpgradeInfo(String oldProductIdentifier, Integer prorationMode) {
this.oldProductIdentifier = oldProductIdentifier;
this.prorationMode = prorationMode;
}

public String getOldProductIdentifier() {
return oldProductIdentifier;
}

public Integer getProrationMode() {
return prorationMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import kotlin.UninitializedPropertyAccessException;

Expand Down Expand Up @@ -145,19 +144,7 @@ public void purchaseProduct(final String productIdentifier,
@Nullable final ReadableMap googleInfo,
@Nullable final String presentedOfferingIdentifier,
final Promise promise) {
String googleOldProductId = null;
Integer googleProrationMode = null;

if (googleProductChangeInfo != null) {
// GoogleProductChangeInfo in V6 and later
googleOldProductId = googleProductChangeInfo.hasKey("oldProductIdentifier") ? googleProductChangeInfo.getString("oldProductIdentifier") : null;
googleProrationMode = googleProductChangeInfo.hasKey("prorationMode") ? googleProductChangeInfo.getInt("prorationMode") : null;

// Legacy UpgradeInfo in V5 and earlier
if (googleOldProductId == null) {
googleOldProductId = googleProductChangeInfo.hasKey("oldSKU") ? googleProductChangeInfo.getString("oldSKU") : null;
}
}
GoogleUpgradeInfo googleUpgradeInfo = getUpgradeInfo(googleProductChangeInfo);

Boolean googleIsPersonalized = googleInfo != null && googleInfo.hasKey("isPersonalizedPrice") ? googleInfo.getBoolean("isPersonalizedPrice") : null;

Expand All @@ -166,8 +153,8 @@ public void purchaseProduct(final String productIdentifier,
productIdentifier,
type,
null,
googleOldProductId,
googleProrationMode,
googleUpgradeInfo.getOldProductIdentifier(),
googleUpgradeInfo.getProrationMode(),
googleIsPersonalized,
presentedOfferingIdentifier,
getOnResult(promise));
Expand All @@ -180,27 +167,16 @@ public void purchasePackage(final String packageIdentifier,
@Nullable final String discountTimestamp,
@Nullable final ReadableMap googleInfo,
final Promise promise) {
String googleOldProductId = null;
Integer googleProrationMode = null;
GoogleUpgradeInfo googleUpgradeInfo = getUpgradeInfo(googleProductChangeInfo);

if (googleProductChangeInfo != null) {
// GoogleProductChangeInfo in V6 and later
googleOldProductId = googleProductChangeInfo.hasKey("oldProductIdentifier") ? googleProductChangeInfo.getString("oldProductIdentifier") : null;
googleProrationMode = googleProductChangeInfo.hasKey("prorationMode") ? googleProductChangeInfo.getInt("prorationMode") : null;

// Legacy UpgradeInfo in V5 and earlier
if (googleOldProductId == null) {
googleOldProductId = googleProductChangeInfo.hasKey("oldSKU") ? googleProductChangeInfo.getString("oldSKU") : null;
}
}
Boolean googleIsPersonalized = googleInfo != null && googleInfo.hasKey("isPersonalizedPrice") ? googleInfo.getBoolean("isPersonalizedPrice") : null;

CommonKt.purchasePackage(
getCurrentActivity(),
packageIdentifier,
offeringIdentifier,
googleOldProductId,
googleProrationMode,
googleUpgradeInfo.getOldProductIdentifier(),
googleUpgradeInfo.getProrationMode(),
googleIsPersonalized,
getOnResult(promise));
}
Expand All @@ -213,17 +189,16 @@ public void purchaseSubscriptionOption(final String productIdentifer,
@Nullable final ReadableMap googleInfo,
@Nullable final String presentedOfferingIdentifier,
final Promise promise) {
String googleOldProductId = upgradeInfo != null && upgradeInfo.hasKey("oldSKU") ? upgradeInfo.getString("oldSKU") : null;
Integer googleProrationMode = upgradeInfo != null && upgradeInfo.hasKey("prorationMode") ? upgradeInfo.getInt("prorationMode") : null;
GoogleUpgradeInfo googleUpgradeInfo = getUpgradeInfo(upgradeInfo);

Boolean googleIsPersonalized = googleInfo != null && googleInfo.hasKey("isPersonalizedPrice") ? googleInfo.getBoolean("isPersonalizedPrice") : null;

CommonKt.purchaseSubscriptionOption(
getCurrentActivity(),
productIdentifer,
optionIdentifier,
googleOldProductId,
googleProrationMode,
googleUpgradeInfo.getOldProductIdentifier(),
googleUpgradeInfo.getProrationMode(),
googleIsPersonalized,
presentedOfferingIdentifier,
getOnResult(promise));
Expand Down Expand Up @@ -514,4 +489,21 @@ public void onError(ErrorContainer errorContainer) {
};
}

private static GoogleUpgradeInfo getUpgradeInfo(ReadableMap upgradeInfo) {
String googleOldProductId = null;
Integer googleProrationMode = null;

if (upgradeInfo != null) {
// GoogleProductChangeInfo in V6 and later
googleOldProductId = upgradeInfo.hasKey("oldProductIdentifier") ? upgradeInfo.getString("oldProductIdentifier") : null;
googleProrationMode = upgradeInfo.hasKey("prorationMode") ? upgradeInfo.getInt("prorationMode") : null;

// Legacy UpgradeInfo in V5 and earlier
if (googleOldProductId == null) {
googleOldProductId = upgradeInfo.hasKey("oldSKU") ? upgradeInfo.getString("oldSKU") : null;
}
}

return new GoogleUpgradeInfo(googleOldProductId, googleProrationMode);
}
}

0 comments on commit 02e9b02

Please sign in to comment.