Search in sources :

Example 36 with Security

use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.

the class PaymentGatewayServices method processManualCcAuth.

// manual auth service
public static Map<String, Object> processManualCcAuth(DispatchContext dctx, Map<String, ? extends Object> context) {
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Delegator delegator = dctx.getDelegator();
    Security security = dctx.getSecurity();
    // security check
    if (!security.hasEntityPermission("MANUAL", "_PAYMENT", userLogin) && !security.hasEntityPermission("ACCOUNTING", "_CREATE", userLogin)) {
        Debug.logWarning("**** Security [" + (new Date()).toString() + "]: " + userLogin.get("userLoginId") + " attempt to run manual payment transaction!", module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentTransactionNotAuthorized", locale));
    }
    String paymentMethodId = (String) context.get("paymentMethodId");
    String productStoreId = (String) context.get("productStoreId");
    String securityCode = (String) context.get("securityCode");
    BigDecimal amount = (BigDecimal) context.get("amount");
    // check the payment method; verify type
    GenericValue paymentMethod;
    try {
        paymentMethod = EntityQuery.use(delegator).from("PaymentMethod").where("paymentMethodId", paymentMethodId).queryOne();
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }
    if (paymentMethod == null || !"CREDIT_CARD".equals(paymentMethod.getString("paymentMethodTypeId"))) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentManualAuthOnlyForCreditCard", locale));
    }
    // get the billToParty object
    GenericValue billToParty;
    try {
        billToParty = paymentMethod.getRelatedOne("Party", false);
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }
    // get the credit card object
    GenericValue creditCard;
    try {
        creditCard = EntityQuery.use(delegator).from("CreditCard").where("paymentMethodId", paymentMethodId).queryOne();
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }
    if (UtilValidate.isEmpty(creditCard)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentCreditCardNotFound", UtilMisc.toMap("paymentMethodId", paymentMethodId), locale));
    }
    // get the transaction settings
    String paymentService = null;
    String paymentConfig = null;
    String paymentGatewayConfigId = null;
    GenericValue paymentSettings = ProductStoreWorker.getProductStorePaymentSetting(delegator, productStoreId, "CREDIT_CARD", "PRDS_PAY_AUTH", false);
    if (paymentSettings == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentSettingNotFound", UtilMisc.toMap("productStoreId", productStoreId, "transactionType", ""), locale));
    } else {
        String customMethodId = paymentSettings.getString("paymentCustomMethodId");
        if (UtilValidate.isNotEmpty(customMethodId)) {
            paymentService = getPaymentCustomMethod(delegator, customMethodId);
        }
        if (UtilValidate.isEmpty(paymentService)) {
            paymentService = paymentSettings.getString("paymentService");
        }
        paymentConfig = paymentSettings.getString("paymentPropertiesPath");
        paymentGatewayConfigId = paymentSettings.getString("paymentGatewayConfigId");
        if (UtilValidate.isEmpty(paymentConfig)) {
            paymentConfig = "payment.properties";
        }
    }
    // prepare the order payment preference (facade)
    GenericValue orderPaymentPref = delegator.makeValue("OrderPaymentPreference", new HashMap<String, Object>());
    orderPaymentPref.set("orderPaymentPreferenceId", "_NA_");
    orderPaymentPref.set("orderId", "_NA_");
    orderPaymentPref.set("presentFlag", "N");
    orderPaymentPref.set("overflowFlag", "Y");
    orderPaymentPref.set("paymentMethodTypeId", "CREDIT_CARD");
    orderPaymentPref.set("paymentMethodId", paymentMethodId);
    if (UtilValidate.isNotEmpty(securityCode)) {
        orderPaymentPref.set("securityCode", securityCode);
    }
    // this record is not to be stored, just passed to the service for use
    // get the default currency
    String currency = EntityUtilProperties.getPropertyValue("general", "currency.uom.id.default", "USD", delegator);
    // prepare the auth context
    Map<String, Object> authContext = new HashMap<>();
    authContext.put("orderId", "_NA_");
    authContext.put("orderItems", new LinkedList<>());
    authContext.put("orderPaymentPreference", orderPaymentPref);
    authContext.put("creditCard", creditCard);
    authContext.put("billToParty", billToParty);
    authContext.put("currency", currency);
    authContext.put("paymentConfig", paymentConfig);
    authContext.put("paymentGatewayConfigId", paymentGatewayConfigId);
    authContext.put("processAmount", amount);
    authContext.put("userLogin", userLogin);
    // call the auth service
    Map<String, Object> response;
    try {
        Debug.logInfo("Running authorization service: " + paymentService, module);
        response = dispatcher.runSync(paymentService, authContext, TX_TIME, true);
    } catch (GenericServiceException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentServiceError", UtilMisc.toMap("paymentService", paymentService, "authContext", authContext), locale));
    }
    if (ServiceUtil.isError(response)) {
        return ServiceUtil.returnError(ServiceUtil.getErrorMessage(response));
    }
    Boolean authResult = (Boolean) response.get("authResult");
    Debug.logInfo("Authorization service returned: " + authResult, module);
    if (authResult != null && authResult) {
        return ServiceUtil.returnSuccess();
    } else {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentAuthorizationFailed", locale));
    }
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) Security(org.apache.ofbiz.security.Security) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 37 with Security

