KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcommercesql > gateway > authorizenet > AuthorizeNetCC


1 package com.jcommercesql.gateway.authorizenet;
2
3 /**
4  * This is part of JCommerceSQL Authorize.net Gateway
5  *
6  * <b>Copyright 2003 by Richard Roehl. All Rights Reserved.</b>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */

23
24 import java.util.Enumeration JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28  * AuthorizeNetCC can be used to perform credit card authorizations through Authorize.net.
29  * <p>
30  * <b>Basic Example 1:</b>
31  * <pre><code>
32  * AuthorizeNetCC anetcc = new AuthorizeNetCC();
33  * anetcc.setURL("secure.authorize.net",443,"/gateway/transact.dll");
34  * anetcc.setMerchantInfo("loginid","transactionkey");
35  * anetcc.addOptionalField("x_Address","123 Main St."); // if needed for AVS
36  * anetcc.addOptionalField("x_Zip","10101"); // if needed for AVS
37  * anetcc.addMerchantDefinedField("m_Comment", "Basic Example 1");
38  * anetcc.setTransaction("1234123412341234","11/09","100.00","AUTH_CAPTURE");
39  * try {
40  * anetcc.submit();
41  * if (anetcc.getResponseCode().equals("1")) {
42  * System.out.println("Your transaction was approved!");
43  * } else {
44  * System.out.println("Your transaction was declined! " + anetcc.getResponseReasonText());
45  * }
46  * } catch(Exception e) {
47  * System.out.println("System Error! " + e);
48  * }
49  * </code></pre>
50  *
51  * <p>
52  * <b>Basic Example 2:</b>
53  * <pre><code>
54  * AuthorizeNetCC anetcc = new AuthorizeNetCC("/path/to/config.prop");
55  * anetcc.addOptionalField("x_Address","123 Main St."); // if needed for AVS
56  * anetcc.addOptionalField("x_Zip","10101"); // if needed for AVS
57  * anetcc.addMerchantDefinedField("m_Comment", "Basic Example 2");
58  * anetcc.setTransaction("1234123412341234","11/09","100.00","AUTH_CAPTURE");
59  * try {
60  * anetcc.submit();
61  * if (anetcc.getResponseCode().equals("1")) {
62  * System.out.println("Your transaction was approved!");
63  * } else {
64  * System.out.println("Your transaction was declined! " + anetcc.getResponseReasonText());
65  * }
66  * } catch(Exception e) {
67  * System.out.println("System Error! " + e);
68  * }
69  * </code></pre>
70  *
71  * This class was designed using the Authorize.net Advanced Integration Method Implementation Guide v1.0.
72  * It is recommended that you download a copy of this guide to fully understand what gateway methods should
73  * be used for your particular situation.
74  * <p>
75  *
76  */

