Search in sources :

Example 86 with GenericServiceException

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

the class SecurePayServiceTest method testdoCapture.

public void testdoCapture() throws Exception {
    Debug.logInfo("=====[testdoCapture] starting....", module);
    GenericValue paymentGatewayResponse = EntityQuery.use(delegator).from("PaymentGatewayResponse").where("paymentGatewayResponseId", "testOrder1000_01").queryOne();
    try {
        Map<String, Object> serviceInput = UtilMisc.<String, Object>toMap("paymentConfig", configFile, "orderPaymentPreference", orderPaymentPreference, "authTrans", paymentGatewayResponse);
        serviceInput.put("captureAmount", refundAmount);
        // run the service
        Map<String, Object> result = dispatcher.runSync("ofbScCapture", serviceInput);
        // verify the results
        String responseMessage = (String) result.get(ModelService.RESPONSE_MESSAGE);
        Debug.logInfo("[testdoCapture] responseMessage: " + responseMessage, module);
        TestCase.assertEquals("Service result is success", ModelService.RESPOND_SUCCESS, responseMessage);
        if (((Boolean) result.get("captureResult")).equals(new Boolean(false))) {
            // returnCode ok?
            Debug.logInfo("[testdoCapture] Error Messages from SecurePay: " + result.get("internalRespMsgs"), module);
            TestCase.fail("Returned messages:" + result.get("internalRespMsgs"));
        } else {
            String captureRefNum = (String) result.get("captureRefNum");
            GenericValue checkPaymentGatewayResponse = EntityQuery.use(delegator).from("PaymentGatewayResponse").where("paymentGatewayResponseId", "testOrder1000_01").queryOne();
            checkPaymentGatewayResponse.set("referenceNum", captureRefNum);
            checkPaymentGatewayResponse.store();
            Debug.logInfo("[testdoCapture] Result from SecurePay: " + result, module);
        }
    } catch (GenericServiceException ex) {
        TestCase.fail(ex.getMessage());
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 87 with GenericServiceException

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

the class PayPalEvents method payPalIPN.

/**
 * PayPal Call-Back Event
 * @throws IOException
 */
public static String payPalIPN(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Locale locale = UtilHttp.getLocale(request);
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    // get the product store
    GenericValue productStore = ProductStoreWorker.getProductStore(request);
    if (productStore == null) {
        Debug.logError("ProductStore is null", module);
        request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "payPalEvents.problemsGettingMerchantConfiguration", locale));
        return "error";
    }
    // get the payment properties file
    GenericValue paymentConfig = ProductStoreWorker.getProductStorePaymentSetting(delegator, productStore.getString("productStoreId"), "EXT_PAYPAL", null, true);
    String configString = null;
    String paymentGatewayConfigId = null;
    if (paymentConfig != null) {
        paymentGatewayConfigId = paymentConfig.getString("paymentGatewayConfigId");
        configString = paymentConfig.getString("paymentPropertiesPath");
    }
    if (configString == null) {
        configString = "payment.properties";
    }
    // get the confirm URL
    String confirmUrl = getPaymentGatewayConfigValue(delegator, paymentGatewayConfigId, "confirmUrl", configString, "payment.paypal.confirm");
    // get the redirect URL
    String redirectUrl = getPaymentGatewayConfigValue(delegator, paymentGatewayConfigId, "redirectUrl", configString, "payment.paypal.redirect");
    if (UtilValidate.isEmpty(confirmUrl) || UtilValidate.isEmpty(redirectUrl)) {
        Debug.logError("Payment properties is not configured properly, no confirm URL defined!", module);
        request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "payPalEvents.problemsGettingMerchantConfiguration", locale));
        return "error";
    }
    // first verify this is valid from PayPal
    Map<String, Object> parametersMap = UtilHttp.getParameterMap(request);
    parametersMap.put("cmd", "_notify-validate");
    // send off the confirm request
    String confirmResp = null;
    String str = UtilHttp.urlEncodeArgs(parametersMap);
    URL u = new URL(redirectUrl);
    URLConnection uc = u.openConnection();
    uc.setDoOutput(true);
    uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    try (BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(uc.getOutputStream(), "UTF-8"))) {
        pw.println(str);
        confirmResp = in.readLine();
        Debug.logError("PayPal Verification Response: " + confirmResp, module);
    } catch (IOException e) {
        Debug.logError(e, "Problems sending verification message.", module);
    }
    Debug.logInfo("Got verification from PayPal, processing..", module);
    boolean verified = false;
    for (String name : parametersMap.keySet()) {
        String value = request.getParameter(name);
        Debug.logError("### Param: " + name + " => " + value, module);
        if (UtilValidate.isNotEmpty(name) && "payer_status".equalsIgnoreCase(name) && UtilValidate.isNotEmpty(value) && "verified".equalsIgnoreCase(value)) {
            verified = true;
        }
    }
    if (!verified) {
        Debug.logError("###### PayPal did not verify this request, need investigation!", module);
    }
    // get the system user
    GenericValue userLogin = null;
    try {
        userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", "system").queryOne();
    } catch (GenericEntityException e) {
        Debug.logError(e, "Cannot get UserLogin for: system; cannot continue", module);
        request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "payPalEvents.problemsGettingAuthenticationUser", locale));
        return "error";
    }
    // get the orderId
    String orderId = request.getParameter("invoice");
    // get the order header
    GenericValue orderHeader = null;
    if (UtilValidate.isNotEmpty(orderId)) {
        try {
            orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId).queryOne();
        } catch (GenericEntityException e) {
            Debug.logError(e, "Cannot get the order header for order: " + orderId, module);
            request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "payPalEvents.problemsGettingOrderHeader", locale));
            return "error";
        }
    } else {
        Debug.logError("PayPal did not callback with a valid orderId!", module);
        request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "payPalEvents.noValidOrderIdReturned", locale));
        return "error";
    }
    if (orderHeader == null) {
        Debug.logError("Cannot get the order header for order: " + orderId, module);
        request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "payPalEvents.problemsGettingOrderHeader", locale));
        return "error";
    }
    // get the transaction status
    String paymentStatus = request.getParameter("payment_status");
    // attempt to start a transaction
    boolean okay = true;
    boolean beganTransaction = false;
    try {
        beganTransaction = TransactionUtil.begin();
        if ("Completed".equals(paymentStatus)) {
            okay = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId);
        } else if ("Failed".equals(paymentStatus) || "Denied".equals(paymentStatus)) {
            okay = OrderChangeHelper.cancelOrder(dispatcher, userLogin, orderId);
        }
        if (okay) {
            // set the payment preference
            okay = setPaymentPreferences(delegator, dispatcher, userLogin, orderId, request);
        }
    } catch (Exception e) {
        String errMsg = "Error handling PayPal notification";
        Debug.logError(e, errMsg, module);
        try {
            TransactionUtil.rollback(beganTransaction, errMsg, e);
        } catch (GenericTransactionException gte2) {
            Debug.logError(gte2, "Unable to rollback transaction", module);
        }
    } finally {
        if (!okay) {
            try {
                TransactionUtil.rollback(beganTransaction, "Failure in processing PayPal callback", null);
            } catch (GenericTransactionException gte) {
                Debug.logError(gte, "Unable to rollback transaction", module);
            }
        } else {
            try {
                TransactionUtil.commit(beganTransaction);
            } catch (GenericTransactionException gte) {
                Debug.logError(gte, "Unable to commit transaction", module);
            }
        }
    }
    if (okay) {
        // call the email confirm service
        Map<String, String> emailContext = UtilMisc.toMap("orderId", orderId);
        try {
            dispatcher.runSync("sendOrderConfirmation", emailContext);
        } catch (GenericServiceException e) {
            Debug.logError(e, "Problems sending email confirmation", module);
        }
    }
    return "success";
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) ParseException(java.text.ParseException) IOException(java.io.IOException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BufferedReader(java.io.BufferedReader) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) OutputStreamWriter(java.io.OutputStreamWriter) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) PrintWriter(java.io.PrintWriter)