use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.

the class PaymentGatewayServices method processManualCcTx.

// manual processing service
public static Map<String, Object> processManualCcTx(DispatchContext dctx, Map<String, ? extends Object> context) {
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Delegator delegator = dctx.getDelegator();
    Security security = dctx.getSecurity();
    // security check
    if (!security.hasEntityPermission("MANUAL", "_PAYMENT", userLogin) && !security.hasEntityPermission("ACCOUNTING", "_CREATE", userLogin)) {
        Debug.logWarning("**** Security [" + (new Date()).toString() + "]: " + userLogin.get("userLoginId") + " attempt to run manual payment transaction!", module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentTransactionNotAuthorized", locale));
    }
    String orderPaymentPreferenceId = (String) context.get("orderPaymentPreferenceId");
    String paymentMethodTypeId = (String) context.get("paymentMethodTypeId");
    String productStoreId = (String) context.get("productStoreId");
    String transactionType = (String) context.get("transactionType");
    String referenceCode = (String) context.get("referenceCode");
    if (referenceCode == null) {
        referenceCode = Long.valueOf(System.currentTimeMillis()).toString();
    }
    // Get the OrderPaymentPreference
    GenericValue paymentPref = null;
    try {
        paymentPref = EntityQuery.use(delegator).from("OrderPaymentPreference").where("orderPaymentPreferenceId", orderPaymentPreferenceId).queryOne();
    } catch (GenericEntityException e) {
        Debug.logWarning(e, "Problem getting OrderPaymentPreference for orderPaymentPreferenceId " + orderPaymentPreferenceId, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingProblemGettingOrderPaymentPreferences", locale) + " " + orderPaymentPreferenceId);
    }
    // Error if no OrderPaymentPreference was found
    if (paymentPref == null) {
        Debug.logWarning("Could not find OrderPaymentPreference with orderPaymentPreferenceId: " + orderPaymentPreferenceId, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingProblemGettingOrderPaymentPreferences", locale) + " " + orderPaymentPreferenceId);
    }
    // Get the OrderHeader
    GenericValue orderHeader = null;
    String orderId = paymentPref.getString("orderId");
    try {
        orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId).queryOne();
    } catch (GenericEntityException e) {
        Debug.logWarning(e, "Problem getting OrderHeader for orderId " + orderId, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceOrder, "OrderOrderNotFound", UtilMisc.toMap("orderId", orderId), locale));
    }
    // Error if no OrderHeader was found
    if (orderHeader == null) {
        Debug.logWarning("Could not find OrderHeader with orderId: " + orderId + "; not processing payments.", module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceOrder, "OrderOrderNotFound", UtilMisc.toMap("orderId", orderId), locale));
    }
    OrderReadHelper orh = new OrderReadHelper(orderHeader);
    // check valid implemented types
    if (!transactionType.equals(CREDIT_SERVICE_TYPE)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentTransactionNotYetSupported", locale));
    }
    // transaction request context
    Map<String, Object> requestContext = new HashMap<>();
    String paymentService = null;
    String paymentConfig = null;
    String paymentGatewayConfigId = null;
    // get the transaction settings
    GenericValue paymentSettings = ProductStoreWorker.getProductStorePaymentSetting(delegator, productStoreId, paymentMethodTypeId, transactionType, false);
    if (paymentSettings == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentSettingNotFound", UtilMisc.toMap("productStoreId", productStoreId, "transactionType", transactionType), locale));
    } else {
        paymentGatewayConfigId = paymentSettings.getString("paymentGatewayConfigId");
        String customMethodId = paymentSettings.getString("paymentCustomMethodId");
        if (UtilValidate.isNotEmpty(customMethodId)) {
            paymentService = getPaymentCustomMethod(delegator, customMethodId);
        }
        if (UtilValidate.isEmpty(paymentService)) {
            paymentService = paymentSettings.getString("paymentService");
        }
        paymentConfig = paymentSettings.getString("paymentPropertiesPath");
        if (paymentConfig == null) {
            paymentConfig = "payment.properties";
        }
        requestContext.put("paymentConfig", paymentConfig);
        requestContext.put("paymentGatewayConfigId", paymentGatewayConfigId);
    }
    // check the service name
    if (paymentService == null || paymentGatewayConfigId == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentSettingNotValid", locale));
    }
    if ("CREDIT_CARD".equals(paymentMethodTypeId)) {
        GenericValue creditCard = delegator.makeValue("CreditCard");
        creditCard.setAllFields(context, true, null, null);
        if (creditCard.get("firstNameOnCard") == null || creditCard.get("lastNameOnCard") == null || creditCard.get("cardType") == null || creditCard.get("cardNumber") == null) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentCreditCardMissingMandatoryFields", locale));
        }
        String expMonth = (String) context.get("expMonth");
        String expYear = (String) context.get("expYear");
        String expDate = expMonth + "/" + expYear;
        creditCard.set("expireDate", expDate);
        requestContext.put("creditCard", creditCard);
        requestContext.put("cardSecurityCode", context.get("cardSecurityCode"));
        GenericValue billingAddress = delegator.makeValue("PostalAddress");
        billingAddress.setAllFields(context, true, null, null);
        if (billingAddress.get("address1") == null || billingAddress.get("city") == null || billingAddress.get("postalCode") == null) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentCreditCardBillingAddressMssingMandatoryFields", locale));
        }
        requestContext.put("billingAddress", billingAddress);
        GenericValue billToEmail = delegator.makeValue("ContactMech");
        billToEmail.set("infoString", context.get("infoString"));
        if (billToEmail.get("infoString") == null) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentCreditCardEmailAddressCannotBeEmpty", locale));
        }
        requestContext.put("billToParty", orh.getBillToParty());
        requestContext.put("billToEmail", billToEmail);
        requestContext.put("referenceCode", referenceCode);
        String currency = EntityUtilProperties.getPropertyValue("general", "currency.uom.id.default", "USD", delegator);
        requestContext.put("currency", currency);
        requestContext.put("creditAmount", context.get("amount"));
    } else {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentTransactionNotYetSupported", locale) + " " + paymentMethodTypeId);
    }
    // process the transaction
    Map<String, Object> response = null;
    try {
        response = dispatcher.runSync(paymentService, requestContext, TX_TIME, true);
        if (ServiceUtil.isError(response)) {
            return ServiceUtil.returnError(ServiceUtil.getErrorMessage(response));
        }
    } catch (GenericServiceException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentServiceError", UtilMisc.toMap("paymentService", paymentService, "authContext", requestContext), locale));
    }
    // get the response result code
    if (response != null && ServiceUtil.isSuccess(response)) {
        Map<String, Object> responseRes;
        try {
            ModelService model = dctx.getModelService("processCreditResult");
            response.put("orderPaymentPreference", paymentPref);
            response.put("userLogin", userLogin);
            Map<String, Object> resCtx = model.makeValid(response, ModelService.IN_PARAM);
            responseRes = dispatcher.runSync(model.name, resCtx);
        } catch (GenericServiceException e) {
            Debug.logError(e, module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentCreditError", UtilMisc.toMap("errorString", e.getMessage()), locale));
        }
        if (responseRes != null && ServiceUtil.isError(responseRes)) {
            return ServiceUtil.returnError(ServiceUtil.getErrorMessage(responseRes));
        }
    } else if (ServiceUtil.isError(response)) {
        saveError(dispatcher, userLogin, paymentPref, response, CREDIT_SERVICE_TYPE, "PGT_CREDIT");
        return ServiceUtil.returnError(ServiceUtil.getErrorMessage(response));
    }
    // check for errors
    if (ServiceUtil.isError(response)) {
        return ServiceUtil.returnError(ServiceUtil.makeErrorMessage(response, null, null, null, null));
    }
    // get the reference number
    String refNum = (String) response.get("creditRefNum");
    String code = (String) response.get("creditCode");
    String msg = (String) response.get("creditMessage");
    Map<String, Object> returnResults = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, "AccountingPaymentTransactionManualResult", UtilMisc.toMap("msg", msg, "code", code, "refNum", refNum), locale));
    returnResults.put("referenceNum", refNum);
    return returnResults;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) Security(org.apache.ofbiz.security.Security) Date(java.util.Date) OrderReadHelper(org.apache.ofbiz.order.order.OrderReadHelper) ModelService(org.apache.ofbiz.service.ModelService) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 38 with Security

