platformPayCreatePaymentMethod method

  1. @override
Future<PlatformPayPaymentMethod> platformPayCreatePaymentMethod({
  1. required PlatformPayPaymentMethodParams params,
  2. bool usesDeprecatedTokenFlow = false,
})
override

Use native payment sheet to create payment method

Implementation

@override
Future<PlatformPayPaymentMethod> platformPayCreatePaymentMethod({
  required PlatformPayPaymentMethodParams params,
  bool usesDeprecatedTokenFlow = false,
}) {
  if (!(params is PlatformPayPaymentMethodParamsWeb)) {
    throw WebUnsupportedError(
        "platformPayCreatePaymentMethod - ${params.runtimeType} is not supported on web");
  }

  Completer<PlatformPayPaymentMethod> completer = Completer();
  stripe_js.PaymentRequest paymentRequest =
      js.paymentRequest(params.options.toJS());
  paymentRequest.onPaymentMethod((response) {
    completer.complete(PlatformPayPaymentMethod(
        paymentMethod: response.paymentMethod.parse()));
    response.complete('success');
  });
  paymentRequest.onCancel(() {
    completer.completeError(StripeException(
        error: LocalizedErrorMessage(
            code: FailureCode.Canceled,
            message: 'Payment request cancelled')));
  });
  paymentRequest.isPaymentAvailable.then((available) {
    if (available) {
      paymentRequest.show();
    } else {
      completer.completeError(StripeException(
          error: LocalizedErrorMessage(
              code: FailureCode.Failed,
              message:
                  "No enabled wallets are available for payment method creation")));
    }
  });

  return completer.future;
}