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