From df753f80b2be1cb964df0bc9c3cfe30fa3bf83f2 Mon Sep 17 00:00:00 2001 From: Pritam Kute Date: Wed, 24 Jun 2020 17:40:55 +0530 Subject: [PATCH] Adding new plugin for integration of First Data payment gateway with Apache OFBiz. (OFBIZ-11837) --- firstdatapaymentgateway/README.adoc | 25 ++ firstdatapaymentgateway/build.gradle | 25 ++ .../config/FirstDataUiLabels.xml | 63 +++ .../config/firstdata.properties | 23 + .../data/FirstDataDemoData.xml | 30 ++ .../data/FirstDataSecurityGroupDemoData.xml | 29 ++ .../FirstDataSecurityPermissionSeedData.xml | 30 ++ .../data/FirstDataTypeData.xml | 27 ++ .../entitydef/entitymodel.xml | 46 ++ firstdatapaymentgateway/ofbiz-component.xml | 51 +++ .../servicedef/services.xml | 54 +++ .../FirstDataPaymentServices.java | 422 ++++++++++++++++++ .../FirstDataPostAuthTemplate.json.ftl | 7 + .../FirstDataPreAuthTemplate.json.ftl | 19 + .../template/FirstDataRefundTemplate.json.ftl | 7 + .../FirstDataReleaseTemplate.json.ftl | 4 + .../template/FirstDataSaleTemplate.json.ftl | 19 + .../webapp/firstdata/WEB-INF/controller.xml | 53 +++ .../webapp/firstdata/WEB-INF/web.xml | 96 ++++ .../webapp/firstdata/index.jsp | 20 + .../widget/CommonScreens.xml | 67 +++ .../widget/FirstDataForms.xml | 38 ++ .../widget/FirstDataMenus.xml | 25 ++ .../widget/FirstDataScreens.xml | 52 +++ 24 files changed, 1232 insertions(+) create mode 100644 firstdatapaymentgateway/README.adoc create mode 100644 firstdatapaymentgateway/build.gradle create mode 100644 firstdatapaymentgateway/config/FirstDataUiLabels.xml create mode 100644 firstdatapaymentgateway/config/firstdata.properties create mode 100644 firstdatapaymentgateway/data/FirstDataDemoData.xml create mode 100644 firstdatapaymentgateway/data/FirstDataSecurityGroupDemoData.xml create mode 100644 firstdatapaymentgateway/data/FirstDataSecurityPermissionSeedData.xml create mode 100644 firstdatapaymentgateway/data/FirstDataTypeData.xml create mode 100644 firstdatapaymentgateway/entitydef/entitymodel.xml create mode 100644 firstdatapaymentgateway/ofbiz-component.xml create mode 100644 firstdatapaymentgateway/servicedef/services.xml create mode 100644 firstdatapaymentgateway/src/main/java/org/apache/ofbiz/firstdatapaymentgateway/FirstDataPaymentServices.java create mode 100644 firstdatapaymentgateway/template/FirstDataPostAuthTemplate.json.ftl create mode 100644 firstdatapaymentgateway/template/FirstDataPreAuthTemplate.json.ftl create mode 100644 firstdatapaymentgateway/template/FirstDataRefundTemplate.json.ftl create mode 100644 firstdatapaymentgateway/template/FirstDataReleaseTemplate.json.ftl create mode 100644 firstdatapaymentgateway/template/FirstDataSaleTemplate.json.ftl create mode 100644 firstdatapaymentgateway/webapp/firstdata/WEB-INF/controller.xml create mode 100644 firstdatapaymentgateway/webapp/firstdata/WEB-INF/web.xml create mode 100644 firstdatapaymentgateway/webapp/firstdata/index.jsp create mode 100644 firstdatapaymentgateway/widget/CommonScreens.xml create mode 100644 firstdatapaymentgateway/widget/FirstDataForms.xml create mode 100644 firstdatapaymentgateway/widget/FirstDataMenus.xml create mode 100644 firstdatapaymentgateway/widget/FirstDataScreens.xml diff --git a/firstdatapaymentgateway/README.adoc b/firstdatapaymentgateway/README.adoc new file mode 100644 index 000000000..6621b7383 --- /dev/null +++ b/firstdatapaymentgateway/README.adoc @@ -0,0 +1,25 @@ +[[FirstDataPaymentGateway]] += First Data Payment Gateway + +[[steps-to-integrate]] +== Steps to integrate First Data Payment Gateway with Apache OFBiz + +* Checkout or install the "firstdatapaymentgateway" plugin from the Apache OFBiz repository. +https://github.com/apache/ofbiz-plugins.git +* Make sure you load the data from following files manually or using loadAll command. +** FirstDataTypeData.xml +** FirstDataSecurityPermissionSeedData.xml +** FirstDataSecurityGroupDemoData.xml +** FirstDataDemoData.xml +* Make sure you have created your test account on First Data and you have enabled REST APIs for your test merchant account. +Also you have got following configuration parameters from the First Data against your merchant account. +** App Name +** API Key +** API Secret +* Now build and start the server. +* Hit following URL and use admin credentials for login. +`https://localhost:8443/firstdata/control/main` +* Fill in the configuration parameters which you have got from First Data in appropriate fields. +Transaction URL to be entered should be of sandbox server. +`https://cert.api.firstdata.com/gateway/v2` +* Now you can place order and check transactions like Auth, Capture, Refund and Release. \ No newline at end of file diff --git a/firstdatapaymentgateway/build.gradle b/firstdatapaymentgateway/build.gradle new file mode 100644 index 000000000..3ebcfec5a --- /dev/null +++ b/firstdatapaymentgateway/build.gradle @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +dependencies { + //Examples of compile-time and runtime dependencies + + //pluginLibsCompile 'junit:junit-dep:4.10' + //pluginLibsRuntime 'junit:junit-dep:4.10' +} \ No newline at end of file diff --git a/firstdatapaymentgateway/config/FirstDataUiLabels.xml b/firstdatapaymentgateway/config/FirstDataUiLabels.xml new file mode 100644 index 000000000..93ec755a9 --- /dev/null +++ b/firstdatapaymentgateway/config/FirstDataUiLabels.xml @@ -0,0 +1,63 @@ + + + + + + FirstData Application + FirstData应用程序 + FirstData應用程式 + + + OFBiz: FirstData + OFBiz: FirstData + + + Part of the Apache OFBiz Family of Open Source Software + Un modulo della famiglia di software open source Apache OFBiz + 开源软件OFBiz的组成部分 + 開源軟體OFBiz的組成部分 + + + You are not allowed to view this page. + 不允许你浏览这个页面。 + 不允許您檢視這個頁面. + + + First Data Configurations + + + Edit Configurations + + + App Name + + + API Key + + + API Secret + + + Transaction URL + + + Enable Data Vault + + diff --git a/firstdatapaymentgateway/config/firstdata.properties b/firstdatapaymentgateway/config/firstdata.properties new file mode 100644 index 000000000..82dc3d2d3 --- /dev/null +++ b/firstdatapaymentgateway/config/firstdata.properties @@ -0,0 +1,23 @@ +############################################################################### +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +############################################################################### + +paymentgateway.firstdata.template.preauth.location=component://firstdatapaymentgateway/template/FirstDataPreAuthTemplate.json.ftl +paymentgateway.firstdata.template.postauth.location=component://firstdatapaymentgateway/template/FirstDataPostAuthTemplate.json.ftl +paymentgateway.firstdata.template.refund.location=component://firstdatapaymentgateway/template/FirstDataRefundTemplate.json.ftl +paymentgateway.firstdata.template.release.location=component://firstdatapaymentgateway/template/FirstDataReleaseTemplate.json.ftl \ No newline at end of file diff --git a/firstdatapaymentgateway/data/FirstDataDemoData.xml b/firstdatapaymentgateway/data/FirstDataDemoData.xml new file mode 100644 index 000000000..e73b58ac5 --- /dev/null +++ b/firstdatapaymentgateway/data/FirstDataDemoData.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/firstdatapaymentgateway/data/FirstDataSecurityGroupDemoData.xml b/firstdatapaymentgateway/data/FirstDataSecurityGroupDemoData.xml new file mode 100644 index 000000000..ee7d7c21e --- /dev/null +++ b/firstdatapaymentgateway/data/FirstDataSecurityGroupDemoData.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + diff --git a/firstdatapaymentgateway/data/FirstDataSecurityPermissionSeedData.xml b/firstdatapaymentgateway/data/FirstDataSecurityPermissionSeedData.xml new file mode 100644 index 000000000..3d6e7b76e --- /dev/null +++ b/firstdatapaymentgateway/data/FirstDataSecurityPermissionSeedData.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + diff --git a/firstdatapaymentgateway/data/FirstDataTypeData.xml b/firstdatapaymentgateway/data/FirstDataTypeData.xml new file mode 100644 index 000000000..c2abb7d61 --- /dev/null +++ b/firstdatapaymentgateway/data/FirstDataTypeData.xml @@ -0,0 +1,27 @@ + + + + + + + + + + \ No newline at end of file diff --git a/firstdatapaymentgateway/entitydef/entitymodel.xml b/firstdatapaymentgateway/entitydef/entitymodel.xml new file mode 100644 index 000000000..57c9cfc8c --- /dev/null +++ b/firstdatapaymentgateway/entitydef/entitymodel.xml @@ -0,0 +1,46 @@ + + + + + + + + Entity of FirstData Component + None + + + + + + App Name + API Key + API Secret + Transaction URL Test or Live + Enable Data Vault + + + + + + + \ No newline at end of file diff --git a/firstdatapaymentgateway/ofbiz-component.xml b/firstdatapaymentgateway/ofbiz-component.xml new file mode 100644 index 000000000..10cec1a43 --- /dev/null +++ b/firstdatapaymentgateway/ofbiz-component.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/firstdatapaymentgateway/servicedef/services.xml b/firstdatapaymentgateway/servicedef/services.xml new file mode 100644 index 000000000..b552fd9e1 --- /dev/null +++ b/firstdatapaymentgateway/servicedef/services.xml @@ -0,0 +1,54 @@ + + + + + FirstData Services + + 1.0 + + + Update a PaymentGatwayFirstData + + + + + + + First Data Payment Authorization Service + + + + First Data Payment Capture Service + + + + First Data Payment Release Service + + + + First Data Payment Refund Service + + + \ No newline at end of file diff --git a/firstdatapaymentgateway/src/main/java/org/apache/ofbiz/firstdatapaymentgateway/FirstDataPaymentServices.java b/firstdatapaymentgateway/src/main/java/org/apache/ofbiz/firstdatapaymentgateway/FirstDataPaymentServices.java new file mode 100644 index 000000000..a3e63478d --- /dev/null +++ b/firstdatapaymentgateway/src/main/java/org/apache/ofbiz/firstdatapaymentgateway/FirstDataPaymentServices.java @@ -0,0 +1,422 @@ +package org.apache.ofbiz.firstdatapaymentgateway; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import freemarker.template.TemplateException; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.binary.Hex; +import org.apache.commons.codec.digest.HmacAlgorithms; +import org.apache.commons.codec.digest.HmacUtils; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.apache.ofbiz.base.util.Debug; +import org.apache.ofbiz.base.util.UtilValidate; +import org.apache.ofbiz.base.util.template.FreeMarkerWorker; +import org.apache.ofbiz.entity.Delegator; +import org.apache.ofbiz.entity.GenericEntityException; +import org.apache.ofbiz.entity.GenericValue; +import org.apache.ofbiz.entity.util.EntityQuery; +import org.apache.ofbiz.entity.util.EntityUtilProperties; +import org.apache.ofbiz.service.DispatchContext; +import org.apache.ofbiz.service.ModelService; +import org.apache.ofbiz.service.ServiceUtil; + +import java.io.IOException; +import java.io.StringWriter; +import java.math.BigDecimal; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; + +public class FirstDataPaymentServices { + private static final String MODULE = FirstDataPaymentServices.class.getName(); + + private static Properties FDProperties = null; + + public static Map ccAuth(DispatchContext ctx, Map 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 result = ServiceUtil.returnSuccess(); + result.put("processAmount", processAmount); + Boolean isSuccess = Boolean.FALSE; + 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 ccAuthReqContext = new HashMap(); + 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 convertedMap = objectMapper.readValue(responseString, new TypeReference>(){}); + + String transactionStatus = (String) convertedMap.get("transactionStatus"); + String transactionId = (String) convertedMap.get("ipgTransactionId"); + String fdOrderId = (String) convertedMap.get("orderId"); + Map processor = objectMapper.convertValue(convertedMap.get("processor"), new TypeReference>(){}); + String gatewayMessage = (String) processor.get("responseMessage"); + int statusCode = response.getStatusLine().getStatusCode(); + result.put("authCode", String.valueOf(statusCode)); + result.put("authMessage", gatewayMessage); + if (UtilValidate.isNotEmpty(transactionId)) { + result.put("authRefNum", transactionId); + result.put("authAltRefNum", fdOrderId); + if ("approved".equalsIgnoreCase(transactionStatus)) { + isSuccess = Boolean.TRUE; + } + } + if (!isSuccess) { + String errorMessage = "Transaction Type:" + (String) convertedMap.get("transactionType") + " Transaction Id: " + transactionId + " Transaction Status: " + transactionStatus; + errorMessage = errorMessage + " Message: " + statusCode + "-" + 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; + } + + public static Map ccCapture(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + BigDecimal captureAmount = (BigDecimal) context.get("captureAmount"); + String paymentGatewayConfigId = (String) context.get("paymentGatewayConfigId"); + String currency = (String) context.get("currency"); + GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference"); + + Map result = ServiceUtil.returnSuccess(); + result.put("captureAmount", captureAmount); + Boolean isSuccess = Boolean.FALSE; + + try { + String clientRequestId = UUID.randomUUID().toString(); + String epochTime = String.valueOf(System.currentTimeMillis()); + + Map ccPostAuthReqContext = new HashMap(); + ccPostAuthReqContext.put("amount", captureAmount); + ccPostAuthReqContext.put("currency", currency); + + StringWriter outWriter = new StringWriter(); + String firstDataPreAuthTemplate = EntityUtilProperties.getPropertyValue("firstdata", "paymentgateway.firstdata.template.postauth.location", delegator); + FreeMarkerWorker.renderTemplate(firstDataPreAuthTemplate, ccPostAuthReqContext, outWriter); + String requestBody = outWriter.toString(); + + String messageSignature = buildMessageSignature(paymentGatewayConfigId, requestBody, clientRequestId, epochTime, delegator); + + GenericValue paymentGatewayResponse = EntityQuery.use(delegator).from("PaymentGatewayResponse") + .where("orderPaymentPreferenceId", orderPaymentPreference.getString("orderPaymentPreferenceId"), "paymentMethodId", orderPaymentPreference.getString("paymentMethodId"), "transCodeEnumId", "PGT_AUTHORIZE", "paymentServiceTypeEnumId", "PRDS_PAY_AUTH") + .queryFirst(); + String authTransactionId = null; + if (UtilValidate.isNotEmpty(paymentGatewayResponse.getString("referenceNum"))) { + authTransactionId = paymentGatewayResponse.getString("referenceNum"); + } else { + authTransactionId = paymentGatewayResponse.getString("altReference"); + } + + CloseableHttpClient httpClient = HttpClients.createDefault(); + StringEntity stringEntity = new StringEntity(requestBody); + HttpPost httpPost = new HttpPost(FDProperties.getProperty("transactionUrl") + "/payments/" + authTransactionId); + 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 convertedMap = objectMapper.readValue(responseString, new TypeReference>(){}); + + String transactionStatus = (String) convertedMap.get("transactionStatus"); + String transactionId = (String) convertedMap.get("ipgTransactionId"); + String fdOrderId = (String) convertedMap.get("orderId"); + Map processor = objectMapper.convertValue(convertedMap.get("processor"), new TypeReference>(){}); + String gatewayMessage = (String) processor.get("responseMessage"); + int statusCode = response.getStatusLine().getStatusCode(); + result.put("captureCode", String.valueOf(statusCode)); + result.put("captureMessage", gatewayMessage); + if (UtilValidate.isNotEmpty(transactionId)) { + result.put("captureRefNum", transactionId); + result.put("captureAltRefNum", fdOrderId); + if ("approved".equalsIgnoreCase(transactionStatus)) { + isSuccess = Boolean.TRUE; + } + } + if (!isSuccess) { + String errorMessage = "Transaction Type:" + (String) convertedMap.get("transactionType") + " Transaction Id: " + transactionId + " Transaction Status: " + transactionStatus; + errorMessage = errorMessage + " Message: " + statusCode + "-" + gatewayMessage; + result.put(ModelService.ERROR_MESSAGE, errorMessage); + } + } catch (TemplateException | IOException | GenericEntityException e) { + Debug.logError(e, "Could not complete First Data transaction: " + e.toString(), MODULE); + } + result.put("captureResult", isSuccess); + return result; + } + + public static Map ccRefund(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + BigDecimal refundAmount = (BigDecimal) context.get("refundAmount"); + String paymentGatewayConfigId = (String) context.get("paymentGatewayConfigId"); + String currency = (String) context.get("currency"); + GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference"); + + Map result = ServiceUtil.returnSuccess(); + result.put("refundAmount", refundAmount); + Boolean isSuccess = Boolean.FALSE; + + try { + String clientRequestId = UUID.randomUUID().toString(); + String epochTime = String.valueOf(System.currentTimeMillis()); + + Map ccRefundReqContext = new HashMap(); + ccRefundReqContext.put("amount", refundAmount); + ccRefundReqContext.put("currency", currency); + + StringWriter outWriter = new StringWriter(); + String firstDataPreAuthTemplate = EntityUtilProperties.getPropertyValue("firstdata", "paymentgateway.firstdata.template.refund.location", delegator); + FreeMarkerWorker.renderTemplate(firstDataPreAuthTemplate, ccRefundReqContext, outWriter); + String requestBody = outWriter.toString(); + + String messageSignature = buildMessageSignature(paymentGatewayConfigId, requestBody, clientRequestId, epochTime, delegator); + + GenericValue paymentGatewayResponse = EntityQuery.use(delegator) + .from("PaymentGatewayResponse") + .where("orderPaymentPreferenceId", orderPaymentPreference.getString("orderPaymentPreferenceId"), "paymentMethodId", + orderPaymentPreference.getString("paymentMethodId"), "transCodeEnumId", "PGT_CAPTURE", + "paymentServiceTypeEnumId", "PRDS_PAY_CAPTURE") + .queryFirst(); + String captureTransactionId = null; + if (UtilValidate.isNotEmpty(paymentGatewayResponse.getString("referenceNum"))) { + captureTransactionId = paymentGatewayResponse.getString("referenceNum"); + } else { + captureTransactionId = paymentGatewayResponse.getString("altReference"); + } + + CloseableHttpClient httpClient = HttpClients.createDefault(); + StringEntity stringEntity = new StringEntity(requestBody); + HttpPost httpPost = new HttpPost(FDProperties.getProperty("transactionUrl") + "/payments/" + captureTransactionId); + 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 convertedMap = objectMapper.readValue(responseString, new TypeReference>(){}); + + String transactionStatus = (String) convertedMap.get("transactionStatus"); + String transactionId = (String) convertedMap.get("ipgTransactionId"); + String fdOrderId = (String) convertedMap.get("orderId"); + Map processor = objectMapper.convertValue(convertedMap.get("processor"), new TypeReference>(){}); + String gatewayMessage = (String) processor.get("responseMessage"); + int statusCode = response.getStatusLine().getStatusCode(); + result.put("refundCode", String.valueOf(statusCode)); + result.put("refundMessage", gatewayMessage); + if (UtilValidate.isNotEmpty(transactionId)) { + result.put("refundRefNum", transactionId); + result.put("refundAltRefNum", fdOrderId); + if ("approved".equalsIgnoreCase(transactionStatus)) { + isSuccess = Boolean.TRUE; + } + } + if (!isSuccess) { + String errorMessage = "Transaction Type:" + (String) convertedMap.get("transactionType") + " Transaction Id: " + transactionId + " Transaction Status: " + transactionStatus; + errorMessage = errorMessage + " Message: " + statusCode + "-" + gatewayMessage; + result.put(ModelService.ERROR_MESSAGE, errorMessage); + } + } catch (TemplateException | IOException | GenericEntityException e) { + Debug.logError(e, "Could not complete First Data transaction: " + e.toString(), MODULE); + } + result.put("refundResult", isSuccess); + return result; + } + + public static Map ccRelease(DispatchContext dctx, Map context) { + Delegator delegator = dctx.getDelegator(); + BigDecimal releaseAmount = (BigDecimal) context.get("releaseAmount"); + String paymentGatewayConfigId = (String) context.get("paymentGatewayConfigId"); + String currency = (String) context.get("currency"); + GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference"); + + Map result = ServiceUtil.returnSuccess(); + result.put("releaseAmount", releaseAmount); + Boolean isSuccess = Boolean.FALSE; + + try { + String clientRequestId = UUID.randomUUID().toString(); + String epochTime = String.valueOf(System.currentTimeMillis()); + + Map ccReleaseReqContext = new HashMap(); + ccReleaseReqContext.put("comments", "The amount " + currency + " " + releaseAmount + " against OrderPaymentPreferenceId " + orderPaymentPreference.getString("orderPaymentPreferenceId") + " is released."); + + StringWriter outWriter = new StringWriter(); + String firstDataPreAuthTemplate = EntityUtilProperties.getPropertyValue("firstdata", "paymentgateway.firstdata.template.release.location", delegator); + FreeMarkerWorker.renderTemplate(firstDataPreAuthTemplate, ccReleaseReqContext, outWriter); + String requestBody = outWriter.toString(); + + String messageSignature = buildMessageSignature(paymentGatewayConfigId, requestBody, clientRequestId, epochTime, delegator); + + GenericValue paymentGatewayResponse = EntityQuery.use(delegator).from("PaymentGatewayResponse") + .where("orderPaymentPreferenceId", orderPaymentPreference.getString("orderPaymentPreferenceId"), "paymentMethodId", orderPaymentPreference.getString("paymentMethodId"), "transCodeEnumId", "PGT_AUTHORIZE", "paymentServiceTypeEnumId", "PRDS_PAY_AUTH") + .queryFirst(); + String releaseTransactionId = null; + if (UtilValidate.isNotEmpty(paymentGatewayResponse.getString("referenceNum"))) { + releaseTransactionId = paymentGatewayResponse.getString("referenceNum"); + } else { + releaseTransactionId = paymentGatewayResponse.getString("altReference"); + } + + CloseableHttpClient httpClient = HttpClients.createDefault(); + StringEntity stringEntity = new StringEntity(requestBody); + HttpPost httpPost = new HttpPost(FDProperties.getProperty("transactionUrl") + "/payments/" + releaseTransactionId); + 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 convertedMap = objectMapper.readValue(responseString, new TypeReference>(){}); + + String transactionStatus = (String) convertedMap.get("transactionStatus"); + String transactionId = (String) convertedMap.get("ipgTransactionId"); + String fdOrderId = (String) convertedMap.get("orderId"); + Map processor = objectMapper.convertValue(convertedMap.get("processor"), new TypeReference>(){}); + String gatewayMessage = (String) processor.get("responseMessage"); + int statusCode = response.getStatusLine().getStatusCode(); + result.put("releaseCode", String.valueOf(statusCode)); + result.put("releaseMessage", gatewayMessage); + if (UtilValidate.isNotEmpty(transactionId)) { + result.put("releaseRefNum", transactionId); + result.put("releaseAltRefNum", fdOrderId); + if ("approved".equalsIgnoreCase(transactionStatus)) { + isSuccess = Boolean.TRUE; + } + } + if (!isSuccess) { + String errorMessage = "Transaction Type:" + (String) convertedMap.get("transactionType") + " Transaction Id: " + transactionId + " Transaction Status: " + transactionStatus; + errorMessage = errorMessage + " Message: " + statusCode + "-" + gatewayMessage; + result.put(ModelService.ERROR_MESSAGE, errorMessage); + } + } catch (TemplateException | IOException | GenericEntityException e) { + Debug.logError(e, "Could not complete First Data transaction: " + e.toString(), MODULE); + } + result.put("releaseResult", isSuccess); + return result; + } + + private static Properties buildFDProperties(String paymentGatewayConfigId, Delegator delegator) { + String transactionUrl = getPaymentGatewayConfigValue(delegator, paymentGatewayConfigId, "transactionUrl"); + String appName = getPaymentGatewayConfigValue(delegator, paymentGatewayConfigId, "appName"); + String apiKey = getPaymentGatewayConfigValue(delegator, paymentGatewayConfigId, "apiKey"); + String apiSecret = getPaymentGatewayConfigValue(delegator, paymentGatewayConfigId, "apiSecret"); + //String enableDataVault = getPaymentGatewayConfigValue(delegator, paymentGatewayConfigId, "enableDataVault"); + + Properties props = new Properties(); + props.put("transactionUrl", transactionUrl); + props.put("appName", appName); + props.put("apiKey", apiKey); + props.put("apiSecret", apiSecret); + //props.put("enableDataVault", enableDataVault); + + if (FDProperties == null) { + FDProperties = props; + } + + return props; + } + + private static String getPaymentGatewayConfigValue(Delegator delegator, String paymentGatewayConfigId, String paymentGatewayConfigParameterName) { + String returnValue = null; + if (UtilValidate.isNotEmpty(paymentGatewayConfigId)) { + try { + GenericValue paymentGatewayFirstData = EntityQuery.use(delegator).from("PaymentGatewayFirstData").where("paymentGatewayConfigId", paymentGatewayConfigId).queryOne(); + if (paymentGatewayFirstData != null) { + Object payflowProField = paymentGatewayFirstData.get(paymentGatewayConfigParameterName); + if (payflowProField != null) { + returnValue = payflowProField.toString().trim(); + } + } + } catch (GenericEntityException e) { + Debug.logError(e, MODULE); + } + } + return returnValue; + } + + private static String buildMessageSignature(String paymentGatewayConfigId, String requestBody, String clientRequestId, String epochTime, Delegator delegator) { + String messageSignature = null; + if (FDProperties == null) { + buildFDProperties(paymentGatewayConfigId, delegator); + } + + String apiKey = FDProperties.getProperty("apiKey"); + final HmacUtils hmacHelper = new HmacUtils(HmacAlgorithms.HMAC_SHA_256, FDProperties.getProperty("apiSecret")); + final Hex hexHelper = new Hex(); + final String msg = apiKey + clientRequestId + epochTime + requestBody; + final byte[] raw = hmacHelper.hmac(msg); + final byte[] hex = hexHelper.encode(raw); + messageSignature = Base64.encodeBase64String(hex); + return messageSignature; + } +} diff --git a/firstdatapaymentgateway/template/FirstDataPostAuthTemplate.json.ftl b/firstdatapaymentgateway/template/FirstDataPostAuthTemplate.json.ftl new file mode 100644 index 000000000..16ddaa359 --- /dev/null +++ b/firstdatapaymentgateway/template/FirstDataPostAuthTemplate.json.ftl @@ -0,0 +1,7 @@ +{ + "requestType": "PostAuthTransaction", + "transactionAmount": { + "total": "${amount}", + "currency": "${currency}" + } +} \ No newline at end of file diff --git a/firstdatapaymentgateway/template/FirstDataPreAuthTemplate.json.ftl b/firstdatapaymentgateway/template/FirstDataPreAuthTemplate.json.ftl new file mode 100644 index 000000000..dbf7a04a6 --- /dev/null +++ b/firstdatapaymentgateway/template/FirstDataPreAuthTemplate.json.ftl @@ -0,0 +1,19 @@ +{ + "transactionAmount": { + "total": "${amount}", + "currency": "${currency}" + }, + "requestType": "PaymentCardPreAuthTransaction", + "paymentMethod": { + "paymentCard": { + "number": "${cardNumber}", + <#if cardSecurityCode?has_content> + "securityCode": "${cardSecurityCode}", + + "expiryDate": { + "month": "${expireMonth}", + "year": "${expireYear}" + } + } + } +} \ No newline at end of file diff --git a/firstdatapaymentgateway/template/FirstDataRefundTemplate.json.ftl b/firstdatapaymentgateway/template/FirstDataRefundTemplate.json.ftl new file mode 100644 index 000000000..fcfca6554 --- /dev/null +++ b/firstdatapaymentgateway/template/FirstDataRefundTemplate.json.ftl @@ -0,0 +1,7 @@ +{ + "requestType": "ReturnTransaction", + "transactionAmount": { + "total": "${amount}", + "currency": "${currency}" + } +} \ No newline at end of file diff --git a/firstdatapaymentgateway/template/FirstDataReleaseTemplate.json.ftl b/firstdatapaymentgateway/template/FirstDataReleaseTemplate.json.ftl new file mode 100644 index 000000000..d1929798e --- /dev/null +++ b/firstdatapaymentgateway/template/FirstDataReleaseTemplate.json.ftl @@ -0,0 +1,4 @@ +{ + "requestType": "VoidTransaction", + "comments": "${comments}" +} \ No newline at end of file diff --git a/firstdatapaymentgateway/template/FirstDataSaleTemplate.json.ftl b/firstdatapaymentgateway/template/FirstDataSaleTemplate.json.ftl new file mode 100644 index 000000000..06a062549 --- /dev/null +++ b/firstdatapaymentgateway/template/FirstDataSaleTemplate.json.ftl @@ -0,0 +1,19 @@ +{ + "transactionAmount": { + "total": "${amount}", + "currency": "${currency}" + }, + "requestType": "PaymentCardSaleTransaction", + "paymentMethod": { + "paymentCard": { + "number": "${cardNumber}", + <#if cardSecurityCode?has_content> + "securityCode": "${cardSecurityCode}", + + "expiryDate": { + "month": "${expireMonth}", + "year": "${expireYear}" + } + } + } +} \ No newline at end of file diff --git a/firstdatapaymentgateway/webapp/firstdata/WEB-INF/controller.xml b/firstdatapaymentgateway/webapp/firstdata/WEB-INF/controller.xml new file mode 100644 index 000000000..2fe151c4c --- /dev/null +++ b/firstdatapaymentgateway/webapp/firstdata/WEB-INF/controller.xml @@ -0,0 +1,53 @@ + + + + + + + + FirstData Component Site Configuration File + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/firstdatapaymentgateway/webapp/firstdata/WEB-INF/web.xml b/firstdatapaymentgateway/webapp/firstdata/WEB-INF/web.xml new file mode 100644 index 000000000..94554aebc --- /dev/null +++ b/firstdatapaymentgateway/webapp/firstdata/WEB-INF/web.xml @@ -0,0 +1,96 @@ + + + + + Apache OFBiz - FirstData Component + FirstData Component of the Apache OFBiz Project + + + + A unique name used to identify/recognize the local dispatcher for the Service Engine + localDispatcherNamefirstdatapaymentgateway + + + The Name of the Entity Delegator to use, defined in entityengine.xml + entityDelegatorNamedefault + + + The location of the main-decorator screen to use for this webapp; referred to as a context variable in screen def XML files. + mainDecoratorLocation + component://firstdatapaymentgateway/widget/CommonScreens.xml + + + Remove unnecessary whitespace from HTML output. + compressHTML + false + + + + ControlFilter + ControlFilter + org.apache.ofbiz.webapp.control.ControlFilter + + allowedPaths + /error:/control:/select:/index.html:/index.jsp:/default.html:/default.jsp:/images + + redirectPath/control/main + + + ContextFilter + ContextFilter + org.apache.ofbiz.webapp.control.ContextFilter + + + SameSiteFilter + SameSiteFilter + org.apache.ofbiz.webapp.control.SameSiteFilter + + ControlFilter/* + ContextFilter/* + SameSiteFilter/* + + org.apache.ofbiz.webapp.control.ControlEventListener + org.apache.ofbiz.webapp.control.LoginEventListener + + + + + Main Control Servlet + ControlServlet + ControlServlet + org.apache.ofbiz.webapp.control.ControlServlet + 1 + + ControlServlet/control/* + + + 60 + + + + index.jsp + index.html + index.htm + + diff --git a/firstdatapaymentgateway/webapp/firstdata/index.jsp b/firstdatapaymentgateway/webapp/firstdata/index.jsp new file mode 100644 index 000000000..4ea7d0c8b --- /dev/null +++ b/firstdatapaymentgateway/webapp/firstdata/index.jsp @@ -0,0 +1,20 @@ +<%-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--%> + +<%response.sendRedirect("control/main");%> \ No newline at end of file diff --git a/firstdatapaymentgateway/widget/CommonScreens.xml b/firstdatapaymentgateway/widget/CommonScreens.xml new file mode 100644 index 000000000..b91abec7f --- /dev/null +++ b/firstdatapaymentgateway/widget/CommonScreens.xml @@ -0,0 +1,67 @@ + + + + + + +
+ + + + + + + + + + + + + + + +
+
+ + +
+ + + + + +
+ + + + + + + + + +
+
+
+
+
+
+
\ No newline at end of file diff --git a/firstdatapaymentgateway/widget/FirstDataForms.xml b/firstdatapaymentgateway/widget/FirstDataForms.xml new file mode 100644 index 000000000..9219800d4 --- /dev/null +++ b/firstdatapaymentgateway/widget/FirstDataForms.xml @@ -0,0 +1,38 @@ + + + + + +
+ + + + + + + + + + +
+
\ No newline at end of file diff --git a/firstdatapaymentgateway/widget/FirstDataMenus.xml b/firstdatapaymentgateway/widget/FirstDataMenus.xml new file mode 100644 index 000000000..93614db64 --- /dev/null +++ b/firstdatapaymentgateway/widget/FirstDataMenus.xml @@ -0,0 +1,25 @@ + + + + + + + + \ No newline at end of file diff --git a/firstdatapaymentgateway/widget/FirstDataScreens.xml b/firstdatapaymentgateway/widget/FirstDataScreens.xml new file mode 100644 index 000000000..e5b43f28c --- /dev/null +++ b/firstdatapaymentgateway/widget/FirstDataScreens.xml @@ -0,0 +1,52 @@ + + + + + + +
+ + + + + + + + + + +
+ + + + + + + + +
+
+
+
+
+
+ +
\ No newline at end of file