Android:Paypal Merchant Id错误为无效凭据

时间:2014-06-18 12:19:09

标签: android paypal

我是android的新手并使用沙盒id完成了paypal集成但是当我使用生产环境时它会崩溃并且出来当我检查logcat时我会得到错误,如下所示

06-18 07:09:40.221: W/DefaultRequestDirector(3943): Authentication error: Unable to respond to any of these challenges: {}
06-18 07:09:40.591: W/paypal.sdk(3943): aa SN:35 PayPal Debug-ID: 7d7a3da2bf9aa [live, 2.2.2;release]
06-18 07:09:40.591: E/paypal.sdk(3943): request failure with http statusCode:401,exception:org.apache.http.client.HttpResponseException: Unauthorized
06-18 07:09:40.641: E/paypal.sdk(3943): request failed with server response:{"error":"invalid_client","error_description":"The client credentials are invalid"}
06-18 07:09:40.641: E/PayPalService(3943): invalid_client

但我在这个应用程序中使用的客户端ID是正确的,但我使用的是一个错误 生产ID。任何人都可以告诉我为什么会发生这种情况以及paypal SDK如何工作

paypal代码

package com.coded.sandeep;

import java.math.BigDecimal;
import java.util.StringTokenizer;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

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; 
public class PaypalActivity extends Activity {


    private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_PRODUCTION;

    // note that these credentials will differ between live & sandbox environments.
    private static final String CONFIG_CLIENT_ID = "AfZKkxA3Detdl4CgpOKoDMUVTSn3VHq633qkQp_F5hL520589653";


    private static PayPalConfiguration config = new PayPalConfiguration()
    .environment(CONFIG_ENVIRONMENT)
    .clientId(CONFIG_CLIENT_ID);

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

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

        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

        startService(intent);
    }

    public void onBuyPressed(View pressed) {

        PayPalPayment thingToBuy = getThingToBuy(PayPalPayment.PAYMENT_INTENT_SALE);

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

        intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);

        startActivityForResult(intent, 0);

    }

     private PayPalPayment getThingToBuy(String paymentIntent) {

        Intent intent1 = getIntent();

        String message = "people"
        String amount = "1.26"



        return new PayPalPayment(new BigDecimal(amount), "GBP", message,
                paymentIntent);

    }

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

                    Log.i("paymentExample", confirm.toJSONObject().toString(4));
                } 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 was submitted. Please see the docs.");
        }
    }

    @Override
    public void onDestroy() {
        stopService(new Intent(this, PayPalService.class));
        super.onDestroy();
    }
}

我正在为Android应用程序进行Paypal集成,但是当我使用ENVIRONMENT_PRODUCTION并添加我在PayPal中创建的实时ID时,我添加了Config_client_id但是在添加我的应用程序之后因为“商家ID无效”而崩溃并弹出错误我怎么能纠正这个我已经发布了我正在使用它的代码。

但是当我使用SANDBOX ENVIRONMENT时,下面的代码工作正常,没有错误

1 个答案:

答案 0 :(得分:4)

尝试完整代码和sanbox ID或客户端ID不同: -

    import java.io.StringReader;
    import java.math.BigDecimal;
    import java.util.ArrayList;
    import java.util.List;

    import org.json.JSONException;
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserFactory;

    import android.app.Activity;
    import android.app.Dialog;
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.drawable.Drawable;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.text.Html;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.RelativeLayout;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.actionbarsherlock.app.ActionBar;
    import com.actionbarsherlock.app.SherlockActivity;
    import com.actionbarsherlock.view.MenuItem;
    import com.coloruapp.model.utilities.Constant;
    import com.paypal.android.sdk.payments.PayPalAuthorization;
    import com.paypal.android.sdk.payments.PayPalConfiguration;
    import com.paypal.android.sdk.payments.PayPalFuturePaymentActivity;
    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;

    public class PayPalMainActivity extends Activity implements OnClickListener{

        /*
         * - Set to PaymentActivity.ENVIRONMENT_PRODUCTION to move real money.
         * 
         * - Set to PaymentActivity.ENVIRONMENT_SANDBOX to use your test credentials from
         * https://developer.paypal.com
         * 
         * - Set to PayPalConfiguration.ENVIRONMENT_NO_NETWORK to kick the tires without communicating
         * to PayPal's servers.
         */
        private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_PRODUCTION;

        // note that these credentials will differ between live & sandbox environments.


        private static final String CONFIG_CLIENT_ID = "Your client id";        //Live Id


        private static final int REQUEST_CODE_PAYMENT = 1;
        private static final int REQUEST_CODE_FUTURE_PAYMENT = 2;


        private static PayPalConfiguration config;


        final Context context = this;

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


              config = new PayPalConfiguration()
             .environment(CONFIG_ENVIRONMENT)
             .clientId(CONFIG_CLIENT_ID)
             // The following are only used in PayPalFuturePaymentActivity.
             .merchantName("Your Merchent Name")
             .defaultUserEmail("Your Email id")
             .languageOrLocale("Your set Language");
             //.merchantPrivacyPolicyUri(Uri.parse("https://www.example.com/privacy"))
             //.merchantUserAgreementUri(Uri.parse("https://www.example.com/legal"));




            Intent intent = new Intent(this, PayPalService.class);
            intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
            startService(intent);
        }


        public void onBuyPressed(View pressed) {
            // change PAYMENT_INTENT_SALE to PAYMENT_INTENT_AUTHORIZE to only authorize payment and capture funds later.
            PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal(String.valueOf(1.26)), "GBP", "people",
                PayPalPayment.PAYMENT_INTENT_AUTHORIZE);

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

            intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);

            startActivityForResult(intent, REQUEST_CODE_PAYMENT);
        }

        public void onFuturePaymentPressed(View pressed) {
            Intent intent = new Intent(PayPalMainActivity.this, PayPalFuturePaymentActivity.class);

            startActivityForResult(intent, REQUEST_CODE_FUTURE_PAYMENT);
        }

        @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 {
                            Log.i("paymentExample", confirm.toJSONObject().toString(4));


                            Toast.makeText(PayPalMainActivity.this, "Payment Successful. You will receive an email shortly", Toast.LENGTH_SHORT).show();



                        } 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 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) {

            // TODO:
            // Send the authorization response to your server, where it can exchange the authorization code
            // for OAuth access and refresh tokens.
            //
            // Your server must then store these tokens, so that your server code can execute payments
            // for this user in the future.

        }

        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();
        }



    }

xml代码: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#161616"
    android:orientation="vertical" >


        <ImageButton
            android:id="@+id/buyItBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:onClick="onBuyPressed" />
    </LinearLayout>



</LinearLayout>