private static void addCartDetails(NVPEncoder encoder, ShoppingCart cart) throws GenericEntityException { encoder.add("CURRENCYCODE", cart.getCurrency()); int line = 0; for (ShoppingCartItem item : cart.items()) { encoder.add("L_NUMBER" + line, item.getProductId()); encoder.add("L_NAME" + line, item.getName()); encoder.add( "L_AMT" + line, item.getBasePrice().setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()); encoder.add("L_QTY" + line, item.getQuantity().toBigInteger().toString()); line++; BigDecimal otherAdjustments = item.getOtherAdjustments(); if (otherAdjustments.compareTo(BigDecimal.ZERO) != 0) { encoder.add("L_NUMBER" + line, item.getProductId()); encoder.add("L_NAME" + line, item.getName() + " Adjustments"); encoder.add( "L_AMT" + line, otherAdjustments.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()); encoder.add("L_QTY" + line, "1"); line++; } } BigDecimal otherAdjustments = cart.getOrderOtherAdjustmentTotal(); if (otherAdjustments.compareTo(BigDecimal.ZERO) != 0) { encoder.add("L_NUMBER" + line, "N/A"); encoder.add("L_NAME" + line, "Order Adjustments"); encoder.add( "L_AMT" + line, otherAdjustments.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()); encoder.add("L_QTY" + line, "1"); line++; } encoder.add("ITEMAMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString()); encoder.add("SHIPPINGAMT", "0.00"); encoder.add("TAXAMT", "0.00"); encoder.add("AMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString()); // NOTE: The docs say this is optional but then won't work without it encoder.add("MAXAMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString()); }
public static Map<String, Object> setExpressCheckout( DispatchContext dctx, Map<String, ? extends Object> context) { ShoppingCart cart = (ShoppingCart) context.get("cart"); Locale locale = cart.getLocale(); if (cart == null || cart.items().size() <= 0) { return ServiceUtil.returnError( UtilProperties.getMessage(resource, "AccountingPayPalShoppingCartIsEmpty", locale)); } GenericValue payPalConfig = getPaymentMethodGatewayPayPal(dctx, context, null); if (payPalConfig == null) { return ServiceUtil.returnError( UtilProperties.getMessage( resource, "AccountingPayPalPaymentGatewayConfigCannotFind", locale)); } NVPEncoder encoder = new NVPEncoder(); // Set Express Checkout Request Parameters encoder.add("METHOD", "SetExpressCheckout"); String token = (String) cart.getAttribute("payPalCheckoutToken"); if (UtilValidate.isNotEmpty(token)) { encoder.add("TOKEN", token); } encoder.add("RETURNURL", payPalConfig.getString("returnUrl")); encoder.add("CANCELURL", payPalConfig.getString("cancelReturnUrl")); if (!cart.shippingApplies()) { encoder.add("NOSHIPPING", "1"); } else { encoder.add("CALLBACK", payPalConfig.getString("shippingCallbackUrl")); encoder.add("CALLBACKTIMEOUT", "6"); // Default to no String reqConfirmShipping = "Y".equals(payPalConfig.getString("requireConfirmedShipping")) ? "1" : "0"; encoder.add("REQCONFIRMSHIPPING", reqConfirmShipping); // Default shipment method encoder.add("L_SHIPPINGOPTIONISDEFAULT0", "true"); encoder.add("L_SHIPPINGOPTIONNAME0", "Calculated Offline"); encoder.add("L_SHIPPINGOPTIONAMOUNT0", "0.00"); } encoder.add("ALLOWNOTE", "1"); encoder.add("INSURANCEOPTIONOFFERED", "false"); if (UtilValidate.isNotEmpty(payPalConfig.getString("imageUrl"))) ; encoder.add("PAYMENTACTION", "Order"); // Cart information try { addCartDetails(encoder, cart); } catch (GenericEntityException e) { Debug.logError(e, module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "AccountingPayPalErrorDuringRetrievingCartDetails", locale)); } NVPDecoder decoder; try { decoder = sendNVPRequest(payPalConfig, encoder); } catch (PayPalException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } Map<String, String> errorMessages = getErrorMessageMap(decoder); if (UtilValidate.isNotEmpty(errorMessages)) { if (errorMessages.containsKey("10411")) { // Token has expired, get a new one cart.setAttribute("payPalCheckoutToken", null); return PayPalServices.setExpressCheckout(dctx, context); } return ServiceUtil.returnError(UtilMisc.toList(errorMessages.values())); } token = decoder.get("TOKEN"); cart.setAttribute("payPalCheckoutToken", token); TokenWrapper tokenWrapper = new TokenWrapper(token); cart.setAttribute("payPalCheckoutTokenObj", tokenWrapper); PayPalServices.tokenCartMap.put(tokenWrapper, new WeakReference<ShoppingCart>(cart)); return ServiceUtil.returnSuccess(); }