You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by mb...@apache.org on 2017/12/18 11:30:28 UTC
svn commit: r1818553 - in
/ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway:
EwayServices.java GatewayConnector.java GatewayRequest.java
GatewayResponse.java
Author: mbrohl
Date: Mon Dec 18 11:30:28 2017
New Revision: 1818553
URL: http://svn.apache.org/viewvc?rev=1818553&view=rev
Log:
Improved: General refactoring and code improvements, package
org.apache.ofbiz.accounting.thirdparty.eway.
(OFBIZ-9881)
Thanks Julian Leichert for reporting and providing the patches.
Modified:
ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/EwayServices.java
ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayConnector.java
ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayRequest.java
ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayResponse.java
Modified: ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/EwayServices.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/EwayServices.java?rev=1818553&r1=1818552&r2=1818553&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/EwayServices.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/EwayServices.java Mon Dec 18 11:30:28 2017
@@ -36,29 +36,29 @@ import org.apache.ofbiz.service.Dispatch
import org.apache.ofbiz.service.ServiceUtil;
public class EwayServices {
-
+
public static final String module = EwayServices.class.getName();
public final static String resource = "AccountingUiLabels";
-
+
// eway charge (auth w/ capture)
- public static Map<String, Object> ewayCharge(DispatchContext dctx, Map<String, Object> context) {
+ public static Map<String, Object> ewayCharge(DispatchContext dctx, Map<String, Object> context) {
String orderId = (String) context.get("orderId");
String cvv2 = (String) context.get("cardSecurityCode");
String custIp = (String) context.get("customerIpAddress");
- BigDecimal processAmount = (BigDecimal) context.get("processAmount");
+ BigDecimal processAmount = (BigDecimal) context.get("processAmount");
GenericValue cc = (GenericValue) context.get("creditCard");
GenericValue address = (GenericValue) context.get("billingAddress");
GenericValue party = (GenericValue) context.get("billToParty");
-
- GatewayRequest req = initRequest(dctx, context, false);
+
+ GatewayRequest req = initRequest(dctx, context, false);
req.setCustomerInvoiceRef(orderId);
req.setTotalAmount(processAmount);
req.setCustomerIPAddress(custIp);
-
+
// bill to party info
req.setCustomerFirstName(UtilFormatOut.checkNull(party.getString("firstName")));
- req.setCustomerLastName(UtilFormatOut.checkNull(party.getString("lastName")));
-
+ req.setCustomerLastName(UtilFormatOut.checkNull(party.getString("lastName")));
+
// card info
String ccName = cc.getString("firstNameOnCard") + " " + cc.getString("lastNameOnCard");
req.setCardHoldersName(ccName);
Modified: ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayConnector.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayConnector.java?rev=1818553&r1=1818552&r2=1818553&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayConnector.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayConnector.java Mon Dec 18 11:30:28 2017
@@ -29,25 +29,26 @@ import org.apache.ofbiz.base.util.Debug;
/**
* Handles connections to the eWay servers.
- *
+ *
* Based on public domain sample code provided by eWay.com.au
*/
public class GatewayConnector {
-
+
private static final String module = GatewayConnector.class.getName();
-
+
private int timeout = 0;
public GatewayConnector(int timeout) {
this.timeout = timeout;
}
-
+
public GatewayConnector() {
this(60);
}
-
+
/**
* Get the timeout value set in the corresponding setter.
+ *
* @return timeout value in seconds, 0 for infinite
*/
public int getTimeout() {
@@ -58,6 +59,7 @@ public class GatewayConnector {
* Set the timout value. Note that setting the timeout for an HttpURLConnection
* is possible only since Java 1.5. This method has no effect on earlier
* versions.
+ *
* @param time timeout value in seconds, 0 for infinite
*/
public void setTimeout(int time) {
@@ -93,7 +95,7 @@ public class GatewayConnector {
connection.setConnectTimeout(timeout*1000);
try (OutputStream out = connection.getOutputStream();
- Writer wout = new OutputStreamWriter(out, "UTF-8")) {
+ Writer wout = new OutputStreamWriter(out, "UTF-8")) {
wout.write(request.toXml());
wout.flush();
@@ -107,11 +109,14 @@ public class GatewayConnector {
// re-throws exception so that the caller knows what went wrong
Debug.logError(e, e.getMessage(), module);
throw e;
- }
- finally {
- // close connection
- if (in != null) in.close();
- connection.disconnect();
+ } finally {
+ // close resources
+ if (in != null) {
+ in.close();
+ }
+ if (connection != null) {
+ connection.disconnect();
+ }
}
}
}
\ No newline at end of file
Modified: ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayRequest.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayRequest.java?rev=1818553&r1=1818552&r2=1818553&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayRequest.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayRequest.java Mon Dec 18 11:30:28 2017
@@ -25,56 +25,56 @@ import org.apache.ofbiz.base.util.Debug;
/**
* A class representing a payment request. It holds the fields of the request,
* provides setters and getters to manipulate them. It also holds information
- * about the type of the request: the request can be of Real-time, CVN, and
+ * about the type of the request: the request can be of Real-time, CVN, and
* Beagle type, or the combination of the latter two. You set the request type
* in the constructor of the object.
- *
+ *
* Based on public domain sample code provided by eWay.com.au
*/
public class GatewayRequest {
-
+
private static final String module = GatewayRequest.class.getName();
-
+
// request urls
public static final String REQUEST_URL_REFUND_TEST = "";
public static final String REQUEST_URL_REFUND = "https://www.eway.com.au/gateway/xmlpaymentrefund.asp";
-
+
public static final String REQUEST_URL_BEAGLE_TEST = "https://www.eway.com.au/gateway_cvn/xmltest/testpage.asp";
public static final String REQUEST_URL_BEAGLE = "https://www.eway.com.au/gateway_cvn/xmlbeagle.asp";
-
+
public static final String REQUEST_URL_CVN_TEST = "https://www.eway.com.au/gateway_cvn/xmltest/testpage.asp";
public static final String REQUEST_URL_CVN = "https://www.eway.com.au/gateway_cvn/xmlpayment.asp";
-
+
public static final String REQUEST_URL_RT_TEST = "https://www.eway.com.au/gateway/xmltest/testpage.asp";
public static final String REQUEST_URL_RT = "https://www.eway.com.au/gateway/xmlpayment.asp";
-
+
/**
* Constant value to be used with the CVN payment method
*/
public static final int REQUEST_METHOD_CVN = 1;
-
+
/**
* Constant value to be used with the Beagle (GeoIP) payment method
*/
public static final int REQUEST_METHOD_BEAGLE = 2;
-
+
/**
- * Constant value to be used with the Refund method
+ * Constant value to be used with the Refund method
*/
public static final int REQUEST_METHOD_REFUND = 4;
-
+
/**
- * The request method used in the transaction, set in the constructor.
- * This value is the boolean
- * combination of the values REQUEST_METHOD_CVN and REQUEST_METHOD_BEAGLE.
- * Defaults to 0, meaning that Real-time payment method is used.
+ * The request method used in the transaction, set in the constructor. This
+ * value is the boolean combination of the values REQUEST_METHOD_CVN and
+ * REQUEST_METHOD_BEAGLE. Defaults to 0, meaning that Real-time payment method
+ * is used.
*/
-
+
private BigDecimal txTotalAmount = BigDecimal.ZERO;
private boolean isTestMode = false;
- private int requestMethod = 0;
-
- private String txCustomerID = "";
+ private int requestMethod = 0;
+
+ private String txCustomerID = "";
private String txCardHoldersName = "";
private String txCardNumber = "";
private String txCardExpiryMonth = "";
@@ -87,76 +87,74 @@ public class GatewayRequest {
private String txCustomerPostcode = "";
private String txCustomerInvoiceRef = "";
private String txCustomerInvoiceDescription = "";
- private String txCVN = "";
+ private String txCVN = "";
private String txOption1 = "";
private String txOption2 = "";
private String txOption3 = "";
private String txCustomerIPAddress = "";
private String txCustomerBillingCountry = "";
private String txRefundPassword = "";
-
-
+
/**
- * Default constructor to be used with the Real-Time payment method. The
- * same as calling <code>GatewayRequest(0)</code>;
+ * Default constructor to be used with the Real-Time payment method. The same as
+ * calling <code>GatewayRequest(0)</code>;
*/
public GatewayRequest() {
requestMethod = 0;
}
-
+
/**
* Constructor to be used with the CVN and Beagle payment methods.
- * @param method Logical combination of the REQUEST_METHOD_CVN and
- * REQUEST_METHOD_BEAGLE constants.
+ *
+ * @param method
+ * Logical combination of the REQUEST_METHOD_CVN and
+ * REQUEST_METHOD_BEAGLE constants.
*/
public GatewayRequest(int method) {
requestMethod = method;
}
-
+
/**
* Gets the request method given when constructing the object.
- * @return the request method as a logical combination of the
- * REQUEST_METHOD_CVN and REQUEST_METHOD_BEAGLE constants.
+ *
+ * @return the request method as a logical combination of the REQUEST_METHOD_CVN
+ * and REQUEST_METHOD_BEAGLE constants.
*/
public int getRequestMethod() {
return requestMethod;
}
-
+
/**
- * Gets the URL for the configured request
+ * Gets the URL for the configured request
*/
public String getUrl() {
- if ((requestMethod & REQUEST_METHOD_REFUND) != 0) {
+ if ((requestMethod & REQUEST_METHOD_REFUND) != 0) {
if (isTestMode()) {
return null;
- } else {
- return REQUEST_URL_REFUND;
}
+ return REQUEST_URL_REFUND;
} else if ((requestMethod & REQUEST_METHOD_BEAGLE) != 0) {
if (isTestMode()) {
return REQUEST_URL_BEAGLE_TEST;
- } else {
- return REQUEST_URL_BEAGLE;
}
+ return REQUEST_URL_BEAGLE;
} else if ((requestMethod & REQUEST_METHOD_CVN) != 0) {
if (isTestMode()) {
return REQUEST_URL_CVN_TEST;
- } else {
- return REQUEST_URL_CVN;
}
+ return REQUEST_URL_CVN;
} else {
if (isTestMode()) {
return REQUEST_URL_RT_TEST;
- } else {
- return REQUEST_URL_RT;
}
+ return REQUEST_URL_RT;
}
}
/*
* Getters and setters follow for each defined field of the request.
*/
-
+
public String getCustomerID() {
return txCustomerID;
}
@@ -164,11 +162,11 @@ public class GatewayRequest {
public void setCustomerID(String value) {
txCustomerID = value;
}
-
+
public String getRefundPassword() {
return txRefundPassword;
}
-
+
public void setRefundPassword(String value) {
txRefundPassword = value;
}
@@ -288,7 +286,7 @@ public class GatewayRequest {
public String getOption1() {
return txOption1;
}
-
+
public void setOption1(String value) {
txOption1 = value;
}
@@ -308,7 +306,7 @@ public class GatewayRequest {
public void setOption3(String value) {
txOption3 = value;
}
-
+
public String getCustomerIPAddress() {
return txCustomerIPAddress;
}
@@ -316,7 +314,7 @@ public class GatewayRequest {
public void setCustomerIPAddress(String value) {
txCustomerIPAddress = value;
}
-
+
public String getCustomerBillingCountry() {
return txCustomerBillingCountry;
}
@@ -324,21 +322,22 @@ public class GatewayRequest {
public void setCustomerBillingCountry(String value) {
txCustomerBillingCountry = value;
}
-
+
public boolean isTestMode() {
return isTestMode;
}
-
+
public void setTestMode(boolean b) {
isTestMode = b;
}
/**
- * Gives the xml representation of this object. This xml will be sent to
- * the gateway. This method is public only for debugging purposes, you
- * might wish to examine the xml content. The special fields of the CVN
- * and Beagle requests are added only if the request belongs to the CVN or
- * Beagle types, respectively.
+ * Gives the xml representation of this object. This xml will be sent to the
+ * gateway. This method is public only for debugging purposes, you might wish to
+ * examine the xml content. The special fields of the CVN and Beagle requests
+ * are added only if the request belongs to the CVN or Beagle types,
+ * respectively.
+ *
* @return The GatewayRequest object as an xml string.
*/
public String toXml() {
@@ -346,18 +345,18 @@ public class GatewayRequest {
// just to concatenate a String together.
Integer totalInt = txTotalAmount.multiply(new BigDecimal(100)).intValue();
-
+
StringBuffer xml = new StringBuffer("<ewaygateway>");
xml.append(createNode("ewayCustomerID", txCustomerID));
xml.append(createNode("ewayTotalAmount", "" + totalInt));
xml.append(createNode("ewayCustomerInvoiceRef", txCustomerInvoiceRef));
xml.append(createNode("ewayCardExpiryMonth", txCardExpiryMonth));
xml.append(createNode("ewayCardExpiryYear", txCardExpiryYear));
-
+
// all charge methods (not refund)
if (requestMethod != REQUEST_METHOD_REFUND) {
xml.append(createNode("ewayCardHoldersName", txCardHoldersName));
- xml.append(createNode("ewayCardNumber", txCardNumber));
+ xml.append(createNode("ewayCardNumber", txCardNumber));
xml.append(createNode("ewayTrxnNumber", txTrxnNumber));
xml.append(createNode("ewayCustomerInvoiceDescription", txCustomerInvoiceDescription));
xml.append(createNode("ewayCustomerFirstName", txCustomerFirstName));
@@ -366,42 +365,42 @@ public class GatewayRequest {
xml.append(createNode("ewayCustomerAddress", txCustomerAddress));
xml.append(createNode("ewayCustomerPostcode", txCustomerPostcode));
}
-
+
// fill in also CVN data if the request is of CVN type
if (requestMethod == REQUEST_METHOD_CVN || requestMethod == REQUEST_METHOD_BEAGLE) {
xml.append(createNode("ewayCVN", txCVN));
}
-
+
xml.append(createNode("ewayOption1", txOption1));
xml.append(createNode("ewayOption2", txOption2));
xml.append(createNode("ewayOption3", txOption3));
-
+
// fill in also Beagle data if the request is of Beagle type
if (requestMethod == REQUEST_METHOD_BEAGLE) {
xml.append(createNode("ewayCustomerIPAddress", txCustomerIPAddress));
- xml.append(createNode("ewayCustomerBillingCountry",
- txCustomerBillingCountry));
+ xml.append(createNode("ewayCustomerBillingCountry",
+ txCustomerBillingCountry));
}
-
+
// fill in the refund password if REFUND type
if (requestMethod == REQUEST_METHOD_REFUND) {
xml.append(createNode("ewayOriginalTrxnNumber", txTrxnNumber));
xml.append(createNode("ewayRefundPassword", txRefundPassword));
}
-
+
xml.append("</ewaygateway>");
-
+
// log the request for test mode
if (isTestMode()) {
Debug.logInfo("[eWay Request] : " + xml.toString(), module);
}
-
+
return xml.toString();
}
/**
* Helper method to build an XML node.
- *
+ *
* @param nodeName
* The name of the node being created.
* @param nodeValue
Modified: ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayResponse.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayResponse.java?rev=1818553&r1=1818552&r2=1818553&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayResponse.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayResponse.java Mon Dec 18 11:30:28 2017
@@ -33,34 +33,34 @@ import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
/**
- * A class representing the payment gateway's response to a request. It holds
- * fields of the response which are filled in when the response arrives and
+ * A class representing the payment gateway's response to a request. It holds
+ * fields of the response which are filled in when the response arrives and
* available through getter methods. This response class supports all 3 payment
* methods.
- *
+ *
* Based on public domain sample code provided by eWay.com.au
*/
public class GatewayResponse {
private static final String module = GatewayResponse.class.getName();
-
+
// private field definitions, values are set to default
-
+
private double txBeagleScore = -1;
private int txReturnAmount = 0;
-
+
private boolean txTrxnStatus = false;
-
+
private String txTrxnNumber = "";
private String txTrxnReference = "";
private String txTrxnOption1 = "";
private String txTrxnOption2 = "";
- private String txTrxnOption3 = "";
+ private String txTrxnOption3 = "";
private String txAuthCode = "";
private String txTrxnError = "";
-
+
// getter methods for the response fields
-
+
public String getTrxnNumber() {
return txTrxnNumber;
}
@@ -92,7 +92,7 @@ public class GatewayResponse {
public int getReturnAmount() {
return txReturnAmount;
}
-
+
public BigDecimal getTransactionAmount() {
BigDecimal amt = new BigDecimal(getReturnAmount());
amt = amt.divide(new BigDecimal(100));
@@ -104,9 +104,9 @@ public class GatewayResponse {
}
/**
- * Gets the beagle score. Defaults to -1 in case of non-Beagle payment
- * methods or if the response does not contain this field.
- *
+ * Gets the beagle score. Defaults to -1 in case of non-Beagle payment methods
+ * or if the response does not contain this field.
+ *
* @return The beagle score or -1 if it was not defined in the response
*/
public double getBeagleScore() {
@@ -114,15 +114,15 @@ public class GatewayResponse {
}
/**
- * Creates the GatewayResponse object by parsing an xml from a stream. Fills
- * in the fields of the object that are available through getters after this
- * method returns.
- *
+ * Creates the GatewayResponse object by parsing an xml from a stream. Fills in
+ * the fields of the object that are available through getters after this method
+ * returns.
+ *
* @param xmlstream
* the stream to parse the response from
* @throws Exception
- * if the xml contains a root element with a bad name or an
- * unknown element, or if the xml is badly formatted
+ * if the xml contains a root element with a bad name or an unknown
+ * element, or if the xml is badly formatted
*/
public GatewayResponse(InputStream xmlstream, GatewayRequest req) throws Exception {
@@ -145,12 +145,14 @@ public class GatewayResponse {
for (int i = 0; i < length; i++) {
Node node = list.item(i);
String name = node.getNodeName();
- if (name == "ewayResponse")
+ if (name == "ewayResponse") {
continue;
+ }
Text textnode = (Text) node.getFirstChild();
String value = "";
- if (textnode != null)
+ if (textnode != null) {
value = textnode.getNodeValue();
+ }
switch(name) {
case "ewayTrxnError":
@@ -159,7 +161,6 @@ public class GatewayResponse {
if ("true".equals(value.toLowerCase(Locale.getDefault()).trim())) {
txTrxnStatus = true;
}
-
case "ewayTrxnNumber":
txTrxnNumber = value;
case "ewayTrxnOption1":
@@ -185,8 +186,6 @@ public class GatewayResponse {
}
}
-
-
if (req.isTestMode()) {
Debug.logInfo("[eWay Reply]\n" + this.toString(), module);
}
@@ -205,7 +204,7 @@ public class GatewayResponse {
buf.append("\t<ewayReturnAmount>").append(txReturnAmount).append("</ewayReturnAmount>\n");
buf.append("\t<ewayAuthCode>").append(txAuthCode).append("</ewayAuthCode>\n");
buf.append("\t<ewayBeagleScore>").append(txBeagleScore).append("</ewayBeagleScore>\n");
- buf.append("\t<ewayTrxnReference>").append(txTrxnReference).append("</ewayTrxnReference>\n");
+ buf.append("\t<ewayTrxnReference>").append(txTrxnReference).append("</ewayTrxnReference>\n");
buf.append("</ewayResponse>").append("\n");
return buf.toString();
}