在Android中集成paypal时出错?

时间:2014-05-22 07:04:19

标签: android paypal

我正在尝试将paypal集成到我的Android应用中。我正在遵循这种方法 http://sunil-android.blogspot.in/2013/10/paypal-android-sdk-with-multiple-in-app.html

我在我的应用程序中添加了Paypal Android SDK库。

但它给我一个错误,即许多像PaymentActivity.ENVIRONMENT_LIVE;这样的方法都无法解决。

感谢您的帮助

4 个答案:

答案 0 :(得分:2)

嘿我根据我的工作做了一些改变 您只需签出并运行它,只需应用您的客户ID即可 希望它能帮到你

<强> 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>

如果有任何变化,请让我知道

答案 1 :(得分:1)

如果您使用的是android studio。然后只需将paypal sdk添加到您的应用程序级build.gradle文件中。

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

现在您可以使用以下代码接受付款。请记住,您还需要您的PayPal客户端ID。您可以通过向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);
}

activityresult代码将是

@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.");
        }
    }
}

来源: Android PayPal Integration Tutorial

答案 2 :(得分:0)

该博客帖子引用了SDK的1.x版本,但您最有可能使用2.x.我建议您使用我们的latest GitHub docs获取最新的集成信息。

答案 3 :(得分:0)

检查您导入的库 只需导入Ravi tamanda android hive演示中给出的库 http://www.androidhive.info/2015/02/android-integrating-paypal-using-php-mysql-part-2

并在您的Paypalaccount中导入jniLibs库