Applies to:
Learn how to enable Apple Pay for the Square In-App Payments SDK.
Applies to:
Learn how to enable Apple Pay for the Square In-App Payments SDK.
Use the following step-by-step instructions to integrate Apple Pay with the In-App Payments SDK on an iOS device. It also shows how to integrate and test the Apple Pay digital wallet in the Square Sandbox and how to move the integration into production.
To
If your current Apple Pay
A single In-App Payments SDK application associated with a Payment Processing Certificate and Apple Pay merchant ID might be listed multiple times in the Apple Store.
Update REPLACE_ME
in the following code with your Square application ID. If you don't set your Square application ID before initializing SQIPCardEntryViewController
, your application terminates with an uncaught exception.
To test an application in the Square Sandbox, set the Developer Console to Sandbox mode before completing the following instructions in this step.
import SquareInAppPaymentsSDK @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Set your Square Application ID SQIPInAppPaymentsSDK.squareApplicationID = "<# REPLACE_ME #>" return true } }
Replace <# REPLACE_ME #>
with your application ID. For example, the application ID sq0ids-KHbh5gEaJuH7GEi1lgGMBQ
replaces the placeholder as shown in the following code:
SQIPInAppPaymentsSDK.squareApplicationID = "sq0ids-KHbh5gEaJuH7GEi1lgGMBQ"
@implementation
for Objective C) to your view controller.PKPaymentRequest
constructor to replace <#REPLACE_ME#>
.Always check the SQIPInAppPaymentsSDK.canUseApplePay
property of the In-App Payments SDK to confirm that Apple Pay can be used before displaying the payment sheet.
import PassKit import SquareInAppPaymentsSDK extension<#YourViewController#> { func requestApplePayAuthorization() { guard SQIPInAppPaymentsSDK.canUseApplePay else { return } let paymentRequest = PKPaymentRequest.squarePaymentRequest( // Set to your Apple merchant ID merchantIdentifier: <#REPLACE_ME#>, countryCode: "US", currencyCode: "USD" ) // Payment summary information will be displayed on the Apple Pay sheet. paymentRequest.paymentSummaryItems = [ PKPaymentSummaryItem(label: "Testing Apple Pay", amount: 1.00), ] let paymentAuthorizationViewController = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest) paymentAuthorizationViewController!.delegate = self present(paymentAuthorizationViewController!, animated: true, completion: nil) } }
Extend your view controller by adding two delegate methods. The two delegate methods that you implement let you handle the following events:
didAuthorizePayment
- A buyer-submitted payment card and the payment authorization sheet created a PKPpayment
object. This method is invoked by the PassKit framework while the payment authorization sheet is still open.didFinish
- This method is invoked after the didAuthorizePayment
completion handler is invoked by your code and the result is displayed to the buyer, or after the buyer cancels payment authorization.If the buyer authorizes payment, a PKPayment
object is returned and can be used in step 6 to get a Square payment token.
import PassKit extension<#YourViewController#>: PKPaymentAuthorizationViewControllerDelegate { func paymentAuthorizationViewController( _: PKPaymentAuthorizationViewController, didAuthorizePayment _: PKPayment, handler _: @escaping ( PKPaymentAuthorizationResult) -> Void ) { // TODO: Add payment->nonce exchange logic. See Step 5: Request a Square // nonce } func paymentAuthorizationViewControllerDidFinish( _: PKPaymentAuthorizationViewController ) { dismiss(animated: true, completion: nil) } }
If the buyer authorizes payment, the PassKit framework calls your
SQIPApplePayNonceRequest
After a payment token is received, send it to your backend to charge or store the card through Square.
You can indicate a success or failure by invoking the handler provided to the paymentAuthorizationViewController(_:didAuthorizePayment:handler:)
method with the appropriate parameters. This allows the Apple Pay payment sheet to display the appropriate UI to the buyer to notify them of the result.
func paymentAuthorizationViewController( _: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping ( PKPaymentAuthorizationResult) -> Void ) { // Exchange the authorized PKPayment for a nonce. let nonceRequest = SQIPApplePayNonceRequest(payment: payment) nonceRequest.perform { cardDetails, error in if let cardDetails = cardDetails { // Send the card nonce to your server to charge the card or store // the card on file. /* MyAPIClient.shared.chargeCard(withNonce: cardDetails.nonce) { transaction, chargeEerror in if let chargeError = chargeError { completion(PKPaymentAuthorizationResult(status: .failure, errors: [chargeError])) } else { completion(PKPaymentAuthorizationResult(status: .success, errors: nil)) } } */ completion(PKPaymentAuthorizationResult(status: .success, errors: nil)) } else if let error = error { print(error) completion(PKPaymentAuthorizationResult(status: .failure, errors: [error])) } } }
To test your Apple Pay integration, you must
Apple doesn't allow Square Sandbox
Testing your Apple Pay integration involves the following steps:
didObtainCardDetails
callback.The payment token generated for Apple Pay can be used to
When your application is ready for production, do the following:
If you need more assistance, contact