Custom Purchase Controller
Use a custom purchase controller with the Unity SDK beta.
By default, Superwall handles purchase and restore flows through the native iOS and Android SDKs. Most Unity integrations should start there.
Use IPurchaseController only when another system, such as your own store abstraction or a third
party purchase SDK, must complete purchases and restores.
Implement IPurchaseController
using System;
using Superwall;
public sealed class GamePurchaseController : IPurchaseController
{
public void PurchaseFromAppStore(string productId, Action<PurchaseResult> completion)
{
// Complete the App Store purchase with your purchase system.
// Then call completion with the result.
completion(PurchaseResult.Purchased());
}
public void PurchaseFromGooglePlay(
string productId,
string basePlanId,
string offerId,
Action<PurchaseResult> completion
)
{
// Complete the Google Play purchase with your purchase system.
// Use basePlanId and offerId if your catalog needs them.
// Then call completion with the result.
completion(PurchaseResult.Purchased());
}
public void RestorePurchases(Action<RestorationResult> completion)
{
// Restore purchases with your purchase system.
completion(RestorationResult.Restored());
}
}Pass the controller when configuring Superwall:
Superwall.Configure(
"MY_PUBLIC_API_KEY",
purchaseController: new GamePurchaseController()
);Sell custom store products
A custom store product is a product whose store is set to Custom on the Superwall dashboard. You sell it through your own payment system, such as Stripe, Paddle or a web checkout, instead of the App Store or Google Play. For how custom store products work on the dashboard and your server, see Custom Stores.
When a player buys one, Superwall calls PurchaseCustomProduct on iOS and Android. It doesn't call PurchaseFromAppStore or PurchaseFromGooglePlay for these products.
PurchaseCustomProduct needs Unity SDK 0.5.0 or later. iOS builds need 0.6.0 or later.
public void PurchaseCustomProduct(
CustomProductPurchase purchase,
Action<PurchaseResult> completion
)
{
// Start your checkout for purchase.ProductId and pass
// purchase.CustomTransactionId along to your server.
MyCheckout.Buy(purchase.ProductId, purchase.CustomTransactionId, succeeded =>
{
if (succeeded)
{
// Superwall doesn't grant entitlements for custom products.
Superwall.Shared.SubscriptionStatus = SubscriptionStatus.CreateActive(entitlements);
completion(PurchaseResult.Purchased());
}
else
{
completion(PurchaseResult.Cancelled());
}
});
}Replace MyCheckout with your own payment code. CustomProductPurchase has these fields:
Prop
Type
When your checkout finishes, call completion once with PurchaseResult.Purchased(), PurchaseResult.Cancelled(), PurchaseResult.Pending() or PurchaseResult.Failed(error). After a successful purchase, set Superwall.Shared.SubscriptionStatus yourself. Superwall doesn't grant entitlements for custom products.
You don't have to implement PurchaseCustomProduct. If you don't, every custom product purchase fails. If your method throws, the purchase fails with the exception's message.
Replace a subscription on Android
A paywall's Purchase action can replace the player's current subscription instead of buying a second one. Use this for upgrades, downgrades and plan switches. Superwall replaces an active subscription that shares an entitlement with the new product.
Subscription replacement needs Unity SDK 0.4.1 or later and only works on Android. The If subscribed setting on the Purchase action is behind a feature flag. If you don't see it in the paywall editor, contact Superwall support.
To turn it on for a purchase button:
- Open your paywall in the paywall editor.
- Select the button that has the Purchase action.
- In If subscribed, choose how to charge for the switch.
- Publish the paywall.
Superwall calls this overload of PurchaseFromGooglePlay for every Google Play purchase. replacement is set only when the button has an If subscribed option and the player has a subscription to replace:
public void PurchaseFromGooglePlay(
string productId,
string basePlanId,
string offerId,
ProductReplacement replacement,
Action<PurchaseResult> completion
)
{
if (replacement != null)
{
// Pass replacement.OldPurchaseToken and replacement.Mode
// to your billing flow's SubscriptionUpdateParams.
}
// Complete the purchase, then call completion with the result.
completion(PurchaseResult.Purchased());
}replacement is null for a normal purchase, and when the player has no subscription to replace. If you don't implement this overload, Superwall calls the four-argument PurchaseFromGooglePlay and ignores the replacement. Without a custom purchase controller, Superwall handles the replacement for you.
ProductReplacement has these fields:
Prop
Type
Map Mode to your billing flow like this:
Mode | If subscribed option | What to do |
|---|---|---|
Default | Switch plan (store default) | Leave the replacement mode unset. Google Play uses the mode set for the base plan in Play Console. |
ChargeProrated | Switch, charge difference now | Use ReplacementMode.CHARGE_PRORATED_PRICE. Replaces now and charges the prorated difference. Google Play allows this only for upgrades. |
ChargeFullPrice | Switch, charge full price now | Use ReplacementMode.CHARGE_FULL_PRICE. Replaces now and charges the new product's full price. |
The editor doesn't offer Switch, charge difference now for Android apps, because Google Play refuses it on a downgrade. You only get ChargeProrated from a paywall that already uses it.
If you use RevenueCat, pass the replacement as its oldProductId and googleReplacementMode options.
For a custom store product, the replacement arrives on CustomProductPurchase.Replacement instead. Its OldProductId comes from the active entitlements you set in SubscriptionStatus, and OldPurchaseToken is null. Make the switch with your own payment system, then update SubscriptionStatus.
Keep Subscription Status in Sync
When your purchase system is the source of truth, update Superwall whenever access changes.
Superwall.Shared.SubscriptionStatus = SubscriptionStatus.CreateActive(entitlements);Or clear access:
Superwall.Shared.SubscriptionStatus = SubscriptionStatus.CreateInactive();See Tracking Subscription State for a complete example.
How is this guide?