Making Purchases

Make a Purchase

To make a purchase call:

Task {
  	 // productStruct is Product struct model from StoreKit2
     // $isPurchasing should be used only in SwiftUI apps, otherwise don't use this parameter
    let result = await Apphud.purchase(productStruct, isPurchasing: $isPurchasing)
    if result.success {
      // handle successful purchase
    }
}
Apphud.purchase(product) { result in
   if let subscription = result.subscription, subscription.isActive(){
      // has active subscription
   } else if let purchase = result.nonRenewingPurchase, purchase.isActive(){
      // has active non-renewing purchase
   } else {
      // handle error or check transaction status.
   }
}
[Apphud purchase:product callback:^(ApphudPurchaseResult * result) {
    if (result.subcription.isActive){
      // has active subscription
    } else if (result.nonRenewingPurchase.isActive){
      // has active non-renewing purchase
    } else {
      // handle error or check transaction status
    }
}];
// Passing offerIdToken is required for subscriptions
// However, for consumable and non-consumable purchases offerIdToken should be null.
// Pass consumableInAppProduct true or false to consume or acknowledge the purchase respectively.
pass true instead of false in case of purchasing consumable in-app purchase.
Apphud.purchase(activity, product, offerIdToken, null, null, false) { result -> }
await Apphud.purchase(product: product)
ApphudSdk.purchaseProduct(product).then((result) => {
      // todo
    });

📘

OfferIdToken on Android

New Google Play Subscriptions API require developers to pass offerIdToken when making a purchase, which can be found inside ProductDetails object. OfferIdToken is a string that represents specific base-plan-offer pair.

This method will return an ApphudPurchaseResult object or ApphudAsynPurchaseResult, which contains transaction, error, subscription or nonRenewingPurchase object in case of non-renewing purchase. See ApphudPurchaseResult.swift, ApphudSubscription.swift, ApphudAsyncPurchaseResult and ApphudPurchaseResult.kt files for details.

You can also read In-App Purchase Testing Guide.

Making a Purchase on VisionOS

To make a purchase on VisionOS device pass UIScene object to a purchase method:

Task {
  	 // product is Product struct model from StoreKit2
  	// scene is visible UIScene object
     // $isPurchasing should be used only in SwiftUI apps, otherwise don't use this parameter
    let result = await Apphud.purchase(product, scene: scene, isPurchasing: $isPurchasing)
    if result.success {
      // handle successful purchase
    }
}

Handling Purchases in Observer Mode

If you make purchases by your own code, then you are responsible for submitting successful purchases to Apphud on Android.

// no code required
// no code required
Apphud.trackPurchase(purchase, productDetails, offerIdToken)
await Apphud.syncPurchases();
await ApphudSdk.syncPurchases();

You don't need to do anything with purchases in Observer Mode on iOS.

🚧

Important Note

Keep in mind, that in Observer Mode you are responsible for acknowledging / consuming all purchases on Android. And on iOS, you are responsible for finishing transactions.

Restore Purchases

You should add this code upon action on tapping 'Restore Purchases' button in your app's UI. Handle result in a callback.

await Apphud.restorePurchases()
Apphud.restorePurchases{ subscriptions, purchases, error in 
   if Apphud.hasActiveSubscription(){
     // has active subscription  
   } else {
     // no active subscription found, check non-renewing purchases or error
   }
}
[Apphud restorePurchasesWithCallback:^(NSArray<ApphudSubscription *> * _Nullable subscriptions, NSArray *purchases, NSError * _Nullable error) {
   if (Apphud.hasActiveSubscription){
     // has active subscription
   } else {
     // no active subscription found, check non-renewing purchases or error
   }
   
}];
Apphud.restorePurchases { subscriptions, purchases, error ->  }
await Apphud.restorePurchases();
// or
await Apphud.syncPurchases();
await ApphudSdk.restorePurchases();
// or
await ApphudSdk.syncPurchases();

Additional Resources: