在Android中使用PayPal订购多个商品

时间:2015-05-16 08:08:46

标签: android paypal

我在Android应用程序中使用PayPal。当我订购单项时,应用程序运行良好。但是当我向购物车添加多个商品并按下订单按钮时​​,应用程序崩溃了。我的问题是如何将多个项目及其详细信息传递给PayPal进行订购。为此,我的代码是。

String price = res.getString("FoodPrice");
totalprice = totalprice + Integer.parseInt(price);
String title = res.getString("FoodTitle");
int quantity = Integer.parseInt(res.getString("quantity"));
PayPalItem ppi = new PayPalItem(title, quantity,
                        new BigDecimal(price), "USD", "sku");
    ppis = new PayPalItem[length];
    ppis[i] = ppi;

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    PayPalPaymentDetails pd = new PayPalPaymentDetails(
                            PayPalItem.getItemTotal(ppis), // price
                            new BigDecimal(10.0), // shipment costs
                            new BigDecimal(0));
                    thingToBuy = new PayPalPayment(new BigDecimal(
                            totalprice), "USD", "shortDescription",
                            PayPalPayment.PAYMENT_INTENT_SALE);
                    thingToBuy.items(ppis);
                    thingToBuy.paymentDetails(pd);

                    Intent intent = new Intent(FoodActivity.this,
                            PaymentActivity.class);

                    intent.putExtra(
                            PayPalService.EXTRA_PAYPAL_CONFIGURATION,
                            config);
                    intent.putExtra(PaymentActivity.EXTRA_PAYMENT,
                            thingToBuy);
                    startActivityForResult(intent, REQUEST_CODE_PAYMENT);

1 个答案:

答案 0 :(得分:1)

请参阅提供的示例here,了解使用可选的付款明细和商品列表。

    private PayPalPayment getStuffToBuy(String paymentIntent) {
    //--- include an item list, payment amount details
    PayPalItem[] items =
        {
                new PayPalItem("sample item #1", 2, new BigDecimal("87.50"), "USD",
                        "sku-12345678"),
                new PayPalItem("free sample item #2", 1, new BigDecimal("0.00"),
                        "USD", "sku-zero-price"),
                new PayPalItem("sample item #3 with a longer name", 6, new BigDecimal("37.99"),
                        "USD", "sku-33333") 
        };
    BigDecimal subtotal = PayPalItem.getItemTotal(items);
    BigDecimal shipping = new BigDecimal("7.21");
    BigDecimal tax = new BigDecimal("4.67");
    PayPalPaymentDetails paymentDetails = new PayPalPaymentDetails(shipping, subtotal, tax);
    BigDecimal amount = subtotal.add(shipping).add(tax);
    PayPalPayment payment = new PayPalPayment(amount, "USD", "sample item", paymentIntent);
    payment.items(items).paymentDetails(paymentDetails);

    //--- set other optional fields like invoice_number, custom field, and soft_descriptor
    payment.custom("This is text that will be associated with the payment that the app can use.");

    return payment;
}