use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.

the class PaymentMethodServices method createEftAccount.

/**
 * Creates EftAccount and PaymentMethod entities according to the parameters passed in the context
 * <b>security check</b>: userLogin partyId must equal partyId, or must have PAY_INFO_CREATE permission
 * @param ctx The DispatchContext that this service is operating in
 * @param context Map containing the input parameters
 * @return Map with the result of the service, the output parameters
 */
public static Map<String, Object> createEftAccount(DispatchContext ctx, Map<String, ? extends Object> context) {
    Map<String, Object> result = new HashMap<>();
    Delegator delegator = ctx.getDelegator();
    Security security = ctx.getSecurity();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    Timestamp now = UtilDateTime.nowTimestamp();
    String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_CREATE", "ACCOUNTING", "_CREATE");
    if (result.size() > 0) {
        return result;
    }
    List<GenericValue> toBeStored = new LinkedList<>();
    GenericValue newPm = delegator.makeValue("PaymentMethod");
    toBeStored.add(newPm);
    GenericValue newEa = delegator.makeValue("EftAccount");
    toBeStored.add(newEa);
    String newPmId = (String) context.get("paymentMethodId");
    if (UtilValidate.isEmpty(newPmId)) {
        try {
            newPmId = delegator.getNextSeqId("PaymentMethod");
        } catch (IllegalArgumentException e) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingEftAccountCannotBeCreated", locale));
        }
    }
    newPm.set("partyId", partyId);
    newPm.set("fromDate", (context.get("fromDate") != null ? context.get("fromDate") : now));
    newPm.set("thruDate", context.get("thruDate"));
    newPm.set("description", context.get("description"));
    newEa.set("bankName", context.get("bankName"));
    newEa.set("routingNumber", context.get("routingNumber"));
    newEa.set("accountType", context.get("accountType"));
    newEa.set("accountNumber", context.get("accountNumber"));
    newEa.set("nameOnAccount", context.get("nameOnAccount"));
    newEa.set("companyNameOnAccount", context.get("companyNameOnAccount"));
    newEa.set("contactMechId", context.get("contactMechId"));
    newPm.set("paymentMethodId", newPmId);
    newPm.set("paymentMethodTypeId", "EFT_ACCOUNT");
    newEa.set("paymentMethodId", newPmId);
    GenericValue newPartyContactMechPurpose = null;
    String contactMechId = (String) context.get("contactMechId");
    if (UtilValidate.isNotEmpty(contactMechId)) {
        // add a PartyContactMechPurpose of BILLING_LOCATION if necessary
        String contactMechPurposeTypeId = "BILLING_LOCATION";
        GenericValue tempVal = null;
        try {
            List<GenericValue> allPCWPs = EntityQuery.use(delegator).from("PartyContactWithPurpose").where("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId).queryList();
            allPCWPs = EntityUtil.filterByDate(allPCWPs, now, "contactFromDate", "contactThruDate", true);
            allPCWPs = EntityUtil.filterByDate(allPCWPs, now, "purposeFromDate", "purposeThruDate", true);
            tempVal = EntityUtil.getFirst(allPCWPs);
        } catch (GenericEntityException e) {
            Debug.logWarning(e.getMessage(), module);
            tempVal = null;
        }
        if (tempVal == null) {
            // no value found, create a new one
            newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", now));
        }
    }
    if (newPartyContactMechPurpose != null) {
        toBeStored.add(newPartyContactMechPurpose);
    }
    try {
        delegator.storeAll(toBeStored);
    } catch (GenericEntityException e) {
        Debug.logWarning(e.getMessage(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingEftAccountCannotBeCreatedWriteFailure", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    result.put("paymentMethodId", newEa.getString("paymentMethodId"));
    result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) Security(org.apache.ofbiz.security.Security) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 39 with Security

use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.

the class PaymentMethodServices method deletePaymentMethod.

/**
 * Deletes a PaymentMethod entity according to the parameters passed in the context
 * <b>security check</b>: userLogin partyId must equal paymentMethod partyId, or must have PAY_INFO_DELETE permission
 * @param ctx The DispatchContext that this service is operating in
 * @param context Map containing the input parameters
 * @return Map with the result of the service, the output parameters
 */
public static Map<String, Object> deletePaymentMethod(DispatchContext ctx, Map<String, ? extends Object> context) {
    Map<String, Object> result = new HashMap<>();
    Delegator delegator = ctx.getDelegator();
    Security security = ctx.getSecurity();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    Timestamp now = UtilDateTime.nowTimestamp();
    // never delete a PaymentMethod, just put a to date on the link to the party
    String paymentMethodId = (String) context.get("paymentMethodId");
    GenericValue paymentMethod = null;
    try {
        paymentMethod = EntityQuery.use(delegator).from("PaymentMethod").where("paymentMethodId", paymentMethodId).queryOne();
    } catch (GenericEntityException e) {
        Debug.logWarning(e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingPaymentMethodCannotBeDeleted", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    if (paymentMethod == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingPaymentMethodCannotBeDeleted", UtilMisc.toMap("errorString", ""), locale));
    }
    // <b>security check</b>: userLogin partyId must equal paymentMethod partyId, or must have PAY_INFO_DELETE permission
    if (paymentMethod.get("partyId") == null || !paymentMethod.getString("partyId").equals(userLogin.getString("partyId"))) {
        if (!security.hasEntityPermission("PAY_INFO", "_DELETE", userLogin) && !security.hasEntityPermission("ACCOUNTING", "_DELETE", userLogin)) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingPaymentMethodNoPermissionToDelete", locale));
        }
    }
    paymentMethod.set("thruDate", now);
    try {
        paymentMethod.store();
    } catch (GenericEntityException e) {
        Debug.logWarning(e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingPaymentMethodCannotBeDeletedWriteFailure", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) HashMap(java.util.HashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Security(org.apache.ofbiz.security.Security) Timestamp(java.sql.Timestamp)

Example 40 with Security

use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.

the class PaymentMethodServices method updateGiftCard.

public static Map<String, Object> updateGiftCard(DispatchContext ctx, Map<String, Object> context) {
    Map<String, Object> result = new HashMap<>();
    Delegator delegator = ctx.getDelegator();
    Security security = ctx.getSecurity();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    Timestamp now = UtilDateTime.nowTimestamp();
    String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_UPDATE", "ACCOUNTING", "_UPDATE");
    if (result.size() > 0) {
        return result;
    }
    List<GenericValue> toBeStored = new LinkedList<>();
    boolean isModified = false;
    GenericValue paymentMethod = null;
    GenericValue newPm = null;
    GenericValue giftCard = null;
    GenericValue newGc = null;
    String paymentMethodId = (String) context.get("paymentMethodId");
    try {
        giftCard = EntityQuery.use(delegator).from("GiftCard").where("paymentMethodId", paymentMethodId).queryOne();
        paymentMethod = EntityQuery.use(delegator).from("PaymentMethod").where("paymentMethodId", paymentMethodId).queryOne();
    } catch (GenericEntityException e) {
        Debug.logWarning(e.getMessage(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingGiftCardCannotBeUpdated", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    if (giftCard == null || paymentMethod == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingGiftCardCannotBeUpdated", UtilMisc.toMap("errorString", paymentMethodId), locale));
    }
    if (!paymentMethod.getString("partyId").equals(partyId) && !security.hasEntityPermission("PAY_INFO", "_UPDATE", userLogin) && !security.hasEntityPermission("ACCOUNTING", "_UPDATE", userLogin)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingGiftCardPartyNotAuthorized", UtilMisc.toMap("partyId", partyId, "paymentMethodId", paymentMethodId), locale));
    }
    // card number (masked)
    String cardNumber = StringUtil.removeSpaces((String) context.get("cardNumber"));
    if (cardNumber.startsWith("*")) {
        // get the masked card number from the db
        String origCardNumber = giftCard.getString("cardNumber");
        StringBuilder origMaskedNumber = new StringBuilder("");
        int cardLength = origCardNumber.length() - 4;
        if (cardLength > 0) {
            for (int i = 0; i < cardLength; i++) {
                origMaskedNumber.append("*");
            }
            origMaskedNumber.append(origCardNumber.substring(cardLength));
        } else {
            origMaskedNumber.append(origCardNumber);
        }
        // compare the two masked numbers
        if (cardNumber.equals(origMaskedNumber.toString())) {
            cardNumber = origCardNumber;
        }
    }
    context.put("cardNumber", cardNumber);
    newPm = GenericValue.create(paymentMethod);
    toBeStored.add(newPm);
    newGc = GenericValue.create(giftCard);
    toBeStored.add(newGc);
    String newPmId = null;
    try {
        newPmId = delegator.getNextSeqId("PaymentMethod");
    } catch (IllegalArgumentException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingGiftCardCannotBeCreated", locale));
    }
    newPm.set("partyId", partyId);
    newPm.set("fromDate", context.get("fromDate"), false);
    newPm.set("thruDate", context.get("thruDate"));
    newPm.set("description", context.get("description"));
    newGc.set("cardNumber", context.get("cardNumber"));
    newGc.set("pinNumber", context.get("pinNumber"));
    newGc.set("expireDate", context.get("expireDate"));
    if (!newGc.equals(giftCard) || !newPm.equals(paymentMethod)) {
        newPm.set("paymentMethodId", newPmId);
        newGc.set("paymentMethodId", newPmId);
        newPm.set("fromDate", (context.get("fromDate") != null ? context.get("fromDate") : now));
        isModified = true;
    }
    if (isModified) {
        // set thru date on old paymentMethod
        paymentMethod.set("thruDate", now);
        toBeStored.add(paymentMethod);
        try {
            delegator.storeAll(toBeStored);
        } catch (GenericEntityException e) {
            Debug.logWarning(e.getMessage(), module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingEftAccountCannotBeUpdated", UtilMisc.toMap("errorString", e.getMessage()), locale));
        }
    } else {
        result.put("paymentMethodId", paymentMethodId);
        result.put("oldPaymentMethodId", paymentMethodId);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
        result.put(ModelService.SUCCESS_MESSAGE, UtilProperties.getMessage(resource, "AccountingNoChangesMadeNotUpdatingEftAccount", locale));
        return result;
    }
    result.put("paymentMethodId", newGc.getString("paymentMethodId"));
    result.put("oldPaymentMethodId", paymentMethodId);
    result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) Security(org.apache.ofbiz.security.Security) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Aggregations

Security (org.apache.ofbiz.security.Security)79 GenericValue (org.apache.ofbiz.entity.GenericValue)69 Delegator (org.apache.ofbiz.entity.Delegator)60 Locale (java.util.Locale)56 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)54 HashMap (java.util.HashMap)36 Timestamp (java.sql.Timestamp)27 LinkedList (java.util.LinkedList)27 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)20 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)18 Map (java.util.Map)12 HttpSession (javax.servlet.http.HttpSession)7 GeneralException (org.apache.ofbiz.base.util.GeneralException)7 BigDecimal (java.math.BigDecimal)6 List (java.util.List)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)4 File (java.io.File)3