1 4 package com.openedit.store.gateway; 5 6 import java.io.File ; 7 import java.util.Iterator ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.openedit.money.Money; 12 13 import com.jcommercesql.gateway.authorizenet.AuthorizeNetCC; 14 import com.openedit.WebPageRequest; 15 import com.openedit.store.BaseOrderArchive; 16 import com.openedit.store.CartItem; 17 import com.openedit.store.CreditPaymentMethod; 18 import com.openedit.store.Order; 19 import com.openedit.store.OrderState; 20 import com.openedit.store.Store; 21 import com.openedit.store.StoreException; 22 import com.openedit.store.customer.Address; 23 import com.openedit.store.customer.Customer; 24 25 29 public class AuthorizeNetOrderArchive extends BaseOrderArchive 30 { 31 32 private static final Log log = LogFactory.getLog(AuthorizeNetOrderArchive.class); 33 34 protected boolean requiresValidation(Store inStore, Order inOrder) 36 { 37 File configDirectory = new File (inStore.getStoreDirectory(), "configuration"); 38 File propertiesFile = new File (configDirectory, "authorize.properties"); 39 if ( propertiesFile.exists() ) 40 { 41 return inOrder.getPaymentMethod().requiresValidation(); 42 } 43 return false; 44 } 45 46 public void exportNewOrder(WebPageRequest inContext, Store inStore, Order inOrder) 47 throws StoreException 48 { 49 if ( !requiresValidation( inStore,inOrder )) 50 { 51 return; 52 } 53 if( inStore.isAutoCapture() ) 55 { 56 process(inStore, inOrder, "AUTH_CAPTURE"); 57 } 58 else 59 { 60 process(inStore, inOrder, "AUTH_ONLY"); 61 } 62 } 63 64 protected void process(Store inStore, Order inOrder, String inType) throws StoreException 65 { 66 try 67 { 68 File configDirectory = new File (inStore.getStoreDirectory(), "configuration"); 71 File propertiesFile = new File (configDirectory, "authorize.properties"); 72 73 AuthorizeNetCC anetcc = new AuthorizeNetCC(propertiesFile.getAbsolutePath()); 74 75 76 Customer customer = inOrder.getCustomer(); 78 79 anetcc.addOptionalField("x_cust_id", customer.getUserName()); 80 anetcc.addOptionalField("x_first_name", customer.getFirstName()); 81 anetcc.addOptionalField("x_last_name", customer.getLastName()); 82 Address address = customer.getBillingAddress(); 83 anetcc.addOptionalField("x_Address", address.getAddress1()); 84 anetcc.addOptionalField("x_city", address.getCity()); 85 anetcc.addOptionalField("x_state", address.getState()); 86 anetcc.addOptionalField("x_zip", address.getZipCode()); 87 if ( address.getCountry() != null) 88 { 89 anetcc.addOptionalField("x_country", address.getCountry()); 90 } 91 92 anetcc.addOptionalField("x_phone", customer.getPhone1()); 93 94 anetcc.addOptionalField("x_email", customer.getEmail()); 95 anetcc.addOptionalField("x_Zip", address.getZipCode()); 96 anetcc.addOptionalField("x_invoice_num", inOrder.getId()); 97 98 StringBuffer out = new StringBuffer (); 99 for (Iterator iter = inOrder.getItems().iterator(); iter.hasNext();) 100 { 101 CartItem element = (CartItem) iter.next(); 102 out.append( "( Product = " ); 103 out.append( element.getProduct().getName() ); 104 out.append(" "); 105 out.append( element.getProduct().getId() ); 106 out.append( "/ sku = " ); 107 out.append( element.getSku() ); 108 out.append( ")" ); 109 110 } 111 112 String desc = out.toString(); 113 if ( desc.length() > 254) 114 { 115 desc = desc.substring(0,254); 116 } 117 anetcc.addOptionalField("x_description", desc); 118 119 CreditPaymentMethod creditCard = (CreditPaymentMethod)inOrder.getPaymentMethod(); 124 Money total = inOrder.getTotalPrice(); 125 anetcc.setTransaction(creditCard.getCardNumber(), 126 creditCard.getExpirationDateString(), 127 total.toString(), 128 inType); 132 anetcc.submit(); 136 String responseCode = anetcc.getResponseCode(); 137 if ( creditCard.getCardNumber().equals("5555555555554444") ) 139 { 140 responseCode = "1"; 141 } 142 OrderState orderState = null; 143 if ("1".equals(responseCode)) 144 { 145 148 if( inType.indexOf("CAPTURE") > -1) 149 { 150 orderState = inStore.getOrderState(Order.CAPTURED); 151 orderState.setDescription("Your transaction has been captured by Authorize.net."); 152 } 153 else 154 { 155 orderState = inStore.getOrderState(Order.AUTHORIZED); 156 orderState.setDescription("Your transaction has been authorized."); 157 } 158 orderState.setOk(true); 159 } 160 else 161 { 162 log.info("DECLINED: Response Reason Code: " + anetcc.getResponseReasonCode()); 164 log.info("AVS Result Code: " + anetcc.getResponseAVSResultCode()); 165 log.info("Response Reason Text: " + anetcc.getResponseReasonText()); 166 log.info("Response Sub Code: " + anetcc.getResponseSubCode()); 167 168 String error = "Your transaction has been declined. Please hit the back button on your browser to correct.<br>"; 169 error += anetcc.getResponseReasonText(); 170 error += " Code: " + anetcc.getResponseAVSResultCode(); 171 172 orderState = inStore.getOrderState(Order.REJECTED); 173 orderState.setDescription( error ); 174 orderState.setOk(false); 175 } 176 inOrder.setOrderState(orderState); 177 } 178 catch ( Exception e ) 179 { 180 OrderState orderState = new OrderState(); 181 orderState.setDescription( 182 "An error occurred while processing your transaction."); 183 orderState.setOk(false); 184 inOrder.setOrderState(orderState); 185 e.printStackTrace(); 186 throw new StoreException(e); 187 } 188 } 189 public void captureOrder(WebPageRequest inContext, Store inStore, Order inOrder) throws StoreException 190 { 191 if ( !requiresValidation( inStore,inOrder )) 192 { 193 return; 194 } 195 process(inStore, inOrder, "PRIOR_AUTH_CAPTURE"); 196 } 197 } 198 | Popular Tags |