Updated 16 September 2022
We can accept payments in your iOS app with the aid of the Braintree iOS SDK. To accept and process cards, PayPal, and also wallets like Apple Pay and Google Pay, use Braintree Direct’s collection of tools. Additionally, it has everything you need to manage data security, improve processes, and stop fraudulent transactions.
Requirement:
We always recommend to utilize the most recent SDK versions. We must use the iOS SDK at least in version 4.10.0 in order to safely communicate with the Braintree gateway.
We can include Braintree in our project in multiple ways. See this Installation
section for more information.
Swift Package Manager:
To add the Braintree
package to your Xcode project, follow and select File > Swift Packages > Add Package Dependency, and enter https://github.com/braintree/braintree_ios
as the repository URL. Select the checkboxes for the specific Braintree libraries you wish to include.
In your app’s source code files, use the following import syntax to include Braintree’s libraries:
1 2 3 4 |
import BraintreeCore import BraintreeCard import BraintreeApplePay import BraintreePayPal |
CocoaPods
1 2 3 4 5 6 |
# Includes Cards and PayPal pod 'Braintree' # Optionally include additional Pods pod 'Braintree/DataCollector' pod 'Braintree/Venmo' |
Note: A warning may appear if you are using the Braintree iOS SDK version 4.x.x in Xcode 12. The iOS Simulator deployment target is set to 8.0, but the range of supported deployment target versions is 9.0 to 14.0.99. There is a known fix for this CocoaPods problem.
Setup for app context switching
You must register a URL type and set your app to handle return URLs in order to manage workflows including moving to another app or SFSafariViewController for authentication.
Register a URL type
Important: If you have multiple targets, make sure to add the return URL type for all of the targets.
Get a Client token:
Client tokens are not compulsory to use; instead of using a client token, you can begin Braintree using a tokenization key. You can omit this line if you’re using a tokenization key and just use that instead for authorization.
A client token, created by your server, comprises the authorization and configuration information required by your client to launch the client SDK.
Your app from your server must request a client token. Please modify this example to use your own backend API if you wish to use our sample integration server instead.
Make an HTTP request:
1 2 3 4 5 6 7 8 9 10 11 |
# for LIVE environment curl -X POST https://api.paypal.com/v1/oauth2/token \ -u $CLIENT_ID:$CLIENT_SECRET \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d 'grant_type=client_credentials&response_type=token&return_authn_schemes=true' # for SANDBOX environment curl -X POST https://api.sandbox.paypal.com/v1/oauth2/token \ -u $CLIENT_ID:$CLIENT_SECRET \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d 'grant_type=client_credentials&response_type=token&return_authn_schemes=true' |
Response:
1 2 3 4 5 6 7 8 |
{ "scope": "...", "access_token": "<ACCESS_TOKEN>", "token_type": "Bearer", "app_id": "...", "expires_in": 32400, "nonce": "..." } |
Add PayPal UI:
Accordingly in Xcode, follow the guide to add package dependencies to your app and enter https://github.com/paypal/iOS-SDK as the repository URL. Also, select the checkboxes for each specific PayPal library you wish to include in your project.
In your app’s source files, use the following import syntax to include PayPal’s libraries:
1 |
import PayPalUI |
Include the PayPal pod in your Podfile
.
1 |
pod 'PayPal' |
UKit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class MyViewController: ViewController { lazy var payPalButton: PayPalButton = { let payPalButton = PayPalButton() payPalButton.addTarget(self, action: #selector(payPalButtonTapped), for: .touchUpInside) return payPalButton }() @objc func paymentButtonTapped() { // Insert your code here } override func viewDidLoad() { super.viewDidLoad() view.addSubview(payPalButton) } } |
Sample Code:
First, you should check if you have registered your URL type and updated your app delegate.
Use BTPayPalDriver
and BTPayPalRequest
to start the process. However, a transaction amount requires to invoke the one-time payment flow. Below is an example of integration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
// Add this in your bridging header: //#import "BraintreePayPal.h" // MyViewController.swift class MyViewController: UIViewController, BTAppSwitchDelegate, BTViewControllerPresentingDelegate { var braintreeClient: BTAPIClient? func startCheckout() { // Example: Initialize BTAPIClient, if you haven't already braintreeClient = BTAPIClient(authorization: "<#CLIENT_AUTHORIZATION#>")! let payPalDriver = BTPayPalDriver(APIClient: braintreeClient) payPalDriver.viewControllerPresentingDelegate = self payPalDriver.appSwitchDelegate = self // Optional // Specify the transaction amount here. "2.32" is used in this example. let request = BTPayPalRequest(amount: "2.32") request.currencyCode = "USD" // Optional; see BTPayPalRequest.h for more options payPalDriver.requestOneTimePayment(request) { (tokenizedPayPalAccount, error) in if let tokenizedPayPalAccount = tokenizedPayPalAccount { print("Got a nonce: \(tokenizedPayPalAccount.nonce)") // Access additional information let email = tokenizedPayPalAccount.email let firstName = tokenizedPayPalAccount.firstName let lastName = tokenizedPayPalAccount.lastName let phone = tokenizedPayPalAccount.phone // See BTPostalAddress.h for details let billingAddress = tokenizedPayPalAccount.billingAddress let shippingAddress = tokenizedPayPalAccount.shippingAddress } else if let error = error { // Handle error here... } else { // Buyer canceled payment approval } } } } |
Shipping Address:
The PayPal Checkout flow may or may not include the collection of shipping addresses. However, if you decide to compile your own list of shipping addresses, it includes in your server-side transaction.
Currency Presentment:
In the PayPal Checkout flow, the customer can see the transaction’s currency. Since it supports all currencies that PayPal’s REST APIs support.
Conclusion:
In this blog, we learned about PayPal Payment Method using Braintree. When available, the Braintree iOS SDK uses device and browser location information for fraud detection. To know more about BrainTree, you can look here.
To read more of my blogs, please visit this link.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.