public static Map ccAuth()

in firstdatapaymentgateway/src/main/java/org/apache/ofbiz/firstdatapaymentgateway/FirstDataPaymentServices.java [65:153]


    public static Map<String, Object> ccAuth(DispatchContext ctx, Map<String, Object> context) {
        Delegator delegator = ctx.getDelegator();
        BigDecimal processAmount = (BigDecimal) context.get("processAmount");
        String orderId = (String) context.get("orderId");
        String currency = (String) context.get("currency");
        String cardSecurityCode = (String) context.get("cardSecurityCode");
        GenericValue creditCard = (GenericValue) context.get("creditCard");
        String paymentGatewayConfigId = (String) context.get("paymentGatewayConfigId");
        Map<String, Object> result = ServiceUtil.returnSuccess();
        result.put("processAmount", processAmount);
        Boolean isSuccess = Boolean.FALSE;
        String gatewayMessage = "";
        String cardNumber = creditCard.getString("cardNumber");

        try {
            String clientRequestId = UUID.randomUUID().toString();
            String epochTime = String.valueOf(System.currentTimeMillis());
            Date expireDate = new SimpleDateFormat("MM/yyyy").parse(creditCard.getString("expireDate"));
            SimpleDateFormat df = new SimpleDateFormat("MM");
            String strMonth = df.format(expireDate);
            df = new SimpleDateFormat("yy");
            String strYear = df.format(expireDate);

            Map<String, Object> ccAuthReqContext = new HashMap<String, Object>();
            ccAuthReqContext.put("amount", processAmount);
            ccAuthReqContext.put("currency", currency);
            ccAuthReqContext.put("cardSecurityCode", cardSecurityCode);
            ccAuthReqContext.put("cardNumber", cardNumber);
            ccAuthReqContext.put("expireMonth", strMonth);
            ccAuthReqContext.put("expireYear", strYear);

            StringWriter outWriter = new StringWriter();
            String firstDataPreAuthTemplate = EntityUtilProperties.getPropertyValue("firstdata", "paymentgateway.firstdata.template.preauth"
                    + ".location", delegator);
            FreeMarkerWorker.renderTemplate(firstDataPreAuthTemplate, ccAuthReqContext, outWriter);
            String requestBody = outWriter.toString();

            String messageSignature = buildMessageSignature(paymentGatewayConfigId, requestBody, clientRequestId, epochTime, delegator);

            CloseableHttpClient httpClient = HttpClients.createDefault();
            StringEntity stringEntity = new StringEntity(requestBody);
            HttpPost httpPost = new HttpPost(fdProperties.getProperty("transactionUrl") + "/payments");
            httpPost.setEntity(stringEntity);
            httpPost.setHeader("Client-Request-Id", clientRequestId);
            httpPost.setHeader("Api-Key", fdProperties.getProperty("apiKey"));
            httpPost.setHeader("Timestamp", epochTime);
            httpPost.setHeader("Message-Signature", messageSignature);
            httpPost.setHeader("Content-Type", "application/json");

            CloseableHttpResponse response = httpClient.execute(httpPost);

            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity);

            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, Object> convertedMap = objectMapper.readValue(responseString, new TypeReference<Map<String, Object>>() {
            });

            String transactionStatus = (String) convertedMap.get("transactionStatus");
            String transactionId = (String) convertedMap.get("ipgTransactionId");
            String fdOrderId = (String) convertedMap.get("orderId");
            result.put("authRefNum", transactionId);
            result.put("authAltRefNum", fdOrderId);
            if ("APPROVED".equalsIgnoreCase(transactionStatus) || "WAITING".equalsIgnoreCase(transactionStatus)) {
                Map<String, Object> processor = objectMapper.convertValue(convertedMap.get("processor"), new TypeReference<Map<String, Object>>() {
                });
                gatewayMessage = (String) processor.get("responseMessage");
                int statusCode = response.getStatusLine().getStatusCode();
                result.put("authCode", String.valueOf(statusCode));
                result.put("authMessage", gatewayMessage);
                if (UtilValidate.isNotEmpty(transactionId)) {
                    if ("APPROVED".equalsIgnoreCase(transactionStatus) || "WAITING".equalsIgnoreCase(transactionStatus)) {
                        isSuccess = Boolean.TRUE;
                    }
                }
            }
            if (!isSuccess) {
                String errorMessage = "Transaction Type:" + (String) convertedMap.get("transactionType") + " Transaction Id: " + transactionId + " "
                        + "Transaction Status: " + transactionStatus;
                gatewayMessage = UtilValidate.isNotEmpty(gatewayMessage) ? gatewayMessage : "";
                errorMessage = errorMessage + " Gateway Message: " + gatewayMessage;
                result.put(ModelService.ERROR_MESSAGE, errorMessage);
            }
        } catch (ParseException | TemplateException | IOException e) {
            Debug.logError(e, "Could not complete First Data transaction: " + e.toString(), MODULE);
        }
        result.put("authResult", isSuccess);
        return result;
    }