KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > gateway > EchoOrderArchive


1 /*
2  * Created on Nov 2, 2004
3  */

4 package com.openedit.store.gateway;
5
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileNotFoundException JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.util.Properties JavaDoc;
11 import java.util.Random JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.openedit.money.Money;
16
17 import com.openecho.Echo;
18 import com.openedit.WebPageRequest;
19 import com.openedit.store.BaseOrderArchive;
20 import com.openedit.store.CreditPaymentMethod;
21 import com.openedit.store.Order;
22 import com.openedit.store.OrderState;
23 import com.openedit.store.Store;
24 import com.openedit.store.StoreException;
25 import com.openedit.store.customer.Address;
26 import com.openedit.store.customer.Customer;
27
28 /**
29  * @author cburkey
30  *
31  */

32 public class EchoOrderArchive extends BaseOrderArchive
33 {
34     // Go here to see transactions: https://wwws.echo-inc.com/Review/
35
private static final Log log = LogFactory.getLog(EchoOrderArchive.class);
36     protected boolean fieldAddRandomAmountToTotal = false;
37
38     /*
39      * (non-javadoc)
40      *
41      * @see com.openedit.store.EmailOrderArchive#exportNewOrder(com.openedit.WebPageContext,
42      * com.openedit.store.Store, com.openedit.store.Order)
43      */

44     public void exportNewOrder(WebPageRequest inContext, Store inStore, Order inOrder)
45         throws StoreException
46     {
47         // Load properties
48
File JavaDoc configDirectory = new File JavaDoc(inStore.getStoreDirectory(), "configuration");
49         File JavaDoc propertiesFile = new File JavaDoc(configDirectory, "echo.properties");
50         FileInputStream JavaDoc stream = null;
51         
52         try
53         {
54             stream = new FileInputStream JavaDoc(propertiesFile);
55         }
56         catch (FileNotFoundException JavaDoc e1)
57         {
58             e1.printStackTrace();
59         }
60         Properties JavaDoc props = new Properties JavaDoc();
61         try
62         {
63             props.load(stream);
64         }
65         catch (IOException JavaDoc e2)
66         {
67             e2.printStackTrace();
68         }
69
70         // id
71
String JavaDoc id = props.getProperty("id");
72         String JavaDoc pin = props.getProperty("pin");
73         String JavaDoc test_id = props.getProperty("test-id");
74         String JavaDoc test_pin = props.getProperty("test-pin");
75         String JavaDoc use_test_id_str = props.getProperty("use-test-id");
76         boolean use_test_id = new Boolean JavaDoc(use_test_id_str).booleanValue();
77         // add_random
78
String JavaDoc add_random_str = props.getProperty("add-random-amount");
79         boolean add_random = new Boolean JavaDoc(add_random_str).booleanValue();
80         setAddRandomAmountToTotal(add_random);
81         // force grand total
82
String JavaDoc force_grand_total = props.getProperty("force-grand-total");
83
84         if (inOrder.getPaymentMethod().requiresValidation())
85         {
86             try
87             {
88                 Echo echo = new Echo();
89
90                 // Customer Info
91
Customer customer = inOrder.getCustomer();
92                 Address address = customer.getBillingAddress();
93                 echo.billing_first_name = customer.getFirstName();
94                 echo.billing_last_name = customer.getLastName();
95                 echo.billing_company_name = customer.getCompany();
96                 echo.billing_address1 = address.getAddress1();
97                 echo.billing_address2 = address.getAddress2();
98                 echo.billing_city = address.getCity();
99                 echo.billing_state = address.getState();
100                 echo.billing_zip = address.get5DigitZipCode();
101                 echo.billing_country = address.getCountry();
102                 echo.billing_phone = customer.getPhone1();
103                 echo.billing_email = customer.getEmail();
104                 //echo.billing_ip_address = getRequest().getRemoteAddr();
105

106                 // Submit Request
107
CreditPaymentMethod creditCard = (CreditPaymentMethod) inOrder.getPaymentMethod();
108                 echo.cc_number = creditCard.getCardNumber();
109                 echo.ccexp_month = creditCard.getExpirationMonthString();
110                 echo.ccexp_year = creditCard.getExpirationYearString();
111                 if (isAddRandomAmountToTotal())
112                 {
113                     Money randomTotalAmount = addRandomAmountToTotal(inOrder.getTotalPrice());
114                     echo.grand_total = randomTotalAmount.toString();
115                 }
116                 else
117                 {
118                     echo.grand_total = inOrder.getTotalPrice().toString();
119                 }
120
121                 // Force grand total if force value has been set
122
if (force_grand_total != null && !"".equals(force_grand_total.trim()))
123                 {
124                     echo.grand_total = force_grand_total;
125                 }
126
127                 if (use_test_id)
128                 {
129                     echo.merchant_echo_id = test_id;
130                     echo.merchant_pin = test_pin;
131                 }
132                 else
133                 {
134                     echo.merchant_echo_id = id;
135                     echo.merchant_pin = pin;
136                 }
137                 echo.merchant_email = inStore.getFromAddress();
138                 echo.order_type = "S";
139                 echo.transaction_type = "AD";
140                 echo.merchant_trace_nbr = inOrder.getId();
141                 echo.counter = "5";
142                 echo.debug = "F";
143                 echo.submit();
144
145                 // Get Responses
146
String JavaDoc avs_result = echo.avs_result();
147                 String JavaDoc echoAuthCode = echo.echoAuthCode();
148                 String JavaDoc echoDeclineCode = echo.echoDeclineCode();
149                 String JavaDoc echoOrderNumber = echo.echoOrderNumber();
150                 String JavaDoc echoReference = echo.echoReference();
151                 String JavaDoc echoResponse = echo.echoResponse();
152                 String JavaDoc echoStatus = echo.echoStatus();
153                 boolean echoSuccess = echo.echoSuccess();
154                 String JavaDoc echoType1 = echo.echoType1();
155                 String JavaDoc echoType2 = echo.echoType2();
156                 String JavaDoc echoType3 = echo.echoType3();
157
158                 OrderState orderState = inOrder.getOrderState();
159
160                 if (echoSuccess
161                     && (avs_result.toUpperCase().equals("X") || avs_result.toUpperCase()
162                         .equals("Y")))
163                 {
164                     echo.transaction_type = "AV";
165                     echo.submit();
166                     avs_result = echo.avs_result();
167                     echoAuthCode = echo.echoAuthCode();
168                     echoDeclineCode = echo.echoDeclineCode();
169                     echoOrderNumber = echo.echoOrderNumber();
170                     echoReference = echo.echoReference();
171                     echoResponse = echo.echoResponse();
172                     echoStatus = echo.echoStatus();
173                     echoSuccess = echo.echoSuccess();
174                     echoType1 = echo.echoType1();
175                     echoType2 = echo.echoType2();
176                     echoType3 = echo.echoType3();
177
178                     if (echoSuccess)
179                     {
180                         // transaction approved
181
orderState.setOk(true);
182                         orderState.setDescription("Your transaction has been approved.");
183                     }
184                     else
185                     {
186
187                         // transaction declined
188
log.info("DECLINED: " + echo.echoType2() + "Response Reason Code: "
189                             + echoDeclineCode + "/" + echo.echoType1());
190                         orderState.setDescription(echo.echoType2() + "<br>"
191                             + "Please make sure that the credit card "
192                             + "number and billing information is correct.<br><br>");
193                         orderState.setOk(false);
194                     }
195                 }
196                 else
197                 {
198                     // Address verification failed
199
log.info(echoType2);
200                     orderState.setDescription(echoType2 + "<br>" + "Code: " + echoType1);
201                     orderState.setOk(false);
202                 }
203             }
204             catch (Exception JavaDoc e)
205             {
206                 OrderState orderState = new OrderState();
207                 orderState.setDescription("An error occurred while processing your transaction.");
208                 orderState.setOk(false);
209                 inOrder.setOrderState(orderState);
210                 e.printStackTrace();
211                 throw new StoreException(e);
212             }
213         }
214         else
215         {
216             OrderState orderState = inOrder.getOrderState();
217             orderState.setOk(true);
218             orderState.setDescription("PO Accepted");
219         }
220     }
221
222     public Money addRandomAmountToTotal(Money inTotal)
223     {
224         Random JavaDoc generator = new Random JavaDoc(System.currentTimeMillis());
225         String JavaDoc randomStr = "";
226         for (int i = 0; i < 4; i++)
227         {
228             int random = generator.nextInt();
229             String JavaDoc randomStr1 = (new Integer JavaDoc(random)).toString();
230             String JavaDoc lastDigit = randomStr1.substring(randomStr1.length() - 1, randomStr1.length());
231             randomStr = randomStr + lastDigit;
232             if (i == 1)
233             {
234                 randomStr = randomStr + ".";
235             }
236         }
237         return inTotal.add(new Money(randomStr));
238     }
239
240     /**
241      * @return Returns the addRandomAmountToTotal.
242      */

243     public boolean isAddRandomAmountToTotal()
244     {
245         return fieldAddRandomAmountToTotal;
246     }
247
248     /**
249      * @param addRandomAmountToTotal The addRandomAmountToTotal to set.
250      */

251     public void setAddRandomAmountToTotal(boolean addRandomAmountToTotal)
252     {
253         fieldAddRandomAmountToTotal = addRandomAmountToTotal;
254     }
255
256 }
Popular Tags