create method

Future<void> create({
  1. required String orderId,
  2. required double priceAmount,
  3. String? targetUserId,
  4. String? description,
  5. Locale locale = const Locale("en", "US"),
  6. Duration timeout = const Duration(seconds: 15),
  7. String? emailTitleOnRequired3DSecure,
  8. String? emailContentOnRequired3DSecure,
})

Specify orderId and create payment information for the amount in priceAmount.

Specifying targetUserId will make the payment to that user. If this is not specified, everything is paid to the operator.

If this is done, no settlement is made. Then confirm to confirm the settlement, and capture to generate the actual flow of money.

orderIdを指定してpriceAmountの金額の決済情報を作成します。

targetUserIdを指定することでそのユーザーに支払いを行います。 これが指定されてない場合はすべて運営側に支払われます。

これを行った場合に決済は行われません。その後confirmを行い決済を確定、captureで実際のお金の流れが発生します。

Implementation

Future<void> create({
  required String orderId,
  required double priceAmount,
  String? targetUserId,
  String? description,
  Locale locale = const Locale("en", "US"),
  Duration timeout = const Duration(seconds: 15),
  String? emailTitleOnRequired3DSecure,
  String? emailContentOnRequired3DSecure,
}) async {
  if (_completer != null) {
    return _completer!.future;
  }
  _completer = Completer<void>();
  try {
    final modelQuery = collection(userId: userId).modelQuery;
    final purchaseCollection = StripePurchaseModelCollection(
      modelQuery.equal(StripePurchaseModelKeys.orderId.name, orderId),
    );
    await purchaseCollection.load();
    if (purchaseCollection.isNotEmpty) {
      throw Exception("Billing has already been created.");
    }
    final functionsAdapter =
        StripePurchaseMasamuneAdapter.primary.functionsAdapter ??
            FunctionsAdapter.primary;

    final response = await functionsAdapter.execute(
      StripeCreatePurchaseAction(
        userId: userId,
        priceAmount: priceAmount,
        revenueAmount: StripePurchaseMasamuneAdapter.primary.revenueRatio,
        currency: StripePurchaseMasamuneAdapter.primary.currency,
        targetUserId: targetUserId,
        orderId: orderId,
        description: description,
        email: StripeMail(
          from: StripePurchaseMasamuneAdapter.primary.supportEmail,
          title: emailTitleOnRequired3DSecure ??
              StripePurchaseMasamuneAdapter
                  .primary.threeDSecureOptions.emailTitle,
          content: emailContentOnRequired3DSecure ??
              StripePurchaseMasamuneAdapter
                  .primary.threeDSecureOptions.emailContent,
        ),
        locale: locale,
      ),
    );

    if (response.purchaseId.isEmpty) {
      throw Exception("Response is invalid.");
    }

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