1 4 package com.openedit.store.gateway; 5 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.FileNotFoundException ; 9 import java.io.IOException ; 10 import java.util.Properties ; 11 import java.util.Random ; 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 32 public class EchoOrderArchive extends BaseOrderArchive 33 { 34 private static final Log log = LogFactory.getLog(EchoOrderArchive.class); 36 protected boolean fieldAddRandomAmountToTotal = false; 37 38 44 public void exportNewOrder(WebPageRequest inContext, Store inStore, Order inOrder) 45 throws StoreException 46 { 47 File configDirectory = new File (inStore.getStoreDirectory(), "configuration"); 49 File propertiesFile = new File (configDirectory, "echo.properties"); 50 FileInputStream stream = null; 51 52 try 53 { 54 stream = new FileInputStream (propertiesFile); 55 } 56 catch (FileNotFoundException e1) 57 { 58 e1.printStackTrace(); 59 } 60 Properties props = new Properties (); 61 try 62 { 63 props.load(stream); 64 } 65 catch (IOException e2) 66 { 67 e2.printStackTrace(); 68 } 69 70 String id = props.getProperty("id"); 72 String pin = props.getProperty("pin"); 73 String test_id = props.getProperty("test-id"); 74 String test_pin = props.getProperty("test-pin"); 75 String use_test_id_str = props.getProperty("use-test-id"); 76 boolean use_test_id = new Boolean (use_test_id_str).booleanValue(); 77 String add_random_str = props.getProperty("add-random-amount"); 79 boolean add_random = new Boolean (add_random_str).booleanValue(); 80 setAddRandomAmountToTotal(add_random); 81 String 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 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 106 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 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 String avs_result = echo.avs_result(); 147 String echoAuthCode = echo.echoAuthCode(); 148 String echoDeclineCode = echo.echoDeclineCode(); 149 String echoOrderNumber = echo.echoOrderNumber(); 150 String echoReference = echo.echoReference(); 151 String echoResponse = echo.echoResponse(); 152 String echoStatus = echo.echoStatus(); 153 boolean echoSuccess = echo.echoSuccess(); 154 String echoType1 = echo.echoType1(); 155 String echoType2 = echo.echoType2(); 156 String 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 orderState.setOk(true); 182 orderState.setDescription("Your transaction has been approved."); 183 } 184 else 185 { 186 187 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 log.info(echoType2); 200 orderState.setDescription(echoType2 + "<br>" + "Code: " + echoType1); 201 orderState.setOk(false); 202 } 203 } 204 catch (Exception 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 generator = new Random (System.currentTimeMillis()); 225 String randomStr = ""; 226 for (int i = 0; i < 4; i++) 227 { 228 int random = generator.nextInt(); 229 String randomStr1 = (new Integer (random)).toString(); 230 String 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 243 public boolean isAddRandomAmountToTotal() 244 { 245 return fieldAddRandomAmountToTotal; 246 } 247 248 251 public void setAddRandomAmountToTotal(boolean addRandomAmountToTotal) 252 { 253 fieldAddRandomAmountToTotal = addRandomAmountToTotal; 254 } 255 256 } | Popular Tags |