How to implement in app purchase for iOS in flutter. How to load data and how to retrieve success response of the purchase request


Here’s my code for the in app purchase but its not showing the purchase success data.

import ‘dart:async’;
import ‘dart:convert’;

import ‘package:in_app_purchase/in_app_purchase.dart’;

class PurchaseService {
final InAppPurchase _connection = InAppPurchase.instance;

Future initStore(String productID) async {
await connection
.isAvailable()
.then((value) {
if (!value) {
throw Exception(‘In-app purchases are not available on this device.’);
}
})
.then((
) => _connection.queryProductDetails({productID}))
.then((productDetails) {
for (ProductDetails entry in productDetails.productDetails) {
print(jsonEncode(entry));
print(‘Product ID: ${entry.id}’);
print(‘Title: ${entry.title}’);
print(‘Description: ${entry.description}’);
print(‘Price: ${entry.price}’);
}
});
}

Future buyProduct(String productID) async {
final PurchaseParam purchaseParam = PurchaseParam(
productDetails: ProductDetails(
id: productID,
title: ‘Product Title’,
description: ‘Product Description’,
price: ‘9.99’,
// Replace with the actual price
currencyCode: ‘USD’,
// Replace with the appropriate currency code
rawPrice: 9.99,
),
);

try {
  final PurchaseDetails purchase = (await _connection.buyConsumable(purchaseParam: purchaseParam)) as PurchaseDetails;
  // Handle successful purchase
  if (purchase.status == PurchaseStatus.purchased) {
    // Handle a successful purchase.
  } else if (purchase.status == PurchaseStatus.error) {
    // Handle purchase error (e.g., user canceled, network error).
    initStore(productID);
  }
} catch (error) {
  // Handle purchase error
  print('Purchase error: $error');
}

}
}

Please help me to sort this. My payment is getting sucessfull but i am not able to retrieve success data.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img