Skip to content

Commit

Permalink
Add restorePurchases to custom entitlement computation package
Browse files Browse the repository at this point in the history
  • Loading branch information
tonidero committed Aug 28, 2023
1 parent 2d8ed01 commit a4a65af
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.revenuecat.purchases.interfaces.SyncPurchasesCallback
import com.revenuecat.purchases.logInWith
import com.revenuecat.purchases.logOutWith
import com.revenuecat.purchases.models.BillingFeature
import com.revenuecat.purchases.restorePurchasesWith
import com.revenuecat.purchases.syncPurchasesWith

@Suppress("unused", "UNUSED_VARIABLE", "EmptyFunctionBlock", "DEPRECATION")
Expand Down Expand Up @@ -48,7 +47,6 @@ private class PurchasesAPI {
purchases.logOut()
purchases.logOut(receiveCustomerInfoCallback)

purchases.restorePurchases(receiveCustomerInfoCallback)
purchases.invalidateCustomerInfoCache()

purchases.getCustomerInfo(receiveCustomerInfoCallback)
Expand Down Expand Up @@ -87,10 +85,6 @@ private class PurchasesAPI {
onError = { _: PurchasesError -> },
onSuccess = { _: CustomerInfo -> },
)
purchases.restorePurchasesWith(
onError = { _: PurchasesError -> },
onSuccess = { _: CustomerInfo -> },
)
purchases.syncPurchasesWith(
onError = { _: PurchasesError -> },
onSuccess = { _: CustomerInfo -> },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.revenuecat.purchases.getOfferingsWith
import com.revenuecat.purchases.getProductsWith
import com.revenuecat.purchases.interfaces.GetStoreProductsCallback
import com.revenuecat.purchases.interfaces.PurchaseCallback
import com.revenuecat.purchases.interfaces.ReceiveCustomerInfoCallback
import com.revenuecat.purchases.interfaces.ReceiveOfferingsCallback
import com.revenuecat.purchases.interfaces.UpdatedCustomerInfoListener
import com.revenuecat.purchases.models.BillingFeature
Expand All @@ -30,6 +31,7 @@ import com.revenuecat.purchases.models.StoreProduct
import com.revenuecat.purchases.models.StoreTransaction
import com.revenuecat.purchases.models.SubscriptionOption
import com.revenuecat.purchases.purchaseWith
import com.revenuecat.purchases.restorePurchasesWith
import java.net.URL
import java.util.concurrent.ExecutorService

Expand All @@ -40,6 +42,10 @@ private class PurchasesCommonAPI {
purchases: Purchases,
) {
val productIds = ArrayList<String>()
val receiveCustomerInfoCallback = object : ReceiveCustomerInfoCallback {
override fun onReceived(customerInfo: CustomerInfo) {}
override fun onError(error: PurchasesError) {}
}
val receiveOfferingsCallback = object : ReceiveOfferingsCallback {
override fun onReceived(offerings: Offerings) {}
override fun onError(error: PurchasesError) {}
Expand All @@ -54,6 +60,8 @@ private class PurchasesCommonAPI {
purchases.getProducts(productIds, productsResponseCallback)
purchases.getProducts(productIds, ProductType.SUBS, productsResponseCallback)

purchases.restorePurchases(receiveCustomerInfoCallback)

val appUserID: String = purchases.appUserID

purchases.removeUpdatedCustomerInfoListener()
Expand Down Expand Up @@ -124,6 +132,10 @@ private class PurchasesCommonAPI {
purchases.purchaseWith(
purchaseParams,
) { _: StoreTransaction?, _: CustomerInfo -> }
purchases.restorePurchasesWith(
onError = { _: PurchasesError -> },
onSuccess = { _: CustomerInfo -> },
)
}

suspend fun checkCoroutines(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.revenuecat.purchases.common.log
import com.revenuecat.purchases.interfaces.Callback
import com.revenuecat.purchases.interfaces.GetStoreProductsCallback
import com.revenuecat.purchases.interfaces.PurchaseCallback
import com.revenuecat.purchases.interfaces.ReceiveCustomerInfoCallback
import com.revenuecat.purchases.interfaces.ReceiveOfferingsCallback
import com.revenuecat.purchases.interfaces.UpdatedCustomerInfoListener
import com.revenuecat.purchases.models.BillingFeature
Expand Down Expand Up @@ -129,6 +130,24 @@ class Purchases internal constructor(
purchasesOrchestrator.removeUpdatedCustomerInfoListener()
}

/**
* Restores purchases made with the current Play Store account for the current user.
* This method will post all purchases associated with the current Play Store account to
* RevenueCat and become associated with the current `appUserID`. If the receipt token is being
* used by an existing user, the current `appUserID` will be aliased together with the
* `appUserID` of the existing user. Going forward, either `appUserID` will be able to reference
* the same user.
*
* You shouldn't use this method if you have your own account system. In that case
* "restoration" is provided by your app passing the same `appUserId` used to purchase originally.
* @param [callback] The listener that will be called when purchase restore completes.
*/
fun restorePurchases(
callback: ReceiveCustomerInfoCallback,
) {
purchasesOrchestrator.restorePurchases(callback)
}

/**
* Updates the current appUserID to a new one, without associating the two.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,26 +195,6 @@ fun Purchases.getCustomerInfoWith(
getCustomerInfo(fetchPolicy, receiveCustomerInfoCallback(onSuccess, onError))
}

/**
* Restores purchases made with the current Play Store account for the current user.
* This method will post all purchases associated with the current Play Store account to
* RevenueCat and become associated with the current `appUserID`. If the receipt token is being
* used by an existing user, the current `appUserID` will be aliased together with the
* `appUserID` of the existing user. Going forward, either `appUserID` will be able to reference
* the same user.
*
* You shouldn't use this method if you have your own account system. In that case
* "restoration" is provided by your app passing the same `appUserId` used to purchase originally.
* @param [onSuccess] Will be called after the call has completed.
* @param [onError] Will be called after the call has completed with an error.
*/
fun Purchases.restorePurchasesWith(
onError: (error: PurchasesError) -> Unit = ON_ERROR_STUB,
onSuccess: (customerInfo: CustomerInfo) -> Unit,
) {
restorePurchases(receiveCustomerInfoCallback(onSuccess, onError))
}

/**
* This method will send all the purchases to the RevenueCat backend. Call this when using your own implementation
* for subscriptions anytime a sync is needed, such as when migrating existing users to RevenueCat. The
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,23 @@ fun Purchases.getProductsWith(
) {
getProducts(productIds, type, getStoreProductsCallback(onGetStoreProducts, onError))
}

/**
* Restores purchases made with the current Play Store account for the current user.
* This method will post all purchases associated with the current Play Store account to
* RevenueCat and become associated with the current `appUserID`. If the receipt token is being
* used by an existing user, the current `appUserID` will be aliased together with the
* `appUserID` of the existing user. Going forward, either `appUserID` will be able to reference
* the same user.
*
* You shouldn't use this method if you have your own account system. In that case
* "restoration" is provided by your app passing the same `appUserId` used to purchase originally.
* @param [onSuccess] Will be called after the call has completed.
* @param [onError] Will be called after the call has completed with an error.
*/
fun Purchases.restorePurchasesWith(
onError: (error: PurchasesError) -> Unit = ON_ERROR_STUB,
onSuccess: (customerInfo: CustomerInfo) -> Unit,
) {
restorePurchases(receiveCustomerInfoCallback(onSuccess, onError))
}

0 comments on commit a4a65af

Please sign in to comment.