Reputation: 43
I am using PayPal in my android application. when i order single item the App works well. but when i add multiple items to cart and press the order button the app crashes. My problem is how can i pass multiple items and its details to PayPal to order it. For this, my piece of code is.
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);
Upvotes: 0
Views: 612
Reputation: 381
Please see the example provided here for using optional payment details and item list.
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;
}
Upvotes: 1