Great! You've successfully implemented In-App Purchases (IAP) in your Flutter app for a non-consumable subscription or product, and the purchase seems to be working on the client side (buyNonConsumable returns true). Now you're asking:
"Now what and how backend does?"
Let me walk you through what should happen next, especially from a backend perspective.
✅ Step 1: Understand What buyNonConsumable(...) Returning true Means
When this line:
var res = await InAppPurchase.instance.buyNonConsumable(purchaseParam: purchaseParam);
returns true, it only means that the purchase flow was initiated successfully. It doesn't confirm that the user actually completed the purchase.
So, the next step is to listen for a purchase update, which gives you confirmation of success or failure.
✅ Step 2: Handle Purchase Updates on the Client Side
You need to set up a listener for purchase updates:
final Stream<List<PurchaseDetails>> purchaseUpdatedStream = InAppPurchase.instance.purchaseStream;
_subscription = purchaseUpdatedStream.listen((purchases) {
purchases.forEach((purchase) => _handlePurchase(purchase));
});
And handle each purchase:
void _handlePurchase(PurchaseDetails purchaseDetails) async {
if (purchaseDetails.status == PurchaseStatus.purchased) {
// Validate receipt with backend
bool valid = await validateReceipt(purchaseDetails); // Send to backend
if (valid) {
// Unlock feature/subscription in app
}
if (Platform.isAndroid) {
InAppPurchase.instance.completePurchase(purchaseDetails);
}
} else if (purchaseDetails.status == PurchaseStatus.error) {
// Handle error
}
}
🔐 Step 3: Backend Validation (Critical)
The most important part of IAP is verifying the receipt with the App Store / Google Play via your backend.
Why Validate on Backend?
To prevent fraud and ensure that users aren’t faking purchases.
What Do You Send to Your Backend?
From purchaseDetails.verificationData, send either:
- serverVerificationData (iOS)
- purchaseToken (Android)
to your backend.
Then, your backend must verify the receipt with:
- Apple's App Store Server API (for iOS)
- Google’s Play Developer API (for Android)
🧠 Backend Flow Overview
Here’s how your backend should work:
1. Receive Purchase Info from App
- From Flutter: send
purchaseToken (Android) or transactionReceipt (iOS) + product ID
- Use secure HTTPS POST request
2. Validate Receipt with App Store / Google Play
For iOS:
Use Apple’s VerifyReceipt endpoint or newer App Store Server API:
Example:
POST https://buy.itunes.apple.com/verifyReceipt
{
"receipt-data": "<base64 encoded receipt>",
"password": "<your shared secret>" // Only for auto-renewable subscriptions
}
For Android:
Use Google’s REST API:
Use OAuth2 to authenticate as a service account with access to Google Play Console.
Call:
GET https://www.googleapis.com/androidpublisher/v3/applications/{packageName}/purchases/subscriptions/{productId}/tokens/{token}
3. Check Response
Make sure the response says:
- Subscription is active
- Not expired
- Matches the product ID
- No errors
4. Update User Status in DB
Once verified, store something like:
{
"user_id": "abc123",
"product_id": "premium_monthly",
"purchase_token": "xyz789",
"expires_at": "2025-04-01T12:00:00Z",
"is_active": true
}
5. Return Success to App
Respond to the app that the purchase is valid and unlock features accordingly.
🔄 Handling Renewals & Expirations
For auto-renewable subscriptions, your backend needs to:
- Poll Apple/Google periodically
- Listen to server notifications (Apple Webhooks, Google Pub/Sub)
- Update expiration dates automatically
This ensures users don’t lose access when their subscription renews.
🔒 Security Tips
- Never trust the client — always validate receipts server-side
- Use HTTPS for all communication
- Don’t hardcode secrets in the app
- Rate limit purchase verification endpoints
- Avoid sharing receipt data between devices/users
✅ Summary: What Comes Next
| Step | Action |
|---|
| ✅ | Setup PurchaseStream listener in Flutter |
| ✅ | Handle PurchaseStatus.purchased |
| ✅ | Send verification data to backend |
| ✅ | Backend validates with Apple/Google |
| ✅ | If valid, update user status in DB |
| ✅ | Respond to app so UI reflects new status |
| ✅ | Handle renewal/expirations using webhooks |
🛠 Tools & Libraries
If you want, I can help you write the backend validation code for Node.js, Python, or any language you're using.
Let me know!