From fb129970b105f43add5c0bedd437ae1298dfdd93 Mon Sep 17 00:00:00 2001 From: Mark Villacampa Date: Fri, 30 Jun 2023 16:35:18 +0200 Subject: [PATCH] [PurchaseTester] Add option to purchase an arbitrary product id (#1099) Co-authored-by: pablo-guardiola <131195486+pablo-guardiola@users.noreply.github.com> --- .../purchasetester/OverviewFragment.kt | 75 ++++++++++++++++++- .../src/main/res/layout/fragment_overview.xml | 7 ++ .../src/main/res/values/strings.xml | 1 + 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/examples/purchase-tester/src/main/java/com/revenuecat/purchasetester/OverviewFragment.kt b/examples/purchase-tester/src/main/java/com/revenuecat/purchasetester/OverviewFragment.kt index f30cdfc742..4fcc6a5955 100644 --- a/examples/purchase-tester/src/main/java/com/revenuecat/purchasetester/OverviewFragment.kt +++ b/examples/purchase-tester/src/main/java/com/revenuecat/purchasetester/OverviewFragment.kt @@ -3,22 +3,32 @@ package com.revenuecat.purchasetester import android.content.Intent import android.net.Uri import android.os.Bundle +import android.text.InputType import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.EditText import android.widget.Toast import androidx.core.view.doOnPreDraw import androidx.fragment.app.Fragment import androidx.navigation.fragment.FragmentNavigatorExtras import androidx.navigation.fragment.findNavController import androidx.recyclerview.widget.LinearLayoutManager +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.transition.MaterialElevationScale +import com.revenuecat.purchases.CustomerInfo import com.revenuecat.purchases.Offering import com.revenuecat.purchases.Offerings +import com.revenuecat.purchases.PurchaseParams import com.revenuecat.purchases.Purchases import com.revenuecat.purchases.PurchasesError import com.revenuecat.purchases.getOfferingsWith +import com.revenuecat.purchases.interfaces.GetStoreProductsCallback +import com.revenuecat.purchases.interfaces.PurchaseCallback import com.revenuecat.purchases.logOutWith +import com.revenuecat.purchases.models.GoogleStoreProduct +import com.revenuecat.purchases.models.StoreProduct +import com.revenuecat.purchases.models.StoreTransaction import com.revenuecat.purchases_sample.R import com.revenuecat.purchases_sample.databinding.FragmentOverviewBinding @@ -28,7 +38,7 @@ class OverviewFragment : Fragment(), OfferingCardAdapter.OfferingCardAdapterList private lateinit var viewModel: OverviewViewModel private lateinit var binding: FragmentOverviewBinding - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { binding = FragmentOverviewBinding.inflate(inflater) binding.customerInfoLogoutButton.setOnClickListener { @@ -50,6 +60,10 @@ class OverviewFragment : Fragment(), OfferingCardAdapter.OfferingCardAdapterList navigateToProxyFragment() } + binding.purchaseProductIdButton.setOnClickListener { + showPurchaseProductIdDialog() + } + viewModel = OverviewViewModel(this) binding.lifecycleOwner = this binding.viewModel = viewModel @@ -103,6 +117,65 @@ class OverviewFragment : Fragment(), OfferingCardAdapter.OfferingCardAdapterList findNavController().navigate(directions, extras) } + private fun showPurchaseProductIdDialog() { + val builder = MaterialAlertDialogBuilder(requireContext()) + builder.setTitle("Enter the Product ID you want to purchase:") + val input = EditText(context) + input.inputType = InputType.TYPE_CLASS_TEXT + builder.setView(input) + builder.setPositiveButton("Purchase") { dialog, which -> + var productId = input.text.toString() + val segments = productId.split(":") + Purchases.sharedInstance.getProducts( + listOf(segments.first()), + object : GetStoreProductsCallback { + override fun onReceived(storeProducts: List) { + if (storeProducts.isEmpty()) { + showToast("A product with ID $productId does not exist") + return + } + var product = storeProducts.first() + if (segments.count() == 2) { + storeProducts.firstOrNull { (it as? GoogleStoreProduct)?.basePlanId == segments.last() } + .let { storeProduct -> + if (storeProduct != null) { + product = storeProduct + } else { + showToast("A product with ID $productId does not exist") + return + } + } + } + Purchases.sharedInstance.purchase( + PurchaseParams.Builder(requireActivity(), product).build(), + object : PurchaseCallback { + override fun onCompleted( + storeTransaction: StoreTransaction, + customerInfo: CustomerInfo, + ) { + showToast("Successful purchase, order id: ${storeTransaction.orderId}") + } + + override fun onError(error: PurchasesError, userCancelled: Boolean) { + showError(error) + } + }, + ) + } + + override fun onError(error: PurchasesError) { + showError(error) + } + }, + ) + } + builder.setNegativeButton("Cancel") { dialog, which -> + dialog.cancel() + } + val dialog = builder.create() + dialog.show() + } + override fun displayError(error: PurchasesError) { showError(error) } diff --git a/examples/purchase-tester/src/main/res/layout/fragment_overview.xml b/examples/purchase-tester/src/main/res/layout/fragment_overview.xml index 07dc82a2d1..7185a43c2b 100644 --- a/examples/purchase-tester/src/main/res/layout/fragment_overview.xml +++ b/examples/purchase-tester/src/main/res/layout/fragment_overview.xml @@ -240,6 +240,13 @@ android:layout_marginEnd="@dimen/padding_large" android:layout_width="wrap_content" android:layout_height="wrap_content"> + Entitlement override (Will grant entitlements) Server down (All requests will return 500) Proxy + Buy Product