OPApplePayContext
@objc
public class OPApplePayContext : NSObject, OloApplePayLauncherDelegate, OPApplePayContextProtocol
A helper class that implements and simplifies ApplePay.
Use of this class looks like this:
- Create a button for ApplePay and connect it to a click handler
- Enable/Disable or Hide/Show the ApplePay button by calling
OloPayAPI.deviceSupportsApplePay()
- In the click handler, do the following
- Check the device supports ApplePay
- Create a
PKPaymentRequest
describing the request (amount, line items, etc)… An easy way to do this is to use theOloPayAPI.createPaymentRequest(...)
helper function - Initialize this class with the payment request from the previous step
- Call
OPApplePayContext.presentApplePay()
to present the Apple Pay sheet and begin the payment process
- Implement
OPApplePayContextDelegate.applePaymentMethodCreated(...)
to submit the basket to Olo’s Ordering API - Optionally implement
OPApplePayContextDelegate.applePaymentCompleted(...)
to handle success and error states when the ApplePay sheet is dimissed
Important
Create a new instance of this class for every payment requestWarning
OPApplePayContext 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, OPApplePayContextDelegate {
// 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 _applePayContext: OPApplePayContextProtocol? = nil
// Called when user taps on ApplePay button to begin ApplePay flow
func submitApplePay() {
let api: OloPayAPIProtocol = OloPayAPI() //This can be mocked for testing purposes
guard api.deviceSupportsApplePay() else {
return
}
do {
let pkPaymentRequest = try api.createPaymentRequest(forAmount: 2.99, inCountry: "US", withCurrency: "USD")
_applePayContext = OPApplePayContext(paymentRequest: pkPaymentRequest, delegate: self) //This can be mocked for testing purposes
_applePayContext?.presentApplePay() {
// Optional logic for when the ApplePay flow is displayed
}
}
catch {
// Handle error conditions. See docs for `OPApplePayContext.presentApplePay()` for more information
}
}
func applePaymentMethodCreated(_ context: OPApplePayContextProtocol, didCreatePaymentMethod paymentMethod: OPPaymentMethod) -> NSError? {
// Use the payment method to submit the basket to Olo's Ordering API (the basket id can be retrieved with `context.basketId`
// If the API returns an error, return that error. If the API call is successful, return nil
}
func applePaymentCompleted(_ context: OPApplePayContextProtocol, didCompleteWith 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
}
}
-
Basket ID convenience property for being able to submit a basket in
OPApplePayContextDelegate.applePaymentMethodCreated(...)
Declaration
Swift
public var basketId: String?
-
Initializes this class.
Declaration
Swift
@objc public required init?(paymentRequest: PKPaymentRequest, delegate: OPApplePayContextDelegate, basketId: String? = nil)
Parameters
paymentRequest
The payment request to use with Apple Pay.
delegate
The delegate.
basketId
The id of the basket associated with this context. Useful in
OPApplePayContextDelegate.applePaymentMethodCreated(...)
Return Value
An
OPApplePayContext
instance ornil
if the request is invalid (e.g. the user is restricted by parental controls or can’t make payments on any of the requests supported networks -
Presents the Apple Pay sheet from the key window (using the merchant id and company label set in
OloPayAPI.setup(...)
) and starts the payment process.Important
This method can only be called once per
OPApplePayContext
instance. Subsequent calls to this method will result in a no-opDeclaration
Swift
public func presentApplePay(completion: OPVoidBlock? = nil) throws
Parameters
completion
Called after the Apple Pay sheet is visible to the user
-
Presents the Apple Pay sheet from the key window and starts the payment process.
Important
This method can only be called once per
OPApplePayContext
instance. Subsequent calls to this method will result in a no-opDeclaration
Swift
public func presentApplePay(merchantId: String, companyLabel: String, completion: OPVoidBlock? = nil) throws
Parameters
merchantId
The merchant id to be used for this Apple Pay transaction. This overrides the value set in
OloPayAPI.setup(...)
companyLabel
The company label to be used for this Apple Pay transaction. This overrides the value set in
OloPayAPI.setup(...)
completion
Called after the Apple Pay sheet is visible to the user