First introduced in IOS 8, Apple pay provides simple, secure, and private payment options for physical goods and services in your app. It makes it easy for users to pay, by simply clicking on a fingerprint to authorize a transaction.
Apple pay can only be used on specific devices, so far these devices include iphone 6, iphone 6+, ipad Air 2, ipad mini 3. This is because Apple pay needs to be supported by a specific hardware chip called secure Element (SE, security Element) that can be used to store and encrypt information.
If there are certain features in your app that you need to buy to unlock (like going to ads), you shouldn't use Apple pay as a payment method. Apple pay is used to resolve purchases of physical goods and services, such as members of the club, hotel bookings, bookings, etc.
Why use Apple Pay
Apple pay greatly simplifies the work of developers. You don't have to manage card numbers yourself, and you don't need a user to sign up for a bank card. You can remove some of the business modules without even needing a user module. Purchase and billing information is automatically returned to Apple pay token for processing. This means that the purchase process is streamlined, resulting in a higher conversion rate.
In WWDC session 702, Apple Pay within Apps, Nick Shearer describes the statistics on the ultra-high conversion rates of some Apple Pay in different business transactions in the United States.
Stubhub found that customers using Apple pay had a conversion rate of more than 20% of traditional customers.
OpenTable found an increase of 50% after Apple pay was adopted.
Staples found that with apply pay, it achieved a 109% conversion rate increase.
Create a simple Store app
We'll create an app with a store that shows how Apple pay handles our transactions. This app has only one item, but it's enough to show how to get started with Apple pay.
This is the final page we are going to create. You can see that when the user clicks the "Buy" button, an Apple pay form pops up.
Enabling Apple Pay
Before we write the code, make sure your app has the ability to use Apple pay. When creating a blank item to play, open the project settings and find capabilities this tab.
You should be able to see the Apple pay section in capabilities and set the status to On. This will allow you to select a development team's authorized account, and hopefully next, Xcode will be able to do the setup work for you.
We need to add a Merchant ID to let Apple know how to encode the current payment information. Click the Add button that appears in the Merchant ID location to fill in your own unique Merchant ID. In this example, we are using Merchant.com.shinobistore.appleplay.
That's it, you can see that Apple pay is set to be available, and you should be able to use it in your app.
Using Apple pay
Now that we've set up the profile, we're going to start creating the UI so that the user can buy the product and pay for it. Open Storyboard Add some UI (e.g.) to make the product sell page.
The UI we just created is a picture with text with title, price, and description. This is not the focus of this demo. We need to add a button and we'll put it under the view. The button we want to add is a Pkpaymentbutton, which was introduced in IOS 8.3. This button is localized and can provide a standard style. Therefore, we strongly recommend using this button to start the Apple pay page.
There are three styles of this button
1 |
White; WhiteOutLine; Black |
There are also two different types of
There are several different ways to set the style of a button. Unfortunately, the settings in Interface Builder are not supported at this time. So open Viewcontroller.swift, overwrite Viewdidload method.
That's all we need to do. It can be adaptive, essentially (this demo) We care about this button. When we click on the button, in the Buynowbuttontapped method, we start the purchase process.
When the UI is created, now we have to deal with the purchase process. First, we need to understand some of the concepts of Apple pay's classes.
Pkpaymentsummaryitem
This Object is a piece of your Apple pay transaction list. It can be a commodity, or it can be a tax, or freight.
Pkpaymentrequest
Pkpaymentrequest merges all the information you want users to see. such as merchant identifier, country code and currency code.
Pkpaymentauthorisationviewcontroller
Pkpaymentauthorisationviewcontroller allows users to authorize pkpaymentrequest in a timely manner and select the delivery address and payment card.
Pkpayment
Pkpayment includes information about the transactions that need to be processed and contains messages that require user confirmation.
All of these classes are included in the Passkit (so start with PK), so you need to introduce this framework in places where Apple pay is used.
Set Payment
The first step is to create a pkpaymentrequest, which we'll describe in detail below
First we create an array of acceptable payment networks, which determines the types of cards that we can use.
Then we want to detect whether the current device can handle these types of transactions. Canmakepaymentsusingnetworks is the Pkpaymentauthorizationviewcontroller standard test equipment has the ability to deal with the method.
If the device has the ability to process transactions, the above code begins to automatically create a transaction request. The comment information indicates the function of each line of code.
Then, as in the code above, set the item information that you want to display in the Apple pay form. They're going to use it in the next paymentsummaryitems.
The interesting thing about this API is that the last one in the array is the total amount of money that the user needs to pay. It will be specifically expressed at the end of the form. In this case, it is the total price. If, you want more entries for reality, you need to manually calculate and at the end of the list, add a pkpaymentsummaryitem.
Finally, set a pkpaymentauthorizationviewcontroller for the request, set the proxy, and then show it to the user.
Now we need to confirm whether the proxy method for Pkpaymentauthorizationviewcontroller is declared. We need to declare these methods to confirm whether a transaction has been generated and to respond to related events after authorization or completion.
In the Paymentauthorizationviewcontroller:didauthorizepayment method, we use our provider to process the transaction data and return the status to the application. The returned pkpayment has a Pkpaymenttoken attribute that we need to send to the paid provider. He is responsible for encoding and private key encryption.
In the Paymentauthorizationviewcontrollerdidfinish method, we simply shut down our viewcontroller.
That's all. Obviously, in the real world, you may also need to send tokens of payment to such Stripe, but this is beyond the scope of this tutorial. We also added a controller to display the receipts. In this example, only the transactionidentifier of the payment token is shown. He is a well-formatted, globally unique string that can be used to verify receipts.
Extended Reading
For more information on Apple Pay, I recommend watching WWDC 2015 's session 702, Apple Pay within Apps. It's a long video, but it's definitely worth watching if you're interested in Apple pay. There is a section in the middle of the session that tells how Apple pay improves the user experience of dealing with transactions.
In addition, on the Apple developer website, there is a document guide to Apple Pay. If you want to integrate Apple pay into your app, it's worth reading.
Finally, don't forget that if you want to watch the project we created in the Demo, you can find him on Github.
Apple Pay (GO)