confirmPayment method

  1. @override
Future<PaymentIntent> confirmPayment(
  1. String paymentIntentClientSecret,
  2. PaymentMethodParams? params, [
  3. PaymentMethodOptions? options
])
override

Implementation

@override
Future<PaymentIntent> confirmPayment(
  String paymentIntentClientSecret,
  PaymentMethodParams? params, [
  PaymentMethodOptions? options,
]) async {
  assert(params != null, 'params are not allowed to be null on the web');
  final response =
      await params!.maybeWhen<Future<stripe_js.PaymentIntentResponse>>(
    card: (usage) {
      return js.confirmCardPayment(
        paymentIntentClientSecret,
        data: stripe_js.ConfirmCardPaymentData(
          paymentMethod: stripe_js.CardPaymentMethodDetails(card: element!),
          setupFutureUsage: options?.setupFutureUsage?.toJs(),
        ),
      );
    },
    cardFromMethodId: (paymentMethodData) {
      // https://stripe.com/docs/js/payment_intents/confirm_card_payment#stripe_confirm_card_payment-existing
      return js.confirmCardPayment(
        paymentIntentClientSecret,
        data: stripe_js.ConfirmCardPaymentData(
          paymentMethod: stripe_js.CardPaymentMethodDetails.id(
            paymentMethodData.paymentMethodId,
          ),
        ),
      );
    },
    cardFromToken: (PaymentMethodDataCardFromToken data) {
      // https: //stripe.com/docs/js/payment_intents/confirm_card_payment#stripe_confirm_card_payment-token
      return js.confirmCardPayment(
        paymentIntentClientSecret,
        data: stripe_js.ConfirmCardPaymentData(
          paymentMethod: stripe_js.CardPaymentMethodDetails.token(
            card: stripe_js.CardTokenPaymentMethod(token: data.token),
          ),
          setupFutureUsage: (options?.setupFutureUsage ??
                  PaymentIntentsFutureUsage.OnSession)
              .toJs(),
        ),
      );
    },
    alipay: (data) {
      // https://stripe.com/docs/js/payment_intents/confirm_alipay_payment#stripe_confirm_alipay_payment-options
      return js.confirmAlipayPayment(
        paymentIntentClientSecret,
        data: stripe_js.ConfirmAlipayPaymentData(
          returnUrl: web.window.location.href,
        ),
      );
    },
    ideal: (paymentData) {
      if (paymentData.bankName == null) throw 'bankName is required for web';
      // https://stripe.com/docs/js/payment_intents/confirm_alipay_payment#stripe_confirm_alipay_payment-options
      return js.confirmIdealPayment(
        paymentIntentClientSecret,
        data: stripe_js.ConfirmIdealPaymentData(
          paymentMethod: stripe_js.IdealPaymentMethodDetails.withBank(
            ideal: stripe_js.IdealBankData(bank: paymentData.bankName!),
          ),
          returnUrl: urlScheme,
          // recommended
          // setup_future_usage:
        ),
      );
    },
    orElse: () {
      throw WebUnsupportedError();
    },
  );
  if (response.error != null) {
    throw StripeError(
      message: response.error?.message ?? '',
      code: response.error!.code,
    );
  }

  return response.paymentIntent!.parse();
}