001: /*
002: * $Id: WorldPayEvents.java,v 1.3 2003/09/02 02:17:15 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.accounting.thirdparty.worldpay;
026:
027: import java.io.IOException;
028: import java.util.List;
029:
030: import javax.servlet.ServletContext;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.ofbiz.base.util.Debug;
035: import org.ofbiz.base.util.UtilFormatOut;
036: import org.ofbiz.base.util.UtilHttp;
037: import org.ofbiz.base.util.UtilMisc;
038: import org.ofbiz.base.util.UtilProperties;
039: import org.ofbiz.entity.GenericDelegator;
040: import org.ofbiz.entity.GenericEntityException;
041: import org.ofbiz.entity.GenericValue;
042: import org.ofbiz.entity.util.EntityUtil;
043: import org.ofbiz.product.catalog.CatalogWorker;
044: import org.ofbiz.product.store.ProductStoreWorker;
045: import org.ofbiz.service.LocalDispatcher;
046:
047: import com.worldpay.core.ArgumentException;
048: import com.worldpay.protocols.http.HTTPURL;
049: import com.worldpay.protocols.http.URLParameters;
050: import com.worldpay.select.PurchaseToken;
051: import com.worldpay.select.Select;
052: import com.worldpay.select.SelectCurrency;
053: import com.worldpay.select.SelectDefs;
054: import com.worldpay.select.SelectException;
055: import com.worldpay.util.Currency;
056: import com.worldpay.util.CurrencyAmount;
057:
058: /**
059: * WorldPay Select Pro Events/Services
060: *
061: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
062: * @version $Revision: 1.3 $
063: * @since 2.0
064: */
065: public class WorldPayEvents {
066:
067: public static final String module = WorldPayEvents.class.getName();
068:
069: public static String worldPayRequest(HttpServletRequest request,
070: HttpServletResponse response) {
071: ServletContext application = ((ServletContext) request
072: .getAttribute("servletContext"));
073: GenericDelegator delegator = (GenericDelegator) request
074: .getAttribute("delegator");
075: LocalDispatcher dispatcher = (LocalDispatcher) request
076: .getAttribute("dispatcher");
077: GenericValue userLogin = (GenericValue) request.getSession()
078: .getAttribute("userLogin");
079:
080: // we need the websiteId for the correct properties file
081: String webSiteId = CatalogWorker.getWebSiteId(request);
082:
083: // get the orderId from the request, stored by previous event(s)
084: String orderId = (String) request.getAttribute("order_id");
085:
086: if (orderId == null) {
087: Debug
088: .logError(
089: "Problems getting orderId, was not found in request",
090: module);
091: request
092: .setAttribute("_ERROR_MESSAGE_",
093: "<li>OrderID not found, please contact customer service.");
094: return "error";
095: }
096:
097: // get the order header for total and other information
098: GenericValue orderHeader = null;
099: try {
100: orderHeader = delegator.findByPrimaryKey("OrderHeader",
101: UtilMisc.toMap("orderId", orderId));
102: } catch (GenericEntityException e) {
103: Debug.logError(e,
104: "Cannot not get OrderHeader from datasource",
105: module);
106: request
107: .setAttribute("_ERROR_MESSAGE_",
108: "<li>Problems getting order information, please contact customer service.");
109: return "error";
110: }
111:
112: // get the contact address to pass over
113: GenericValue contactAddress = null;
114: try {
115: List addresses = delegator.findByAnd("OrderContactMech",
116: UtilMisc.toMap("orderId", orderId,
117: "contactMechPurposeTypeId",
118: "BILLING_LOCATION"));
119: if (addresses == null || addresses.size() == 0)
120: addresses = delegator.findByAnd("OrderContactMech",
121: UtilMisc.toMap("orderId", orderId,
122: "contactMechPurposeTypeId",
123: "SHIPPING_LOCATION"));
124: GenericValue contactMech = EntityUtil.getFirst(addresses);
125: contactAddress = delegator.findByPrimaryKey(
126: "PostalAddress", UtilMisc.toMap("contactMechId",
127: contactMech.getString("contactMechId")));
128: } catch (GenericEntityException e) {
129: Debug.logWarning(e,
130: "Problems getting order contact information",
131: module);
132: }
133:
134: // get the country geoID
135: GenericValue countryGeo = null;
136: if (contactAddress != null) {
137: try {
138: countryGeo = contactAddress.getRelatedOne("CountryGeo");
139: } catch (GenericEntityException e) {
140: Debug.logWarning(e,
141: "Problems getting country geo entity", module);
142: }
143: }
144:
145: // string of customer's name
146: String name = null;
147: if (contactAddress != null) {
148: if (contactAddress.get("attnName") != null
149: && contactAddress.getString("attnName").length() > 0)
150: name = contactAddress.getString("attnName");
151: else if (contactAddress.get("toName") != null
152: && contactAddress.getString("toName").length() > 0)
153: name = contactAddress.getString("toName");
154: }
155:
156: // build an address string
157: StringBuffer address = null;
158: if (contactAddress != null) {
159: address = new StringBuffer();
160: if (contactAddress.get("address1") != null) {
161: address.append(contactAddress.getString("address1")
162: .trim());
163: }
164: if (contactAddress.get("address2") != null) {
165: if (address.length() > 0)
166: address.append(" ");
167: address.append(contactAddress.getString("address2")
168: .trim());
169: }
170: if (contactAddress.get("city") != null) {
171: if (address.length() > 0)
172: address.append(" ");
173: address.append(contactAddress.getString("city").trim());
174: }
175: if (contactAddress.get("stateProvinceGeoId") != null) {
176: if (contactAddress.get("city") != null)
177: address.append(", ");
178: address.append(contactAddress.getString(
179: "stateProvinceGeoId").trim());
180: }
181: }
182:
183: // get the telephone number to pass over
184: String phoneNumber = null;
185: GenericValue phoneContact = null;
186:
187: // get the email address to pass over
188: String emailAddress = null;
189: GenericValue emailContact = null;
190: try {
191: List emails = delegator.findByAnd("OrderContactMech",
192: UtilMisc.toMap("orderId", orderId,
193: "contactMechPurposeTypeId", "ORDER_EMAIL"));
194: GenericValue firstEmail = EntityUtil.getFirst(emails);
195: emailContact = delegator.findByPrimaryKey("ContactMech",
196: UtilMisc.toMap("contactMechId", firstEmail
197: .getString("contactMechId")));
198: emailAddress = emailContact.getString("infoString");
199: } catch (GenericEntityException e) {
200: Debug.logWarning(e, "Problems getting order email address",
201: module);
202: }
203:
204: // get the product store
205: GenericValue productStore = null;
206: try {
207: productStore = orderHeader.getRelatedOne("ProductStore");
208: } catch (GenericEntityException e) {
209: Debug.logError(e,
210: "Unable to get ProductStore from OrderHeader",
211: module);
212:
213: }
214: if (productStore == null) {
215: Debug.logError("ProductStore is null", module);
216: request
217: .setAttribute("_ERROR_MESSAGE_",
218: "<li>Problems getting merchant configuration, please contact customer service.");
219: return "error";
220: }
221:
222: // get the payment properties file
223: GenericValue paymentConfig = ProductStoreWorker
224: .getProductStorePaymentSetting(delegator, productStore
225: .getString("productStoreId"), "EXT_WORLDPAY",
226: null, true);
227: String configString = null;
228: if (paymentConfig != null) {
229: configString = paymentConfig
230: .getString("paymentPropertiesPath");
231: }
232:
233: if (configString == null) {
234: configString = "payment.properties";
235: }
236:
237: String instId = UtilProperties.getPropertyValue(configString,
238: "payment.worldpay.instId", "NONE");
239: String authMode = UtilProperties.getPropertyValue(configString,
240: "payment.worldpay.authMode", "A");
241: String testMode = UtilProperties.getPropertyValue(configString,
242: "payment.worldpay.testMode", "100");
243: String fixContact = UtilProperties.getPropertyValue(
244: configString, "payment.worldpay.fixContact", "N");
245: String hideContact = UtilProperties.getPropertyValue(
246: configString, "payment.worldpay.hideContact", "N");
247: String confirmTemplate = UtilProperties.getPropertyValue(
248: configString, "payment.worldpay.confirmTemplate", "");
249: String timeout = UtilProperties.getPropertyValue(configString,
250: "payment.worldpay.timeout", "0");
251: String company = UtilFormatOut.checkEmpty(productStore
252: .getString("companyName"), "");
253: String defCur = UtilFormatOut.checkEmpty(productStore
254: .getString("defaultCurrencyUomId"), "USD");
255:
256: // order description
257: String description = "Order #" + orderId;
258: if (company != null && company.length() > 0)
259: description = description + " from " + company;
260:
261: // check the instId - very important
262: if (instId == null || instId.equals("NONE")) {
263: Debug.logError(
264: "Worldpay InstId not found, cannot continue",
265: module);
266: request
267: .setAttribute("_ERROR_MESSAGE_",
268: "<li>Problems getting merchant configuration, please contact customer service.");
269: return "error";
270: }
271:
272: int instIdInt = 0;
273: try {
274: instIdInt = Integer.parseInt(instId);
275: } catch (NumberFormatException nfe) {
276: Debug.logError(nfe,
277: "Problem converting instId string to integer",
278: module);
279: request
280: .setAttribute("_ERROR_MESSAGE_",
281: "<li>Problems getting merchant configuration, please contact customer service.");
282: return "error";
283: }
284:
285: // check the testMode
286: int testModeInt = -1;
287: if (testMode != null) {
288: try {
289: testModeInt = Integer.parseInt(testMode);
290: } catch (NumberFormatException nfe) {
291: Debug
292: .logWarning(
293: nfe,
294: "Problems getting the testMode value, setting to 0",
295: module);
296: testModeInt = 0;
297: }
298: }
299:
300: // create the purchase link
301: String purchaseURL = null;
302: HTTPURL link = null;
303: URLParameters linkParms = null;
304: try {
305: purchaseURL = Select.getPurchaseURL();
306: link = new HTTPURL(purchaseURL);
307: linkParms = link.getParameters();
308: } catch (SelectException e) {
309: Debug.logError(e, "Problems creating the purchase url",
310: module);
311: request
312: .setAttribute("_ERROR_MESSAGE_",
313: "<li>Problem creating link to WorldPay, please contact customer service.");
314: return "error";
315: } catch (ArgumentException e) {
316: Debug.logError(e, "Problems creating HTTPURL link", module);
317: request
318: .setAttribute("_ERROR_MESSAGE_",
319: "<li>Problem creating link to WorldPay, please contact customer service.");
320: return "error";
321: }
322:
323: // create the currency amount
324: double orderTotal = orderHeader.getDouble("grandTotal")
325: .doubleValue();
326: CurrencyAmount currencyAmount = null;
327: try {
328: Currency currency = SelectCurrency
329: .getInstanceByISOCode(defCur);
330: currencyAmount = new CurrencyAmount(orderTotal, currency);
331: } catch (ArgumentException ae) {
332: Debug.logError(ae, "Problems building CurrencyAmount",
333: module);
334: request
335: .setAttribute("_ERROR_MESSAGE_",
336: "<li>Merchant Configuration Error, please contact customer service.");
337: return "error";
338: }
339:
340: // create a purchase token
341: PurchaseToken token = null;
342: try {
343: token = new PurchaseToken(instIdInt, currencyAmount,
344: orderId);
345: } catch (SelectException e) {
346: Debug.logError(e, "Cannot create purchase token", module);
347: } catch (ArgumentException e) {
348: Debug.logError(e, "Cannot create purchase token", module);
349: }
350: if (token == null) {
351: request
352: .setAttribute("_ERROR_MESSAGE_",
353: "<li>Problems creating a purchase token, please contact customer service.");
354: return "error";
355: }
356:
357: // set the auth/test modes
358: try {
359: token.setAuthorisationMode(authMode);
360: } catch (SelectException e) {
361: Debug.logWarning(e,
362: "Problems setting the authorization mode", module);
363: }
364: token.setTestMode(testModeInt);
365:
366: // set the token to the purchase link
367: try {
368: linkParms
369: .setValue(SelectDefs.SEL_purchase, token.produce());
370: } catch (SelectException e) {
371: Debug.logError(e, "Problems producing token", module);
372: request
373: .setAttribute("_ERROR_MESSAGE_",
374: "<li>Problems producing purchase token, please contact customer service.");
375: return "error";
376: }
377:
378: // set the customer data in the link
379: linkParms.setValue(SelectDefs.SEL_desc, description);
380: linkParms.setValue(SelectDefs.SEL_name, name != null ? name
381: : "");
382: linkParms.setValue(SelectDefs.SEL_address,
383: address != null ? address.toString() : "");
384: linkParms.setValue(SelectDefs.SEL_postcode,
385: contactAddress != null ? contactAddress
386: .getString("postalCode") : "");
387: linkParms.setValue(SelectDefs.SEL_country, countryGeo
388: .getString("geoCode"));
389: linkParms.setValue(SelectDefs.SEL_tel,
390: phoneNumber != null ? phoneNumber : "");
391: linkParms.setValue(SelectDefs.SEL_email,
392: emailAddress != null ? emailAddress : "");
393:
394: // set some optional data
395: if (fixContact != null
396: && fixContact.toUpperCase().startsWith("Y")) {
397: linkParms.setValue(SelectDefs.SEL_fixContact, "Y");
398: }
399: if (hideContact != null
400: && hideContact.toUpperCase().startsWith("Y")) {
401: linkParms.setValue("hideContact", "Y"); // why is this not in SelectDefs??
402: }
403:
404: // now set some send-back parameters
405: linkParms.setValue("M_controlPath", (String) request
406: .getAttribute("_CONTROL_PATH_"));
407: linkParms.setValue("M_userLoginId", userLogin
408: .getString("userLoginId"));
409: linkParms.setValue("M_dispatchName", dispatcher.getName());
410: linkParms.setValue("M_delegatorName", delegator
411: .getDelegatorName());
412: linkParms.setValue("M_webSiteId", webSiteId);
413: linkParms.setValue("M_localLocale", UtilHttp.getLocale(request)
414: .toString());
415: linkParms.setValue("M_confirmTemplate",
416: confirmTemplate != null ? confirmTemplate : "");
417:
418: // redirect to worldpay
419: try {
420: response.sendRedirect(link.produce());
421: } catch (IOException e) {
422: Debug.logError(e, "Problems redirecting to Worldpay",
423: module);
424: request
425: .setAttribute("_ERROR_MESSAGE_",
426: "<li>Problems connecting with WorldPay, please contact customer service.");
427: return "error";
428: }
429:
430: return "success";
431: }
432:
433: }
|