简体   繁体   English

Paypal Android SDK 在 onActivityResult 中获取自定义值

[英]Paypal Android SDK get custom value in onActivityResult

I noticed Paypal Android SDK added an option to add custom or invoice to PaypalPayment sincev2.5.0 .我注意到贝宝Android SDK中增加了一个选项来添加custominvoice ,因为到PaypalPaymentV2.5.0 I am now able to add a item to PaypalPayment along with a custom value, something like this:我现在可以向 PaypalPayment 添加一个项目以及一个自定义值,如下所示:

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 .但是,他们的文档中没有提到如何在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即使我使用发票编号而不是自定义,似乎也没有任何方法可以从PaymentConfirmation对象中检索,也没有它的方法PaypalPayment

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):可以按如下方式通过反射获取该字段,但在生产中执行此操作可能是一个糟糕的主意(一方面,我不知道字段名称“l”是否稳定,尽管它在处理付款时的 Android Studio 调试器):

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?请注意,您必须捕获一些异常 - 请参阅如何在 Java 中读取私有字段? for more info.了解更多信息。 Really PayPal just needs to release an updated SDK and provide access to this field via a getter.实际上,PayPal 只需要发布更新的 SDK 并通过 getter 提供对该字段的访问。 Again, I can't imagine actually using this in production, but it works on Android 4.2 at least.同样,我无法想象在生产中实际使用它,但它至少适用于 Android 4.2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM