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:

  1. Create an instance of this class
  2. Create a button for Apple Pay, connect it to a click handler (see ODWApplePayButton), and optionally use ODWApplePayLauncher.canMakePayments() to determine visibility or enabled states
  3. In the click handler, do the following
    1. Check the device supports Apple Pay (see ODWApplePayLauncher.canMakePayments())
    2. Call ODWApplePayLauncher.present(...) to present the Apple Pay sheet and begin the payment process
  4. Implement ODWApplePayLauncherDelegate.applePaymentCreated(...) to retrieve payment details and process the payment
  5. 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
    }
}