PayPal Integration with Android

我只是一个虾纸丫 提交于 2020-06-09 21:58:42

问题


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.


回答1:


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




回答2:


You can create you custom listener something like this :

Create a custom listener :

  OnDoubleTap mListener;

    // Double tap custome listenre to edit photoes
    public interface OnDoubleTap {
        public void onEvent(Uri imgPath, int mPos);

    }

    public void setDoubleTapListener(OnDoubleTap eventListener) {
        mListener = eventListener;

    }

Now call this wherever you want like this :

mListener.onEvent(Uri, 1));

Now whenever you call this listener this will fire in your activity where you use this listener like this :

myCanvas.setDoubleTapListener(new OnDoubleTap() {

        @Override
        public void onEvent(Uri imgPath, int Pos) {
            // TODO Auto-generated method stub
            Toast.makeText(mContext, "LISTENER WORKING !!!", Toast.LENGTH_SHORT).show();

        }
    });

Where myCanvas is object of class where you create you listener.




回答3:


Try this solution:

PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal(price),getResources().getString(R.string.curruncy_code), getResources().getString(R.string.app_name),
                    PayPalPayment.PAYMENT_INTENT_SALE);
            Intent intent = new Intent(CreateEventStep4.this, PaymentActivity.class);
            intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
            startActivityForResult(intent, REQUEST_PAYPAL_PAYMENT);

PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
            if (confirm != null) {
                try {

                    Log.e("paymentExample", confirm.toJSONObject().toString());


                    JSONObject jsonObj=new JSONObject(confirm.toJSONObject().toString());

                    String paymentId=jsonObj.getJSONObject("response").getString("id");
                    System.out.println("payment id:-=="+paymentId);

                    screenShotFile = takeScreenshot(frmTemplate);
                    uploadImage(myEvent.getEvent_id(),screenShotFile);

                } catch (JSONException e) {
                    Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
                }


来源:https://stackoverflow.com/questions/11234743/paypal-integration-with-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!