7

I'm trying to setup inapp paypall MECL transaction.

But it keeps failing in getting a device token (couldNotFetchDeviceReferenceToken)

and beside of that when the library is initialized, i get the button but the onClickListeneren gets only the first click when i click multiple times on the button (while not doing anything in the onClick)..

Getting the followin errors:

12-19 16:59:41.731: ERROR/Error(4745): Authentication failed, button not enabled.
12-19 16:59:41.739: ERROR/paypal(4745): FAIL receiving Token
12-19 17:00:06.544: ERROR/PP Init(4745): INITIALIZE_FAILURE

Is there anyone who knows what i'm doing wrong?

here is some of the source:

    // The PayPal server to be used - can also be ENV_NONE and ENV_LIVE
private static final int server = PayPal.ENV_SANDBOX;
// The ID of your application that you received from PayPal
private static final String appID = "APP-.....xxxxxxx"; 

Handler hRefresh = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what){
            case INITIALIZE_SUCCESS:
                //We have initialized the application, close the dialog and launch the WebView
                setupButtons();
                break;
            case INITIALIZE_FAILURE:
                setupButtons();
                //Initialization failure, close the dialog, update the page and show a toast
                //mProgDialog.cancel();
                //currentPage.update();
                break;
        }
    }
};

In the onCreate:

// Initialize the library. We'll do it in a separate thread because it requires communication with the server
        // which may take some time depending on the connection strength/speed.
        Thread libraryInitializationThread = new Thread() {
            public void run() {
                initLibrary();

                // The library is initialized so let's launch it by notifying our handler
                if (PayPal.getInstance().isLibraryInitialized()) {
                    Log.e("PP Init", "INITIALIZE_SUCCESS");
                    hRefresh.sendEmptyMessage(INITIALIZE_SUCCESS);

                }
                else {
                    Log.e("PP Init", "INITIALIZE_FAILURE");
                    hRefresh.sendEmptyMessage(INITIALIZE_FAILURE);
                }
            }
        };
        libraryInitializationThread.start();

.

/**
 * Create our CheckoutButton and update the UI.
 */
public void setupButtons() {
    PayPal pp = PayPal.getInstance();
    // Get the CheckoutButton. There are five different sizes. The text on the button can either be of type TEXT_PAY or TEXT_DONATE.
    launchSimplePayment = pp.getCheckoutButton(DailyOfferOrderActivity.this, PayPal.BUTTON_194x37, CheckoutButton.TEXT_PAY);
    // You'll need to have an OnClickListener for the CheckoutButton. For this application, MPL_Example implements OnClickListener and we
    // have the onClick() method below.
    launchSimplePayment.setOnClickListener(DailyOfferOrderActivity.this);
    // The CheckoutButton is an android LinearLayout so we can add it to our display like any other View.
    layoutSimplePayment.addView(launchSimplePayment);


    info.setText("");
    info.setVisibility(View.GONE);
}




        public void onClick(View v) {

    /**
     * For each call to checkout() and preapprove(), we pass in a ResultDelegate. If you want your application
     * to be notified as soon as a payment is completed, then you need to create a delegate for your application.
     * The delegate will need to implement PayPalResultDelegate and Serializable. See our ResultDelegate for
     * more details.
     */     
    if(v == launchSimplePayment) {
        if(checkParameters()){
            new startPPTask().execute();
        }
        layoutSimplePayment.removeAllViews();
        setupButtons();

    }
}

this is the paypal webview class:

public class PayPalWebViewActivity extends Activity {
private WebView mWebView;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.paypalwebview);

    Intent intent = getIntent();
    String token = intent.getStringExtra("token");
    String deviceToken = intent.getStringExtra("devicetoken");


    //String url = "https://www.sandbox.paypal.com/cgi-bin/webscr?";
    String url = "https://www.paypal.com/cgi-bin/webscr?";


    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new HelloWebViewClient());

    String temp = url + "cmd=_express-checkout-mobile&useraction=commit&token=" + token + "&drt=" + deviceToken;
    //Log.e("test", temp);
    mWebView.loadUrl(temp);
    //+ "&drt=NULL"
}


    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}


private class HelloWebViewClient extends WebViewClient {
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) {
         //Log.e("done", "start: " + url);
     }
        // intercept page finished event
     @Override 
     public void onPageFinished(WebView view, String url) {
            //Log.e("done", "done: " + url);
            view.loadUrl("javascript:javascript:(function() { " +
                    "window.navigator.standalone=1;" +
                    "})()");
     }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        HttpClient httpclient = new DefaultHttpClient();

        // Prepare a request object
        HttpGet httpget = new HttpGet(url); 

        // Execute the request
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            // Examine the response status

            //Log.e("Get Request", response.toString());
            // Get hold of the response entity
            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need
            // to worry about connection release

            if (entity != null) {

                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                //Log.e("test", result);
                instream.close();
            } 

        } catch (IOException e) {
            e.printStackTrace();
        } 


        view.loadUrl(url);  

        return true;
    }



 private String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

}

Luciano
  • 2,691
  • 4
  • 38
  • 64
  • Nope sorry, i can't help you with that. I've implemented paypal for other company so i'm not the owner of the source code.. Check the MECL documentations it should gif enough feedback to setup paypal mecl – Luciano Feb 23 '12 at 12:27
  • I don't know anything about the webservice, i didnt work on the that. See the code above for the onClick.. – Luciano Mar 05 '12 at 10:19
  • checkparameter checks if all user input field are filled in correctly. And the startpptask is starting the webview activity with the device token and the token received from the webservice. – Luciano Mar 05 '12 at 13:33
  • i don't see the points of showing it, because checking if inputfields aren't empty is offtopic, and starting a new webview activity is just basic java android programming: Intent intent = new Intent(context, paypalwebvieclass.class); intent.putExtra("token", valueFromSetExpressCheckoutResponse); intent.putExtra("devicetoken", _deviceReferenceToken); context.startActivity(intent); – Luciano Mar 05 '12 at 14:08
  • why do you dont start a new question, so i cant check your code so i can help you? – Luciano Mar 05 '12 at 14:10
  • Shall i see your `paypalwebviewclass.class` file – Praveenkumar Mar 14 '12 at 12:26
  • I've added my webview class @SpK – Luciano Mar 14 '12 at 12:40
  • 2
    You should hide or make a fake of `appID = "APP-XXXXX"`. You shouldn't publish it. –  Mar 14 '12 at 12:48
  • If i use that url whatever you're using in your Webview class means, it shows me like [this](http://i.stack.imgur.com/THBdC.png) – Praveenkumar Mar 14 '12 at 12:48
  • @haibison You know anything about `Mobile Express Checkout Library` – Praveenkumar Mar 14 '12 at 12:59
  • @SpK, Sorry I haven't yet coded with any payment method :-( –  Mar 14 '12 at 13:03
  • btw @haibison that appID whas just the development appID for the sandbox.. Same as in de documentation (so nothing personal:P) – Luciano Mar 14 '12 at 13:16
  • I guess you already got this documentation @SpK https://cms.paypal.com/cms_content/US/en_US/files/developer/PP_MECL_Developer_Guide_and_Reference_Android.pdf – Luciano Mar 14 '12 at 13:56

1 Answers1

7

found the problem for not receiving token: forgot the following permission:READ_PHONE_STATE

for the button i can't find the problem, the temparary solution i use is just remove the button and setupButtons() again..

Luciano
  • 2,691
  • 4
  • 38
  • 64