svn commit: r538970 - in /ofbiz/trunk/applications/order: servicedef/ src/org/ofbiz/order/order/ src/org/ofbiz/order/shoppingcart/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r538970 - in /ofbiz/trunk/applications/order: servicedef/ src/org/ofbiz/order/order/ src/org/ofbiz/order/shoppingcart/

jacopoc
Author: jacopoc
Date: Thu May 17 08:33:14 2007
New Revision: 538970

URL: http://svn.apache.org/viewvc?view=rev&rev=538970
Log:
Pretty important refactoring of the processOrderPayments service (and related methods): the service (triggered by ecas on order edit) doesn't load a cart from the order anymore; this is possible because there is now a (static) CheckOutHelper.processPayment method that doesn't rely on the cart object.
There is still huge room for improvements in this area, but this is a first step.

Modified:
    ofbiz/trunk/applications/order/servicedef/services.xml
    ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java
    ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/CheckOutEvents.java
    ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/CheckOutHelper.java
    ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java

Modified: ofbiz/trunk/applications/order/servicedef/services.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/servicedef/services.xml?view=diff&rev=538970&r1=538969&r2=538970
==============================================================================
--- ofbiz/trunk/applications/order/servicedef/services.xml (original)
+++ ofbiz/trunk/applications/order/servicedef/services.xml Thu May 17 08:33:14 2007
@@ -318,7 +318,6 @@
     <service name="processOrderPayments" engine="java" auth="true"
             location="org.ofbiz.order.order.OrderServices" invoke="processOrderPayments">
         <description>Process payments for an order</description>
-        <attribute name="shoppingCart" type="org.ofbiz.order.shoppingcart.ShoppingCart" mode="IN" optional="true"/>
         <attribute name="orderId" type="String" mode="IN" optional="false"/>
     </service>
 

Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java?view=diff&rev=538970&r1=538969&r2=538970
==============================================================================
--- ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java (original)
+++ ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java Thu May 17 08:33:14 2007
@@ -3417,37 +3417,19 @@
         LocalDispatcher dispatcher = dctx.getDispatcher();
         GenericDelegator delegator = dctx.getDelegator();
         GenericValue userLogin = (GenericValue) context.get("userLogin");
-        ShoppingCart cart = (ShoppingCart) context.get("shoppingCart");
         String orderId = (String) context.get("orderId");
 
         OrderReadHelper orh = new OrderReadHelper(delegator, orderId);
         String productStoreId = orh.getProductStoreId();
 
-        if (cart == null) {
-            // load the order into a shopping cart
-            Map loadCartResp = null;
-            try {
-                loadCartResp = dispatcher.runSync("loadCartFromOrder", UtilMisc.toMap("orderId", orderId, "skipInventoryChecks", Boolean.TRUE, "skipProductChecks", Boolean.TRUE, "userLogin", userLogin));
-            } catch (GenericServiceException e) {
-                return ServiceUtil.returnError(e.getMessage());
-            }
-            cart = (ShoppingCart) loadCartResp.get("shoppingCart");
-
-            if (cart == null) {
-                return ServiceUtil.returnError("ERROR: Null shopping cart object returned!");
-            }
-            
-            cart.setOrderId(orderId);
-        }
-        CheckOutHelper coh = new CheckOutHelper(dispatcher, delegator, cart);
         // process the payments
