iosswiftin-app-purchasesubscriptionin-app-subscription

How do I get "transaction_id" and "receipt_data" from Superwall?


I have implemented Superwall in my Swift iOS App. I haven't implemented StoreKit. I have shown paywall like below:

//superwall delegate
  Superwall.shared.delegate = self
            
//show paywall
Superwall.shared.register(event: SuperwallEvents.CAMPAIGN_TRIGGER.rawValue) {
print("Action to take if they are on a paid plan")
 }

Below is the delegate method:

// MARK: - Superwall Delegate
extension InputCameraScreen: SuperwallDelegate {
    func handleSuperwallEvent(withInfo eventInfo: SuperwallEventInfo) {
        switch eventInfo.event {
        case .transactionComplete(let transaction, let product, let paywallInfo):
            print("Converted from paywall originalTransactionIdentifier: \(transaction?.originalTransactionIdentifier ?? "")")
            print("Converted from paywall storeTransactionId: \(transaction?.storeTransactionId ?? "")")
            print("Converted from paywall productIdentifier: \(product.productIdentifier)")
            print("Converted from paywall paywallInfo: \(paywallInfo.identifier)")
        case .transactionRestore(let restoreType, let paywallInfo):
            print("transactionRestore restoreType \(restoreType)")
        default:
            print("default \(#function) - \(eventInfo.event)")
        }
    }
}

I have some queries:

  1. Do I need StoreKit along with Superwall? I have downloaded Example Projects and I followed "Superwall-UIKit-Swift.xcodeproj" this example which doesn't implement StoreKit. So, I'm a little bit confused about implementing StoreKit along with Superwall.

  2. So, users can purchase subscriptions even if they don't have an account, But after purchasing subscriptions how do i connect this purchase with user's account?

  3. How do I get "transaction_id" and "receipt_data" from Superwall? I have to send this data to the server.

Apologies if I have missed some points or misunderstood something. This is my first time implementing subscriptions in my app. I would genuinely appreciate any guidance someone could provide.

Thank you!


Solution

  • Dev rel at Superwall here, let me answer your questions one by one:

    Do I need StoreKit along with Superwall? I have downloaded Example Projects and I followed "Superwall-UIKit-Swift.xcodeproj" this example which doesn't implement StoreKit. So, I'm a little bit confused about implementing StoreKit along with Superwall.

    No! We handle every aspect of the transaction, no need to dip down into StoreKit unless you need more fine-grain control over the purchasing pipeline. Under the hood, we are just using StoreKit anyways (on iOS at least).

    So, users can purchase subscriptions even if they don't have an account, But after purchasing subscriptions how do i connect this purchase with user's account?

    That's where your own application logic comes in. We can tell you about their subscription status via Superwall.shared.subscriptionStatus and then you can forward that or otherwise attach that paid status to your user. You could even listen for changes in that status, too:

    subscribedCancellable = Superwall.shared.$subscriptionStatus
      .receive(on: DispatchQueue.main)
      .sink { [weak self] status in
        switch status {
        case .unknown:
          self?.subscriptionLabel.text = "Loading subscription status."
        case .active:
          self?.subscriptionLabel.text = "You currently have an active subscription. Therefore, the paywall will never show. For the purposes of this app, delete and reinstall the app to clear subscriptions."
        case .inactive:
          self?.subscriptionLabel.text = "You do not have an active subscription so the paywall will show when clicking the button."
        }
      }
    

    Now, if the user could purchase your subscription from somewhere else, like on your website - then you would want to use a PurchaseController to match up their subscription status manually. Docs on that here.

    How do I get "transaction_id" and "receipt_data" from Superwall? I have to send this data to the server.

    You're on the right path in your code sample:

    func handleSuperwallEvent(withInfo eventInfo: SuperwallEventInfo) {
        switch eventInfo.event {
        case .transactionComplete(let transaction, let product, let paywallInfo):
            // Use either sk1Transaction or sk2Transaction 
            if let sk2xaction = transaction?.sk2Transaction {
                // You have the StoreKit transaction here to query as needed
            }
        default:
            print("Default event: \(eventInfo.event.description)")
        }
    }
    

    Getting receipt data is different in StoreKit 1/2, I'm not sure which you're using. Here's a good resource for StoreKit 1 and in StoreKit 2, that concept doesn't apply since it's been more or less replaced with Transaction.currentEntitlements. Check out our blog post over StoreKit 2 to see how it all works.

    Hope this helps!