PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/classes/com/sapienter/jbilling/server/payment/tasks/PaymentAtlasTask.java

https://github.com/bibulous/SkyrackJbill2.2
Java | 312 lines | 246 code | 47 blank | 19 comment | 35 complexity | ca97a80c4a1f4aee4cec5904168b76ec MD5 | raw file
  1. /*
  2. jBilling - The Enterprise Open Source Billing System
  3. Copyright (C) 2003-2009 Enterprise jBilling Software Ltd. and Emiliano Conde
  4. This file is part of jbilling.
  5. jbilling is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU Affero General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. jbilling is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Affero General Public License for more details.
  13. You should have received a copy of the GNU Affero General Public License
  14. along with jbilling. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. package com.sapienter.jbilling.server.payment.tasks;
  17. import java.math.BigDecimal;
  18. import java.net.MalformedURLException;
  19. import java.net.URL;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.log4j.Logger;
  25. import org.apache.xmlrpc.XmlRpcException;
  26. import org.apache.xmlrpc.client.XmlRpcClient;
  27. import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
  28. import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
  29. import com.sapienter.jbilling.server.payment.PaymentAuthorizationBL;
  30. import com.sapienter.jbilling.server.payment.PaymentAuthorizationDTOEx;
  31. import com.sapienter.jbilling.server.payment.PaymentDTOEx;
  32. import com.sapienter.jbilling.server.payment.db.PaymentAuthorizationDTO;
  33. import com.sapienter.jbilling.server.payment.db.PaymentResultDAS;
  34. import com.sapienter.jbilling.server.pluggableTask.PaymentTaskWithTimeout;
  35. import com.sapienter.jbilling.server.pluggableTask.TaskException;
  36. import com.sapienter.jbilling.server.pluggableTask.admin.PluggableTaskException;
  37. import com.sapienter.jbilling.server.user.ContactBL;
  38. import com.sapienter.jbilling.server.user.CreditCardBL;
  39. import com.sapienter.jbilling.server.user.contact.db.ContactDTO;
  40. import com.sapienter.jbilling.server.util.Constants;
  41. public class PaymentAtlasTask extends PaymentTaskWithTimeout {
  42. public static final String PARAMETER_MERCHANT_ACCOUNT_CODE = "merchant_account_code";
  43. public static final String PARAMETER_TEST = "test";
  44. public static final String PARAMETER_AVS = "submit_avs";
  45. public static final String PARAMETER_PASSWORD = "password";
  46. private static final String URL = "https://atlasportal.net:8443/gates/xmlrpc";
  47. private static final String TEST_URL = "https://atlasbilling.net:8443/gates/xmlrpc";
  48. private static final int CONNECTION_TIME_OUT = 10000; // in millisec
  49. private static final int REPLY_TIME_OUT = 30000; // in millisec
  50. private Logger log = null;
  51. public boolean process(PaymentDTOEx paymentInfo)
  52. throws PluggableTaskException {
  53. boolean retValue = false;
  54. log = Logger.getLogger(PaymentAtlasTask.class);
  55. if (paymentInfo.getPayoutId() != null) {
  56. return true;
  57. }
  58. try {
  59. if (paymentInfo.getCreditCard() == null) {
  60. log.error("Can't process without a credit card");
  61. throw new TaskException("Credit card not present in payment");
  62. }
  63. if (paymentInfo.getAch() != null) {
  64. log.error("Can't process with a cheque");
  65. throw new TaskException("Can't process ACH charge");
  66. }
  67. if (paymentInfo.getIsRefund() == 1
  68. && (paymentInfo.getPayment() == null || paymentInfo
  69. .getPayment().getAuthorization() == null)) {
  70. log
  71. .error("Can't process refund without a payment with an authorization record");
  72. throw new TaskException("Refund without previous authorization");
  73. }
  74. validateParameters();
  75. Map<String, Object> data;
  76. if (paymentInfo.getIsRefund() == 0) {
  77. data = getChargeData(paymentInfo);
  78. } else {
  79. data = getRefundData(paymentInfo);
  80. }
  81. if ("true".equals(getOptionalParameter(PARAMETER_AVS, "false"))) {
  82. addAVSFields(paymentInfo.getUserId(), data);
  83. log.debug("returning after avs " + data);
  84. }
  85. PaymentAuthorizationDTO response = makeCall(data, true);
  86. paymentInfo.setAuthorization(response);
  87. if ("1".equals(response.getCode1())) {
  88. paymentInfo.setPaymentResult(new PaymentResultDAS().find(Constants.RESULT_OK));
  89. log.debug("result is ok");
  90. } else {
  91. paymentInfo.setPaymentResult(new PaymentResultDAS().find(Constants.RESULT_FAIL));
  92. log.debug("result is fail");
  93. }
  94. PaymentAuthorizationBL bl = new PaymentAuthorizationBL();
  95. bl.create(response, paymentInfo.getId());
  96. } catch (MalformedURLException e) {
  97. log.error("MalformedURLException exception when calling Atlas", e);
  98. paymentInfo.setPaymentResult(new PaymentResultDAS().find(Constants.RESULT_UNAVAILABLE));
  99. retValue = true;
  100. } catch (XmlRpcException e) {
  101. log.error("XmlRpcException exception when calling Atlas", e);
  102. paymentInfo.setPaymentResult(new PaymentResultDAS().find(Constants.RESULT_UNAVAILABLE));
  103. retValue = false;
  104. } catch (PluggableTaskException e) {
  105. log.error("PluggableTaskException", e);
  106. throw e;
  107. } catch (Exception e) {
  108. log.error("Exception", e);
  109. throw new PluggableTaskException(e);
  110. }
  111. log.debug("returning " + retValue);
  112. return retValue;
  113. }
  114. private void validateParameters() throws PluggableTaskException {
  115. ensureGetParameter(PARAMETER_MERCHANT_ACCOUNT_CODE);
  116. ensureGetParameter(PARAMETER_PASSWORD);
  117. }
  118. public void failure(Integer userId, Integer retry) {
  119. }
  120. private Map<String, Object> getData(PaymentDTOEx paymentInfo)
  121. throws PluggableTaskException {
  122. Map<String, Object> data = new HashMap<String, Object>();
  123. data.put("merchantAccountCode",
  124. ensureGetParameter(PARAMETER_MERCHANT_ACCOUNT_CODE));
  125. if (paymentInfo.getUserId() != null)
  126. data.put("customerAccountCode", String.valueOf(paymentInfo
  127. .getUserId()));
  128. data.put("accountNumber", paymentInfo.getCreditCard().getNumber());
  129. data.put("name", paymentInfo.getCreditCard().getName());
  130. data.put("amount", paymentInfo.getAmount().multiply(new BigDecimal("100")).intValue());
  131. data.put("taxAmount", 0);
  132. String securityCode = paymentInfo.getCreditCard().getSecurityCode();
  133. if (securityCode != null)
  134. data.put("cvv2", securityCode);
  135. data.put("expirationDate", CreditCardBL.get4digitExpiry(paymentInfo
  136. .getCreditCard()));
  137. data.put("transactionDate", paymentInfo.getPaymentDate());
  138. data.put("transactionCode", paymentInfo.getId() + "");
  139. return data;
  140. }
  141. private Map<String, Object> getChargeData(PaymentDTOEx paymentInfo)
  142. throws PluggableTaskException {
  143. Map<String, Object> data = getData(paymentInfo);
  144. data.put("creditIndicator", Boolean.FALSE);
  145. data.put("type", "sale-request");
  146. return data;
  147. }
  148. private Map<String, Object> getRefundData(PaymentDTOEx paymentInfo)
  149. throws PluggableTaskException {
  150. Map<String, Object> data = getData(paymentInfo);
  151. data.put("itemCode", paymentInfo.getPayment().getAuthorization()
  152. .getTransactionId());
  153. data.put("creditIndicator", Boolean.TRUE);
  154. data.put("type", "credit-request");
  155. return data;
  156. }
  157. private void addAVSFields(Integer userId, Map<String, Object> data) {
  158. try {
  159. ContactBL contact = new ContactBL();
  160. contact.set(userId);
  161. ContactDTO entity = contact.getEntity();
  162. data.put("city", entity.getCity());
  163. data.put("email", entity.getEmail());
  164. data.put("customerAccountCode", userId.toString());
  165. data.put("phone", entity.getPhoneNumber());
  166. data.put("state", entity.getStateProvince());
  167. data.put("street", entity.getAddress1() + " "
  168. + entity.getAddress2());
  169. data.put("zipCode", entity.getPostalCode());
  170. data.put("isOrganization", Boolean.FALSE);
  171. } catch (Exception e) {
  172. log.error("Exception when trying to add the AVS fields", e);
  173. }
  174. }
  175. private PaymentAuthorizationDTO makeCall(Map<String, Object> data,
  176. boolean isCharge) throws XmlRpcException, MalformedURLException,
  177. PluggableTaskException {
  178. log = Logger.getLogger(PaymentAtlasTask.class);
  179. URL callURL = null;
  180. if ("true".equals(getOptionalParameter(PARAMETER_TEST, "false"))) {
  181. callURL = new URL(TEST_URL);
  182. log.debug("Running Atlas task in test mode!");
  183. } else {
  184. callURL = new URL(URL);
  185. }
  186. String merchantAccountCode = ensureGetParameter(PARAMETER_MERCHANT_ACCOUNT_CODE);
  187. int merchantCode = Integer.parseInt(merchantAccountCode);
  188. merchantCode = merchantCode - (merchantCode % 1000);
  189. XmlRpcClient paymentProcessor = new XmlRpcClient();
  190. paymentProcessor.setTransportFactory(new XmlRpcCommonsTransportFactory(
  191. paymentProcessor));
  192. XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
  193. config.setServerURL(callURL);
  194. config.setEnabledForExtensions(true);
  195. config.setConnectionTimeout(CONNECTION_TIME_OUT);
  196. config.setReplyTimeout(REPLY_TIME_OUT);
  197. paymentProcessor.setConfig(config);
  198. List<Map<String, Object>> transactionRequestList = new ArrayList<Map<String, Object>>(
  199. 1);
  200. transactionRequestList.add(data);
  201. Map<String, Object> configParam = new HashMap<String, Object>();
  202. if (isCharge) {
  203. configParam.put("waitConfirmation", "ignore");
  204. } else {
  205. configParam.put("waitConfirmation", "terminate");
  206. }
  207. Object[] params = new Object[] { String.valueOf(merchantCode),
  208. ensureGetParameter(PARAMETER_PASSWORD), transactionRequestList,
  209. configParam };
  210. Object[] resresponse = (Object[]) paymentProcessor.execute(
  211. "XMLRPCGates.processRetail", params);
  212. Map<String, Object> transactionResponseMap = (Map<String, Object>) resresponse[0];
  213. log.debug("Got response:" + transactionResponseMap);
  214. boolean isCredit = "credit-response".equals(transactionResponseMap
  215. .get("type"));
  216. PaymentAuthorizationDTO dbRow = new PaymentAuthorizationDTO();
  217. if (!isCredit
  218. && "A01".equals(transactionResponseMap.get("responseCode"))) {
  219. dbRow.setCode1("1"); // code if 1 it is ok
  220. } else if (isCredit
  221. && "A02".equals(transactionResponseMap.get("responseCode"))) {
  222. dbRow.setCode1("1");
  223. } else if ('A' != ((String) transactionResponseMap.get("responseCode"))
  224. .charAt(0)) {
  225. dbRow.setCode1("2");
  226. } else {
  227. dbRow.setCode1("0");
  228. }
  229. dbRow.setCode3((String) transactionResponseMap.get("responseCode"));
  230. dbRow.setResponseMessage((String) transactionResponseMap
  231. .get("responseMessage"));
  232. dbRow.setApprovalCode((String) transactionResponseMap
  233. .get("processorCode"));
  234. dbRow.setAvs((String) transactionResponseMap.get("avsResultCode"));
  235. dbRow.setTransactionId((String) transactionResponseMap
  236. .get("referenceNumber"));
  237. dbRow.setProcessor("Intrannuity");
  238. return dbRow;
  239. }
  240. public boolean preAuth(PaymentDTOEx payment) throws PluggableTaskException {
  241. log = Logger.getLogger(PaymentAtlasTask.class);
  242. try {
  243. validateParameters();
  244. Map<String, Object> data = getChargeData(payment);
  245. PaymentAuthorizationDTO response = makeCall(data, false);
  246. PaymentAuthorizationDTO authDtoEx = new PaymentAuthorizationDTO(
  247. response);
  248. PaymentAuthorizationBL bl = new PaymentAuthorizationBL();
  249. bl.create(authDtoEx, payment.getId());
  250. payment.setAuthorization(authDtoEx);
  251. return false;
  252. } catch (Exception e) {
  253. log.error("error trying to pre-authorize", e);
  254. return true;
  255. }
  256. }
  257. public boolean confirmPreAuth(PaymentAuthorizationDTO auth,
  258. PaymentDTOEx paymentInfo) throws PluggableTaskException {
  259. return true;
  260. }
  261. }