How to integrate PayPal in Eclipse and Android Studio?

Posted on Mar 23 2017 - 2:25pm by Techy Hints

Hi Friends, As I was getting so much problem in integration of PayPal in Eclipse and Android Studio for my Android app. Finally, I got the solution after so many practices. So, that I am sharing the whole thing and steps which I found and follow to integrate PayPal. I have shared for both the platforms Eclipse and Android Studio.

If there is any problem in resolving this issue or you found this post not useful please write us at [email protected] or simply comment in the comment box.

Step1.

First we need a PayPal Developer account to test our app.

You can create account using the following link:

https://developer.paypal.com/

PayPal-Developer-login-page.png

 Click on Login button and follow the steps:

 

image2.png

 

Click on Create Account Button under Dashboard/Sandbox/Accounts. There are two types of test account named Personal (Buyer Account) or Business (Merchant Account).

 

image3.png

 

Create an Application on PayPal Developer Account

https://developer.paypal.com/developer/applications/

 

image4.png 

 

Click on Create App Button.

After Creating the App on PayPal Developer Account and Copy the Client ID and paste it in your MainActivity.java.

private static final String CONFIG_CLIENT_ID = “Your_Client_Id”;

Set environment to Sandbox

private static final String CONFIG_ENVIRONMENT = PayPalConfiguration. ENVIRONMENT_SANDBOX;

 

Step 2.

Create Layout  activity_main.xml

“http://schemas.android.com/apk/res/android”

    android:layout_width=“fill_parent”

    android:layout_height=“fill_parent”

    android:orientation=“vertical” >

    <LinearLayout

        android:layout_width=“match_parent”

        android:layout_height=“match_parent”

        android:orientation=“vertical” >

        <LinearLayout

            android:layout_width=“match_parent”

            android:layout_height=“wrap_content”

            android:layout_weight=“1”

            android:gravity=“center”

            android:orientation=“vertical” >

            <ImageView

                android:id=“@+id/imageView1”

                android:layout_width=“wrap_content”

                android:layout_height=“wrap_content”

                android:layout_gravity=“center”

                android:src=“@drawable/product” />

              

        <LinearLayout

            android:layout_width=“match_parent”

            android:layout_height=“wrap_content”

            android:gravity=“center”

            android:orientation=“vertical” >

            <TextView

                android:id=“@+id/textView1”

                android:layout_width=“wrap_content”

                android:layout_height=“wrap_content”

                android:layout_weight=“1”

                android:text=“@string/amount”

                android:textAppearance=“?android:attr/textAppearanceSmall” />

            <Button

                android:id=“@+id/order”

                android:layout_width=“wrap_content”

                android:layout_height=“wrap_content”

                android:layout_weight=“1”

                android:text=“@string/take_order” />

       

   

 

Create Class MainActivity.java

public class MainActivity extends Activity {

 

/ * – Set to PaymentActivity.ENVIRONMENT_PRODUCTION to move real money.

 * – Set to PaymentActivity.ENVIRONMENT_SANDBOX to use your test credentials

 * from https://developer.paypal.com  */

private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX;      

private static final String CONFIG_CLIENT_ID = “Your_Client_Id”;

private static final int REQUEST_CODE_PAYMENT = 1;

private static final int REQUEST_CODE_FUTURE_PAYMENT = 2;

private static PayPalConfiguration config = new PayPalConfiguration()

                     .environment(CONFIG_ENVIRONMENT)

                     .clientId(CONFIG_CLIENT_ID)             

                     .merchantName(“MerchantName”);

