Author: sichen
Date: Mon Nov 13 17:48:07 2006 New Revision: 474623 URL: http://svn.apache.org/viewvc?view=rev&rev=474623 Log: OFBIZ-442: Modify createInvoicesFromShipment so that additional shipping charges are authorized and captured along with rest of order amount. Refactored and created new service to release an individual order payment preference. Thanks to Chris Liberty. Modified: incubator/ofbiz/trunk/applications/accounting/config/AccountingUiLabels.properties incubator/ofbiz/trunk/applications/accounting/servicedef/services_paymentmethod.xml incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/PaymentGatewayServices.java Modified: incubator/ofbiz/trunk/applications/accounting/config/AccountingUiLabels.properties URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/accounting/config/AccountingUiLabels.properties?view=diff&rev=474623&r1=474622&r2=474623 ============================================================================== --- incubator/ofbiz/trunk/applications/accounting/config/AccountingUiLabels.properties (original) +++ incubator/ofbiz/trunk/applications/accounting/config/AccountingUiLabels.properties Mon Nov 13 17:48:07 2006 @@ -610,6 +610,7 @@ AccountingProblemStoringOrderAdjustments=Problem storing OrderAdjustments: ${orderAdjustments} AccountingErrorCreatingOrderAdjustmentBillingFromOrder=Error creating OrderAdjustmentBilling from order AccountingTroubleCallingCalculateInvoicedAdjustmentTotalService=Accounting trouble calling calculateInvoicedAdjustmentTotal service +AccountingTroubleCallingReleaseOrderPaymentPreferenceService=Trouble calling releaseOrderPaymentPreference service #Potentional Common definitions FormFieldTitle_emailAddressFrom=From Emailadress Modified: incubator/ofbiz/trunk/applications/accounting/servicedef/services_paymentmethod.xml URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/accounting/servicedef/services_paymentmethod.xml?view=diff&rev=474623&r1=474622&r2=474623 ============================================================================== --- incubator/ofbiz/trunk/applications/accounting/servicedef/services_paymentmethod.xml (original) +++ incubator/ofbiz/trunk/applications/accounting/servicedef/services_paymentmethod.xml Mon Nov 13 17:48:07 2006 @@ -248,6 +248,12 @@ <attribute name="processResult" type="String" mode="OUT" optional="false"/> </service> + <service name="releaseOrderPaymentPreference" engine="java" + location="org.ofbiz.accounting.payment.PaymentGatewayServices" invoke="releaseOrderPaymentPreference" auth="true"> + <description>Releases payment authorization for a single OrderPaymentPreference</description> + <attribute name="orderPaymentPreferenceId" type="String" mode="IN" optional="false"/> + </service> + <service name="capturePaymentsByInvoice" engine="java" location="org.ofbiz.accounting.payment.PaymentGatewayServices" invoke="capturePaymentsByInvoice" auth="true"> <description>Captures (settles) pre-authorized order payments by invoice</description> Modified: incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java?view=diff&rev=474623&r1=474622&r2=474623 ============================================================================== --- incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java (original) +++ incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java Mon Nov 13 17:48:07 2006 @@ -31,6 +31,7 @@ import org.ofbiz.accounting.payment.BillingAccountWorker; import org.ofbiz.accounting.payment.PaymentWorker; +import org.ofbiz.accounting.payment.PaymentGatewayServices; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilFormatOut; @@ -1269,7 +1270,7 @@ // If part of the order was paid via credit card, try to charge it for the additional shipping List orderPaymentPreferences = new ArrayList(); try { - orderPaymentPreferences = delegator.findByAnd("OrderPaymentPreference", UtilMisc.toMap("orderId", orderId)); + orderPaymentPreferences = delegator.findByAnd("OrderPaymentPreference", UtilMisc.toMap("orderId", orderId, "paymentMethodTypeId", "CREDIT_CARD")); } catch( GenericEntityException e ) { String errMsg = UtilProperties.getMessage(resource, "AccountingProblemGettingOrderPaymentPreferences", locale); Debug.logError(e, errMsg, module); @@ -1278,18 +1279,46 @@ // Use the first credit card we find, for the sake of simplicity String paymentMethodId = null; - Iterator oppit = orderPaymentPreferences.iterator(); - while (oppit.hasNext()) { - GenericValue orderPaymentPreference = (GenericValue) oppit.next(); - if (orderPaymentPreference.getString("paymentMethodTypeId").equals("CREDIT_CARD")) { - paymentMethodId = orderPaymentPreference.getString("paymentMethodId"); - break; - } + GenericValue cardOrderPaymentPref = EntityUtil.getFirst(orderPaymentPreferences); + if (cardOrderPaymentPref != null) { + paymentMethodId = cardOrderPaymentPref.getString("paymentMethodId"); } if (paymentMethodId != null ) { + + // Release all outstanding (not settled or cancelled) authorizations, while keeping a running + // total of their amounts so that the total plus the additional shipping charges can be authorized again + // all at once. + BigDecimal totalNewAuthAmount = new BigDecimal(totalAdditionalShippingCharges.doubleValue()).setScale(decimals, rounding); + Iterator oppit = orderPaymentPreferences.iterator(); + while (oppit.hasNext()) { + GenericValue orderPaymentPreference = (GenericValue) oppit.next(); + if (! (orderPaymentPreference.getString("statusId").equals("PAYMENT_SETTLED") || orderPaymentPreference.getString("statusId").equals("PAYMENT_CANCELLED"))) { + GenericValue authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference); + if (authTransaction != null && authTransaction.get("amount") != null) { + + // Update the total authorized amount + totalNewAuthAmount = totalNewAuthAmount.add(authTransaction.getBigDecimal("amount").setScale(decimals, rounding)); + + // Release the authorization for the OrderPaymentPreference + Map prefReleaseResult = null; + try { + prefReleaseResult = dispatcher.runSync("releaseOrderPaymentPreference", UtilMisc.toMap("orderPaymentPreferenceId", orderPaymentPreference.getString("orderPaymentPreferenceId"), "userLogin", context.get("userLogin"))); + } catch( GenericServiceException e ) { + String errMsg = UtilProperties.getMessage(resource, "AccountingTroubleCallingReleaseOrderPaymentPreferenceService", locale); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + if (ServiceUtil.isError(prefReleaseResult) || ServiceUtil.isFailure(prefReleaseResult)) { + String errMsg = ServiceUtil.getErrorMessage(prefReleaseResult); + Debug.logError(errMsg, module); + return ServiceUtil.returnError(errMsg); + } + } + } + } - // Create a new OrderPaymentPreference for the order to handle the additional charge. Don't + // Create a new OrderPaymentPreference for the order to handle the new (totalled) charge. Don't // set the maxAmount so that it doesn't interfere with other authorizations Map serviceContext = UtilMisc.toMap("orderId", orderId, "paymentMethodId", paymentMethodId, "paymentMethodTypeId", "CREDIT_CARD", "userLogin", context.get("userLogin")); String orderPaymentPreferenceId = null; @@ -1307,7 +1336,7 @@ try { // Use an overrideAmount because the maxAmount wasn't set on the OrderPaymentPreference - authResult = dispatcher.runSync("authOrderPaymentPreference", UtilMisc.toMap("orderPaymentPreferenceId", orderPaymentPreferenceId, "overrideAmount", new Double(totalAdditionalShippingCharges.doubleValue()), "userLogin", context.get("userLogin"))); + authResult = dispatcher.runSync("authOrderPaymentPreference", UtilMisc.toMap("orderPaymentPreferenceId", orderPaymentPreferenceId, "overrideAmount", new Double(totalNewAuthAmount.doubleValue()), "userLogin", context.get("userLogin"))); } catch (GenericServiceException e) { String errMsg = UtilProperties.getMessage(resource, "AccountingTroubleCallingAuthOrderPaymentPreferenceService", locale); Debug.logError(e, errMsg, module); Modified: incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/PaymentGatewayServices.java URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/PaymentGatewayServices.java?view=diff&rev=474623&r1=474622&r2=474623 ============================================================================== --- incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/PaymentGatewayServices.java (original) +++ incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/PaymentGatewayServices.java Mon Nov 13 17:48:07 2006 @@ -657,13 +657,10 @@ Map result = new HashMap(); - // get the order header and payment preferences - GenericValue orderHeader = null; + // get the payment preferences List paymentPrefs = null; try { - // first get the order header - orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId)); // get the valid payment prefs List othExpr = UtilMisc.toList(new EntityExpr("paymentMethodTypeId", EntityOperator.EQUALS, "EFT_ACCOUNT")); othExpr.add(new EntityExpr("paymentMethodTypeId", EntityOperator.EQUALS, "CREDIT_CARD")); @@ -687,11 +684,6 @@ return result; } - // error if no order was found - if (orderHeader == null) { - return ServiceUtil.returnError("Could not find OrderHeader with orderId: " + orderId + "; not processing payments."); - } - // return complete if no payment prefs were found if (paymentPrefs == null || paymentPrefs.size() == 0) { Debug.logWarning("No OrderPaymentPreference records available for release", module); @@ -700,144 +692,215 @@ return result; } - OrderReadHelper orh = new OrderReadHelper(orderHeader); - String currency = orh.getCurrency(); - // iterate over the prefs and release each one List finished = new ArrayList(); Iterator payments = paymentPrefs.iterator(); while (payments.hasNext()) { GenericValue paymentPref = (GenericValue) payments.next(); - - // look up the payment configuration settings - String serviceName = null; - String paymentConfig = null; - - // get the payment settings i.e. serviceName and config properties file name - GenericValue paymentSettings = getPaymentSettings(orh.getOrderHeader(), paymentPref, RELEASE_SERVICE_TYPE, false); - if (paymentSettings != null) { - paymentConfig = paymentSettings.getString("paymentPropertiesPath"); - serviceName = paymentSettings.getString("paymentService"); - if (serviceName == null) { - Debug.logWarning("No payment release service for - " + paymentPref.getString("paymentMethodTypeId"), module); - continue; // no release service available -- has been logged - } - } else { - Debug.logWarning("No payment release settings found for - " + paymentPref.getString("paymentMethodTypeId"), module); - continue; // no release service available -- has been logged + Map releaseContext = UtilMisc.toMap("userLogin", userLogin, "orderPaymentPreferenceId", paymentPref.getString("orderPaymentPreferenceId")); + Map releaseResult = null; + try { + releaseResult = dispatcher.runSync("releaseOrderPaymentPreference", releaseContext); + } catch( GenericServiceException e ) { + String errMsg = "Problem calling releaseOrderPaymentPreference service for orderPaymentPreferenceId" + paymentPref.getString("orderPaymentPreferenceId"); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + if (ServiceUtil.isError(releaseResult)) { + Debug.logError(ServiceUtil.getErrorMessage(releaseResult), module); + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(releaseResult)); + } else if (! ServiceUtil.isFailure(releaseResult)) { + finished.add(paymentPref); } + } + result = ServiceUtil.returnSuccess(); + if (finished.size() == paymentPrefs.size()) { + result.put("processResult", "COMPLETE"); + } else { + result.put("processResult", "FAILED"); + } - if (paymentConfig == null || paymentConfig.length() == 0) { - paymentConfig = "payment.properties"; + return result; + } + + /** + * + * Releases authorization for a single OrderPaymentPreference through service calls to the defined processing service for the ProductStore/PaymentMethodType + * @return SUCCESS|FAILED|ERROR for complete processing of payment. + */ + public static Map releaseOrderPaymentPreference(DispatchContext dctx, Map context) { + GenericDelegator delegator = dctx.getDelegator(); + LocalDispatcher dispatcher = dctx.getDispatcher(); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + String orderPaymentPreferenceId = (String) context.get("orderPaymentPreferenceId"); + + Map result = ServiceUtil.returnSuccess(); + + // Get the OrderPaymentPreference + GenericValue paymentPref = null; + try { + paymentPref = delegator.findByPrimaryKey("OrderPaymentPreference", UtilMisc.toMap("orderPaymentPreferenceId", orderPaymentPreferenceId)); + } catch( GenericEntityException e ) { + String errMsg = "Problem getting OrderPaymentPreference for orderPaymentPreferenceId " + orderPaymentPreferenceId; + Debug.logWarning(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + + // Error if no OrderPaymentPreference was found + if (paymentPref == null) { + String errMsg = "Could not find OrderPaymentPreference with orderPaymentPreferenceId: " + orderPaymentPreferenceId; + Debug.logWarning(errMsg, module); + return ServiceUtil.returnError(errMsg); + } + + // Get the OrderHeader + GenericValue orderHeader = null; + try { + orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", paymentPref.getString("orderId"))); + } catch( GenericEntityException e ) { + String errMsg = "Problem getting OrderHeader for orderId " + paymentPref.getString("orderId"); + Debug.logWarning(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + + // Error if no OrderHeader was found + if (orderHeader == null) { + String errMsg = "Could not find OrderHeader with orderId: " + paymentPref.getString("orderId") + "; not processing payments."; + Debug.logWarning(errMsg, module); + return ServiceUtil.returnError(errMsg); + } + + OrderReadHelper orh = new OrderReadHelper(orderHeader); + String currency = orh.getCurrency(); + + // look up the payment configuration settings + String serviceName = null; + String paymentConfig = null; + + // get the payment settings i.e. serviceName and config properties file name + GenericValue paymentSettings = getPaymentSettings(orderHeader, paymentPref, RELEASE_SERVICE_TYPE, false); + if (paymentSettings != null) { + paymentConfig = paymentSettings.getString("paymentPropertiesPath"); + serviceName = paymentSettings.getString("paymentService"); + if (serviceName == null) { + String errMsg = "No payment release service for - " + paymentPref.getString("paymentMethodTypeId"); + Debug.logWarning(errMsg, module); + return ServiceUtil.returnError(errMsg); } + } else { + String errMsg = "No payment release settings found for - " + paymentPref.getString("paymentMethodTypeId"); + Debug.logWarning(errMsg, module); + return ServiceUtil.returnError(errMsg); + } - GenericValue authTransaction = PaymentGatewayServices.getAuthTransaction(paymentPref); - Map releaseContext = new HashMap(); - releaseContext.put("orderPaymentPreference", paymentPref); - releaseContext.put("releaseAmount", authTransaction.getDouble("amount")); - releaseContext.put("currency", currency); - releaseContext.put("paymentConfig", paymentConfig); - releaseContext.put("userLogin", userLogin); + if (paymentConfig == null || paymentConfig.length() == 0) { + paymentConfig = "payment.properties"; + } - // run the defined service - Map releaseResult = null; - try { - releaseResult = dispatcher.runSync(serviceName, releaseContext, TX_TIME, true); - } catch (GenericServiceException e) { - Debug.logError(e, "Problem releasing payment", module); - } - - // get the release result code - if (releaseResult != null && !ServiceUtil.isError(releaseResult)) { - Boolean releaseResponse = (Boolean) releaseResult.get("releaseResult"); - - // create the PaymentGatewayResponse - String responseId = delegator.getNextSeqId("PaymentGatewayResponse"); - GenericValue pgResponse = delegator.makeValue("PaymentGatewayResponse", null); - pgResponse.set("paymentGatewayResponseId", responseId); - pgResponse.set("paymentServiceTypeEnumId", RELEASE_SERVICE_TYPE); - pgResponse.set("orderPaymentPreferenceId", paymentPref.get("orderPaymentPreferenceId")); - pgResponse.set("paymentMethodTypeId", paymentPref.get("paymentMethodTypeId")); - pgResponse.set("paymentMethodId", paymentPref.get("paymentMethodId")); - pgResponse.set("transCodeEnumId", "PGT_RELEASE"); - - // set the release info - pgResponse.set("referenceNum", releaseResult.get("releaseRefNum")); - pgResponse.set("altReference", releaseResult.get("releaseAltRefNum")); - pgResponse.set("gatewayCode", releaseResult.get("releaseCode")); - pgResponse.set("gatewayFlag", releaseResult.get("releaseFlag")); - pgResponse.set("gatewayMessage", releaseResult.get("releaseMessage")); - pgResponse.set("transactionDate", UtilDateTime.nowTimestamp()); + GenericValue authTransaction = PaymentGatewayServices.getAuthTransaction(paymentPref); + Map releaseContext = new HashMap(); + releaseContext.put("orderPaymentPreference", paymentPref); + releaseContext.put("releaseAmount", authTransaction.getDouble("amount")); + releaseContext.put("currency", currency); + releaseContext.put("paymentConfig", paymentConfig); + releaseContext.put("userLogin", userLogin); - // store the gateway response - try { - pgResponse.create(); - } catch (GenericEntityException e) { - Debug.logError(e, "Problem storing PaymentGatewayResponse entity; authorization was released! : " + pgResponse, module); - } + // run the defined service + Map releaseResult = null; + try { + releaseResult = dispatcher.runSync(serviceName, releaseContext, TX_TIME, true); + } catch (GenericServiceException e) { + String errMsg = "Problem releasing payment"; + Debug.logError(e,errMsg, module); + return ServiceUtil.returnError(errMsg); + } - // create the internal messages - List messages = (List) releaseResult.get("internalRespMsgs"); - if (messages != null && messages.size() > 0) { - Iterator i = messages.iterator(); - while (i.hasNext()) { - GenericValue respMsg = delegator.makeValue("PaymentGatewayRespMsg", null); - String respMsgId = delegator.getNextSeqId("PaymentGatewayRespMsg"); - String message = (String) i.next(); - respMsg.set("paymentGatewayRespMsgId", respMsgId); - respMsg.set("paymentGatewayResponseId", responseId); - respMsg.set("pgrMessage", message); - try { - delegator.create(respMsg); - } catch (GenericEntityException e) { - Debug.logError(e, module); - return ServiceUtil.returnError("Unable to create PaymentGatewayRespMsg record"); - } - } - } + // get the release result code + if (releaseResult != null && !ServiceUtil.isError(releaseResult)) { + Boolean releaseResponse = (Boolean) releaseResult.get("releaseResult"); + + // create the PaymentGatewayResponse + String responseId = delegator.getNextSeqId("PaymentGatewayResponse"); + GenericValue pgResponse = delegator.makeValue("PaymentGatewayResponse", null); + pgResponse.set("paymentGatewayResponseId", responseId); + pgResponse.set("paymentServiceTypeEnumId", RELEASE_SERVICE_TYPE); + pgResponse.set("orderPaymentPreferenceId", paymentPref.get("orderPaymentPreferenceId")); + pgResponse.set("paymentMethodTypeId", paymentPref.get("paymentMethodTypeId")); + pgResponse.set("paymentMethodId", paymentPref.get("paymentMethodId")); + pgResponse.set("transCodeEnumId", "PGT_RELEASE"); + + // set the release info + pgResponse.set("referenceNum", releaseResult.get("releaseRefNum")); + pgResponse.set("altReference", releaseResult.get("releaseAltRefNum")); + pgResponse.set("gatewayCode", releaseResult.get("releaseCode")); + pgResponse.set("gatewayFlag", releaseResult.get("releaseFlag")); + pgResponse.set("gatewayMessage", releaseResult.get("releaseMessage")); + pgResponse.set("transactionDate", UtilDateTime.nowTimestamp()); - if (releaseResponse != null && releaseResponse.booleanValue()) { - paymentPref.set("statusId", "PAYMENT_CANCELLED"); - try { - paymentPref.store(); - } catch (GenericEntityException e) { - Debug.logError(e, "Problem storing updated payment preference; authorization was released!", module); - } - finished.add(paymentPref); + // store the gateway response + try { + pgResponse.create(); + } catch (GenericEntityException e) { + Debug.logError(e, "Problem storing PaymentGatewayResponse entity; authorization was released! : " + pgResponse, module); + } - // cancel any payment records - List paymentList = null; + // create the internal messages + List messages = (List) releaseResult.get("internalRespMsgs"); + if (messages != null && messages.size() > 0) { + Iterator i = messages.iterator(); + while (i.hasNext()) { + GenericValue respMsg = delegator.makeValue("PaymentGatewayRespMsg", null); + String respMsgId = delegator.getNextSeqId("PaymentGatewayRespMsg"); + String message = (String) i.next(); + respMsg.set("paymentGatewayRespMsgId", respMsgId); + respMsg.set("paymentGatewayResponseId", responseId); + respMsg.set("pgrMessage", message); try { - paymentList = paymentPref.getRelated("Payment"); + delegator.create(respMsg); } catch (GenericEntityException e) { - Debug.logError(e, "Unable to get Payment records from OrderPaymentPreference : " + paymentPref, module); + String errMsg = "Unable to create PaymentGatewayRespMsg record"; + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); } + } + } + + if (releaseResponse != null && releaseResponse.booleanValue()) { + paymentPref.set("statusId", "PAYMENT_CANCELLED"); + try { + paymentPref.store(); + } catch (GenericEntityException e) { + Debug.logError(e, "Problem storing updated payment preference; authorization was released!", module); + } - if (paymentList != null) { - Iterator pi = paymentList.iterator(); - while (pi.hasNext()) { - GenericValue pay = (GenericValue) pi.next(); - pay.set("statusId", "PMNT_CANCELLED"); - try { - pay.store(); - } catch (GenericEntityException e) { - Debug.logError(e, "Unable to store Payment : " + pay, module); - } + // cancel any payment records + List paymentList = null; + try { + paymentList = paymentPref.getRelated("Payment"); + } catch (GenericEntityException e) { + Debug.logError(e, "Unable to get Payment records from OrderPaymentPreference : " + paymentPref, module); + } + + if (paymentList != null) { + Iterator pi = paymentList.iterator(); + while (pi.hasNext()) { + GenericValue pay = (GenericValue) pi.next(); + pay.set("statusId", "PMNT_CANCELLED"); + try { + pay.store(); + } catch (GenericEntityException e) { + Debug.logError(e, "Unable to store Payment : " + pay, module); } } - } else { - Debug.logError("Release failed for pref : " + paymentPref, module); } - } else if (ServiceUtil.isError(releaseResult)) { - saveError(dispatcher, userLogin, paymentPref, releaseResult, "PRDS_PAY_RELEASE", "PGT_RELEASE"); + } else { + String errMsg = "Release failed for pref : " + paymentPref; + Debug.logError(errMsg, module); + result = ServiceUtil.returnFailure(errMsg); } - } - - result = ServiceUtil.returnSuccess(); - if (finished.size() == paymentPrefs.size()) { - result.put("processResult", "COMPLETE"); - } else { - result.put("processResult", "FAILED"); + } else if (ServiceUtil.isError(releaseResult)) { + saveError(dispatcher, userLogin, paymentPref, releaseResult, "PRDS_PAY_RELEASE", "PGT_RELEASE"); + result = ServiceUtil.returnError(ServiceUtil.getErrorMessage(releaseResult)); } return result; |
Free forum by Nabble | Edit this page |