capture method

Future<void> capture({
  1. required DocumentBase<StripePurchaseModel> purchase,
  2. double? priceAmountOverride,
  3. Duration timeout = const Duration(seconds: 15),
})

Executed when the actual flow of money is generated after confirm.

You can override the payment amount by specifying priceAmountOverride.

However, this amount should be set to be less than the amount specified at purchase.

confirm後に実際の金銭の流れを発生させる際に実行します。

priceAmountOverrideを指定すると支払金額を上書きできます。

ただし、この金額はpurchase時に指定した金額を下回るように設定してください。

Implementation

Future<void> capture({
  required DocumentBase<StripePurchaseModel> purchase,
  double? priceAmountOverride,
  Duration timeout = const Duration(seconds: 15),
}) async {
  if (_completer != null) {
    return _completer!.future;
  }
  _completer = Completer<void>();
  try {
    final value = purchase.value;
    if (value == null) {
      throw Exception(
        "Purchase information is empty. Please run [create] method.",
      );
    }
    if (value.error) {
      throw Exception(
        "There has been an error with your payment. Please check and Refresh your payment information once.",
      );
    }
    if (value.canceled) {
      throw Exception("This purchase has already canceled.");
    }
    if (!value.confirm || !value.verified) {
      throw Exception(
        "The payment has not been confirmed yet. Please confirm the payment by clicking [confirm] and then execute.",
      );
    }
    if (value.captured) {
      throw Exception("This purchase has already captured.");
    }
    if (priceAmountOverride != null && value.amount < priceAmountOverride) {
      throw Exception(
        "You cannot capture an amount higher than the billing amount already saved.",
      );
    }
    final functionsAdapter =
        StripePurchaseMasamuneAdapter.primary.functionsAdapter ??
            FunctionsAdapter.primary;

    final response = await functionsAdapter.execute(
      StripeCapturePurchaseAction(
        userId: userId,
        orderId: value.orderId,
        priceAmountOverride: priceAmountOverride,
      ),
    );

    if (response.purchaseId.isEmpty) {
      throw Exception("Response is invalid.");
    }
    await Future.doWhile(() async {
      await Future.delayed(const Duration(milliseconds: 100));
      await purchase.reload();
      return purchase.value != null && !purchase.value!.confirm;
    }).timeout(timeout);
    _completer?.complete();
    _completer = null;
    notifyListeners();
  } catch (e) {
    _completer?.completeError(e);
    _completer = null;
    rethrow;
  } finally {
    _completer?.complete();
    _completer = null;
  }
}