Arulnadhan
Arulnadhan

Reputation: 921

Error while integrating paypal in Android?

I am trying to integrate paypal in my Android App. I am following this method http://sunil-android.blogspot.in/2013/10/paypal-android-sdk-with-multiple-in-app.html

I have added the Paypal Android SDK library in my App.

But it throws me a error that many methods like this PaymentActivity.ENVIRONMENT_LIVE; cannot be resolved.

Thanks for your help

Upvotes: 2

Views: 2932

Answers (4)

Kashfa Khan
Kashfa Khan

Reputation: 2493

If you are using android studio. Then just add the paypal sdk to your app level build.gradle file.

compile 'com.paypal.sdk:paypal-android-sdk:2.14.2'

Now you can use the following code to accept payment. Remember you also need your paypal client id. You can get it by creating an app to developer.paypal.com

private void getPayment() {
    //Getting the amount from editText
    paymentAmount = editTextAmount.getText().toString();

    //Creating a paypalpayment 
    PayPalPayment payment = new PayPalPayment(new BigDecimal(String.valueOf(paymentAmount)), "USD", "Simplified Coding Fee",
            PayPalPayment.PAYMENT_INTENT_SALE);

    //Creating Paypal Payment activity intent 
    Intent intent = new Intent(this, PaymentActivity.class);

    //putting the paypal configuration to the intent 
    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

    //Puting paypal payment to the intent 
    intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);

    //Starting the intent activity for result
    //the request code will be used on the method onActivityResult 
    startActivityForResult(intent, PAYPAL_REQUEST_CODE);
}

The activityresult code would be

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //If the result is from paypal 
    if (requestCode == PAYPAL_REQUEST_CODE) {

        //If the result is OK i.e. user has not canceled the payment 
        if (resultCode == Activity.RESULT_OK) {
            //Getting the payment confirmation 
            PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);

            //if confirmation is not null 
            if (confirm != null) {
                try {
                    //Getting the payment details 
                    String paymentDetails = confirm.toJSONObject().toString(4);
                    Log.i("paymentExample", paymentDetails);

                    //Starting a new activity for the payment details and also putting the payment details with intent 
                    startActivity(new Intent(this, ConfirmationActivity.class)
                            .putExtra("PaymentDetails", paymentDetails)
                            .putExtra("PaymentAmount", paymentAmount));

                } catch (JSONException e) {
                    Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
                }
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            Log.i("paymentExample", "The user canceled.");
        } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
            Log.i("paymentExample", "An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
        }
    }
}

Source: Android PayPal Integration Tutorial

Upvotes: 1

Garima Mishra
Garima Mishra

Reputation: 84

Hey I have done some changes in it according to my work You just check out and Run it and just apply your Client ID Hope it will help you

MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.paypal.android.sdk.payments.PayPalConfiguration;
import com.paypal.android.sdk.payments.PayPalPayment;
import com.paypal.android.sdk.payments.PayPalService;
import com.paypal.android.sdk.payments.PaymentActivity;
import com.paypal.android.sdk.payments.PaymentConfirmation;

import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;


public class MainActivity extends AppCompatActivity {

    Button btn_apply;


    private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX;
    private static final String CONFIG_CLIENT_ID = "Your Client ID";
    // when testing in sandbox, this is likely the -facilitator email address.
    private static final String CONFIG_RECEIVER_EMAIL = "";
    private static final int REQUEST_PAYPAL_PAYMENT = 1;
    private static final int REQUEST_CODE_PAYMENT = 1;
    private static PayPalConfiguration paypalConfig = new PayPalConfiguration()
            .environment(CONFIG_ENVIRONMENT).clientId(
                    CONFIG_CLIENT_ID);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_apply = (Button) findViewById(R.id.buyItBtn);



        btn_apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal("1.75"), "USD", "hipster jeans", PayPalPayment.PAYMENT_INTENT_SALE);

                    Intent intent = new Intent(MainActivity.this, PaymentActivity.class);

                    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, paypalConfig);
                    intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);


                    startActivityForResult(intent, REQUEST_CODE_PAYMENT);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_PAYPAL_PAYMENT) {
            if (resultCode == Activity.RESULT_OK) {
                PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
                if (confirm != null) {


                    try {
                        System.out.println("Responseeee" + confirm);
                        Log.i("paymentExample", confirm.toJSONObject().toString());
                        JSONObject jsonObj = new JSONObject(confirm.toJSONObject().toString());
                        String paymentId = jsonObj.getJSONObject("response").getString("id");
                        System.out.println("payment id:-==" + paymentId);
                        Toast.makeText(getApplicationContext(), "Payment Successful", Toast.LENGTH_LONG).show();
                        Intent main = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(main);
                        finish();
 } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else if (resultCode == Activity.RESULT_CANCELED) {
                    Log.i("paymentExample", "The user canceled.");
                } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
                    Log.i("paymentExample", "An invalid payment was submitted. Please see the docs.");
                }
            }


        }
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.zalak.paypal_proj" >

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/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="io.card.payment.CardIOActivity"
            android:configChanges="keyboardHidden|orientation" />
        <activity android:name="io.card.payment.DataEntryActivity" />
    </application>

If there is any changes do let my know

Upvotes: 2

Giri Mishra
Giri Mishra

Reputation: 11

Check the Library you imported Just import the library as given in the demo of Ravi tamanda android hive http://www.androidhive.info/2015/02/android-integrating-paypal-using-php-mysql-part-2

and import library of jniLibs in Your Paypalaccount

Upvotes: 0

Jeff Brateman
Jeff Brateman

Reputation: 3277

That blog post refers to version 1.x of the SDK, but you're most likely using 2.x. I recommend you use our latest GitHub docs for the latest integration information.

Upvotes: 0

Related Questions