Vasco Patricio Vasco Patricio - 4 months ago 23
Android Question

PayPal Integration with Android

I have seen some related questions but none focusing on the specific problem I have:

I'm using the PayPal MPL Library.

I build my PayPalPayment object, then create the activity for the checkout to occur. That runs fine. My problem is, on the ResultDelegate I need to call a function from my activity, that occurs after the payment and makes some changes (such as storing SharedPreferences, etc.).

So something like this:

public class ResultDelegate implements PayPalResultDelegate, Serializable {
public void onPaymentSucceeded(String payKey, String paymentStatus) {
System.out.println("SUCCESS, You have successfully completed your transaction.");
System.out.println("PayKey: "+payKey);
System.out.println("PayStatus: "+paymentStatus);

callMyCustomAfterPaymentFunction();
}
...
}


Now the thing is, I tried to create a constructor for ResultDelegate that accepts my activity. My existing code is:

//On the activity class
public class MainMenuActivity extends Activity {
public void onCreate(Bundle savedInstanceState)
{
...
Button buy = (Button) findViewByID(R.id.buy_button);
buy.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
new PurchaseTask(activity).execute();
}
}
}
}

public class PurchaseTask extends AsyncTask <String, Void, String> {
protected String doInBackground()
{
...
PayPal pp = PayPal.getInstance();
CheckoutButton cb = pp.getCheckoutButton(...);
cb.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
ResultDelegate delegate = new ResultDelegate(myActivity);
Intent checkout = PayPal.getInstance().checkout(paument, activity, delegate);
activity.StartActivity(checkoutIntent);
}
}
}
}

//On the ResultDelegate class

public class ResultDelegate implements PayPalResultDelegate, Serializable {
private Activity myActivity;

public void onPaymentSucceeded(String payKey, String paymentStatus) {
System.out.println("SUCCESS, You have successfully completed your transaction.");
System.out.println("PayKey: "+payKey);
System.out.println("PayStatus: "+paymentStatus);

myActivity.performAfterPaymentOperations();
}
...
}


So the goal is to call the activity function from the ResultDelegate. Or even simpler, just to be able to store some SharedPreference changes when the ResultDelegate onPaymentSucceeded() fires.

But I get a NotSerializableException mentioning that the my MyActivity field is not Serializable.

So, then I added the transient identifier to my activity field inside the ResultDelegate, but now I get a NullPointerException.

Answer

Paypal Mobile Chekout guide

Implementation provided on paypal website is different from yours. They are doing startActivityForResult() to start PaypalActivity. and when in onActivityResult() method they are checking statusCode to check transaction status and act accordingly.

Follow that document for your implementation.

Here in your code, I donot find a point for using AsyncTask. Your ResultDelegate is Serializable where as Activity is not thats why it is throwing NotSerializableException.

Edit:

As you are developing for Google Android platform, then why not to use Google Checkout In-App?

Edit:

This method will be called when your PaypalActivity will finish. That activity will pass resultCode to this onActivityResult method.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (resultCode) {
    case Activity.RESULT_OK:
        // The payment succeeded
        String payKey = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
        // Tell the user their payment succeeded
        break;
    case Activity.RESULT_CANCELED:
        // The payment was canceled
        // Tell the user their payment was canceled
        break;
    case PayPalActivity.RESULT_FAILURE:
        // The payment failed -- we get the error from the EXTRA_ERROR_ID
        // and EXTRA_ERROR_MESSAGE
        String errorID = data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
        String errorMessage = data
                .getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
        // Tell the user their payment was failed.
    }
}

regards, Aqif Hamid