- Integrating Apple Pay into your iOS App
- Let’s Get Started
- Time to Start Coding
- Implementing Apple Pay
- Build your app and website.
- Developer Account Setup
- Apple Pay for Apps
- Apple Pay on the Web
- Apple Pay in Messages
- Integrate, test, and verify.
- E-Commerce Platforms and Payment Service Providers
- Sandbox Testing
- Check the guidelines.
- App Store Review Guidelines
- Apple pay on the Web Acceptable Use Guidelines
- Apple Pay Integration in iOS using swift
- Apple Pay Sandbox Testing
- 1. Create sandbox tester account.
- 2. Adding a test card number :
Integrating Apple Pay into your iOS App
Apple Pay is arguably the best form of payment in an iOS app. It’s easy and efficient for both the user and the developer. Long gone are the days of typing in your credit card info — users can now quickly make transactions through ApplePay and your app should support this service.
Note: To implement Apple Pay, you must have an Apple Developer’s Account. Learn more about the developer’s program .
Let’s Get Started
First, download the starter project here. Take a look at this project, in the ViewController.swift file, we have the Shoe struct:
We’ll use this along with the array of Shoes to populate our picker view and retrieve the price later on (feel free to change the shoes if you’d like to).
To get started with Apple Pay, we need to create a merchant ID in the developer portal. This ID will allow Apple to identify your app as a “merchant” for purchases. Before we create one, you’ll need a unique bundle identifier for your app. In the app settings, go to the identity and put your own bundle identifier as you would if you were making your own app (e.g. com.yourname.app-name).
Next, head over to the developer member center and select “Certificates, Identifiers, and Profiles” in the menu. Select “Merchant IDs” from the left menu. Tap the plus “+” on the top right-hand corner.
In the description, enter the name of your app, such as “Shoe Store”. In the identifier, type “merchant.” + your bundle identifier. For me, that would be “merchant.com.pranavwadhwa.Shoe-Store”. Tap “Continue” and then “Register”.
For the next part, we’re going to need a certificate signing request (CSR). Open up Keychain Access, and from the top menu select “Certificate Assistance” and “Request a Certificate from a Certificate Authority” as shown in the picture.
Put your email address in the “User Email Address” field and select the “saved to disk” option for your request (you can leave the CA email address empty).
We’re almost back to coding! We just need to create a payment processing certificate, which will be used to secure transaction data . Go back to the member center and select “All Certificates” from the side menu. Tap the plus “+” button on the top right corner to create a new certificate.
Under “Production”, select “Apple Pay Payment Processing Certificate”.
Then, select the merchant ID you created earlier. Next, under “Apple Pay Payment Processing Certificate”, tap on “Create Certificate”. Apple will ask, “Will payments associated with this Merchant ID be processed exclusively in China?” Select yes. Continue through the process, and it will ask you to upload your CSR that you saved from Keychain Access.
Great job! You created your certificate. Download it and save it with your project. Then, double click on it, and it will be processed in Keychain Access.
Let’s head back over to Xcode and enable Apple Pay there. Under “Capabilities”, turn on Apple Pay, and ensure that you have selected the correct merchant ID (If you don’t see yours, try tapping the + and enter the name of your merchant ID).
Time to Start Coding
Open the ViewController.swift file, and inside the class, add the following function:
If you’ve used alerts in the past, you’ll know that this will simply display an alert with a simple dismiss action. We’ll use this to display info to the user as they go through the Apple Pay process. Now, go to the buyShoeTapped method:
First, let’s find the current shoe that the user has picked and create a PKPaymentSummaryItem using that info. Add the following lines:
This will find the selected row in the picker (as we only have one component), get the corresponding shoe, and create a payment item using that information. We will use this item when filling out the payment request. Next, we have to declare a list of methods the user can pay with. In order to do that, we have to add
at the top of the file. Now, back in our buyShoeTapped function, let’s identify our payment methods:
With this, we have to check if the user can make payments with one of these networks and let them know if they can’t:
Now, we’re going to create a PKPaymentRequest that will contain information about the transaction. Add the following:
Let’s take a close look at this.
Lines 1 and 2 establish the currency and country code — you can change these to fit your app.
Line 3 verifies your merchant ID. Make sure to change line 3 and input the name of the merchant ID you created.
Line 4 checks the type of transaction. PKMerchantCapability.capability3DS uses the 3-D Secure protocol, a secure way of processing debit and credit cards.
Lastly, lines 5 and 6 use the payment networks and the payment item we created earlier. This is only the basic information; you can add more information in the request, such as the billing information, shipping methods, and supported countries. Check the full list of properties of the PKPaymentRequest. if needed.
Finally, let’s present the View Controller to the user.
You’ll see an error — don’t panic, we’ll fix it soon. Here, we’re using a guard statement to safely unwrap the PKPaymentAuthorizationViewController , and displaying it accordingly. The error we’re facing is that we assigned the paymentVC ’ s delegate to the ViewController , but the ViewController does not currently conform to that protocol. Let’s fix that. Below the class, let’s create an extension:
We’re almost done! We just need to add these two functions. In the first one, paymentAuthorizationViewControllerDidFinish , add the following:
This will dismiss the payment view controller after it’s done. In the second function, didAuthorizePayment , add the following to dismiss the view controller (if it hasn’t already been dismissed by the didFinish function) and display a success message to the user:
We’re finished! Run the app, and you’ll see the payment view controller.
Unfortunately, it won’t actually make the transaction yet as we haven’t added a backend to receive the payment. If you want to add that backend, look into backing Apple Pay with Stripe.
Источник
Implementing Apple Pay
Discover how to set up your developer account to support Apple Pay, verify that transactions are completed successfully, and make sure your implementation of Apple Pay follows guidelines for apps and the web.
Build your app and website.
Developer Account Setup
Learn about the identifiers and certificates in your Apple Developer account that are needed to implement Apple Pay for apps and websites. If you’re supporting Apple Pay in your app and website, we recommend using the same Merchant ID and certificate for both. Contact your payment service provider to confirm any additional transaction requirements applicable to your region.
Apple Pay for Apps
Get APIs for payment sheet interactions, authorization, updates, errors, and more.
Apple Pay on the Web
Safari supports two JavaScript APIs that let you accept Apple pay payments from customers on your website.
Apple Pay in Messages
Allow customers to pay quickly and easily with Apple Pay in an iMessage app or directly in a Messages for Business session.
Integrate, test, and verify.
E-Commerce Platforms and Payment Service Providers
The most popular e-commerce platforms and payment service providers support Apple Pay within apps and on the web. Use an Apple Pay SDK from a payment service provider to integrate Apple Pay in your app or website.
Apple Pay Web Merchant Registration API
Approved e-commerce platform and payment service providers can use the Apple Pay Web Merchant Registration API, along with their merchant IDs, to register and unregister their merchants’ websites for Apple Pay.
Sandbox Testing
The Apple Pay sandbox environment allows you test your implementation of Apple Pay with test credit and debit cards.
Check the guidelines.
App Store Review Guidelines
Before submitting your app for review, make sure it follows these guidelines to help progress smoothly through the review process.
Apple pay on the Web Acceptable Use Guidelines
Before deploying Apple Pay on your website, make sure your implementation follows these guidelines.
Design guidelines
Learn how to optimize the design and checkout experience for Apple Pay.
Payment service providers
Support Apple Pay quickly and reliably with an SDK or API from a payment service provider (PSP).
View payment service providers
Contact us
Have a question or request? We can help by phone or email.
Источник
Apple Pay Integration in iOS using swift
1. First step would be to get the Apple Merchant Id.
a) Login to your Apple Developer Account.
b) Go to “Certificates, Identifiers & Profiles”. Choose iOS, tvOS, watchOS from the Navigation menu on the left.
- Select Merchant Id section in the same left menu.
- Click on “+” button.
c) A new tab will open. Add Merchant Id description and a unique Identifier and continue until the same is added. Save the Merchant Id you created.
2. Payment Processing Certificate:
a) Tap on the merchant ID that you created earlier. A tab will open up, select “Edit” option.
b) A new tab will open. Tap on Create Certificate.
c) Open Keychain Access in you Mac using “cmd + Space”.
d) Request a new certificate from authority.
e) Fill the Email and Common Name fields. Check “Save to disk” and “Let me specify Key Pair Information”. Click continue.
f) Select Algorithm to “ECC” and Key Size to “256 bits”. Click Continue.
g) Continue with step 2. Upload the certificate that you have created.
h) After upload is complete, please download the Apple generated certificate.
3. Configure Apple Pay :
a) Setup Apple pay in Xcode project.
4. Add below line into podfile and install pods.
# Pods for Apple Pay Demo
pod ‘Stripe/ApplePay’
5. After pod installation, make sure that you are using “project.xcworkspace”.
Note: Please don’t use “project.xcodeproj”
6. Add below code at the top of your Controller class to import pod libraries. There are chances that you don’t find these SDKs in suggestion. Don’t worry it will take some time. Or you can type the code manually.
7. Extend your view controller class with “PKPaymentAuthorizationViewControllerDelegate” class.
class ViewController: UIViewController, PKPaymentAuthorizationViewControllerDelegate
8. Add below code in your controller class.
var paymentRequest: PKPaymentRequest!
var transactionId = “”
var status = “”
var amount = 100
/**
* This function is called on tap of ApplePay Payment option in Payment Page.
* **Date:** 01-Oct-2018
*/
func setupApplePay()
<
paymentRequest = PKPaymentRequest()
paymentRequest.currencyCode = “USD”
paymentRequest.countryCode = “US”
paymentRequest.merchantIdentifier = “Your Merchant Id”
// Payment networks array
let paymentNetworks = [PKPaymentNetwork.amex, .visa, .masterCard, .discover]
if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: paymentNetworks)
<
paymentRequest.supportedNetworks = paymentNetworks
paymentRequest.merchantCapabilities = .capability3DS
let item = PKPaymentSummaryItem(label: “Order Total”, amount: NSDecimalNumber(string: “\(amount)”))
let applePayVC = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
applePayVC.delegate = self
self.present(applePayVC, animated: true, completion: nil)
>
else
<
// Notify the user that he/she needs to set up the Apple Pay on the device
// Below code calls a common function to display alert message. You can either create an alert or can just print something on console.
CommonMethods.displayAlert(message: “Apple Pay is not available on this device.”, viewController: self)
print(“Apple Pay is not available on this device”)
>
>
/**
* This function is called when Apple Pay payment will be authorized.
* **Date:** 01-Oct-2018
*/
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Void)
<
// Payment token can be found like this
print(payment.token)
// Get response from the server and set the PKPaymentAuthorizationStatus
let status = PKPaymentAuthorizationStatus(rawValue: 0)!
switch status.rawValue <
case 0:
self.status = “approved”
// perform Functionality on Apple Pay Successfull Payment
default:
self.status = “failed”
>
>
/**
* This function is called when Apple Pay payment authorization is finished.
* **Date:** 01-Oct-2018
*/
func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController)
<
self.dismiss(animated: true, completion: nil)
>
9. You can call the function setupApplePay() whenever you want to start Apple Pay transaction. Either you can call the same function in viewDidLoad() or you can call this function on tap of a button.
Example: In case of button tap, you need to add a button in UI, draw IBAction of button. IBAction is a function that will be called on a tap of the button from which is referenced. In this action, you can call this function.
@IBAction func onTapBtnPay(_ sender: Any) <
setupApplePay()
Apple Pay Sandbox Testing
1. Create sandbox tester account.
a) Login to https://appstoreconnect.apple.com/
b) Select Users and Access.
c) Go to testers section in left navigation Menu. Then click on “+” button.
d) A popup will appear. Please fill that form.
Note: Please use mail that is not registered with Apple.
f) Sign out of your Apple ID on all testing devices and sign back in with your new sandbox tester account.
2. Adding a test card number :
a) Make sure you have logged out from iCloud and iTunes.
b) Sign into your test device using the sandbox tester account you created.
c) Add a sandbox card.
Example :
FPAN: 4761 1200 1000 0492
Expiration Date: 11/2022
CVV: 533
You can find more cards here.
Note : The issue you might face that card will not be added. It will shoe some error. Possible resolution maybe – you might have not logged out of device completely. Please logout form iCloud and iTunes.
d) After adding the card, you can proceed with Apple Pay transaction.
Create your own eCommerce mobile app with Knowband’s mobile app builder and integrate PayU just by following these simple steps.
Источник