OPApplePayLauncher
@available(iOSApplicationExtension, unavailable)
@available(macCatalystApplicationExtension, unavailable)
@objc
public class OPApplePayLauncher : NSObject, PKPaymentAuthorizationControllerDelegate, OPApplePayLauncherProtocol
A helper class that implements and simplifies ApplePay.
Use of this class looks like this:
- Create an instance of this class
- Create instances of
OPApplePayConfigurationandOPApplePayLauncherDelegateand update theconfigurationanddelegateproperties of this class - Create a button for ApplePay and connect it to a click handler
- Enable/Disable or Hide/Show the ApplePay button by calling
OPApplePayLauncher.canMakePayments() - In the click handler, call
OPApplePayLauncher.present()to present the Apple Pay sheet and begin the payment process - Implement
OPApplePayLauncherDelegate.paymentMethodCreated(...)to submit the basket to Olo’s Ordering API - Optionally implement
OPApplePayLauncherDelegate.applePayDismissed(...)to handle success and error states when the ApplePay sheet is dimissed
Warning
OPApplePayLauncher needs to be created as a class member variable rather than a variable with function scope or else it can becomenil while the ApplePay sheet is presented and callback methods won’t get called
Example Implementation
class ViewController: UIViewController, OPApplePayLauncherDelegate {
// This needs to be a class member variable or it can go out of
// scope during the ApplePay flow and become nil, preventing callbacks
// from executing
var _applePayLauncher: OPApplePayLauncherProtocol
required init() {
_applePayLauncher = OPApplePayLauncher()
super.init()
_applePayLauncher.delegate = self
_applePayLauncher.configuration = OPApplePayConfiguration(
merchantId: "merchant.com.your.applepay.id",
companyLabel: "Your Company Name"
)
}
// Called when user taps on ApplePay button to begin ApplePay flow
func submitApplePay() {
// To allow mocking this check, it could instead be called like this:
// type(of: _applePayLauncher).canMakePayments()
guard OPApplePayLauncher.canMakePayments() else {
return
}
do {
let amount: NSDecimalNumber = 1.23
try _applePayLauncher?.present(for: amount) {
// Optional logic for when the ApplePay flow is displayed
}
}
catch {
// Handle error conditions. See docs for `OPApplePayLauncher.present()` for more information
}
}
func paymentMethodCreated(from launcher: OPApplePayLauncherProtocol, with paymentMethod: OPPaymentMethod) -> NSError? {
// Use the payment method to submit the basket to Olo's Ordering API (the basket id can be retrieved with `launcher.basketId`
// If the API returns an error, return that error. If the API call is successful, return nil
}
func applePayDismissed(from launcher: OPApplePayLauncherProtocol, with status: OPPaymentStatus, error: Error?) {
// This is called after the payment sheet has been dismissed
// Use the status and error parameters to determine if payment was successful
}
}
-
Whether or not this device can make Apple Pay payments via a supported card network Supported ApplePay card networks are: American Express, Visa, Mastercard, Discover
Important
While this should be used in determining if an Apple Pay button can be displayed, it should not be the only determining factor. It is also important to determine whether a restaurant/vendor supports Apple Pay as a payment method, which can be determined using the Olo Ordering API.
Declaration
Swift
public static func canMakePayments() -> BoolReturn Value
trueif the device is currently able to make Apple Pay payments via one of the supported networks, orfalseif not -
Initializes this class.
Declaration
Swift
@objc public init( configuration: OPApplePayConfiguration? = nil, delegate: OPApplePayLauncherDelegate? = nil )Parameters
configurationConfiguration parameters for Apple Pay
delegateThe delegate.
Return Value
An
OPApplePayLauncherinstance -
Basket ID convenience property for being able to submit a basket in
OPApplePayLauncherDelegate.paymentMethodCreated(...)Declaration
Swift
public var basketId: String? -
Configuration parameters for processing Apple Pay payments
Declaration
Swift
public var configuration: OPApplePayConfiguration? { get set } -
Delegate to handle callbacks during the Apple Pay flow
Declaration
Swift
public var delegate: OPApplePayLauncherDelegate? { get set } -
Presents the Apple Pay sheet
Throws
OPApplePayLauncherError.configurationNotSet,OPApplePayLauncherError.delegateNotSet,OPApplePayLauncherError.emptyMerchantId,OPApplePayLauncherError.emptyCompanyLabel,OPApplePayLauncherError.invalidCountryCode,OPApplePayLauncherError.applePayNotSupported,OPApplePayLauncherError.lineItemTotalMismatchErrorDeclaration
Swift
public func present(for amount: NSDecimalNumber, completion: OPVoidBlock? = nil) throwsParameters
forThe amount to be displayed on the Apple Pay sheet
completionCalled after the Apple Pay sheet is visible to the user
-
Presents the Apple Pay sheet
Throws
OPApplePayLauncherError.configurationNotSet,OPApplePayLauncherError.delegateNotSet,OPApplePayLauncherError.emptyMerchantId,OPApplePayLauncherError.emptyCompanyLabel,OPApplePayLauncherError.invalidCountryCode,OPApplePayLauncherError.applePayNotSupported,OPApplePayLauncherError.lineItemTotalMismatchErrorDeclaration
Swift
public func present( for amount: NSDecimalNumber, with lineItems: [PKPaymentSummaryItem]?, validateLineItems: Bool = true, completion: OPVoidBlock? = nil ) throwsParameters
forThe amount to be displayed on the Apple Pay sheet
withA list of line items to be displayed in the Apple Pay sheet. If line items are not needed, this property must be nil and line items will not be displayed
validateLineItemsIf
true, throwsOPApplePayLauncherError.lineItemTotalMismatchErrorif the sum of the line items does not equal the total amount passed in. Default istruecompletionCalled after the Apple Pay sheet is visible to the user
-
Declaration
Swift
public func paymentAuthorizationController( _ controller: PKPaymentAuthorizationController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void ) -
Declaration
Swift
public func paymentAuthorizationControllerDidFinish(_ controller: PKPaymentAuthorizationController) -
Declaration
Swift
public func presentationWindow(for controller: PKPaymentAuthorizationController) -> UIWindow?