Search in sources :

Example 56 with LocalDispatcher

use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.

the class PaymentGatewayServices method retryFailedOrderAuth.

public static Map<String, Object> retryFailedOrderAuth(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    String orderId = (String) context.get("orderId");
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    // get the order header
    GenericValue orderHeader = null;
    try {
        orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId).queryOne();
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.toString());
    }
    // make sure we have a valid order record
    if (orderHeader == null || orderHeader.get("statusId") == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceOrder, "OrderOrderNotFound", UtilMisc.toMap("orderId", orderId), locale));
    }
    // check the current order status
    if (!"ORDER_CREATED".equals(orderHeader.getString("statusId"))) {
        // if we are out of the created status; then we were either cancelled, rejected or approved
        Debug.logWarning("Was re-trying a failed auth for orderId [" + orderId + "] but it is not in the ORDER_CREATED status, so skipping.", module);
        return ServiceUtil.returnSuccess();
    }
    // run the auth service and check for failure(s)
    Map<String, Object> serviceResult = null;
    try {
        serviceResult = dispatcher.runSync("authOrderPayments", UtilMisc.<String, Object>toMap("orderId", orderId, "userLogin", userLogin));
    } catch (GenericServiceException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.toString());
    }
    if (ServiceUtil.isError(serviceResult)) {
        return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
    }
    // check to see if there was a processor failure
    String authResp = (String) serviceResult.get("processResult");
    if (authResp == null) {
        authResp = "ERROR";
    }
    if ("ERROR".equals(authResp)) {
        Debug.logWarning("The payment processor had a failure in processing, will not modify any status", module);
    } else {
        if ("FAILED".equals(authResp)) {
            // declined; update the order status
            OrderChangeHelper.rejectOrder(dispatcher, userLogin, orderId);
        } else if ("APPROVED".equals(authResp)) {
            // approved; update the order status
            OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId);
        }
    }
    Map<String, Object> result = ServiceUtil.returnSuccess();
    result.put("processResult", authResp);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 57 with LocalDispatcher

use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.

the class PaymentGatewayServices method processAuthResult.

private static void processAuthResult(DispatchContext dctx, Map<String, Object> result, GenericValue userLogin, GenericValue paymentPreference) throws GeneralException {
    LocalDispatcher dispatcher = dctx.getDispatcher();
    result.put("userLogin", userLogin);
    result.put("orderPaymentPreference", paymentPreference);
    ModelService model = dctx.getModelService("processAuthResult");
    Map<String, Object> context = model.makeValid(result, ModelService.IN_PARAM);
    // in case we rollback make sure this service gets called
    dispatcher.addRollbackService(model.name, context, true);
    // invoke the service
    Map<String, Object> resResp;
    try {
        resResp = dispatcher.runSync(model.name, context);
    } catch (GenericServiceException e) {
        Debug.logError(e, module);
        throw e;
    }
    if (ServiceUtil.isError(resResp)) {
        throw new GeneralException(ServiceUtil.getErrorMessage(resResp));
    }
}
Also used : LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GeneralException(org.apache.ofbiz.base.util.GeneralException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) ModelService(org.apache.ofbiz.service.ModelService)

Example 58 with LocalDispatcher

use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.

the class PaymentGatewayServices method processCreditResult.