77
78 public final class AuthorizeNetCC extends AuthorizeNet {
79
80   //int AUTH_CAPTURE=0;
81
//int AUTH_ONLY=1;
82

83   //int[] TRANS_TYPES={AUTH_CAPTURE,AUTH_ONLY,CAPTURE_ONLY,CREDIT,VOID,PRIOR_AUTH_CAPTURE}
84
//protected URL="";
85
// Common to both Credit Card and eCheck transactions
86
//x_Version="";
87
//x_Delim_Data="";
88
//x_Login="";
89
//x_Tran_Key="";
90
//x_Amount="";
91

92   // Minimum information required for credit card = common fields above + following 3 fields
93
protected String JavaDoc x_Card_Num="";
94   protected String JavaDoc x_Exp_Date="";
95   protected String JavaDoc x_Type="";
96
97   /**
98     * Initializing Constructor
99     *
100     */

101   public AuthorizeNetCC() {};
102
103   /**
104     * Initializing Constructor.
105     * @param filename Name of properties file containing Authorize.Net host, port, path, login ID and transaction key
106     *
107     */

108   public AuthorizeNetCC( String JavaDoc filename ) throws IOException JavaDoc, InvalidPropException {
109     this.properties=loadPropFile(filename);
110     this.propFileName=filename;
111   };
112
113 //----------------------------------------------------------------------------
114
/* public static void main(String[] args) {
115
116     System.out.println("Going in");
117
118     //String postdata="x_Version=3.1&x_Delim_Data=True&x_Login=test&x_Tran_Key=123&x_Amount=5.99&x_Card_Num=370000000000002&x_Exp_Date=11/09&x_Type=AUTH_CAPTURE&x_Test_Request=TRUE&x_Delim_Char=.&x_Encap_Char=\"";
119
120     AuthorizeNetCC a = new AuthorizeNetCC();
121     a.setTransaction("1234123412341234","11/04","25.99","AUTH_CAPTURE");
122     a.setHost("secure.authorize.net");
123     a.setPort(443);
124     a.setPath("/gateway/transact.dll");
125     a.setLogin("/gateway/transact.dll");
126     a.setTranKey("/gateway/transact.dll");
127 try {
128     a.submit();
129 } catch(Exception e) {System.out.println("Submit Exception: " +e); }
130     System.out.println("Going out");
131
132     System.out.println("Here is postdata: " + a.getPostData());
133     System.out.println("Here is raw data: "+ new String(a.rawResult));
134      
135   }
136 */

137
138 //----------------------------------------------------------------------------
139
/**
140     * Set Credit Card Number
141     * @param cardNum Credit Card Number
142     */

143   public void setCardNum(String JavaDoc cardNum) { this.x_Card_Num=cardNum; }
144
145 //----------------------------------------------------------------------------
146
/**
147     * Returns the Credit Card Number the was set by the <code>setCreditCard()</code> method.
148     *
149     */

150   public String JavaDoc getCardNum() { return x_Card_Num; }
151
152 //----------------------------------------------------------------------------
153
/**
154     * Set Credit Card Expiration Date.
155     * @param expDate Credit Card Expiration Date
156     * @see AuthorizeNetCC#setTransaction(String cardNum, String expDate, String amount, String transType) setTransaction()
157     *
158     */

159   public void setExpDate(String JavaDoc expDate) { this.x_Exp_Date=expDate; }
160
161 //----------------------------------------------------------------------------
162
/**
163     * Returns Credit Card Expiration Date as set by the <code>setExpDate()</code> method.
164     *
165     */

166   public String JavaDoc getExpDate() { return x_Exp_Date; }
167
168 //----------------------------------------------------------------------------
169
/**
170     * Set the Credit Card Transaction Type.
171     *
172     * Example Types are: AUTH_CAPTURE, AUTH_ONLY, CAPTURE_ONLY, CREDIT, VOID, PRIOR_AUTH_CAPTURE.<p>
173     * See the Authorize.net Advanced Implementation Guide for the correct values for your transaction.
174     *
175     * @param transType Credit Card Transaction Type
176     * @see <a HREF="http://authorize.net">Authorize.net</a>
177     */

178   public void setType(String JavaDoc transType) { this.x_Type=transType; }
179
180 //----------------------------------------------------------------------------
181
/**
182     * Returns the Credit Card Transaction Type as set by the <code>setType()</code> method.
183     *
184     */

185   public String JavaDoc getType() { return x_Type; }
186
187 //----------------------------------------------------------------------------
188
/**
189     * Set Transaction Information.
190     *
191     * This method is provided as an alternative to setting the items with individual calls.
192     *
193     * @param cardNum Credit Card Number
194     * @param expDate Credit Card Expiration Date
195     * @param amount Amount of Transaction
196     * @param transType Transaction Type
197     *
198     * @see AuthorizeNetCC#setType(String transType) setType
199     */

200   public void setTransaction(String JavaDoc cardNum, String JavaDoc expDate, String JavaDoc amount, String JavaDoc transType) {
201
202     setCardNum(cardNum);
203     setExpDate(expDate);
204     setAmount(amount);
205     setType(transType);
206   }
207
208 //----------------------------------------------------------------------------
209
/**
210     *
211     *
212     */

213   private boolean verifyMinCCFieldsSet() {
214
215     boolean result=true;
216     if (x_Card_Num==null) result = false;
217     if (x_Exp_Date==null || x_Delim_Data.equals("") ) result = false;
218     if (x_Type==null || x_Type.equals("") ) result = false;
219     return result;
220   }
221
222 //----------------------------------------------------------------------------
223
/**
224     * Show Post Data.
225     *
226     * The Authorize.net transaction gateway expects a list of parameters and values to be
227     * posted to their site. Use this function to print out the the list for troubleshooting
228     * purposes. Note: this will only return data once the submit function has been called.
229     *
230     */

231   public String JavaDoc getPostData() {
232     return postData;
233   }
234 //----------------------------------------------------------------------------
235
/**
236     * Submit Credit Card Transaction For Approval.
237     *
238     * A RequiredFieldException is thrown if the following fields are not set:
239     * credit card number, expiration date, amount, transaction type
240     * authorize.net server name, port, path,
241     * merchant account login and transaction key.
242     */

243   public void submit() throws RequiredFieldException,Exception JavaDoc {
244
245     String JavaDoc host=getHost();
246     String JavaDoc path=getPath();
247     int port=getPort();
248     String JavaDoc postdata;
249
250     if (!verifyMinCCFieldsSet()) {
251       throw new RequiredFieldException("One of the following fields is not set: Card_Num,Exp_Date,Type");
252     }
253
254     if (!verifyMinimumFieldsSet()) {
255       throw new RequiredFieldException("One of the following fields is not set: login, transaction key, amount");
256     }
257
258     if ( host==null || host.equals("") ) {
259     throw new RequiredFieldException("Host Field Not Set");
260     }
261     if ( path==null || path.equals("") ) {
262     throw new RequiredFieldException("URL Path Field Not Set");
263     }
264
265     // add requiredfields to post data
266
postdata=AuthorizeNetCodes.REQ_FIELD_VERSION+"="+getVersion();
267     postdata+="&"+AuthorizeNetCodes.REQ_FIELD_LOGIN+"="+getLogin();
268     postdata+="&"+AuthorizeNetCodes.REQ_FIELD_TRAN_KEY+"="+getTranKey();
269     postdata+="&"+AuthorizeNetCodes.REQ_FIELD_AMOUNT+"="+getAmount();
270     postdata+="&"+AuthorizeNetCodes.REQ_FIELD_CARD_NUM+"="+getCardNum();
271     postdata+="&"+AuthorizeNetCodes.REQ_FIELD_EXP_DATE+"="+getExpDate();
272     postdata+="&"+AuthorizeNetCodes.REQ_FIELD_TYPE+"="+getType();
273     postdata+="&"+AuthorizeNetCodes.REQ_FIELD_DELIM_DATA+"="+getDelimData();
274     postdata+="&"+AuthorizeNetCodes.REQ_FIELD_DELIM_CHAR+"="+getDelimChar();
275
276     // add optional fields to post data
277
Enumeration JavaDoc e = optionalFields.keys();
278     while (e.hasMoreElements()) {
279       String JavaDoc field = (String JavaDoc)e.nextElement();
280       postdata+="&"+field+"="+(String JavaDoc)optionalFields.get(field);
281     }
282
283     // add merchant defined fields to post data
284
int idx=0;
285     while (merchantDefinedFields.size() > idx) {
286         postdata+="&"+(String JavaDoc)merchantDefinedFields.get(idx)+"="+(String JavaDoc)merchantDefinedFields.get(idx+1);
287         idx+=2;
288     }
289
290      this.postData=postdata;
291
292     submitANetPost(host,port,path,postdata);
293  
294   } // end submit()
295
}
296
Popular Tags