Apple Pay Mimic

Apple Pay Mimic contains mimics to PassKit

Getting Started

Requires iOS 12

  • Add apple_pay_mimic dependency to your pubspec.yaml
  • Call dart await ApplePayMimic.init() for plugin initialization
  • You are ready

Table of Mimics

Enums

PassKit ApplePayMimic
PKPaymentNetwork APayPaymentNetwork
PKMerchantCapability APayMerchantCapability
PKShippingType APayShippingType
PKContactField APayContactField
PKPaymentSummaryItemType APayPaymentSummaryItemType
PKPaymentMethodType APayPaymentItemType

Structures

PassKit ApplePayMimic
PKPaymentSummaryItem APayPaymentSummaryItem
PKShippingMethod APayShippingMethod
PKContact APayContact
PKPaymentToken APayPaymentToken
PKPaymentMethod APayPaymentMethod
PKPayment APayPayment
PersonNameComponents APayPersonNameComponents
CNPostalAddress APayPostalAddress

Actions

PassKit ApplePayMimic
PKPaymentRequest.availableNetworks ApplePayMimic.availableNetworks
PKPaymentAuthorizationController.canMakePayments ApplePayMimic.canMakePayments
PKPaymentAuthorizationController.present ApplePayMimic.processPayment

Widgets

PassKit ApplePayMimic
PKPaymentButton ApplePayButton

Usage

Drawing a native PKPaymentButton

Container(
  height: 44, // ApplePayButton required a bounded constraints
  child: ApplePayButton(
    onPressed: _processPayment,
    style: APayPaymentButtonStyle.white,
    type: APayPaymentButtonType.checkout,
  ),
),

Getting a basic payment information

final List<APayPaymentNetwork> availableNetworks = await ApplePayMimic.availableNetworks();
final bool canMakePayments = await ApplePayMimic.canMakePayments();

/// A mimic to canMakePayments with params 
/// @see https://developer.apple.com/documentation/passkit/pkpaymentauthorizationcontroller/1649455-canmakepayments
final merchantCapabilities = [APayMerchantCapability.capability3DS];
final canMakePaymentsCard = await ApplePayMimic.canMakePayments(CanMakePaymentsRequest(
  usingNetworks: availableNetworks, // optional
  capabilities: merchantCapabilities, // optional
));

Making a payment request

/// @see https://developer.apple.com/documentation/passkit/pkpaymentrequest
final request = ProcessPaymentRequest(
  // required arguments
  merchantIdentifier: 'merchant.id',
  countryCode: 'US',
  currencyCode: 'USD',
  shippingType: APayShippingType.shipping, // PKShippingType type
  merchantCapabilities: [APayMerchantCapability.capability3DS],
  paymentSummaryItems: items, // list of APayPaymentSummaryItem (PKPaymentSummaryItem)
  supportedNetworks: availableNetworks,
);
/// Lets define a callbacks. Callbacks mimic to PKPaymentAuthorizationControllerDelegate callback methods
/// @see https://developer.apple.com/documentation/passkit/pkpaymentauthorizationcontrollerdelegate

/// @see https://developer.apple.com/documentation/passkit/pkpaymentauthorizationcontrollerdelegate/2867956-paymentauthorizationcontroller
final onSelectShippingContact = FutureOr<APayRequestShippingContactUpdate>(SelectShippingContactRequest request, ShippingContactUpdateBuilder builder) async {
  if(request.shippingContact.postalAddress == null) {
    return builder.failure([APayPaymentError.shippingAddressInvalid()]); /// Yes, you mimics for specify PK errors
  }
  
  return builder.success();
}

/// @see https://developer.apple.com/documentation/passkit/pkpaymentauthorizationcontrollerdelegate/2867955-paymentauthorizationcontroller
final onSelectPaymentMethod = FutureOr<APayRequestPaymentMethodUpdate> Function(SelectPaymentMethodRequest request, PaymentMethodUpdateBuilder builder) async {
  return builder.success();
}

/// @see https://developer.apple.com/documentation/passkit/pkpaymentauthorizationcontrollerdelegate/2867953-paymentauthorizationcontroller
final onSelectShippingMethod = FutureOr<APayRequestShippingMethodUpdate> Function(SelectShippingMethodRequest request, ShippingMethodUpdateBuilder builder) {
  final newItemsResponse = await api.newItemsBasedOnShippingMethod(request.shippingMethod);
  final List<APayPaymentSummaryItems> newItems = yourTransformFunction(newItemsResponse);
  
  return builder.success(paymentSummaryItems: items);
}

/// @see https://developer.apple.com/documentation/passkit/pkpaymentauthorizationcontrollerdelegate/2867952-paymentauthorizationcontroller
final onAuthorize = FutureOr<APayPaymentAuthorizationResult> Function(AuthorizePaymentRequest request, AuthorizationResultBuilder builder) {
  /// process authorize request here
  return builder.success();
}

await ApplePayMimic.processPayment(
  request: request,
  onSelectShippingContact: onSelectShippingContact,
  onSelectPaymentMethod: onSelectPaymentMethod,
  onSelectShippingMethod: onSelectShippingMethod,
  onAuthorize: onAuthorize,
  onError: (error) {
    /// Probably an internal error. Package in early dev
  },
  onDismissed: () {
    /// Called after Apple Pay widget dismissed after user authorize a payment, when error for example 
  }
)

Libraries

apple_pay_mimic