public static Map<String, Object> processCreditResult(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    String currencyUomId = (String) context.get("currencyUomId");
    GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference");
    Boolean creditResponse = (Boolean) context.get("creditResult");
    Locale locale = (Locale) context.get("locale");
    // create the PaymentGatewayResponse
    String responseId = delegator.getNextSeqId("PaymentGatewayResponse");
    GenericValue pgCredit = delegator.makeValue("PaymentGatewayResponse");
    pgCredit.set("paymentGatewayResponseId", responseId);
    pgCredit.set("paymentServiceTypeEnumId", CREDIT_SERVICE_TYPE);
    pgCredit.set("orderPaymentPreferenceId", paymentPref.get("orderPaymentPreferenceId"));
    pgCredit.set("paymentMethodTypeId", paymentPref.get("paymentMethodTypeId"));
    pgCredit.set("paymentMethodId", paymentPref.get("paymentMethodId"));
    pgCredit.set("transCodeEnumId", "PGT_CREDIT");
    // set the credit info
    pgCredit.set("amount", context.get("creditAmount"));
    pgCredit.set("referenceNum", context.get("creditRefNum"));
    pgCredit.set("altReference", context.get("creditAltRefNum"));
    pgCredit.set("gatewayCode", context.get("creditCode"));
    pgCredit.set("gatewayFlag", context.get("creditFlag"));
    pgCredit.set("gatewayMessage", context.get("creditMessage"));
    pgCredit.set("transactionDate", UtilDateTime.nowTimestamp());
    pgCredit.set("currencyUomId", currencyUomId);
    // create the internal messages
    List<GenericValue> messageEntities = new LinkedList<>();
    List<String> messages = UtilGenerics.cast(context.get("internalRespMsgs"));
    if (UtilValidate.isNotEmpty(messages)) {
        for (String message : messages) {
            GenericValue respMsg = delegator.makeValue("PaymentGatewayRespMsg");
            String respMsgId = delegator.getNextSeqId("PaymentGatewayRespMsg");
            respMsg.set("paymentGatewayRespMsgId", respMsgId);
            respMsg.set("paymentGatewayResponseId", responseId);
            respMsg.set("pgrMessage", message);
            // store the messages
            messageEntities.add(respMsg);
        }
    }
    // save the response and respective messages
    savePgrAndMsgs(dctx, pgCredit, messageEntities);
    if (creditResponse != null && creditResponse.booleanValue()) {
        paymentPref.set("statusId", "PAYMENT_CANCELLED");
        try {
            paymentPref.store();
        } catch (GenericEntityException e) {
            Debug.logError(e, "Problem storing updated payment preference; authorization was credit!", module);
        }
        // cancel any payment records
        List<GenericValue> paymentList = null;
        try {
            paymentList = paymentPref.getRelated("Payment", null, null, false);
        } catch (GenericEntityException e) {
            Debug.logError(e, "Unable to get Payment records from OrderPaymentPreference : " + paymentPref, module);
        }
        if (paymentList != null) {
            Iterator<GenericValue> pi = paymentList.iterator();
            while (pi.hasNext()) {
                GenericValue pay = pi.next();
                try {
                    Map<String, Object> cancelResults = dispatcher.runSync("setPaymentStatus", UtilMisc.toMap("userLogin", userLogin, "paymentId", pay.get("paymentId"), "statusId", "PMNT_CANCELLED"));
                    if (ServiceUtil.isError(cancelResults)) {
                        throw new GenericServiceException(ServiceUtil.getErrorMessage(cancelResults));
                    }
                } catch (GenericServiceException e) {
                    Debug.logError(e, "Unable to cancel Payment : " + pay, module);
                }
            }
        }
    } else {
        Debug.logError("Credit failed for pref : " + paymentPref, module);
        return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "AccountingTroubleCallingCreditOrderPaymentPreferenceService", UtilMisc.toMap("paymentPref", paymentPref), locale));
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 59 with LocalDispatcher

use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.

the class PaymentGatewayServices method processRefundResult.

