Skip to main content
Solved

How to implement purchase(package:) using SubscriptionStoreView?

  • December 11, 2024
  • 1 reply
  • 112 views

Forum|alt.badge.img+3

Hi, I’m developing iOS 18+ app with RevenueCat 5.12.1.

I found SubscriptionStoreView is almost perfect for me to make a paywall.

My earlier code without SubscriptionStoreView used a custom purchase button like this.

    var buttonToPurchase: some View {

        let packages = offering?.availablePackages ?? []
        let package = packages.first ?? nil

        Button {

            guard let currentPackage = package else { return }

            Purchases.shared.purchase(package: currentPackage) { (transaction, info, error, userCancelled) in

                if info?.entitlements[Constants.entitlementID]?.isActive == true {
                    navigationViewModel.path.removeAll()
                } else if let error = error {
                    self.error = error as NSError
                    self.displayError = true
                }
            }

        } label: {
            Text("Continue")
        }
    }

 

I don’t know how to apply Purchase.shared.purchase(package:) into SubscriptionStoreView.

I know SubscriptionStoreView provides two modifiers

.onInAppPurchaseStart { product in … }

.onInAppPurchaseCompletion { product, result in … }

I also know SubscriptionStoreView displays inherently ‘subscribe button’.

But my code below seems to be wrong because it triggers subscription sheet twice, and I wonder, when a firstly triggered subscription is completed, if RevenueCat server is appropriately tracking the subscription with correct anonymousID for customer.

SubscriptionStoreView.forOffering(offering!) {
    premiumDescription
}
.onInAppPurchaseCompletion { product, result in
    let packages = offering?.availablePackages ?? []
    let package = packages.first ?? nil

    if case .success(.success(let transaction)) = result {
        print("Purchased successfully: \(transaction.signedDate)")

        guard let currentPackage = package else { return }

        Purchases.shared.purchase(package: currentPackage) { (transaction, info, error, userCancelled) in                                                                        

            if info?.entitlements[Constants.entitlementID]?.isActive == true {
                navigationViewModel.path.removeAll()

            } else if let error = error {
                 self.error = error as NSError
                 self.displayError = true
            }                      
        }
    } else {
        print("Purchase was cancelled.")
    }
}

Please help me 🙏 Thanks in advance!! 

Best answer by bg_kim

I figured out that RevenueCat observer mode can support the purchase logic from the default purchase button in SubscriptionStoreView.
 

https://www.revenuecat.com/docs/migrating-to-revenuecat/sdk-or-not/finishing-transactions
 

View original
Did this post help you find an answer to your question?
This post has been closed for comments

1 reply

Forum|alt.badge.img+3
  • Author
  • Helper
  • 3 replies
  • Answer
  • December 16, 2024

I figured out that RevenueCat observer mode can support the purchase logic from the default purchase button in SubscriptionStoreView.
 

https://www.revenuecat.com/docs/migrating-to-revenuecat/sdk-or-not/finishing-transactions
 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings