Apple Pay (Server Implementation)


In the first part, We have setup the client implementation for Apple Pay. To process the payment token information from Apple, you need to add server implementation.

 Generating Apple Pay Certificates

The first things you’ll need to do after creating your Stripe account is to tell Stripe about your app and generate a certificate using a Certificate Signing Request, or CSR provided by Stripe.

Head to the Apple Pay section of your Stripe account at Press ‘Create New Certificate’ to download a .certSigningRequest file.

Now go to your developer portal and navigate to Member Center\Certificates, Identifiers & Profiles\Identifier\Merchant IDs. Select your app’s merchant ID and click Edit.

Navigate to Create Certificate\Continue\Choose File, select the .certSigningRequest you downloaded from Stripe and then click Generate. Click Download to receive your .cer file.

Go back to Stripe, in the Apple Pay section where you created the CSR and upload your new certificate file. Click on the API Keys section of your account; record the Test Secret Key and Test Publishable Key for use later in your app. These test keys will let you to make ‘fake’ payments from Apple Pay.

You’re all set to send payment requests with your merchant ID to Stripe to be decrypted and processed.

Your task is to take the authorization token and send it to Stripe for processing. This consists of three steps:

  1. Send the authorization token via the PKPayment object to Stripe for decryption. If successful, Stripe returns their own “Stripe” token.
  2. Send the Stripe token to a server of your own to let your backend know about the order and perform any necessary processing.
  3. Sending the Stripe token back to Stripe to trigger the charge.

Creating a product backend Server

For any app that uses Apple Pay, you’ll likely have a corresponding backend to handle the actual order fulfillment. For this tutorial, you’ll create a lightweight server to receive payment requests from your app and send them to Stripe for processing.

Stripe provides several libraries you can find on their website to help creating your payment request. For now, you’ll create a single-file Python server using the lightweight Python REST framework Flask along with the Stripe Python SDK.

First, you’ll need pip, which is a command-line utility for installing python packages. If you don’t have pip installed, you can install it by following the instructions found here.

Next, open Terminal and use pip to install Flask by entering the following command:

Next, install the Python Stripe library with the following command:

Next, use a text editor to create a file called You can place this in the same directory as your Xcode project, if you like.


Following the numbered comments above, here’s what the code does:

  1. Creates a “route” that accepts an HTTP POST at /pay.
  2. Sets your Stripe Secret Key. Make sure you use your own Secret Key!
  3. Parses the JSON request.
  4. Creates a charge using the Stripe Python SDK. The amount is measured in cents.
  5. Returns the string Success back to your app.

Start your the server by executing the Python script in Terminal:

You’ll know it’s running when you see something like the following in your console:

Integrating the Stripe Apple Pay SDK

Now that you have a lightweight server to accept payments, you just need to send them from your app! Stripe provides an iOS framework available on GitHub that does most of the heavy lifting for you; the best way to manage this framework is through CocoaPods.

Create a file in the same directory as your .xcodeproj file and name it Podfile.

Open Podfile and add the following line to declare the Stripe Apple Pay library as a dependency:

Next, open Terminal and navigate to the directory containing your Podfile.

Run the following command to bring in the Stripe SDK:

As always with CocoaPods, ensure you close your project in Xcode and re-open it using .xcworspace instead of the .xcodeproj

You’ll also need to add a simple preprocessor symbol to your project to enable Apple Pay within the Stripe SDK.

In the project navigator, navigate to Project Name\project target\Build Settings and add the string STRIPE_ENABLE_APPLEPAY to both Debug and Release under Preprocessor Macros, as shown below:

Since the Stripe SDK is written in Objective-C, you’ll need to create a bridging header to use in your Swift code.

In the project navigator, right-click on the root Project folder and select New File. Select iOS\Source\Header File to create a bridging file and name it ProjectName-Bridging-Header.h.

Now open ProjectName-Bridging-Header.h and replace its contents with the following import:

Now, it is time for us to implement up the delegate methods as defined in ‘PKPaymentAuthorizationViewControllerDelegate’.

This function uses the Stripe SDK to convert a PKPayment object into an STPToken object by sending the PKPayment to Stripe’s servers for decryption. You can then send the returned STPToken object to your server.

If everything goes well, you invoke your completion block with PKPaymentAuthorizationStatus.Success. If you encounter an issue such as a network error, you simply return PKPaymentAuthorizationStatus.Failure.

Here’s a more detailed look at the code above, comment by comment:.

  1. Set the publishable Stripe key for your app in the Stripe SDK. Make sure to replace the key above with your own publishable key.
  2. Invoke the Stripe API with the authorized Apple Pay token, which sends the token to Stripe. You invoke a completion block when this action completes. If successful, this returns an STPToken.
  3. Build an URLRequest to invoke a POST call to the /pay URL. Ensure you replace the IP above with your own server’s IP. This is your computer’s IP on your network, followed by the port listed from your Python server, such as “http:// Ip address:5000/pay”. For this to work, your iOS device and your Mac need to be on the same network.
  4. Construct the JSON for the request. This includes the token property of STPToken to be sent to Stripe, the amount, and the description. In a real-life scenario, you might also include things such as product IDs in your app so your server knows which item or items were ordered.
  5. Send the request to the server. You invoke the completion block with success or failure depending on the error result.
  6. Build and run your app one last time, order all the products you want.

Once the payments are processed successfully, the Stripe dashboard lists the transactions performed in the dashboard. Here is a look at the Dashboard,


We have successfully created a fully functional app  and server (capable of making payments using Apple Pay).  If you’d like to read more in depth on Apple Pay, you can check out the developer site at The site also has a list of the other payment platforms other than Stripe that support Apple Pay.

I hope you enjoyed this Apple Pay Integration series. Apple Pay is one of the best new features of iOS 8 and you can bet it will be a popular way to perform online financial transactions.