ODWApplePayLauncher
@objc
public class ODWApplePayLauncher : NSObject, PKPaymentAuthorizationControllerDelegate, ODWApplePayLauncherProtocol
A drop-in class that presents an Apple Pay sheet to collect a customer’s payment details. When the launcher finishes, the payment data can be retrieved
from ODWApplePayLauncherDelegate.applePaymentCompleted(...).
Use of the class will typically look like this:
- Create an instance of this class
- Provide an
ODWConfigurationinstance (via the constructor or theODWApplePayLauncher.configurationproperty - Provide an
ODWApplePayLauncherDelegateinstance (via the constructor or theODWApplePayLauncher.delegateproperty
- Provide an
- Create a button for Apple Pay, connect it to a click handler (see
ODWApplePayButton), and optionally useODWApplePayLauncher.canMakePayments()to determine visibility or enabled states - In the click handler, do the following
- Check the device supports Apple Pay (see
ODWApplePayLauncher.canMakePayments()) - Call
ODWApplePayLauncher.present(...)to present the Apple Pay sheet and begin the payment process
- Check the device supports Apple Pay (see
- Implement
ODWApplePayLauncherDelegate.applePaymentCreated(...)to retrieve payment details and process the payment - Optionally implement
ODWApplePayLauncherDelegate.applePaymentCompleted(...)to handle success and error states when the Apple Pay sheet is dismissed
Important
The ODWApplePayLauncher.delegate and ODWApplePayLauncher.configuration properties must be defined prior to calling ODWApplePayLauncher.present(...)
Warning
The ODWApplePayLauncher instance must be created as a class member variable rather than a variable with function scope or else it will become
nil while the Apple Pay sheet is presented and callback methods won’t get called
Example Implementation
class ViewController: UIViewController, ODWApplePayLauncherDelegate {
// This needs to be a class member variable or it can go out of
// scope during the Apple Pay flow and become nil, preventing callbacks
// from executing
var _applePayLauncher: ODWApplePayLauncherProtocol
override init() {
val config = ODWConfiguration(
merchantId: "exampleMerchantId",
companyLabel: "Example Company",
currencyCode: "USD",
countryCode: "US"
)
_applePayLauncher = ODWApplePayLauncher(config: config, delegate: nil)
super.init()
_applePayLauncher.delegate = self
}
// Called when the user taps on Apple Pay button to begin the Apple Pay flow
func submitApplePay() {
guard _applePayLauncher.canMakePayments() else {
return
}
do {
let amount: NSDecimalNumber = 1.23
try _applePayLauncher.present(for: amount) {
// Optional logic for when the ApplePay flow is displayed (prior to being dismissed)
}
}
catch {
// Handle error conditions. See docs for `ODWApplePayLauncher.present(...)` for more information
}
}
func applePaymentCreated(_ launcher: ODWApplePayLauncherProtocol, _ payment: ODWPaymentDataProtocol) -> NSError? {
// Submit the payment data for payment processting (generally Olo's ordering API)
// If the API returns an error, return that error. If the API call is successful, return nil
// The error returned (or lack of one) will be used to by iOS to determine if the payment sheet shows a
// success or error animation when dismissing the Apple Pay sheet
}
func applePaymentCompleted(_ context: ODWApplePayLauncherProtocol, didCompleteWith status: ODWPaymentStatus, error: Error?) {
// This is called after the payment sheet has been dismissed
// Use the status and error parameters to determine if payment was successful
}
}
-
Initializes this class.
Declaration
Swift
@objc public init(configuration: ODWConfiguration? = nil, delegate: ODWApplePayLauncherDelegate? = nil)Parameters
configurationConfiguration parameters for processing Apple Pay payments. If not provided here, the
ODWApplePayLauncher.configurationproperty must be set prior to callingODWApplePayLauncher.present(...)delegateDelegate to handle callbacks during the Apple Pay flow. If not provided here, the
ODWApplePayLauncher.delegateproperty must be set prior to callingODWApplePayLauncher.present(...) -
Configuration parameters for processing Apple Pay payments. This property must be set prior to calling
ODWApplePayLauncher.present(...)Declaration
Swift
public var configuration: ODWConfiguration? { get set } -
Delegate to handle callbacks during the Apple Pay flow. This property must be set prior to calling
ODWApplePayLauncher.present(...)Declaration
Swift
public var delegate: ODWApplePayLauncherDelegate? { get set } -
Presents the Apple Pay sheet. Use the
delegateto listen for and handle the result of showing the Apple Pay sheet to the user.Throws
ODWApplePayLauncherError.applePayNotSupportedODWApplePayLauncherError.configurationNotSetODWApplePayLauncherError.delegateNotSetODWApplePayLauncherError.emptyCompanyLabelODWApplePayLauncherError.emptyMerchantIdODWApplePayLauncherError.invalidCountryCodeODWApplePayLauncherError.invalidCurrencyCodeODWApplePayLauncherError.negativeAmountDeclaration
Swift
public func present(forAmount amount: NSDecimalNumber, completion: VoidBlock? = nil) throwsParameters
amountThe amount intended to be collected
completionCalled after the Apple Pay sheet is visible to the user
-
Presents the Apple Pay sheet. Use the
delegateto listen for and handle the result of showing the Apple Pay sheet to the user.Throws
ODWApplePayLauncherError.applePayNotSupportedODWApplePayLauncherError.configurationNotSetODWApplePayLauncherError.delegateNotSetODWApplePayLauncherError.emptyCompanyLabelODWApplePayLauncherError.emptyMerchantIdODWApplePayLauncherError.invalidCountryCodeODWApplePayLauncherError.invalidCurrencyCodeODWApplePayLauncherError.negativeAmountODWApplePayLauncherError.lineItemTotalMismatchDeclaration
Swift
public func present(forAmount amount: NSDecimalNumber, with lineItems: [PKPaymentSummaryItem], completion: VoidBlock? = nil) throwsParameters
amountThe amount intended to be collected
lineItemsA list of line items to be displayed in the Apple Pay sheet. If line items are not needed, this parameter can be an empty array.
Important: The sum total of the line items will be compared to theamountparameter. If they do not match, anODWApplePayLauncherError.lineItemTotalMismatcherror will be thrown. If this validation is not desired, use apresent(...)method that takes avalidateLineItemsparameter.completionCalled after the Apple Pay sheet is visible to the user
-
Presents the Apple Pay sheet. Use the
delegateto listen for and handle the result of showing the Apple Pay sheet to the user.Throws
ODWApplePayLauncherError.applePayNotSupportedODWApplePayLauncherError.configurationNotSetODWApplePayLauncherError.delegateNotSetODWApplePayLauncherError.emptyCompanyLabelODWApplePayLauncherError.emptyMerchantIdODWApplePayLauncherError.invalidCountryCodeODWApplePayLauncherError.invalidCurrencyCodeODWApplePayLauncherError.negativeAmountODWApplePayLauncherError.lineItemTotalMismatchDeclaration
Swift
public func present(forAmount amount: NSDecimalNumber, with lineItems: [PKPaymentSummaryItem], validateLineItems: Bool, completion: VoidBlock?) throwsParameters
amountThe amount to authorize for the transaction
lineItemsA list of line items to be displayed in the Apple Pay sheet. If line items are not needed, this parameter can be an empty array
validateLineItemsIf
trueandlineItemsis not empty, throwsODWApplePayLauncherError.lineItemTotalMismatchif the sum of the line items does not equal the total amount passedcompletionCalled after the Apple Pay sheet is visible to the user
-
Check whether or not this device can make Apple Pay payments, based on the card networks configured by the
configurationpropertyImportant
When determining if the Apple Pay button should be displayed it is important to also verify the restaurant/vendor supports Apple Pay as a payment
Declaration
Swift
@objc public func canMakePayments() -> BoolReturn Value
trueif the device is able to make Apple Pay payments based on the card networks configured by theconfigurationproperty