Manually Handling Purchases and Subscription Status
Using a PurchaseController
is only recommended for advanced use cases. By default, Superwall handles all
subscription-related logic and purchasing operations for you out of the box.
By default, Superwall handles basic subscription-related logic for you:
- Purchasing: When the user initiates a checkout on a paywall.
- Restoring: When the user restores previously purchased products.
- Subscription Status: When the user's subscription status changes to active or expired (by checking the local receipt).
However, if you want more control, you can pass in a PurchaseController
when configuring the SDK via configure(apiKey:purchaseController:options:)
and manually set Superwall.shared.subscriptionStatus
to take over this responsibility.
Step 1: Creating a PurchaseController
A PurchaseController
handles purchasing and restoring via protocol methods that you implement.
export class MyPurchaseController extends PurchaseController {
// 1
async purchaseFromAppStore(productId: string): Promise<PurchaseResult> {
// TODO
// ----
// Purchase via StoreKit, RevenueCat, Qonversion or however
// you like and return a valid PurchaseResult
}
async purchaseFromGooglePlay(
productId: string,
basePlanId?: string,
offerId?: string
): Promise<PurchaseResult> {
// TODO
// ----
// Purchase via Google Billing, RevenueCat, Qonversion or however
// you like and return a valid PurchaseResult
}
// 2
async restorePurchases(): Promise<RestorationResult> {
// TODO
// ----
// Restore purchases and return true if successful.
}
}
Here’s what each method is responsible for:
- Purchasing a given product. In here, enter your code that you use to purchase a product. Then, return the result of the purchase as a
PurchaseResult
. For Flutter, this is separated into purchasing from the App Store and Google Play. This is an enum that contains the following cases, all of which must be handled:.cancelled
: The purchase was cancelled..purchased
: The product was purchased..pending
: The purchase is pending/deferred and requires action from the developer..failed(Error)
: The purchase failed for a reason other than the user cancelling or the payment pending.
- Restoring purchases. Here, you restore purchases and return a
RestorationResult
indicating whether the restoration was successful or not. If it was, return.restore
, orfailed
along with the error reason.
Step 2: Configuring the SDK With Your PurchaseController
Pass your purchase controller to the configure(apiKey:purchaseController:options:)
method:
export default function App() {
React.useEffect(() => {
const apiKey = Platform.OS === "ios" ? "MY_IOS_API_KEY" : "MY_ANDROID_API_KEY"
const purchaseController = new MyPurchaseController()
Superwall.configure({
apiKey: apiKey,
purchaseController: purchaseController,
})
}, [])
}
Step 3: Keeping subscriptionStatus
Up-To-Date
You must set Superwall.shared.subscriptionStatus
every time the user's subscription status changes, otherwise the SDK won't know who to show a paywall to. This is an enum that has three possible cases:
.unknown
: This is the default value. In this state, paywalls will not show and their presentation will be automatically delayed untilsubscriptionStatus
changes to a different value..active(let entitlements)
: Indicates that the user has an active entitlement. Paywalls will not show in this state unless you remotely set the paywall to ignore subscription status. A user can have one or more active entitlement..inactive
: Indicates that the user doesn't have an active entitlement. Paywalls can show in this state.
Here's how you might do this:
// When a subscription is purchased, restored, validated, expired, etc...
myService.addSubscriptionStatusListener((subscriptionInfo: SubscriptionInfo) => {
const entitlements = Object.keys(subscriptionInfo.entitlements.active).map((id) => ({
id,
}))
if (entitlements.length === 0) {
Superwall.shared.setSubscriptionStatus(SubscriptionStatus.Inactive())
} else {
Superwall.shared.setSubscriptionStatus(
SubscriptionStatus.Active(entitlements.map((id) => new Entitlement(id)))
)
}
})
subscriptionStatus
is cached between app launches Listening for subscription status changes
If you need a simple way to observe when a user's subscription status changes, on iOS you can use the Publisher
for it. Here's an example:
Superwall.shared.subscriptionStatusEmitter.addListener("change", (status) => {
switch (status.status) {
case "ACTIVE":
break
default:
break
}
})
You can do similar tasks with the SuperwallDelegate
, such as viewing which product was purchased from a paywall.
How is this guide?