completePurchase method

  1. @override
Future<BillingResultWrapper> completePurchase(
  1. PurchaseDetails purchase
)

Mark that purchased content has been delivered to the user.

You are responsible for completing every PurchaseDetails whose PurchaseDetails.status is PurchaseStatus.purchased or PurchaseStatus.restored. Completing a PurchaseStatus.pending purchase will cause an exception. For convenience, PurchaseDetails.pendingCompletePurchase indicates if a purchase is pending for completion.

Warning

On iOS/macOS, If you do not call completePurchase for a transaction, that transaction will remain in Apple's unfinished transaction queue. This has two consequences:

  1. The transaction will be repeatedly re-delivered on the purchaseStream every time the app is restarted.
  2. Any subsequent attempts to buy the same product ID will fail with a purchase error indicating a duplicate transaction is pending. On Android, If you do not call completePurchase for a transaction on Android, Google Play will automatically refund and revoke the purchase after 3 days.

The method will throw a PurchaseException when the purchase could not be finished. Depending on the PurchaseException.errorCode the developer should try to complete the purchase via this method again, or retry the completePurchase method at a later time. If the PurchaseException.errorCode indicates you should not retry there might be some issue with the app's code or the configuration of the app in the respective store. The developer is responsible to fix this issue. The PurchaseException.message field might provide more information on what went wrong.

Implementation

@override
Future<BillingResultWrapper> completePurchase(PurchaseDetails purchase) async {
  assert(
    purchase is GooglePlayPurchaseDetails,
    'On Android, the `purchase` should always be of type `GooglePlayPurchaseDetails`.',
  );

  final googlePurchase = purchase as GooglePlayPurchaseDetails;

  if (googlePurchase.billingClientPurchase.isAcknowledged) {
    return const BillingResultWrapper(responseCode: BillingResponse.ok);
  }

  return billingClientManager.runWithClient(
    (BillingClient client) =>
        client.acknowledgePurchase(purchase.verificationData.serverVerificationData),
  );
}