public static Map<String, Object> processRefundResult(DispatchContext dctx, Map<String, ? extends Object> context) {
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Delegator delegator = dctx.getDelegator();
    Locale locale = (Locale) context.get("locale");
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference");
    String currencyUomId = (String) context.get("currencyUomId");
    String payToPartyId = (String) context.get("payToPartyId");
    String payFromPartyId = (String) context.get("payFromPartyId");
    // create the PaymentGatewayResponse record
    String responseId = delegator.getNextSeqId("PaymentGatewayResponse");
    GenericValue response = delegator.makeValue("PaymentGatewayResponse");
    response.set("paymentGatewayResponseId", responseId);
    response.set("paymentServiceTypeEnumId", REFUND_SERVICE_TYPE);
    response.set("orderPaymentPreferenceId", paymentPref.get("orderPaymentPreferenceId"));
    response.set("paymentMethodTypeId", paymentPref.get("paymentMethodTypeId"));
    response.set("paymentMethodId", paymentPref.get("paymentMethodId"));
    response.set("transCodeEnumId", "PGT_REFUND");
    // set the capture info
    response.set("amount", context.get("refundAmount"));
    response.set("currencyUomId", currencyUomId);
    response.set("referenceNum", context.get("refundRefNum"));
    response.set("altReference", context.get("refundAltRefNum"));
    response.set("gatewayCode", context.get("refundCode"));
    response.set("gatewayFlag", context.get("refundFlag"));
    response.set("gatewayMessage", context.get("refundMessage"));
    response.set("transactionDate", UtilDateTime.nowTimestamp());
    // save the response
    savePgr(dctx, response);
    // create the internal messages
    List<String> messages = UtilGenerics.cast(context.get("internalRespMsgs"));
    if (UtilValidate.isNotEmpty(messages)) {
        Iterator<String> i = messages.iterator();
        while (i.hasNext()) {
            GenericValue respMsg = delegator.makeValue("PaymentGatewayRespMsg");
            String respMsgId = delegator.getNextSeqId("PaymentGatewayRespMsg");
            String message = i.next();
            respMsg.set("paymentGatewayRespMsgId", respMsgId);
            respMsg.set("paymentGatewayResponseId", responseId);
            respMsg.set("pgrMessage", message);
            // save the message
            savePgr(dctx, respMsg);
        }
    }
    Boolean refundResult = (Boolean) context.get("refundResult");
    if (refundResult != null && refundResult.booleanValue()) {
        // mark the preference as refunded
        paymentPref.set("statusId", "PAYMENT_REFUNDED");
        try {
            paymentPref.store();
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
        // handle the (reverse) payment
        Map<String, Object> paymentCtx = UtilMisc.<String, Object>toMap("paymentTypeId", "CUSTOMER_REFUND");
        paymentCtx.put("paymentMethodTypeId", paymentPref.get("paymentMethodTypeId"));
        paymentCtx.put("paymentMethodId", paymentPref.get("paymentMethodId"));
        paymentCtx.put("paymentGatewayResponseId", responseId);
        paymentCtx.put("partyIdTo", payToPartyId);
        paymentCtx.put("partyIdFrom", payFromPartyId);
        paymentCtx.put("statusId", "PMNT_SENT");
        paymentCtx.put("paymentPreferenceId", paymentPref.get("orderPaymentPreferenceId"));
        paymentCtx.put("currencyUomId", currencyUomId);
        paymentCtx.put("amount", context.get("refundAmount"));
        paymentCtx.put("userLogin", userLogin);
        paymentCtx.put("paymentRefNum", context.get("refundRefNum"));
        paymentCtx.put("comments", "Refund");
        String paymentId = null;
        try {
            Map<String, Object> payRes = dispatcher.runSync("createPayment", paymentCtx);
            if (ServiceUtil.isError(payRes)) {
                return ServiceUtil.returnError((String) payRes.get(ModelService.ERROR_MESSAGE));
            } else {
                paymentId = (String) payRes.get("paymentId");
            }
        } catch (GenericServiceException e) {
            Debug.logError(e, "Problem creating Payment", module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentCreationError", locale));
        }
        if (paymentId == null) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentCreationError", locale));
        }
        Map<String, Object> result = ServiceUtil.returnSuccess();
        result.put("paymentId", paymentId);
        result.put("refundAmount", context.get("refundAmount"));
        return result;
    } else {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentRefundError", locale));
    }
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 60 with LocalDispatcher

use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.

the class PaymentGatewayServices method refundPayment.

public static Map<String, Object> refundPayment(DispatchContext dctx, Map<String, ? extends Object> context) {
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference");
    BigDecimal refundAmount = (BigDecimal) context.get("refundAmount");
    Locale locale = (Locale) context.get("locale");
    GenericValue orderHeader = null;
    try {
        orderHeader = paymentPref.getRelatedOne("OrderHeader", false);
    } catch (GenericEntityException e) {
        Debug.logError(e, "Cannot get OrderHeader from OrderPaymentPreference", module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingProblemGettingOrderPaymentPreferences", locale) + " " + e.toString());
    }
    OrderReadHelper orh = new OrderReadHelper(orderHeader);
    GenericValue paymentSettings = null;
    if (orderHeader != null) {
        paymentSettings = getPaymentSettings(orderHeader, paymentPref, REFUND_SERVICE_TYPE, false);
    }
    String serviceName = null;
    String paymentGatewayConfigId = null;
    if (paymentSettings != null) {
        String customMethodId = paymentSettings.getString("paymentCustomMethodId");
        if (UtilValidate.isNotEmpty(customMethodId)) {
            serviceName = getPaymentCustomMethod(orh.getOrderHeader().getDelegator(), customMethodId);
        }
        if (UtilValidate.isEmpty(serviceName)) {
            serviceName = paymentSettings.getString("paymentService");
        }
        String paymentConfig = paymentSettings.getString("paymentPropertiesPath");
        paymentGatewayConfigId = paymentSettings.getString("paymentGatewayConfigId");
        if (serviceName != null) {
            Map<String, Object> serviceContext = new HashMap<>();
            serviceContext.put("orderPaymentPreference", paymentPref);
            serviceContext.put("paymentConfig", paymentConfig);
            serviceContext.put("paymentGatewayConfigId", paymentGatewayConfigId);
            serviceContext.put("currency", orh.getCurrency());
            // get the creditCard/address/email
            String payToPartyId = null;
            try {
                payToPartyId = getBillingInformation(orh, paymentPref, new HashMap<String, Object>());
            } catch (GenericEntityException e) {
                Debug.logError(e, "Problems getting billing information", module);
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingBillingAccountNotFound", UtilMisc.toMap("billingAccountId", ""), locale));
            }
            BigDecimal processAmount = refundAmount.setScale(decimals, rounding);
            serviceContext.put("refundAmount", processAmount);
            serviceContext.put("userLogin", userLogin);
            // call the service
            Map<String, Object> refundResponse = null;
            try {
                refundResponse = dispatcher.runSync(serviceName, serviceContext, TX_TIME, true);
            } catch (GenericServiceException e) {
                Debug.logError(e, "Problem refunding payment through processor", module);
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentRefundError", locale));
            }
            if (ServiceUtil.isError(refundResponse)) {
                saveError(dispatcher, userLogin, paymentPref, refundResponse, REFUND_SERVICE_TYPE, "PGT_REFUND");
                return ServiceUtil.returnError(ServiceUtil.getErrorMessage(refundResponse));
            }
            // get the pay to party ID for the order (will be the payFrom)
            String payFromPartyId = getPayToPartyId(orderHeader);
            // process the refund result
            Map<String, Object> refundResRes;
            try {
                ModelService model = dctx.getModelService("processRefundResult");
                Map<String, Object> refundResCtx = model.makeValid(context, ModelService.IN_PARAM);
                refundResCtx.put("currencyUomId", orh.getCurrency());
                refundResCtx.put("payToPartyId", payToPartyId);
                refundResCtx.put("payFromPartyId", payFromPartyId);
                refundResCtx.put("refundRefNum", refundResponse.get("refundRefNum"));
                refundResCtx.put("refundAltRefNum", refundResponse.get("refundAltRefNum"));
                refundResCtx.put("refundMessage", refundResponse.get("refundMessage"));
                refundResCtx.put("refundResult", refundResponse.get("refundResult"));
                // The refund amount could be different from what we tell the payment gateway due to issues
                // such as having to void the entire original auth amount and re-authorize the new order total.
                BigDecimal actualRefundAmount = (BigDecimal) refundResponse.get("refundAmount");
                if (actualRefundAmount != null && actualRefundAmount.compareTo(processAmount) != 0) {
                    refundResCtx.put("refundAmount", refundResponse.get("refundAmount"));
                }
                refundResRes = dispatcher.runSync(model.name, refundResCtx);
                if (ServiceUtil.isError(refundResRes)) {
                    return ServiceUtil.returnError(ServiceUtil.getErrorMessage(refundResRes));
                }
            } catch (GenericServiceException e) {
                Debug.logError(e, module);
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentRefundError", locale) + " " + e.getMessage());
            }
            return refundResRes;
        } else {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentRefundServiceNotDefined", locale));
        }
    } else {
        return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "AccountingPaymentSettingNotFound", UtilMisc.toMap("productStoreId", orderHeader.getString("productStoreId"), "transactionType", REFUND_SERVICE_TYPE), locale));
    }
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) BigDecimal(java.math.BigDecimal) OrderReadHelper(org.apache.ofbiz.order.order.OrderReadHelper) ModelService(org.apache.ofbiz.service.ModelService)

Aggregations

LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)427 GenericValue (org.apache.ofbiz.entity.GenericValue)356 Delegator (org.apache.ofbiz.entity.Delegator)324 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)321 Locale (java.util.Locale)296 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)270 HashMap (java.util.HashMap)214 BigDecimal (java.math.BigDecimal)135 GeneralException (org.apache.ofbiz.base.util.GeneralException)87 Timestamp (java.sql.Timestamp)81 LinkedList (java.util.LinkedList)79 IOException (java.io.IOException)59 Map (java.util.Map)51 HttpSession (javax.servlet.http.HttpSession)49 OrderReadHelper (org.apache.ofbiz.order.order.OrderReadHelper)28 ModelService (org.apache.ofbiz.service.ModelService)28 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)24 ShoppingCart (org.apache.ofbiz.order.shoppingcart.ShoppingCart)23 Security (org.apache.ofbiz.security.Security)20 ByteBuffer (java.nio.ByteBuffer)19