KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package com.openedit.store.gateway;
5
6 import java.io.File JavaDoc;
7 import java.util.Iterator JavaDoc;
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 /**
26  * @author cburkey
27  *
28  */

29 public class AuthorizeNetOrderArchive extends BaseOrderArchive
30 {
31
32     private static final Log log = LogFactory.getLog(AuthorizeNetOrderArchive.class);
33
34     //If not configured then this archive is skipped
35
protected boolean requiresValidation(Store inStore, Order inOrder)
36     {
37         File JavaDoc configDirectory = new File JavaDoc(inStore.getStoreDirectory(), "configuration");
38         File JavaDoc propertiesFile = new File JavaDoc(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         // "AUTH_ONLY"); //AUTH_CAPTURE, AUTH_ONLY, CAPTURE_ONLY, CREDIT, VOID, PRIOR_AUTH_CAPTURE.
54
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 JavaDoc inType) throws StoreException
65     {
66         try
67         {
68             // See examples at http://www.jcommercesql.com/anet/
69
// load properties (e.g. IP address, username, password) for accessing authorize.net
70
File JavaDoc configDirectory = new File JavaDoc(inStore.getStoreDirectory(), "configuration");
71             File JavaDoc propertiesFile = new File JavaDoc(configDirectory, "authorize.properties");
72
73             AuthorizeNetCC anetcc = new AuthorizeNetCC(propertiesFile.getAbsolutePath());
74
75             
76             // load customer address info from order (in case needed for AVS)
77
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 JavaDoc out = new StringBuffer JavaDoc();
99             for (Iterator JavaDoc 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 JavaDoc desc = out.toString();
113             if ( desc.length() > 254)
114             {
115                 desc = desc.substring(0,254);
116             }
117             anetcc.addOptionalField("x_description", desc);
118
119             //anetcc.setTestMode()
120
// set credit card number, expiration date, and transaction amount
121
// Transaction is AUTH_ONLY, meaning we are checking that the account allows
122
// that amount to be charged, but are not actually charging the account just yet.
123
CreditPaymentMethod creditCard = (CreditPaymentMethod)inOrder.getPaymentMethod();
124             Money total = inOrder.getTotalPrice();
125             anetcc.setTransaction(creditCard.getCardNumber(),
126                 creditCard.getExpirationDateString(),
127                 total.toString(),
128                 inType); //AUTH_CAPTURE, AUTH_ONLY, CAPTURE_ONLY, CREDIT, VOID, PRIOR_AUTH_CAPTURE.
129
//System.out.println("total price = " + inOrder.getCart().getTotalPrice());
130
//System.out.println("exp date = " + creditCard.getExpirationDateString());
131

132             // submit request
133
//anetcc.removeTestMode();
134
//anetcc.addOptionalField(AuthorizeNetCodes.REQ_FIELD_TEST_REQUEST,"false");
135
anetcc.submit();
136             String JavaDoc responseCode = anetcc.getResponseCode();
137             //creditCard.getCardNumber().equals("5105105105105100") ||
138
if ( creditCard.getCardNumber().equals("5555555555554444") )
139             {
140                 responseCode = "1";
141             }
142             OrderState orderState = null;
143             if ("1".equals(responseCode))
144             {
145                 // transaction approved
146
//super.exportNewOrder(inContext, inStore, inOrder);
147

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                 // transaction declined
163
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 JavaDoc 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 JavaDoc 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