【问题标题】:Error while integrating paypal in Android?在 Android 中集成 paypal 时出错?
【发布时间】:2014-07-11 02:02:12
【问题描述】:

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

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

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

感谢您的帮助

【问题讨论】:

  • 你导入com.paypal.android.sdk.payments.PaymentActivity包了吗?
  • ENVIRONMENT_LIVEPaymentActivity 中。我猜你使用的是旧的 sdk。
  • 我正在使用最新的SDK,我刚刚检查了它:(

标签: android paypal


【解决方案1】:

嘿,我根据我的工作做了一些改变 您只需签出并运行它,然后应用您的客户 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>

如果有任何变化,请告诉我

【讨论】:

    【解决方案2】:

    如果您使用的是安卓工作室。然后只需将 paypal sdk 添加到您的应用级 build.gradle 文件中。

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

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

    活动结果代码将是

    @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

    【讨论】:

      【解决方案3】:

      那篇博文指的是 SDK 的 1.x 版本,但您很可能使用的是 2.x。我建议您使用我们的latest GitHub docs 获取最新的集成信息。

      【讨论】:

        【解决方案4】:

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

        并在您的 Paypal 帐户中导入 jniLibs 库

        【讨论】:

          猜你喜欢
          • 2016-11-07
          • 2021-02-09
          • 1970-01-01
          • 2014-07-22
          • 2015-03-10
          • 2019-07-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多