PayPal Smart Button Mobile Integration

This document provides details for integrating the PayPal Smart Button with eBay's Order API for android and iOS. See the PayPal Native Checkout SDK document for PayPal requirements, prerequisites, and an explanation of how to integrate your app with PayPal Express Checkout SDK.

Android integration

The android integration has been divided into sections; adding the repositories and dependencies, setting up the SDK, and invoking the SDK.

Note: The following code examples are based on nativexo:3.4.3.

Add the repositories to top level project build.gradle

  allprojects {
    repositories {
        ...
        maven { url "https://github.com/paypal/paypalcheckout-android/raw/nativeSDK" }
        maven { url "https://github.com/syrjs/maven/raw/master" }
        ...
    } }

Add dependencies to the build.gradle module

dependencies {
  ...
  implementation 'com.paypal.pyplcheckout:nativexo:3.4.3'
  implementation 'syr.js.org:core:1.5.2'
  ...
}

Setup for the SDK

Add the following to the AndroidManifest.xml file.

<!-- Accepts URIs that begin with "eBay://xo” -->
<activity android:name=".activity.PaymentActivity">
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:host="xo"
            android:scheme="eBay" />
    </intent-filter>
</activity>
<!--To ensure that the user makes it back to the app from the PayPal web experience, a custom scheme is employed.
Please register an activity with a custom scheme you chose for the app. The host should stay paypalxo. -->
<activity android:name="com.paypal.pyplcheckout.PYPLCheckoutReceiver" >
    <intent-filter android:autoVerify="true" >
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="paypalxo"
            android:scheme="sampleapp" />
    </intent-filter>
</activity>

Invoke the SDK

The following code is for when the buyer clicks the PayPal Smart Button.

Note: The partner must create the PayPal checkout button per PayPal branding guidelines and add a listener.

 

Important!  webBrowserOnlyMode must be set to true.

Button payPalBtn = (Button) findViewById(R.id.button_paypalBtn);
payPalBtn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
         //paypalCartId can be obtained from the eBay Order APIs. (See PayPal Smart Button eBay guest payment flow for details.)
         //Assume the paypalCartId is stored in paypalCartIdView field 
         TextView paypalCartIdView = (TextView) findViewById(R.id.textView_paypal_cart_id);
         String  paypalCartId = paypalCartIdView.getText().toString(); //an example of paypalCartId is "8C167935RU828824X"

         PYPLCheckoutEnvironment environment = PYPLCheckoutEnvironment.getInstance();
         // set the PayPal environment
         environment.setkPYPLEnvironment (Environment.SANDBOX);

         //set WebBrowserOnly = true
         environment.setkPYPLWebBrowserOnly(true);
  
         // set the scheme the app is using
         environment.setkPYPLUrlScheme("sampleapp");

         // set parameters need to be passed to PayPal
         String[] myStringArray;
         myStringArray = new String[]{"locale.x=de_DE", "&native_xo=1&return_uri=eBay://xo"};
         environment.setkPYPLQueryStringParameters(myStringArray);

         //set checkoutDeligate.
         environment.setkCheckoutDelegate(new PYPLCheckoutDelegate() {
             //This completeCheckout method will be called when user completes the PayPal flow. The returnParams contains PayerID
             @Override
             public void completeCheckout(HashMap<String, String> returnParams) {
               TextView paypalPayerIdView = (TextView) findViewById(R.id.textView_paypal_payer_id);
               paypalPayerIdView.setText(returnParams.get("PayerID"));
             }
          });

          //launch PayPal button with paypalCartId
          PYPLCheckout.getInstance().startCheckoutWithECToken(this, paypalCartId)
   }
});

 

iOS integration

The iOS integration has been divided into sections; installing the SDK, setting up the SDK, and invoking the SDK.

Install PayPal Checkout SDK using Cocoapods

The first step is to install the PayPal iOS Checkout SDK. For details, see Integrating With Cocoapods.

Setup for SDK

Add the following configuration to the top-level Info.plist.

<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
      <array>
        <string>sampleapp</string>
      </array>
    </dict>
</array>

Invoke the SDK

The following code is for when the buyer clicks the PayPal Smart Button.

Note: The partner must create the PayPal checkout button per PayPal branding guidelines and add a listener.

 

Important!  webBrowserOnlyMode must be set to true.

@IBAction func paypalButtonPressed(_ sender: Any) {
   //paypalCartId can be obtained from the eBay Order APIs. (See PayPal Smart Button eBay guest payment flow for details.) 
      // Assume the paypalCartId is stored in payPalCartIdTextView field
        
   let paypalCartId = payPalCartIdTextView.text //an example of paypalCartId is "8C167935RU828824X"
        
   if let checkout = PYPLCheckout.sharedInstance() as? PYPLCheckout {
      // set the PayPal environment
      checkout.serviceEnvironment = .sandBox
            
      //set WebBrowserOnly = true
      checkout.webBrowserOnlyMode = true
              
      //set the scheme the app is using
      checkout.setEnvironmentWithKey("kPYPLUrlScheme", value: "sampleapp" as NSObject)
                     
      // set parameters need to be passed to PayPal
      let queryItems = [
          NSURLQueryItem(name: "locale.x", value: "de_DE"),
      ]
      checkout.setEnvironmentWithKey("kPYPLQueryStringParameters", value: queryItems as NSObject)

      //start the PayPal flow
      checkout.start(withECToken: paypalCartId, withDelegate: self)
   }
}
       
// The PayPalCheckoutCompleted function will be called when user completes the PayPal flow. The details object contains PayerID
@objc public func PayPalCheckoutCompleted(_ details: NSDictionary) {
    let payerId = details.object(forKey: "PayerID") as? String ?? ""
    payerIdTextView.text = payerId
}
       
@objc public func PayPalCheckoutCanceled() {
    print("canceled")
}
       
@objc func PayPalCheckoutError(error: NSDictionary) -> Void{
     print(error)
}