📄 checkouthelper.java
字号:
if (RETRY_ON_ERROR == null) { RETRY_ON_ERROR = "Y"; } // Get the orderId/total from the cart. double orderTotal = this.cart.getGrandTotal(); String orderId = this.cart.getOrderId(); // Get the paymentMethodTypeIds - this will need to change when ecom supports multiple payments List paymentMethodTypeIds = this.cart.getPaymentMethodTypeIds(); // 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)); } catch (GenericEntityException e) { throw new GeneralException("Problems getting payment preferences", e); } // filter out cancelled preferences List canExpr = UtilMisc.toList(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "PAYMENT_CANCELLED")); allPaymentPreferences = EntityUtil.filterByAnd(allPaymentPreferences, canExpr); // check for online payment methods or in-hand payment types with verbal or external refs List exprs = UtilMisc.toList(new EntityExpr("manualRefNum", EntityOperator.NOT_EQUAL, null)); List manualRefPaymentPrefs = EntityUtil.filterByAnd(allPaymentPreferences, exprs); if (manualRefPaymentPrefs != null && manualRefPaymentPrefs.size() > 0) { Iterator i = manualRefPaymentPrefs.iterator(); while (i.hasNext()) { GenericValue opp = (GenericValue) i.next(); Map authCtx = new HashMap(); authCtx.put("orderPaymentPreference", opp); if (opp.get("paymentMethodId") == null) { authCtx.put("serviceTypeEnum", "PRDS_PAY_EXTERNAL"); } authCtx.put("processAmount", opp.getDouble("maxAmount")); authCtx.put("authRefNum", opp.getString("manualRefNum")); authCtx.put("authResult", Boolean.TRUE); authCtx.put("userLogin", userLogin); authCtx.put("currencyUomId", cart.getCurrency()); Map authResp = dispatcher.runSync("processAuthResult", authCtx); if (authResp != null && ServiceUtil.isError(authResp)) { throw new GeneralException(ServiceUtil.getErrorMessage(authResp)); } // approve the order OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId, manualHold); if ("Y".equalsIgnoreCase(productStore.getString("manualAuthIsCapture"))) { Map captCtx = new HashMap(); captCtx.put("orderPaymentPreference", opp); if (opp.get("paymentMethodId") == null) { captCtx.put("serviceTypeEnum", "PRDS_PAY_EXTERNAL"); } captCtx.put("payToPartyId", productStore.get("payToPartyId")); captCtx.put("captureResult", Boolean.TRUE); captCtx.put("captureAmount", opp.getDouble("maxAmount")); captCtx.put("captureRefNum", opp.getString("manualRefNum")); captCtx.put("userLogin", userLogin); captCtx.put("currencyUomId", cart.getCurrency()); Map capResp = dispatcher.runSync("processCaptureResult", captCtx); if (capResp != null && ServiceUtil.isError(capResp)) { throw new GeneralException(ServiceUtil.getErrorMessage(capResp)); } } } } // check for online payment methods needing authorization Map paymentFields = UtilMisc.toMap("statusId", "PAYMENT_NOT_AUTH"); List onlinePaymentPrefs = EntityUtil.filterByAnd(allPaymentPreferences, paymentFields); if (onlinePaymentPrefs != null && onlinePaymentPrefs.size() > 0) { requireAuth = true; } // Invoke payment processing. if (requireAuth) { if (orderTotal == 0) { // if there is nothing to authorize; don't bother boolean ok = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId, manualHold); if (!ok) { throw new GeneralException("Problem with order change; see above error"); } } // now there should be something to authorize; go ahead Map paymentResult = null; try { // invoke the payment gateway service. paymentResult = dispatcher.runSync("authOrderPayments", UtilMisc.toMap("orderId", orderId, "userLogin", userLogin), 180, true); } catch (GenericServiceException e) { Debug.logWarning(e, module); throw new GeneralException("Error in authOrderPayments service: " + e.toString(), e.getNested()); } if (Debug.verboseOn()) Debug.logVerbose("Finsished w/ Payment Service", module); if (paymentResult != null && ServiceUtil.isError(paymentResult)) { throw new GeneralException(ServiceUtil.getErrorMessage(paymentResult)); } // grab the customer messages -- only passed back in the case of an error or failure List messages = (List) paymentResult.get("authResultMsgs"); if (paymentResult != null && paymentResult.containsKey("processResult")) { String authResp = (String) paymentResult.get("processResult"); if (authResp.equals("FAILED")) { // order was NOT approved if (Debug.verboseOn()) Debug.logVerbose("Payment auth was NOT a success!", module); boolean ok = OrderChangeHelper.rejectOrder(dispatcher, userLogin, orderId); if (!ok) { throw new GeneralException("Problem with order change; see above error"); } // null out the orderId for next pass. cart.setOrderId(null); if (messages == null || messages.size() == 0) { return ServiceUtil.returnError(DECLINE_MESSAGE); } else { return ServiceUtil.returnError(messages); } } else if (authResp.equals("APPROVED")) { // order WAS approved if (Debug.verboseOn()) Debug.logVerbose("Payment auth was a success!", module); // set the order and item status to approved boolean ok = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId, manualHold); if (!ok) { throw new GeneralException("Problem with order change; see above error"); } } else if (authResp.equals("ERROR")) { // service failed if (Debug.verboseOn()) Debug.logVerbose("Payment auth failed due to processor trouble.", module); if (!faceToFace && "Y".equalsIgnoreCase(RETRY_ON_ERROR)) { // never do this for a face to face purchase regardless of store setting return ServiceUtil.returnSuccess(ERROR_MESSAGE); } else { boolean ok = OrderChangeHelper.cancelOrder(dispatcher, userLogin, orderId); 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 { return ServiceUtil.returnError(messages); } } } else { // should never happen return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderPleaseContactCustomerService;PaymentReturnCodeUnknown.", (cart != null ? cart.getLocale() : Locale.getDefault()))); } } else { // result returned null == service failed if (Debug.verboseOn()) Debug.logVerbose("Payment auth failed due to processor trouble.", module); if (!faceToFace && "Y".equalsIgnoreCase(RETRY_ON_ERROR)) { // never do this for a face to face purchase regardless of store setting return ServiceUtil.returnSuccess(ERROR_MESSAGE); } else { boolean ok = OrderChangeHelper.cancelOrder(dispatcher, userLogin, orderId); 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 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"); } } } else { // There is nothing to do, we just treat this as a success } // check to see if we should auto-invoice/bill if (faceToFace) { Debug.log("Face-To-Face Sale - " + orderId, module); this.adjustFaceToFacePayment(allPaymentPreferences, userLogin); boolean ok = OrderChangeHelper.completeOrder(dispatcher, userLogin, orderId); Debug.log("Complete Order Result - " + ok, module); if (!ok) { throw new GeneralException("Problem with order change; see error logs"); } } return ServiceUtil.returnSuccess(); } public void adjustFaceToFacePayment(List allPaymentPrefs, GenericValue userLogin) 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 { grandTotal = new Double(formatter.parse(grandTotalString).doubleValue()); } catch (ParseException e) { throw new GeneralException("Problem getting parsed currency amount from DecimalFormat", e); } double prefTotal = 0.00; if (allPaymentPrefs != null) { Iterator i = allPaymentPrefs.iterator(); while (i.hasNext()) { GenericValue pref = (GenericValue) i.next(); Double maxAmount = pref.getDouble("maxAmount"); if (maxAmount == null) maxAmount = new Double(0.00); prefTotal += maxAmount.doubleValue(); } } String payTotalString = formatter.format(prefTotal); Double payTotal = null; try { payTotal = new Double(formatter.parse(payTotalString).doubleValue()); } catch (ParseException e) { throw new GeneralException("Problem getting parsed currency amount from DecimalFormat", e); } if (grandTotal == null) grandTotal = new Double(0.00); if (payTotal == null) payTotal = new Double(0.00); if (payTotal.doubleValue() > grandTotal.doubleValue()) { double diff = (payTotal.doubleValue() - grandTotal.doubleValue()) * -1; String diffString = formatter.format(diff); Double change = null; try { change = new Double(formatter.parse(diffString).doubleValue()); } catch (ParseException e) { throw new GeneralException("Problem getting parsed currency amount from DecimalFormat", e);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -