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
    }
}];
Apphud.purchase(activity, product) { result -> }
await Apphud.purchase(product: product)
await ApphudSdk.purchase(productIdentifier);

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 Tips.

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, details, paywallIdentifier)
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 ->  }
//or 
Apphud.syncPurchases()
await Apphud.restorePurchases();
// or
await Apphud.syncPurchases();
await ApphudSdk.restorePurchases();
// or
await ApphudSdk.syncPurchases();

Additional Resources: