Skip to content

Commit

Permalink
[PurchaseTester] Add option to purchase an arbitrary product id (#1099)
Browse files Browse the repository at this point in the history

Co-authored-by: pablo-guardiola <131195486+pablo-guardiola@users.noreply.github.com>
  • Loading branch information
MarkVillacampa and pablo-guardiola authored Jun 30, 2023
1 parent 6f336b1 commit fb12997
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {
Expand All @@ -50,6 +60,10 @@ class OverviewFragment : Fragment(), OfferingCardAdapter.OfferingCardAdapterList
navigateToProxyFragment()
}

binding.purchaseProductIdButton.setOnClickListener {
showPurchaseProductIdDialog()
}

viewModel = OverviewViewModel(this)
binding.lifecycleOwner = this
binding.viewModel = viewModel
Expand Down Expand Up @@ -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<StoreProduct>) {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@
android:layout_marginEnd="@dimen/padding_large"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.button.MaterialButton
android:id="@+id/purchase_product_id_button"
android:text="@string/buy_product"
android:layout_gravity="end|bottom"
android:layout_marginEnd="@dimen/padding_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/proxy_button"
android:text="@string/proxy"
Expand Down
1 change: 1 addition & 0 deletions examples/purchase-tester/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
<string name="proxy_entitlement_override">Entitlement override (Will grant entitlements)</string>
<string name="proxy_server_down">Server down (All requests will return 500)</string>
<string name="proxy">Proxy</string>
<string name="buy_product">Buy Product</string>
</resources>

0 comments on commit fb12997

Please sign in to comment.