Allow customers to securely make payments using Apple Pay on their iPhone, iPad, and Apple Watch.
Stripe users can accept Apple Pay in iOS applications in iOS 9 and above, and on the web in Safari starting with iOS 10 or macOS Sierra. There are no additional fees to process Apple Pay payments, and the pricing is the same as other card transactions.
Apple Pay is compatible with most Stripe products and features (e.g., subscriptions), allowing you to use it in place of a traditional payment form whenever possible. Use it to accept payments for physical or digital goods, donations, subscriptions, and more (note that Apple Pay cannot be used instead of in-app purchases).
Apple Pay is available to cardholders at participating banks in supported countries. Refer to Apple’s participating banks documentation to learn which banks and countries are supported.
Using Stripe and Apple Pay vs. in-app purchases
Apple Pay doesn’t replace Apple’s In-App Purchase API. You can use any of Stripe’s supported payment methods and Apple Pay in your iOS app to sell physical goods (e.g., groceries and clothing) or for services your business provides (e.g., club memberships and hotel reservations). These payments are processed through Stripe and you only need to pay Stripe’s processing fee.
Apple’s developer terms require their In-App Purchase API be used for digital “content, functionality, or services,” such as premium content for your app or subscriptions for digital content. Payments made using the In-App Purchase API are processed by Apple and subject to their transaction fees.
Accept Apple Pay
Stripe offers a variety of methods to add Apply Pay as a payment method. For integration details, select the method you prefer:
Fill out the form with a description and identifier. Your description is for your own records and you can modify it in the future. Stripe recommends using the name of your app, like merchant.com.{{YOUR_APP_NAME}} as the identifier.
Add the Apple Pay capability to your app. In Xcode, open your project settings, choose the Signing & Capabilities tab, and add the Apple Pay capability. You might be prompted to log in to your developer account at this point. Select the merchant ID you created earlier, and your app is ready to accept Apple Pay.
If you’re using STPPaymentContext, that class handles the rest for you. Refer to iOS basic integration for more details.
Before displaying Apple Pay as a payment option in your app, determine if the user’s device supports Apple Pay and that they have a card added to their wallet:
CheckoutViewController.swift
importStripeimportPassKitclassCheckoutViewController:UIViewController,STPApplePayContextDelegate{let applePayButton:PKPaymentButton=PKPaymentButton(paymentButtonType:.plain, paymentButtonStyle:.black)overridefuncviewDidLoad(){super.viewDidLoad()// Only offer Apple Pay if the customer can pay with it
applePayButton.isHidden =!StripeAPI.deviceSupportsApplePay()
applePayButton.addTarget(self, action: #selector(handleApplePayButtonTapped),for:.touchUpInside)}// ...continued in next step}
Then, configure the PKPaymentRequest to display your business name and the total. You can also collect information like billing details or shipping information.
See Apple’s documentation for full guidance on how to customize the payment request.
CheckoutViewController.swift
funchandleApplePayButtonTapped(){let merchantIdentifier ="merchant.com.your_app_name"let paymentRequest =StripeAPI.paymentRequest(withMerchantIdentifier: merchantIdentifier, country:"US", currency:"USD")// Configure the line items on the payment request
paymentRequest.paymentSummaryItems =[// The final line should represent your company;// it'll be prepended with the word "Pay" (i.e. "Pay iHats, Inc $50")PKPaymentSummaryItem(label:"iHats, Inc", amount:50.00),]// ...continued in next step}
Create an STPApplePayContext instance with the PKPaymentRequest and use it to present the Apple Pay sheet:
CheckoutViewController.swift
funchandleApplePayButtonTapped(){// ...continued from previous step// Initialize an STPApplePayContext instanceiflet applePayContext =STPApplePayContext(paymentRequest: paymentRequest, delegate:self){// Present Apple Pay payment sheet
applePayContext.presentApplePay(on:self)}else{// There is a problem with your Apple Pay configuration}}
Make an endpoint that creates a PaymentIntent with an amount and currency. Always decide how much to charge on the server side, a trusted environment, as opposed to the client side. This prevents malicious customers from choosing their own prices.
Terminal
curlhttps://api.stripe.com/v1/payment_intents\
-u
sk_test_4eC39HqLyjWDarjtT1zdp7dc
: \
-d "amount"=1099\
-d "currency"="usd"
Client-side
Implement applePayContext:didCreatePaymentMethod:completion: to call the completion block with the PaymentIntent client secret retrieved from the endpoint above.
After you call the completion block, STPApplePayContext completes the payment, dismisses the Apple Pay sheet, and calls applePayContext:didCompleteWithStatus:error: with the status of the payment. Implement this method to show a receipt to your customer.
CheckoutViewController.swift
extensionCheckoutViewController{funcapplePayContext(_ context:STPApplePayContext, didCreatePaymentMethod paymentMethod:STPPaymentMethod, paymentInformation:PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock){let clientSecret =...// Retrieve the PaymentIntent client secret from your backend (see Server-side step above)// Call the completion block with the client secret or an errorcompletion(clientSecret, error);}funcapplePayContext(_ context:STPApplePayContext, didCompleteWith status:STPPaymentStatus, error:Error?){switch status {case.success:// Payment succeeded, show a receipt viewbreakcase.error:// Payment failed, show the errorbreakcase.userCancellation:// User cancelled the paymentbreak
@unknown default:fatalError()}}}
Finally, handle post-payment events to do things like sending an order confirmation email to your customer, logging the sale in a database, or starting a shipping workflow.
Troubleshooting
If you’re seeing errors from the Stripe API when attempting to create tokens, you most likely have a problem with your Apple Pay Certificate. You’ll need to generate a new certificate and upload it to Stripe, as described on this page. Make sure you use a CSR obtained from your Dashboard and not one you generated yourself. Xcode often incorrectly caches old certificates, so in addition to generating a new certificate, Stripe recommends creating a new Apple Merchant ID as well.
If you receive the error:
You haven’t added your Apple merchant account to Stripe
it’s likely your app is sending data encrypted with a previous (non-Stripe) CSR/Certificate. Make sure any certificates generated by non-Stripe CSRs are revoked under your Apple Merchant ID. If this doesn’t resolve the issue, delete the merchant ID in your Apple account and re-create it. Then, create a new certificate based on the same (Stripe-provided) CSR that was previously used. You don’t need to upload this new certificate to Stripe. When finished, toggle the Apple Pay Credentials off and on in your app to ensure they refresh properly.
Recurring payments
You can use Apple Pay tokens to create one-off payments or subscriptions. For repeat purchases that aren’t related to a subscription, Stripe recommends that you create single-use tokens. Your customer must authenticate with the Apple Pay payment sheet each time—attempting to reuse payment information for a non-subscription payment can result in it being declined.
Testing Apple Pay
Stripe test card information can’t be saved to Wallet in iOS. Instead, Stripe recognizes when you’re using your test API keys and returns a successful test card token for you to use. This allows you to make test payments using a live card without it being charged.