【问题标题】:Paypal Android SDK get custom value in onActivityResultPaypal Android SDK 在 onActivityResult 中获取自定义值
【发布时间】:2016-02-10 20:11:08
【问题描述】:

我注意到Paypal Android SDK 添加了一个选项以将custominvoice 添加到PaypalPayment,因为v2.5.0。我现在可以向 PaypalPayment 添加一个项目以及一个自定义值,如下所示:

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

但是,他们的文档中没有提到如何在onActivityResult 中恢复此自定义值。这是我的代码:

    @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");
                }
            }
        }

即使我使用发票编号而不是自定义,似乎也没有任何方法可以从 PaymentConfirmation 对象中检索,也没有它的方法 PaypalPayment

【问题讨论】:

    标签: android android-intent paypal paypal-mobile-sdk


    【解决方案1】:

    可以按如下方式通过反射获得该字段,但在生产中这样做可能是一个可怕的想法(一方面,我不知道是否字段名称“l”是稳定的,尽管它在处理付款时在 Android Studio 调试器中具有正确的值):

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

    请注意,您必须捕获许多异常 - 请参阅 How do I read a private field in Java? 了解更多信息。真正的 PayPal 只需要发布一个更新的 SDK 并通过 getter 提供对该字段的访问。同样,我无法想象在生产中实际使用它,但它至少适用于 Android 4.2。

    【讨论】:

    • 我认为将自定义值存储在某个会话对象中并将其与confirm JSON 一起发送到服务器并使用 REST API 验证它会更明智。我们可以在活动destroy 生命周期中设置一些超时或方法来清除会话对象。你怎么看?
    • 这基本上就是我所做的。我的用例是同一片段上的多个 PayPal 按钮,因此我制作了一个 HashMap,将每个 Button View 对象与我的自定义“paypal 小部件”对象相关联。当用户点击按钮时,我可以确定哪个自定义小部件(和自定义 ID)与该特定按钮相关联,并将其存储在片段中的“activePaypalWidget”中。然后 onActivityResult 我可以从 activePaypalWidget 获取我的自定义值,在我的服务器确认调用中使用它,最后清除片段中的变量以准备下一次按下按钮。
    猜你喜欢
    • 2015-06-20
    • 2021-12-18
    • 2019-07-29
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多