-        if (!"PURCHASE_ORDER".equals(cart.getOrderType())) {
+        if (!"PURCHASE_ORDER".equals(orh.getOrderTypeId())) {
             GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator);
             Map paymentResp = null;
             try {
                 Debug.log("Calling process payments...", module);
                 //Debug.set(Debug.VERBOSE, true);
-                paymentResp = coh.processPayment(productStore, userLogin);
+                paymentResp = CheckOutHelper.processPayment(orderId, orh.getOrderGrandTotal(), orh.getCurrency(), productStore, userLogin, false, false, dispatcher, delegator);
                 //Debug.set(Debug.VERBOSE, false);
             } catch (GeneralException e) {
                 Debug.logError(e, module);

Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/CheckOutEvents.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/CheckOutEvents.java?view=diff&rev=538970&r1=538969&r2=538970
==============================================================================
--- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/CheckOutEvents.java (original)
+++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/CheckOutEvents.java Thu May 17 08:33:14 2007
@@ -528,6 +528,13 @@
         GenericValue productStore = ProductStoreWorker.getProductStore(cart.getProductStoreId(), delegator);
         Map callResult = checkOutHelper.processPayment(productStore, userLogin, false, holdOrder);
 
+        if (ServiceUtil.isError(callResult)) {
+            // clear out the rejected payment methods (if any) from the cart, so they don't get re-authorized
+            cart.clearDeclinedPaymentMethods(delegator);
+            // null out the orderId for next pass
+            cart.setOrderId(null);
+        }
+
         // generate any messages required
         ServiceUtil.getMessages(request, callResult, null);
 

Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/CheckOutHelper.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/CheckOutHelper.java?view=diff&rev=538970&r1=538969&r2=538970
==============================================================================
--- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/CheckOutHelper.java (original)
+++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/CheckOutHelper.java Thu May 17 08:33:14 2007
@@ -821,14 +821,18 @@
     }
 
     public Map processPayment(GenericValue productStore, GenericValue userLogin) throws GeneralException {
-        return processPayment(productStore, userLogin, false, false);
+        return CheckOutHelper.processPayment(this.cart.getOrderId(), this.cart.getGrandTotal(), this.cart.getCurrency(), productStore, userLogin, false, false, dispatcher, delegator);
     }
 
     public Map processPayment(GenericValue productStore, GenericValue userLogin, boolean faceToFace) throws GeneralException {
-        return processPayment(productStore, userLogin, faceToFace, false);
+        return CheckOutHelper.processPayment(this.cart.getOrderId(), this.cart.getGrandTotal(), this.cart.getCurrency(), productStore, userLogin, faceToFace, false, dispatcher, delegator);
     }
 
     public Map processPayment(GenericValue productStore, GenericValue userLogin, boolean faceToFace, boolean manualHold) throws GeneralException {
+        return CheckOutHelper.processPayment(this.cart.getOrderId(), this.cart.getGrandTotal(), this.cart.getCurrency(), productStore, userLogin, faceToFace, manualHold, dispatcher, delegator);
+    }
+
+    public static Map processPayment(String orderId, double orderTotal, String currencyUomId, GenericValue productStore, GenericValue userLogin, boolean faceToFace, boolean manualHold, LocalDispatcher dispatcher, GenericDelegator delegator) throws GeneralException {
         // Get some payment related strings
         String DECLINE_MESSAGE = productStore.getString("authDeclinedMessage");
         String ERROR_MESSAGE = productStore.getString("authErrorMessage");
@@ -837,15 +841,11 @@
             RETRY_ON_ERROR = "Y";
         }
 
-        // Get the orderId/total from the cart.
-        double orderTotal = this.cart.getGrandTotal();
-        String orderId = this.cart.getOrderId();
-
         // Check the payment preferences; if we have ANY w/ status PAYMENT_NOT_AUTH invoke payment service.
         boolean requireAuth = false;
         List allPaymentPreferences = null;
         try {
-            allPaymentPreferences = this.delegator.findByAnd("OrderPaymentPreference", UtilMisc.toMap("orderId", orderId));
+            allPaymentPreferences = delegator.findByAnd("OrderPaymentPreference", UtilMisc.toMap("orderId", orderId));
         } catch (GenericEntityException e) {
             throw new GeneralException("Problems getting payment preferences", e);
         }
@@ -870,7 +870,7 @@
                 authCtx.put("authRefNum", opp.getString("manualRefNum"));
                 authCtx.put("authResult", Boolean.TRUE);
                 authCtx.put("userLogin", userLogin);
-                authCtx.put("currencyUomId", cart.getCurrency());
+                authCtx.put("currencyUomId", currencyUomId);
 
                 Map authResp = dispatcher.runSync("processAuthResult", authCtx);
                 if (authResp != null && ServiceUtil.isError(authResp)) {
@@ -891,7 +891,7 @@
                     captCtx.put("captureAmount", opp.getDouble("maxAmount"));
                     captCtx.put("captureRefNum", opp.getString("manualRefNum"));
                     captCtx.put("userLogin", userLogin);
-                    captCtx.put("currencyUomId", cart.getCurrency());
+                    captCtx.put("currencyUomId", currencyUomId);
 
                     Map capResp = dispatcher.runSync("processCaptureResult", captCtx);
                     if (capResp != null && ServiceUtil.isError(capResp)) {
@@ -950,12 +950,6 @@
                     if (!ok) {
                         throw new GeneralException("Problem with order change; see above error");
                     }
-
-                    // clear out the rejected payment methods from the cart, so they don't get re-authorized
-                    cart.clearDeclinedPaymentMethodsFromOrder(delegator, orderId);
-
-                    // null out the orderId for next pass.
-                    cart.setOrderId(null);
                     if (messages == null || messages.size() == 0) {
                         return ServiceUtil.returnError(DECLINE_MESSAGE);
                     } else {
@@ -983,8 +977,6 @@
                         if (!ok) {
                             throw new GeneralException("Problem with order change; see above error");
                         }
-                        // null out orderId for next pass
-                        this.cart.setOrderId(null);
                         if (messages == null || messages.size() == 0) {
                             return ServiceUtil.returnError(ERROR_MESSAGE);
                         } else {
@@ -993,7 +985,7 @@
                     }
                 } else {
                     // should never happen
-                    return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderPleaseContactCustomerService;PaymentReturnCodeUnknown.", (cart != null ? cart.getLocale() : Locale.getDefault())));
+                    return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderPleaseContactCustomerService;PaymentReturnCodeUnknown.", Locale.getDefault()));
                 }
             } else {
                 // result returned null == service failed
@@ -1006,48 +998,23 @@
                     if (!ok) {
                         throw new GeneralException("Problem with order change; see above error");
                     }
-                    // null out orderId for next pass
-                    this.cart.setOrderId(null);
                     return ServiceUtil.returnError(ERROR_MESSAGE);
                 }
             }
         } else {
             // Get the paymentMethodTypeIds - this will need to change when ecom supports multiple payments
-            List paymentMethodTypeIds = this.cart.getPaymentMethodTypeIds();
-            if (paymentMethodTypeIds.contains("CASH") || paymentMethodTypeIds.contains("EXT_COD") || paymentMethodTypeIds.contains("EXT_BILLACT")) {
-                boolean hasOther = false;
-                // TODO: this is set but not checked anywhere
-                boolean validAmount = false;
-
-                Iterator pmti = paymentMethodTypeIds.iterator();
-                while (pmti.hasNext()) {
-                    String type = (String) pmti.next();
-                    if (!"CASH".equals(type) && !"EXT_COD".equals(type) && !"EXT_BILLACT".equals(type)) {
-                        hasOther = true;
-                        break;
-                    }
-                }
-
-                if (!hasOther) {
-                    if (!paymentMethodTypeIds.contains("CASH") && !paymentMethodTypeIds.contains("EXT_COD")) {
-                        // only billing account, make sure we have enough to cover
-                        String billingAccountId = cart.getBillingAccountId();
-                        double billAcctCredit = this.availableAccountBalance(billingAccountId);
-                        double billingAcctAmt = cart.getBillingAccountAmount();
-                        if (billAcctCredit >= billingAcctAmt) {
-                            if (cart.getGrandTotal() > billAcctCredit) {
-                                validAmount = false;
-                            } else {
-                                validAmount = true;
-                            }
-                        }
-                    }
-
-                    // approve this as long as there are only COD and Billing Account types
-                    boolean ok = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId, manualHold);
-                    if (!ok) {
-                        throw new GeneralException("Problem with order change; see above error");
-                    }
+            List cashCodBaExpr = UtilMisc.toList(new EntityExpr("paymentMethodTypeId", EntityOperator.EQUALS, "CASH"),
+                                           new EntityExpr("paymentMethodTypeId", EntityOperator.EQUALS, "EXT_COD"),
+                                           new EntityExpr("paymentMethodTypeId", EntityOperator.EQUALS, "EXT_BILLACT"));
+            List cashCodBaPaymentPreferences = EntityUtil.filterByAnd(allPaymentPreferences, cashCodBaExpr);
+
+            if (UtilValidate.isNotEmpty(cashCodBaPaymentPreferences) &&
+                    UtilValidate.isNotEmpty(allPaymentPreferences) &&
+                    cashCodBaPaymentPreferences.size() == allPaymentPreferences.size()) {
+                // approve this as long as there are only CASH, COD and Billing Account types
+                boolean ok = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId, manualHold);
+                if (!ok) {
+                    throw new GeneralException("Problem with order change; see above error");
                 }
             } else {
                 // There is nothing to do, we just treat this as a success
@@ -1057,7 +1024,7 @@
         // check to see if we should auto-invoice/bill
         if (faceToFace) {
             Debug.log("Face-To-Face Sale - " + orderId, module);
-            this.adjustFaceToFacePayment(allPaymentPreferences, userLogin);
+            CheckOutHelper.adjustFaceToFacePayment(orderId, orderTotal, allPaymentPreferences, userLogin, delegator);
             boolean ok = OrderChangeHelper.completeOrder(dispatcher, userLogin, orderId);
             Debug.log("Complete Order Result - " + ok, module);
             if (!ok) {
@@ -1067,11 +1034,10 @@
         return ServiceUtil.returnSuccess();
     }
 
-    public void adjustFaceToFacePayment(List allPaymentPrefs, GenericValue userLogin) throws GeneralException {
+    public static void adjustFaceToFacePayment(String orderId, double cartTotal, List allPaymentPrefs, GenericValue userLogin, GenericDelegator delegator) throws GeneralException {
         String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00");
         DecimalFormat formatter = new DecimalFormat(currencyFormat);
 
-        double cartTotal = this.cart.getGrandTotal();
         String grandTotalString = formatter.format(cartTotal);
         Double grandTotal = null;
         try {
@@ -1113,7 +1079,7 @@
             }
             GenericValue newPref = delegator.makeValue("OrderPaymentPreference", null);
             newPref.set("orderPaymentPreferenceId", delegator.getNextSeqId("OrderPaymentPreference"));
-            newPref.set("orderId", cart.getOrderId());
+            newPref.set("orderId", orderId);
             newPref.set("paymentMethodTypeId", "CASH");
             newPref.set("statusId", "PAYMENT_RECEIVED");
             newPref.set("maxAmount", change);

Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java?view=diff&rev=538970&r1=538969&r2=538970
==============================================================================
--- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java (original)
+++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java Thu May 17 08:33:14 2007
@@ -1561,20 +1561,23 @@
     }
 
     /** remove declined payment methods for an order from cart.  The idea is to call this after an attempted order is rejected */
-    public void clearDeclinedPaymentMethodsFromOrder(GenericDelegator delegator, String orderId) {
-        try {
-            List declinedPaymentMethods = delegator.findByAnd("OrderPaymentPreference", UtilMisc.toMap("orderId", orderId, "statusId", "PAYMENT_DECLINED"));
-            if (!UtilValidate.isEmpty(declinedPaymentMethods)) {
-                List paymentMethodIdsToRemove = new ArrayList();
-                for (Iterator iter = declinedPaymentMethods.iterator(); iter.hasNext(); ) {
-                    GenericValue opp = (GenericValue) iter.next();
-                    paymentMethodIdsToRemove.add(opp.getString("paymentMethodId"));
+    public void clearDeclinedPaymentMethods(GenericDelegator delegator) {
+        String orderId = this.getOrderId();
+        if (UtilValidate.isNotEmpty(orderId)) {
+            try {
+                List declinedPaymentMethods = delegator.findByAnd("OrderPaymentPreference", UtilMisc.toMap("orderId", orderId, "statusId", "PAYMENT_DECLINED"));
+                if (!UtilValidate.isEmpty(declinedPaymentMethods)) {
+                    List paymentMethodIdsToRemove = new ArrayList();
+                    for (Iterator iter = declinedPaymentMethods.iterator(); iter.hasNext(); ) {
+                        GenericValue opp = (GenericValue) iter.next();
+                        paymentMethodIdsToRemove.add(opp.getString("paymentMethodId"));
+                    }
+                    clearPaymentMethodsById(paymentMethodIdsToRemove);
                 }
-                clearPaymentMethodsById(paymentMethodIdsToRemove);
+            } catch (GenericEntityException ex) {
+                Debug.logError("Unable to remove declined payment methods from cart due to " + ex.getMessage(), module);
+                return;
             }
-        } catch (GenericEntityException ex) {
-            Debug.logError("Unable to remove declined payment methods from cart due to " + ex.getMessage(), module);
-            return;
         }
     }