                     .merchantPrivacyPolicyUri(

                                  Uri.parse(“https://www.example.com/privacy”))

                     .merchantUserAgreementUri(

                                  Uri.parse(“https://www.example.com/legal”));

PayPalPayment thingToBuy;

@Override

protected void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_main);

              Intent intent = new Intent(this, PayPalService.class);

              intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

              startService(intent);

              findViewById(R.id.order).setOnClickListener(new OnClickListener() {

                     @Override

                     public void onClick(View v) {

                     thingToBuy = getThingToBuy(PayPalPayment.PAYMENT_INTENT_SALE);

                     Intent intent = new Intent(MainActivity.this,

                                         PaymentActivity.class);

                     intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);

                     startActivityForResult(intent, REQUEST_CODE_PAYMENT);

                     }

              });

 

       }

public void onFuturePaymentPressed(View pressed) {

              Intent intent = new Intent(MainActivity.this,

                           PayPalFuturePaymentActivity.class);            

              intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

              startActivityForResult(intent, REQUEST_CODE_FUTURE_PAYMENT);

       }

private PayPalPayment getThingToBuy(String paymentIntent) {

        return new PayPalPayment(new BigDecimal(“5.60”), “USD”, “item”,

                paymentIntent);

      }

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

              if (requestCode == REQUEST_CODE_PAYMENT) {

                     if (resultCode == Activity.RESULT_OK) {

                           PaymentConfirmation confirm = data

                                         .getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);

                           if (confirm != null) {

                                  try {

                                         System.out.println(confirm.toJSONObject().toString(4));

                                         System.out.println(confirm.getPayment().toJSONObject()

                                                       .toString(4));

 Toast.makeText(getApplicationContext(), “Order placed”,Toast.LENGTH_LONG).show();

                                  } catch (JSONException e) {

                                         e.printStackTrace();

                                  }

                           }

                     } else if (resultCode == Activity.RESULT_CANCELED) {

                           System.out.println(“The user canceled.”);

                     } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {

                     System.out.println(“An invalid Payment or PayPalConfiguration was submitted. Please see the docs.”);

                     }

              } else if (requestCode == REQUEST_CODE_FUTURE_PAYMENT) {

                     if (resultCode == Activity.RESULT_OK) {

                           PayPalAuthorization auth = data

                                         .getParcelableExtra(PayPalFuturePaymentActivity.EXTRA_RESULT_AUTHORIZATION);

                            if (auth != null) {

                                                try {

                                         Log.i(“FuturePaymentExample”,auth.toJSONObject().toString(4));

       String authorization_code = auth.getAuthorizationCode();

       Log.i(“FuturePaymentExample”, authorization_code);

       sendAuthorizationToServer(auth);

       Toast.makeText(getApplicationContext(),”Future Payment code received from     PayPal”,

       Toast.LENGTH_LONG).show();

 

} catch (JSONException e) {

       Log.e(“FuturePaymentExample”,”an extremely unlikely failure occurred: “, e);

                                  }

              }

                     } else if (resultCode == Activity.RESULT_CANCELED) {

                           Log.i(“FuturePaymentExample”, “The user canceled.”);

                     } else if (resultCode == PayPalFuturePaymentActivity.RESULT_EXTRAS_INVALID) {

                           Log.i(“FuturePaymentExample”,

                                         “Probably the attempt to previously start the PayPalService had an invalid PayPalConfiguration. Please see the docs.”);

                     }

              }

       }

       private void sendAuthorizationToServer(PayPalAuthorization authorization) {

       }

       public void onFuturePaymentPurchasePressed(View pressed) {

              // Get the Application Correlation ID from the SDK

              String correlationId = PayPalConfiguration

                           .getApplicationCorrelationId(this);

              Log.i(“FuturePaymentExample”, “Application Correlation ID: “

                           + correlationId);

              // TODO: Send correlationId and transaction details to your server for

              // processing with

              // PayPal…

              Toast.makeText(getApplicationContext(),

                           “App Correlation ID received from SDK”, Toast.LENGTH_LONG)

                           .show();

       }

       @Override

       public void onDestroy() {

              // Stop service when done

              stopService(new Intent(this, PayPalService.class));

              super.onDestroy();

       }

}

Required Permission of AndroidManifest File

(Bold xml code below Only for Eclipse)

 

<uses-sdk

        android:minSdkVersion=”16″

        android:targetSdkVersion=”23″ /> 

   

    <uses-feature android:name=”android.hardware.camera” android:required=”false” />

    <uses-feature android:name=”android.hardware.camera.autofocus”   android:required=”false” />

 

    <uses-permission android:name=”android.permission.CAMERA” />

    <uses-permission android:name=”android.permission.VIBRATE” />

    <uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”/>

    <uses-permission android:name=”android.permission.ACCESS_WIFI_STATE”/>

    <uses-permission android:name=”android.permission.INTERNET”/> 

    <application

        android:allowBackup=”true”

        android:icon=”@drawable/ic_launcher”

        android:label=”@string/app_name”

        android:theme=”@style/AppTheme” >

        <activity

            android:name=”.MainActivity”

            android:label=”@string/app_name” >

            <intent-filter>

                <action android:name=”android.intent.action.MAIN” />

                 <category android:name=”android.intent.category.LAUNCHER” />

            intent-filter>

        activity>       

         <service android:name=”com.paypal.android.sdk.payments.PayPalService” android:exported=”false” />

         <activity android:name=”com.paypal.android.sdk.payments.PaymentActivity” />

        <activity android:name=”com.paypal.android.sdk.payments.LoginActivity” />

        <activity android:name=”com.paypal.android.sdk.payments.PaymentMethodActivity” />

        <activity android:name=”com.paypal.android.sdk.payments.PaymentConfirmActivity” />

        <activity android:name=”com.paypal.android.sdk.payments.PayPalFuturePaymentActivity” />

        <activity android:name=”com.paypal.android.sdk.payments.FuturePaymentConsentActivity” />

        <activity android:name=”com.paypal.android.sdk.payments.FuturePaymentInfoActivity” />

        <activity android:name=”com.paypal.android.sdk.payments.PaymentCompletedActivity”/>

        <activity

            android:name=”io.card.payment.CardIOActivity”

            android:configChanges=”keyboardHidden|orientation” />

        <activity android:name=”io.card.payment.DataEntryActivity” />

    application 

 

Import the libraries in Eclipse Project

 

To run PayPal SDK 2.13.1 on Eclipse project you need 3 jars

  • PayPalAndroidSDK-2.13.1.jar
  • okhttp-3.0.1.jar
  • okio-1.6.0.jar

 Click on this link or copy-paste it into your browser to download the zip File

 
http://goo.gl/CWSAQP


 
Import the libraries in Android Studio Project

The PayPal Android SDK is now available at Maven Repository. The latest version is available via mavenCentral(). Just add the following dependency from mavenCentral():

compile ‘com.paypal.sdk:paypal-android-sdk:2.13.1’



Source by trssoftwaresolution

About the Author

Geeky Magazine provides you with the daily latest technical news, reviews, startups, and every new in the internet world.