Search in sources :

Example 66 with GenericServiceException

use of org.apache.ofbiz.service.GenericServiceException 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 67 with GenericServiceException

use of org.apache.ofbiz.service.GenericServiceException 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 68 with GenericServiceException

use of org.apache.ofbiz.service.GenericServiceException 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)

Example 69 with GenericServiceException

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

the class PaymentGatewayServices method authPayment.

private static Map<String, Object> authPayment(LocalDispatcher dispatcher, GenericValue userLogin, OrderReadHelper orh, GenericValue paymentPreference, BigDecimal totalRemaining, boolean reauth, BigDecimal overrideAmount) throws GeneralException {
    String paymentConfig = null;
    String serviceName = null;
    String paymentGatewayConfigId = null;
    // get the payment settings i.e. serviceName and config properties file name
    String serviceType = AUTH_SERVICE_TYPE;
    if (reauth) {
        serviceType = REAUTH_SERVICE_TYPE;
    }
    GenericValue paymentSettings = getPaymentSettings(orh.getOrderHeader(), paymentPreference, serviceType, false);
    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");
        }
        paymentConfig = paymentSettings.getString("paymentPropertiesPath");
        paymentGatewayConfigId = paymentSettings.getString("paymentGatewayConfigId");
    } else {
        throw new GeneralException("Could not find any valid payment settings for order with ID [" + orh.getOrderId() + "], and payment operation (serviceType) [" + serviceType + "]");
    }
    // make sure the service name is not null
    if (serviceName == null) {
        throw new GeneralException("Invalid payment processor, serviceName is null: " + paymentSettings);
    }
    // make the process context
    Map<String, Object> processContext = new HashMap<>();
    // get the visit record to obtain the client's IP address
    GenericValue orderHeader = orh.getOrderHeader();
    String visitId = orderHeader.getString("visitId");
    GenericValue visit = null;
    if (visitId != null) {
        try {
            visit = orderHeader.getDelegator().findOne("Visit", UtilMisc.toMap("visitId", visitId), false);
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
    }
    if (visit != null && visit.get("clientIpAddress") != null) {
        processContext.put("customerIpAddress", visit.getString("clientIpAddress"));
    }
    GenericValue productStore = orderHeader.getRelatedOne("ProductStore", false);
    processContext.put("userLogin", userLogin);
    processContext.put("orderId", orh.getOrderId());
    processContext.put("orderItems", orh.getOrderItems());
    // TODO refactor the payment API to handle support all addresses
    processContext.put("shippingAddress", EntityUtil.getFirst(orh.getShippingLocations()));
    processContext.put("paymentConfig", paymentConfig);
    processContext.put("paymentGatewayConfigId", paymentGatewayConfigId);
    processContext.put("currency", orh.getCurrency());
    processContext.put("orderPaymentPreference", paymentPreference);
    if (paymentPreference.get("securityCode") != null) {
        processContext.put("cardSecurityCode", paymentPreference.get("securityCode"));
    }
    // get the billing information
    getBillingInformation(orh, paymentPreference, processContext);
    // default charge is totalRemaining
    BigDecimal processAmount = totalRemaining;
    // use override or max amount available
    if (overrideAmount != null) {
        processAmount = overrideAmount;
    } else if (paymentPreference.get("maxAmount") != null) {
        processAmount = paymentPreference.getBigDecimal("maxAmount");
    }
    // Check if the order is a replacement order
    boolean replacementOrderFlag = isReplacementOrder(orderHeader);
    // don't authorized more then what is required
    if (!replacementOrderFlag && processAmount.compareTo(totalRemaining) > 0) {
        processAmount = totalRemaining;
    }
    // format the decimal
    processAmount = processAmount.setScale(decimals, rounding);
    if (Debug.verboseOn()) {
        Debug.logVerbose("Charging amount: " + processAmount, module);
    }
    processContext.put("processAmount", processAmount);
    // invoke the processor
    Map<String, Object> processorResult = null;
    try {
        // invoke the payment processor; allow 5 minute transaction timeout and require a new tx; we'll capture the error and pass back nicely
        GenericValue creditCard = (GenericValue) processContext.get("creditCard");
        // only try other exp dates if orderHeader.autoOrderShoppingListId is not empty, productStore.autoOrderCcTryExp=Y and this payment is a creditCard
        boolean tryOtherExpDates = "Y".equals(productStore.getString("autoOrderCcTryExp")) && creditCard != null && UtilValidate.isNotEmpty(orderHeader.getString("autoOrderShoppingListId"));
        // if we are not trying other expire dates OR if we are and the date is after today, then run the service
        if (!tryOtherExpDates || UtilValidate.isDateAfterToday(creditCard.getString("expireDate"))) {
            processorResult = dispatcher.runSync(serviceName, processContext, TX_TIME, true);
            if (ServiceUtil.isError(processorResult)) {
                return ServiceUtil.returnError(ServiceUtil.getErrorMessage(processorResult));
            }
        }
        // try other expire dates if the expireDate is not after today, or if we called the auth service and resultBadExpire = true
        if (tryOtherExpDates && (!UtilValidate.isDateAfterToday(creditCard.getString("expireDate")) || (processorResult != null && Boolean.TRUE.equals(processorResult.get("resultBadExpire"))))) {
            // try adding 2, 3, 4 years later with the same month
            String expireDate = creditCard.getString("expireDate");
            int dateSlash1 = expireDate.indexOf("/");
            String month = expireDate.substring(0, dateSlash1);
            String year = expireDate.substring(dateSlash1 + 1);
            // start adding 2 years, if comes back with resultBadExpire try again up to twice incrementing one year
            year = StringUtil.addToNumberString(year, 2);
            // note that this is set in memory only for now, not saved to the database unless successful
            creditCard.set("expireDate", month + "/" + year);
            // don't need to set back in the processContext, it's already there: processContext.put("creditCard", creditCard);
            processorResult = dispatcher.runSync(serviceName, processContext, TX_TIME, true);
            if (ServiceUtil.isError(processorResult)) {
                return ServiceUtil.returnError(ServiceUtil.getErrorMessage(processorResult));
            }
            // note that these additional tries will only be done if the service return is not an error, in that case we let it pass through to the normal error handling
            if (ServiceUtil.isSuccess(processorResult) && Boolean.TRUE.equals(processorResult.get("resultBadExpire"))) {
                // okay, try one more year...
                year = StringUtil.addToNumberString(year, 1);
                creditCard.set("expireDate", month + "/" + year);
                processorResult = dispatcher.runSync(serviceName, processContext, TX_TIME, true);
                if (ServiceUtil.isError(processorResult)) {
                    return ServiceUtil.returnError(ServiceUtil.getErrorMessage(processorResult));
                }
            }
            if (ServiceUtil.isSuccess(processorResult) && Boolean.TRUE.equals(processorResult.get("resultBadExpire"))) {
                // okay, try one more year... and this is the last try
                year = StringUtil.addToNumberString(year, 1);
                creditCard.set("expireDate", month + "/" + year);
                processorResult = dispatcher.runSync(serviceName, processContext, TX_TIME, true);
                if (ServiceUtil.isError(processorResult)) {
                    return ServiceUtil.returnError(ServiceUtil.getErrorMessage(processorResult));
                }
            }
            // at this point if we have a successful result, let's save the new creditCard expireDate
            if (ServiceUtil.isSuccess(processorResult) && Boolean.TRUE.equals(processorResult.get("authResult"))) {
                // TODO: this is bad; we should be expiring the old card and creating a new one instead of editing it
                creditCard.store();
            }
        }
    } catch (GenericServiceException e) {
        Debug.logError(e, "Error occurred on: " + serviceName + ", Order ID is: [" + orh.getOrderId() + "]", module);
        throw new GeneralException("Problems invoking payment processor! Will retry later. Order ID is: [" + orh.getOrderId() + "]", e);
    }
    if (processorResult != null) {
        // check for errors from the processor implementation
        if (ServiceUtil.isError(processorResult)) {
            Debug.logError("Processor failed; will retry later: " + processorResult.get(ModelService.ERROR_MESSAGE), module);
            // log the error message as a gateway response when it fails
            saveError(dispatcher, userLogin, paymentPreference, processorResult, AUTH_SERVICE_TYPE, "PGT_AUTHORIZE");
            // this is the one place where we want to return null because the calling method will look for this
            return null;
        }
        // pass the payTo partyId to the result processor; we just add it to the result context.
        String payToPartyId = getPayToPartyId(orh.getOrderHeader());
        processorResult.put("payToPartyId", payToPartyId);
        // add paymentSettings to result; for use by later processors
        processorResult.put("paymentSettings", paymentSettings);
        // and pass on the currencyUomId
        processorResult.put("currencyUomId", orh.getCurrency());
    }
    return processorResult;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralException(org.apache.ofbiz.base.util.GeneralException) HashMap(java.util.HashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) BigDecimal(java.math.BigDecimal)

Example 70 with GenericServiceException

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

the class PaymentGatewayServices method processReleaseResult.

public static Map<String, Object> processReleaseResult(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 releaseResponse = (Boolean) context.get("releaseResult");
    Locale locale = (Locale) context.get("locale");
    // create the PaymentGatewayResponse
    String responseId = delegator.getNextSeqId("PaymentGatewayResponse");
    GenericValue pgResponse = delegator.makeValue("PaymentGatewayResponse");
    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("amount", context.get("releaseAmount"));
    pgResponse.set("referenceNum", context.get("releaseRefNum"));
    pgResponse.set("altReference", context.get("releaseAltRefNum"));
    pgResponse.set("gatewayCode", context.get("releaseCode"));
    pgResponse.set("gatewayFlag", context.get("releaseFlag"));
    pgResponse.set("gatewayMessage", context.get("releaseMessage"));
    pgResponse.set("transactionDate", UtilDateTime.nowTimestamp());
    pgResponse.set("currencyUomId", currencyUomId);
    // store the gateway response
    savePgr(dctx, pgResponse);
    // 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);
            // store the messages
            savePgr(dctx, respMsg);
        }
    }
    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);
        }
        // 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("Release failed for pref : " + paymentPref, module);
        return ServiceUtil.returnFailure(UtilProperties.getMessage(resourceOrder, "AccountingTroubleCallingReleaseOrderPaymentPreferenceService", locale) + " " + paymentPref);
    }
    return ServiceUtil.returnSuccess();
}
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)

Aggregations

GenericServiceException (org.apache.ofbiz.service.GenericServiceException)417 GenericValue (org.apache.ofbiz.entity.GenericValue)339 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)303 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)251 Delegator (org.apache.ofbiz.entity.Delegator)250 Locale (java.util.Locale)246 HashMap (java.util.HashMap)221 BigDecimal (java.math.BigDecimal)139 LinkedList (java.util.LinkedList)79 GeneralException (org.apache.ofbiz.base.util.GeneralException)68 Timestamp (java.sql.Timestamp)66 Map (java.util.Map)54 IOException (java.io.IOException)43 HttpSession (javax.servlet.http.HttpSession)36 ModelService (org.apache.ofbiz.service.ModelService)33 OrderReadHelper (org.apache.ofbiz.order.order.OrderReadHelper)24 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)22 ArrayList (java.util.ArrayList)21 LinkedHashMap (java.util.LinkedHashMap)20 List (java.util.List)20