Paypal Integration Guide to Minimize Security Risk in iOS App

In this digital era, Mobile payments are being adopted everywhere in the world. From big brands to small online retailers, each and every business is taking advantage of Paypal payment gateway API. And the reason is obvious! Today, integrating Paypal gateway in native iOS app is ideal to capture the online payment market.

In this Paypal tutorial, you’re going to learn how to implement Paypal payment integration code for allowing users to make an online payment.

Let’s Get Started

Go to

and login with your credentials. If you don’t have a Paypal account, then register yourself first.

when you log in

go to Dashboard -> My Apps & Credentials


Click on “Create App”

Enter your app name and go for “Create App”


You will get client_id of “Sandbox” and “Live” mode.




Once you get the Client_ID of sandbox and live, we’ll integrate in app project.

Create a new project under file menu and select “Single View Application”.


In the next tab, name your app project. Here, we’re naming it as “SOPaypal”.


Create a pod file and install following pod.

pod ‘PayPal-iOS-SDK’, ‘~> 2.15’

Next, add bridging header “SOPaypal-Bridging-Header.h” and set it into your project build settings and find the “Swift Compiler – Code Generation” section.

import PayPalMobile in “SOPaypal-Bridging-Header.h”

#import "PayPalMobile.h"

Go to “Appdelegate.swift” and initialize paypal with live/sandbox environment in “didFinishLaunchingWithOptions”


  //Paypal initilize with client_id for Enviornment (Live/Sandbox)

        PayPalMobile.initializeWithClientIdsForEnvironments([PayPalEnvironmentProduction: "APP_ID_PAYPAL_LIVE_ENVIORNMENT", PayPalEnvironmentSandbox: "APP_ID_PAYPAL_SANDBOX_ENVIORNMENT"])

In ViewController.swift take outlet of table view and load shopping list in it as well as create object of PayPalConfiguration() method.

