001: /*
002: * Created on Nov 2, 2004
003: */
004: package org.openedit.store.gateway;
005:
006: import java.io.File;
007: import java.io.FileInputStream;
008: import java.io.FileNotFoundException;
009: import java.io.IOException;
010: import java.util.Properties;
011: import java.util.Random;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015: import org.openedit.money.Money;
016: import org.openedit.store.CreditPaymentMethod;
017: import org.openedit.store.Store;
018: import org.openedit.store.StoreException;
019: import org.openedit.store.customer.Address;
020: import org.openedit.store.customer.Customer;
021: import org.openedit.store.orders.BaseOrderProcessor;
022: import org.openedit.store.orders.Order;
023: import org.openedit.store.orders.OrderState;
024:
025: import com.openecho.Echo;
026: import com.openedit.WebPageRequest;
027:
028: /**
029: * @author cburkey
030: *
031: */
032: public class EchoOrderProcessor extends BaseOrderProcessor {
033: // Go here to see transactions: https://wwws.echo-inc.com/Review/
034: private static final Log log = LogFactory
035: .getLog(EchoOrderProcessor.class);
036: protected boolean fieldAddRandomAmountToTotal = false;
037:
038: /*
039: * (non-javadoc)
040: *
041: * @see com.openedit.store.EmailOrderArchive#exportNewOrder(com.openedit.WebPageContext,
042: * com.openedit.store.Store, com.openedit.store.Order)
043: */
044: public void processNewOrder(WebPageRequest inContext,
045: Store inStore, Order inOrder) throws StoreException {
046: // Load properties
047: File configDirectory = new File(inStore.getStoreDirectory(),
048: "configuration");
049: File propertiesFile = new File(configDirectory,
050: "echo.properties");
051: FileInputStream stream = null;
052:
053: try {
054: stream = new FileInputStream(propertiesFile);
055: } catch (FileNotFoundException e1) {
056: e1.printStackTrace();
057: }
058: Properties props = new Properties();
059: try {
060: props.load(stream);
061: } catch (IOException e2) {
062: e2.printStackTrace();
063: }
064:
065: // id
066: String id = props.getProperty("id");
067: String pin = props.getProperty("pin");
068: String test_id = props.getProperty("test-id");
069: String test_pin = props.getProperty("test-pin");
070: String use_test_id_str = props.getProperty("use-test-id");
071: boolean use_test_id = new Boolean(use_test_id_str)
072: .booleanValue();
073: // add_random
074: String add_random_str = props.getProperty("add-random-amount");
075: boolean add_random = new Boolean(add_random_str).booleanValue();
076: setAddRandomAmountToTotal(add_random);
077: // force grand total
078: String force_grand_total = props
079: .getProperty("force-grand-total");
080:
081: if (inOrder.getPaymentMethod().requiresValidation()) {
082: try {
083: Echo echo = new Echo();
084:
085: // Customer Info
086: Customer customer = inOrder.getCustomer();
087: Address address = customer.getBillingAddress();
088: echo.billing_first_name = customer.getFirstName();
089: echo.billing_last_name = customer.getLastName();
090: echo.billing_company_name = customer.getCompany();
091: echo.billing_address1 = address.getAddress1();
092: echo.billing_address2 = address.getAddress2();
093: echo.billing_city = address.getCity();
094: echo.billing_state = address.getState();
095: echo.billing_zip = address.get5DigitZipCode();
096: echo.billing_country = address.getCountry();
097: echo.billing_phone = customer.getPhone1();
098: echo.billing_email = customer.getEmail();
099: //echo.billing_ip_address = getRequest().getRemoteAddr();
100:
101: // Submit Request
102: CreditPaymentMethod creditCard = (CreditPaymentMethod) inOrder
103: .getPaymentMethod();
104: echo.cc_number = creditCard.getCardNumber();
105: echo.ccexp_month = creditCard
106: .getExpirationMonthString();
107: echo.ccexp_year = creditCard.getExpirationYearString();
108: if (isAddRandomAmountToTotal()) {
109: Money randomTotalAmount = addRandomAmountToTotal(inOrder
110: .getTotalPrice());
111: echo.grand_total = randomTotalAmount.toString();
112: } else {
113: echo.grand_total = inOrder.getTotalPrice()
114: .toString();
115: }
116:
117: // Force grand total if force value has been set
118: if (force_grand_total != null
119: && !"".equals(force_grand_total.trim())) {
120: echo.grand_total = force_grand_total;
121: }
122:
123: if (use_test_id) {
124: echo.merchant_echo_id = test_id;
125: echo.merchant_pin = test_pin;
126: } else {
127: echo.merchant_echo_id = id;
128: echo.merchant_pin = pin;
129: }
130: echo.merchant_email = inStore.getFromAddress();
131: echo.order_type = "S";
132: echo.transaction_type = "AD";
133: echo.merchant_trace_nbr = inOrder.getId();
134: echo.counter = "5";
135: echo.debug = "F";
136: echo.submit();
137:
138: // Get Responses
139: String avs_result = echo.avs_result();
140: String echoAuthCode = echo.echoAuthCode();
141: String echoDeclineCode = echo.echoDeclineCode();
142: String echoOrderNumber = echo.echoOrderNumber();
143: String echoReference = echo.echoReference();
144: String echoResponse = echo.echoResponse();
145: String echoStatus = echo.echoStatus();
146: boolean echoSuccess = echo.echoSuccess();
147: String echoType1 = echo.echoType1();
148: String echoType2 = echo.echoType2();
149: String echoType3 = echo.echoType3();
150:
151: OrderState orderState = inOrder.getOrderState();
152:
153: if (echoSuccess
154: && (avs_result.toUpperCase().equals("X") || avs_result
155: .toUpperCase().equals("Y"))) {
156: echo.transaction_type = "AV";
157: echo.submit();
158: avs_result = echo.avs_result();
159: echoAuthCode = echo.echoAuthCode();
160: echoDeclineCode = echo.echoDeclineCode();
161: echoOrderNumber = echo.echoOrderNumber();
162: echoReference = echo.echoReference();
163: echoResponse = echo.echoResponse();
164: echoStatus = echo.echoStatus();
165: echoSuccess = echo.echoSuccess();
166: echoType1 = echo.echoType1();
167: echoType2 = echo.echoType2();
168: echoType3 = echo.echoType3();
169:
170: if (echoSuccess) {
171: // transaction approved
172: orderState.setOk(true);
173: orderState
174: .setDescription("Your transaction has been approved.");
175: } else {
176:
177: // transaction declined
178: log.info("DECLINED: " + echo.echoType2()
179: + "Response Reason Code: "
180: + echoDeclineCode + "/"
181: + echo.echoType1());
182: orderState
183: .setDescription(echo.echoType2()
184: + "<br>"
185: + "Please make sure that the credit card "
186: + "number and billing information is correct.<br><br>");
187: orderState.setOk(false);
188: }
189: } else {
190: // Address verification failed
191: log.info(echoType2);
192: orderState.setDescription(echoType2 + "<br>"
193: + "Code: " + echoType1);
194: orderState.setOk(false);
195: }
196: } catch (Exception e) {
197: OrderState orderState = new OrderState();
198: orderState
199: .setDescription("An error occurred while processing your transaction.");
200: orderState.setOk(false);
201: inOrder.setOrderState(orderState);
202: e.printStackTrace();
203: throw new StoreException(e);
204: }
205: } else {
206: OrderState orderState = inOrder.getOrderState();
207: orderState.setOk(true);
208: orderState.setDescription("PO Accepted");
209: }
210: }
211:
212: public Money addRandomAmountToTotal(Money inTotal) {
213: Random generator = new Random(System.currentTimeMillis());
214: String randomStr = "";
215: for (int i = 0; i < 4; i++) {
216: int random = generator.nextInt();
217: String randomStr1 = (new Integer(random)).toString();
218: String lastDigit = randomStr1.substring(
219: randomStr1.length() - 1, randomStr1.length());
220: randomStr = randomStr + lastDigit;
221: if (i == 1) {
222: randomStr = randomStr + ".";
223: }
224: }
225: return inTotal.add(new Money(randomStr));
226: }
227:
228: /**
229: * @return Returns the addRandomAmountToTotal.
230: */
231: public boolean isAddRandomAmountToTotal() {
232: return fieldAddRandomAmountToTotal;
233: }
234:
235: /**
236: * @param addRandomAmountToTotal The addRandomAmountToTotal to set.
237: */
238: public void setAddRandomAmountToTotal(boolean addRandomAmountToTotal) {
239: fieldAddRandomAmountToTotal = addRandomAmountToTotal;
240: }
241:
242: }
|