Example 88 with GenericServiceException

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

the class SagePayPaymentServices method processCardReleasePayment.

private static Map<String, Object> processCardReleasePayment(DispatchContext ctx, Map<String, Object> context) {
    Map<String, Object> result = ServiceUtil.returnSuccess();
    Locale locale = (Locale) context.get("locale");
    LocalDispatcher dispatcher = ctx.getDispatcher();
    String paymentGatewayConfigId = (String) context.get("paymentGatewayConfigId");
    BigDecimal amount = (BigDecimal) context.get("releaseAmount");
    GenericValue authTransaction = (GenericValue) context.get("authTransaction");
    String orderId = (String) authTransaction.get("altReference");
    String refNum = (String) authTransaction.get("referenceNum");
    try {
        Map<String, Object> paymentResult = dispatcher.runSync("SagePayPaymentRelease", UtilMisc.toMap("paymentGatewayConfigId", paymentGatewayConfigId, "vendorTxCode", orderId, "releaseAmount", amount.toString(), "vpsTxId", refNum, "securityKey", authTransaction.get("gatewayFlag"), "txAuthNo", authTransaction.get("gatewayCode")));
        Debug.logInfo("SagePay - SagePayPaymentRelease result : " + paymentResult, module);
        String status = (String) paymentResult.get("status");
        String statusDetail = (String) paymentResult.get("statusDetail");
        if (status != null && "OK".equals(status)) {
            Debug.logInfo("SagePay Payment Released for Order : " + orderId, module);
            result = SagePayUtil.buildCardReleasePaymentResponse(Boolean.TRUE, null, amount, refNum, orderId, statusDetail);
        } else {
            Debug.logInfo("SagePay - Invalid status " + status + " received for order : " + orderId, module);
            result = SagePayUtil.buildCardReleasePaymentResponse(Boolean.FALSE, null, amount, refNum, orderId, statusDetail);
        }
    } catch (GenericServiceException e) {
        Debug.logError(e, "Error in calling SagePayPaymentRelease", module);
        result = ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingSagePayPaymentReleaseException", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) BigDecimal(java.math.BigDecimal)

Example 89 with GenericServiceException

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

the class SagePayPaymentServices method processCardAuthorisationPayment.

private static Map<String, Object> processCardAuthorisationPayment(DispatchContext ctx, Map<String, Object> context) {
    Map<String, Object> result = ServiceUtil.returnSuccess();
    LocalDispatcher dispatcher = ctx.getDispatcher();
    Locale locale = (Locale) context.get("locale");
    Map<String, String> billingInfo = buildCustomerBillingInfo(context);
    String paymentGatewayConfigId = (String) context.get("paymentGatewayConfigId");
    try {
        Map<String, Object> paymentResult = dispatcher.runSync("SagePayPaymentAuthentication", UtilMisc.toMap("paymentGatewayConfigId", paymentGatewayConfigId, "vendorTxCode", billingInfo.get("orderId"), "cardHolder", billingInfo.get("cardHolder"), "cardNumber", billingInfo.get("cardNumber"), "expiryDate", billingInfo.get("expiryDate"), "cardType", billingInfo.get("cardType"), "cv2", billingInfo.get("cv2"), "description", billingInfo.get("description"), "amount", billingInfo.get("amount"), "currency", billingInfo.get("currency"), "billingAddress", billingInfo.get("billingAddress"), "billingPostCode", billingInfo.get("billingPostCode")));
        Debug.logInfo("SagePay - SagePayPaymentAuthentication result : " + paymentResult, module);
        String transactionType = (String) paymentResult.get("transactionType");
        String status = (String) paymentResult.get("status");
        String statusDetail = (String) paymentResult.get("statusDetail");
        String vpsTxId = (String) paymentResult.get("vpsTxId");
        String securityKey = (String) paymentResult.get("securityKey");
        String txAuthNo = (String) paymentResult.get("txAuthNo");
        String vendorTxCode = (String) paymentResult.get("vendorTxCode");
        String amount = (String) paymentResult.get("amount");
        if (status != null && "OK".equals(status)) {
            Debug.logInfo("SagePay - Payment authorized for order : " + vendorTxCode, module);
            result = SagePayUtil.buildCardAuthorisationPaymentResponse(Boolean.TRUE, txAuthNo, securityKey, new BigDecimal(amount), vpsTxId, vendorTxCode, statusDetail);
            if ("PAYMENT".equals(transactionType)) {
                Map<String, Object> captureResult = SagePayUtil.buildCardCapturePaymentResponse(Boolean.TRUE, txAuthNo, securityKey, new BigDecimal(amount), vpsTxId, vendorTxCode, statusDetail);
                result.putAll(captureResult);
            }
        } else if (status != null && "INVALID".equals(status)) {
            Debug.logInfo("SagePay - Invalid authorisation request for order : " + vendorTxCode, module);
            result = SagePayUtil.buildCardAuthorisationPaymentResponse(Boolean.FALSE, null, null, BigDecimal.ZERO, "INVALID", vendorTxCode, statusDetail);
        } else if (status != null && "MALFORMED".equals(status)) {
            Debug.logInfo("SagePay - Malformed authorisation request for order : " + vendorTxCode, module);
            result = SagePayUtil.buildCardAuthorisationPaymentResponse(Boolean.FALSE, null, null, BigDecimal.ZERO, "MALFORMED", vendorTxCode, statusDetail);
        } else if (status != null && "NOTAUTHED".equals(status)) {
            Debug.logInfo("SagePay - NotAuthed authorisation request for order : " + vendorTxCode, module);
            result = SagePayUtil.buildCardAuthorisationPaymentResponse(Boolean.FALSE, null, securityKey, BigDecimal.ZERO, vpsTxId, vendorTxCode, statusDetail);
        } else if (status != null && "REJECTED".equals(status)) {
            Debug.logInfo("SagePay - Rejected authorisation request for order : " + vendorTxCode, module);
            result = SagePayUtil.buildCardAuthorisationPaymentResponse(Boolean.FALSE, null, securityKey, new BigDecimal(amount), vpsTxId, vendorTxCode, statusDetail);
        } else {
            Debug.logInfo("SagePay - Invalid status " + status + " received for order : " + vendorTxCode, module);
            result = SagePayUtil.buildCardAuthorisationPaymentResponse(Boolean.FALSE, null, null, BigDecimal.ZERO, "ERROR", vendorTxCode, statusDetail);
        }
    } catch (GenericServiceException e) {
        Debug.logError(e, "Error in calling SagePayPaymentAuthentication", module);
        result = ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingSagePayPaymentAuthorisationException", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    return result;
}
Also used : Locale(java.util.Locale) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) BigDecimal(java.math.BigDecimal)

Example 90 with GenericServiceException

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

the class SagePayPaymentServices method processCardCapturePayment.

private static Map<String, Object> processCardCapturePayment(DispatchContext ctx, Map<String, Object> context) {
    Map<String, Object> result = ServiceUtil.returnSuccess();
    LocalDispatcher dispatcher = ctx.getDispatcher();
    Locale locale = (Locale) context.get("locale");
    String paymentGatewayConfigId = (String) context.get("paymentGatewayConfigId");
    GenericValue authTransaction = (GenericValue) context.get("authTransaction");
    BigDecimal amount = (BigDecimal) context.get("captureAmount");
    String vendorTxCode = (String) authTransaction.get("altReference");
    String vpsTxId = (String) authTransaction.get("referenceNum");
    String securityKey = (String) authTransaction.get("gatewayFlag");
    String txAuthCode = (String) authTransaction.get("gatewayCode");
    try {
        Map<String, Object> paymentResult = dispatcher.runSync("SagePayPaymentAuthorisation", UtilMisc.toMap("paymentGatewayConfigId", paymentGatewayConfigId, "vendorTxCode", vendorTxCode, "vpsTxId", vpsTxId, "securityKey", securityKey, "txAuthNo", txAuthCode, "amount", amount.toString()));
        Debug.logInfo("SagePay - SagePayPaymentAuthorisation result : " + paymentResult, module);
        String status = (String) paymentResult.get("status");
        String statusDetail = (String) paymentResult.get("statusDetail");
        if (status != null && "OK".equals(status)) {
            Debug.logInfo("SagePay Payment Released for Order : " + vendorTxCode, module);
            result = SagePayUtil.buildCardCapturePaymentResponse(Boolean.TRUE, txAuthCode, securityKey, amount, vpsTxId, vendorTxCode, statusDetail);
        } else {
            Debug.logInfo("SagePay - Invalid status " + status + " received for order : " + vendorTxCode, module);
            result = SagePayUtil.buildCardCapturePaymentResponse(Boolean.FALSE, txAuthCode, securityKey, amount, vpsTxId, vendorTxCode, statusDetail);
        }
    } catch (GenericServiceException e) {
        Debug.logError(e, "Error in calling SagePayPaymentAuthorisation", module);
        result = ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingSagePayPaymentAuthorisationException", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) BigDecimal(java.math.BigDecimal)

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