payWithApplePay method

Future<PaymentStatus> payWithApplePay(
  1. ApplePaySettings applePay
)

Perform a transaction natively with Apple Pay.

This method will throw a NOT_SUPPORTED error on any platform other than iOS.

Implementation

Future<PaymentStatus> payWithApplePay(ApplePaySettings applePay) async {
  if (defaultTargetPlatform != TargetPlatform.iOS) {
    throw HyperpayException(
      'Apple Pay is not supported on $defaultTargetPlatform.',
      'NOT_SUPPORTED',
    );
  }

  try {
    final result = await _channel.invokeMethod(
      'start_payment_transaction',
      {
        'checkoutID': _checkoutID,
        'brand': BrandType.applepay.name.toUpperCase(),
        ...applePay.toJson(),
      },
    );

    log('$result', name: "HyperpayPlugin/platformResponse");

    if (result == 'canceled') {
      // Checkout session is still going on.
      return PaymentStatus.init;
    }

    final status = await paymentStatus(
      _checkoutID,
      headers: _checkoutSettings?.headers,
    );

    final String code = status['code'];

    if (code.paymentStatus == PaymentStatus.rejected) {
      throw HyperpayException(
          "Rejected payment.", code, status['description']);
    } else {
      log('${code.paymentStatus}', name: "HyperpayPlugin/paymentStatus");

      _clearSession();
      _checkoutID = '';

      return code.paymentStatus;
    }
  } catch (e) {
    log('$e', name: "HyperpayPlugin/payWithApplePay");
    rethrow;
  }
}