Paypal Android SDK get custom value in onActivityResult

624 Views Asked by At

I noticed Paypal Android SDK added an option to add custom or invoice to PaypalPayment since v2.5.0. I am now able to add a item to PaypalPayment along with a custom value, something like this:

PayPalPayment payment = new PayPalPayment(new BigDecimal("1.75"), "USD", "sample item",
            PayPalPayment.PAYMENT_INTENT_SALE);
payment.custom("my custom value");

However, it is not mentioned in their documentation how to recover this custom value in onActivityResult. Here is my code:

    @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 {
//How do I get `custom` from `confirm` object here???

                            Log.e(TAG, confirm.toJSONObject().toString(4));
                            Log.e(TAG, confirm.getPayment().toJSONObject()
                                    .toString(4));

                            String paymentId = confirm.toJSONObject()
                                    .getJSONObject("response").getString("id");

                            String payment_client = confirm.getPayment()
                                    .toJSONObject().toString();

                            Log.e(TAG, "paymentId: " + paymentId
                                    + ", payment_json: " + payment_client);

                            //Payment verification logic

                        } catch (JSONException e) {
                            Log.e(TAG, "Meow! Coders ye be warned: ",e);
                        }
                    }
                } else if (resultCode == Activity.RESULT_CANCELED) {
                    Log.e(TAG, "The user canceled.");
                } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
                    Log.e(TAG,"Invalid Payment");
                }
            }
        }

Even if I use invoice number instead of custom, there doesn't seem any method available to retrieve from PaymentConfirmation object, nor its method PaypalPayment

1

There are 1 best solutions below

2
On

You can get the field via reflection as follows, but it's probably a terrible idea to do this in production (for one thing, I have no idea whether or not the field name "l" is stable, though it has the correct value in the Android Studio debugger when processing a payment):

Field custom = confirm.getPayment().getClass().getDeclaredField("l");
custom.setAccessible(true);
String customID = (String) custom.get(confirm.getPayment());
Log.i(TAG, customID);

Note that you'll have to trap a number of exceptions - see How do I read a private field in Java? for more info. Really PayPal just needs to release an updated SDK and provide access to this field via a getter. Again, I can't imagine actually using this in production, but it works on Android 4.2 at least.