@IBOutlet weak var tblShopping: UITableView!

   var quotesArray : NSMutableArray = [

       ["Product": "Shirt", "Price": “200”, "Note": "Nice Shirt"],

       ["Product": "Shoes", "Price": “120”, "Note": "Nice Shoes"],


var payPalConfig = PayPalConfiguration()

let items:NSMutableArray = NSMutableArray()

   //MARK: UITableViewDataSource


   func numberOfSectionsInTableView(tableView: UITableView) -> Int {

       return 1



   func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

       return quotesArray.count



   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

       let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

       configureCell(cell, forRowAtIndexPath: indexPath)

       return cell



   func configureCell(cell: UITableViewCell, forRowAtIndexPath: NSIndexPath) {

       let lblProduct : UILabel = cell.contentView.viewWithTag(1) as! UILabel

       let lblPrice : UILabel = cell.contentView.viewWithTag(2) as! UILabel

       let lblNote : UILabel = cell.contentView.viewWithTag(3) as! UILabel


       let dict : NSDictionary = quotesArray.objectAtIndex(forRowAtIndexPath.row) as! NSDictionary

       lblProduct.text = dict.valueForKey("Product") as? String

       lblPrice.text = "$" + (dict.valueForKey("Price") as! String)

       lblNote.text = dict.valueForKey("Note") as? String




Once tableView loads the shopping data, then integrate the code for Paypal payment in didSelectRowAtIndexPath.


//MARK: UITableViewDelegate


   func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

       let dict : NSDictionary = quotesArray.objectAtIndex(indexPath.row) as! NSDictionary


//Configure the marchant



//Set selected product in “PayPalItem” for purchase

       self.setItems(dict.valueForKey("Product") as? String, noOfItem: "1", strPrice: dict.valueForKey("Price") as? String, strCurrency: "USD", strSku: nil)


//Go for pay in paypal for selected paypal items

       self.goforPayNow(nil, taxPrice: nil, totalAmount: nil, strShortDesc: "Paypal", strCurrency: "USD")


Now following code will complete your payment in Paypal.

Add PayPalPaymentDelegate in ViewController.swift

class ViewController: UIViewController, PayPalPaymentDelegate

Now use following snippet for paypal integration.

//It will provide access to the card too for the payment.

   func acceptCreditCards() -> Bool {

       return self.payPalConfig.acceptCreditCards



   func setAcceptCreditCards(acceptCreditCards: Bool) {

       self.payPalConfig.acceptCreditCards = self.acceptCreditCards()



//Set environment connection.

   var environment:String = PayPalEnvironmentNoNetwork {

       willSet(newEnvironment) {

           if (newEnvironment != environment) {





//Configure paypal and set Marchant Name

func configurePaypal(strMarchantName:String) {

       if items.count>0 {



       // Set up payPalConfig

       payPalConfig.acceptCreditCards = self.acceptCreditCards();

       payPalConfig.merchantName = strMarchantName

       payPalConfig.merchantPrivacyPolicyURL = NSURL(string: "")

       payPalConfig.merchantUserAgreementURL = NSURL(string: "")


       payPalConfig.languageOrLocale = NSLocale.preferredLanguages()[0]


       payPalConfig.payPalShippingAddressOption = .PayPal;


       print("PayPal iOS SDK Version: \(PayPalMobile.libraryVersion())")




  //Start Payment for selected shopping items

   func goforPayNow(shipPrice:String?, taxPrice:String?, totalAmount:String?, strShortDesc:String?, strCurrency:String?) {

       var subtotal : NSDecimalNumber = 0

       var shipping : NSDecimalNumber = 0

       var tax : NSDecimalNumber = 0

       if items.count > 0 {

           subtotal = PayPalItem.totalPriceForItems(items as [AnyObject])

       } else {

           subtotal = NSDecimalNumber(string: totalAmount)



       // Optional: include payment details

       if (shipPrice != nil) {

           shipping = NSDecimalNumber(string: shipPrice)


       if (taxPrice != nil) {

           tax = NSDecimalNumber(string: taxPrice)



       var description = strShortDesc

       if (description == nil) {

           description = ""



       let paymentDetails = PayPalPaymentDetails(subtotal: subtotal, withShipping: shipping, withTax: tax)


       let total = subtotal.decimalNumberByAdding(shipping).decimalNumberByAdding(tax)


       let payment = PayPalPayment(amount: total, currencyCode: strCurrency!, shortDescription: description!, intent: .Sale)


       payment.items = items as [AnyObject]

       payment.paymentDetails = paymentDetails


       self.payPalConfig.acceptCreditCards = self.acceptCreditCards();


       if self.payPalConfig.acceptCreditCards == true {

           print("We are able to do the card payment")



       if (payment.processable) {

           let objVC = PayPalPaymentViewController(payment: payment, configuration: payPalConfig, delegate: self)


           self.presentViewController(objVC!, animated: true, completion: { () -> Void in

               print("Paypal Presented")



       else {

           print("Payment not processalbe: \(payment)")








//PayPalPaymentDelegate methods

   func payPalPaymentDidCancel(paymentViewController: PayPalPaymentViewController) {

       paymentViewController.dismissViewControllerAnimated(true) { () -> Void in

           print("and Dismissed")


       print("Payment cancel")



   func payPalPaymentViewController(paymentViewController: PayPalPaymentViewController, didCompletePayment completedPayment: PayPalPayment) {

       paymentViewController.dismissViewControllerAnimated(true) { () -> Void in

           print("and done")


       print("Paymane is going on")


That’s it! Once you run your app project, it should look like this:


However, this was just a simple demo integration. And integrating Paypal in your iOS app could benefit your business in so many ways such as minimizing security risk, and simple payment process for app users.

So if you want to integrate Paypal payment gateway in your iOS app, you can always contact us for the same. We’re an iPhone app development company with more than 5 years of experience and developed over 2000+ mobile applications.

You can also grab a copy of Paypal integration code from Github.

Author Bio
Hitesh Trivedi
Hitesh Trivedi
Designation: iOS Team Lead

Hitesh Trivedi is an iOS Team Lead at Space-O Technologies. He has over 10 years of experience in iOS app development. He has guided to develop over 100 iPhone apps with unique features and functionalities. He has special expertise in Swift and Objective-C.