refund method

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

purchase refund process.

If refundAmount is specified, the amount will be refunded. However, the amount must be less than the amount specified at the time of purchase.

purchaseの返金処理を行います。

refundAmountを指定するとその金額だけ返金されます。ただし、purchase時に指定した金額を下回るように設定してください。

Implementation

Future<void> refund({
  required DocumentBase<StripePurchaseModel> purchase,
  double? refundAmount,
  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.captured || !value.success) {
      throw Exception("The payment is not yet in your jurisdiction.");
    }
    if (value.refund) {
      throw Exception("The payment is already refunded.");
    }
    if (refundAmount != null && value.amount < refundAmount) {
      throw Exception(
        "The amount to be refunded exceeds the original amount.",
      );
    }
    final functionsAdapter =
        StripePurchaseMasamuneAdapter.primary.functionsAdapter ??
            FunctionsAdapter.primary;

    await functionsAdapter.execute(
      StripeRefundPurchaseAction(
        userId: userId,
        orderId: value.orderId,
        refundAmount: refundAmount,
      ),
    );

    await Future.doWhile(() async {
      await Future.delayed(const Duration(milliseconds: 100));
      await purchase.reload();
      return purchase.value != null && !purchase.value!.refund;
    }).timeout(timeout);
    _completer?.complete();
    _completer = null;
    notifyListeners();
  } catch (e) {
    _completer?.completeError(e);
    _completer = null;
    rethrow;
  } finally {
    _completer?.complete();
    _completer = null;
  }
}