You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android端PayPal单次支付UI自定义配置技术咨询

自定义Android PayPal单次支付UI配置方案

需求背景

正在开发Android应用的PayPal单次支付功能,仅需实现点击按钮完成支付的核心流程。目前功能已跑通,但需要对PayPal默认UI做以下自定义调整:

  • 已通过ShippingPreference.NO_SHIPPING移除收货地址,要求授权金额与支付金额完全一致;
  • 移除PayPal Credit支付选项;
  • 禁止用户在支付界面添加新卡片。

当前现有代码

结账配置代码

paymentButtonContainer.setup(
            new CreateOrder() {
                @Override
                public void create(@NotNull CreateOrderActions createOrderActions) {
                    ArrayList<PurchaseUnit> purchaseUnits = new ArrayList<>();
                    purchaseUnits.add(
                            new PurchaseUnit.Builder()
                                    .amount(new Amount.Builder()
                                            .currencyCode(CurrencyCode.GBP)
                                            .value("6.00")
                                            .build()
                                    ).shipping(new Shipping.Builder()
                                            .address(null)
                                            .build()
                                    ).build()
                    );
                    OrderRequest order = new OrderRequest(
                            OrderIntent.CAPTURE,
                            new AppContext.Builder()
                                    .userAction(UserAction.PAY_NOW)
                                    .shippingPreference(ShippingPreference.NO_SHIPPING)
                                    .build(),
                            purchaseUnits
                    );
                    createOrderActions.create(order, (CreateOrderActions.OnOrderCreated) null);
                }
            }, new OnApprove() {
                @Override
                public void onApprove(@NotNull Approval approval) {
                    approval.getOrderActions().capture(new OnCaptureComplete() {
                        @Override
                        public void onCaptureComplete(@NotNull CaptureOrderResult result) {
                            Log.i(TAG, String.format("CaptureOrderResult: %s", result));
                            Toast.makeText(MainActivity.this, "Payment Approved", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            },
            null,
            new OnCancel() {
                @Override
                public void onCancel() {
                    Log.d(TAG, "Buyer cancelled the PayPal experience.");
                    Toast.makeText(MainActivity.this, "Payment Cancelled", Toast.LENGTH_SHORT).show();
                }
            }, new OnError() {
                @Override
                public void onError(@NonNull ErrorInfo errorInfo) {
                    Log.d(TAG, String.format("Error: %s", errorInfo));
                    Toast.makeText(MainActivity.this, "Payment Error", Toast.LENGTH_SHORT).show();
                }
            }
    );

Application配置代码

returnURL = BuildConfig.APPLICATION_ID + "://paypalpay";

paypalConfig = new CheckoutConfig(this,
        CLIENT_ID,
        Environment.SANDBOX,
        CurrencyCode.GBP,
        UserAction.PAY_NOW,
        returnURL);

PayPalCheckout.setConfig(paypalConfig);

针对性修改方案

1. 确保授权金额与支付金额一致

在PurchaseUnit.Builder中显式添加authorizationAmount配置,与订单金额保持完全一致:

new PurchaseUnit.Builder()
        .amount(new Amount.Builder()
                .currencyCode(CurrencyCode.GBP)
                .value("6.00")
                .build()
        )
        // 授权金额与支付金额对齐
        .authorizationAmount(new Amount.Builder()
                .currencyCode(CurrencyCode.GBP)
                .value("6.00")
                .build()
        )
        .shipping(new Shipping.Builder()
                .address(null)
                .build()
        )
        .build()

2. 移除PayPal Credit支付选项

在AppContext.Builder中添加disableFunding配置,指定禁用CREDIT类型的支付方式:

new AppContext.Builder()
        .userAction(UserAction.PAY_NOW)
        .shippingPreference(ShippingPreference.NO_SHIPPING)
        // 禁用PayPal Credit
        .disableFunding(Collections.singletonList(FundingOption.CREDIT))
        .build()

3. 禁止添加新卡片

在CheckoutConfig初始化时,调用enableCardFields(false)关闭卡片添加功能:

paypalConfig = new CheckoutConfig(this,
        CLIENT_ID,
        Environment.SANDBOX,
        CurrencyCode.GBP,
        UserAction.PAY_NOW,
        returnURL)
        // 禁止用户添加新卡片
        .enableCardFields(false);

修改后的完整代码

结账配置代码

paymentButtonContainer.setup(
        new CreateOrder() {
            @Override
            public void create(@NotNull CreateOrderActions createOrderActions) {
                ArrayList<PurchaseUnit> purchaseUnits = new ArrayList<>();
                purchaseUnits.add(
                        new PurchaseUnit.Builder()
                                .amount(new Amount.Builder()
                                        .currencyCode(CurrencyCode.GBP)
                                        .value("6.00")
                                        .build()
                                )
                                .authorizationAmount(new Amount.Builder()
                                        .currencyCode(CurrencyCode.GBP)
                                        .value("6.00")
                                        .build()
                                )
                                .shipping(new Shipping.Builder()
                                        .address(null)
                                        .build()
                                )
                                .build()
                );
                OrderRequest order = new OrderRequest(
                        OrderIntent.CAPTURE,
                        new AppContext.Builder()
                                .userAction(UserAction.PAY_NOW)
                                .shippingPreference(ShippingPreference.NO_SHIPPING)
                                .disableFunding(Collections.singletonList(FundingOption.CREDIT))
                                .build(),
                        purchaseUnits
                );
                createOrderActions.create(order, (CreateOrderActions.OnOrderCreated) null);
            }
        }, new OnApprove() {
            @Override
            public void onApprove(@NotNull Approval approval) {
                approval.getOrderActions().capture(new OnCaptureComplete() {
                    @Override
                    public void onCaptureComplete(@NotNull CaptureOrderResult result) {
                        Log.i(TAG, String.format("CaptureOrderResult: %s", result));
                        Toast.makeText(MainActivity.this, "支付已确认", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        },
        null,
        new OnCancel() {
            @Override
            public void onCancel() {
                Log.d(TAG, "用户取消了PayPal支付流程");
                Toast.makeText(MainActivity.this, "支付已取消", Toast.LENGTH_SHORT).show();
            }
        }, new OnError() {
            @Override
            public void onError(@NonNull ErrorInfo errorInfo) {
                Log.d(TAG, String.format("错误信息: %s", errorInfo));
                Toast.makeText(MainActivity.this, "支付出错", Toast.LENGTH_SHORT).show();
            }
        }
);

Application配置代码

returnURL = BuildConfig.APPLICATION_ID + "://paypalpay";

paypalConfig = new CheckoutConfig(this,
        CLIENT_ID,
        Environment.SANDBOX,
        CurrencyCode.GBP,
        UserAction.PAY_NOW,
        returnURL)
        .enableCardFields(false);

PayPalCheckout.setConfig(paypalConfig);

内容的提问来源于stack exchange,提问作者PeterB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 03:44:54