KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > accounting > thirdparty > authorizedotnet > AuthorizeResponse


1 /*
2  * Copyright 2001-2006 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16
17 /**
18  *
19  * @author Fred Forrester (foresterf@fredforester.org)
20  */

21 package org.ofbiz.accounting.thirdparty.authorizedotnet;
22
23 import java.util.*;
24
25 public class AuthorizeResponse {
26
27     private String JavaDoc rawResp = null;
28     private Vector response = new Vector();
29     private String JavaDoc respCode = "";
30     private String JavaDoc reasonCode = "";
31     private String JavaDoc reasonText = "";
32     private String JavaDoc version = "3.0";
33     private int maxPos = 39; //maximum number of field positions in response. there are more, but currently none are used.
34

35     //constant names for response fields
36
public static int RESPONSE_CODE = 1;
37     public static int RESPONSE_SUBCODE = 2;
38     public static int RESPONSE_REASON_CODE = 3;
39     public static int RESPONSE_REASON_TEXT = 4;
40     public static int APPROVAL_CODE = 5;
41     public static int AUTHORIZATION_CODE = 5;
42     public static int AVS_RESULT_CODE = 6;
43     public static int TRANSACTION_ID = 7;
44
45     // 8 - 37 echoed from request
46
public static int INVOICE_NUMBER = 8;
47     public static int DESCRIPTION = 9;
48     public static int AMOUNT = 10;
49     public static int METHOD = 11;
50     public static int TRANSACTION_TYPE = 12;
51     public static int CUSTOMER_ID = 13;
52     public static int CARDHOLDER_FIRST_NAME = 14;
53     public static int CARDHOLDER_LAST_NAME = 15;
54     public static int COMPANY = 16;
55     public static int BILLING_ADDRESS = 17;
56     public static int CITY = 18;
57     public static int STATE = 19;
58     public static int ZIP = 20;
59     public static int COUNTRY = 21;
60     public static int PHONE = 22;
61     public static int FAX = 23;
62     public static int EMAIL = 24;
63     public static int SHIP_TO_FIRST_NAME = 25;
64     public static int SHIP_TO_LAST_NAME = 26;
65     public static int SHIP_TO_COMPANY = 27;
66     public static int SHIP_TO_ADDRESS = 28;
67     public static int SHIP_TO_CITY = 29;
68     public static int SHIP_TO_STATE = 30;
69     public static int SHIP_TO_ZIP = 31;
70     public static int SHIP_TO_COUNTRY = 32;
71     public static int TAX_AMOUNT = 33;
72     public static int DUTY_AMOUNT = 34;
73     public static int FREIGHT_AMOUNT = 35;
74     public static int TAX_EXEMPT_FLAG = 36;
75     public static int PO_NUMBER = 37;
76     public static int MD5_HASH = 38;
77     public static int CID_RESPONSE_CODE = 39;
78     //public static int CAVV_RESPONSE_CODE = 40;
79

80     //some other constants
81
public static String JavaDoc APPROVED = "Approved";
82     public static String JavaDoc DECLINED = "Declined";
83     public static String JavaDoc ERROR = "Error";
84
85
86     public AuthorizeResponse(String JavaDoc resp) {
87         this(resp, "|");
88     }
89
90     public AuthorizeResponse(String JavaDoc resp, String JavaDoc delim) {
91         this.rawResp = resp;
92         this.response = splitResp(resp, delim);
93         setApproval();
94     }
95
96     private void setApproval() {
97         String JavaDoc rc = (String JavaDoc)response.get(RESPONSE_CODE);
98
99         if(rc.equals("1")) {
100             this.respCode = APPROVED;
101         }
102
103         if(rc.equals("2")) {
104             this.respCode = DECLINED;
105         }
106
107         if(rc.equals("3")) {
108             this.respCode = ERROR;
109         }
110
111         this.reasonCode = (String JavaDoc)response.get(RESPONSE_REASON_CODE);
112         this.reasonText = (String JavaDoc)response.get(RESPONSE_REASON_TEXT);
113
114     }
115
116     public void setVersion(String JavaDoc version)
117     {
118         if (version != null && version.length() > 0)
119         {
120             if (version.equals("3.0") || version.equals("3.1"))
121                 this.version = version;
122         }
123
124     }
125
126     public String JavaDoc getResponseCode() {
127         return this.respCode;
128     }
129
130     public String JavaDoc getReasonCode() {
131         return this.reasonCode;
132     }
133
134     public String JavaDoc getReasonText() {
135         return this.reasonText;
136     }
137
138     public String JavaDoc getResponseField(int posNum) {
139
140         if (this.version.equals("3.0"))
141         {
142             if (posNum == this.CID_RESPONSE_CODE)
143                 return "M";
144         }
145         if(posNum < 1 || posNum > maxPos) {
146             return "unknown_field";
147         }
148         else {
149             return (String JavaDoc)response.get(posNum);
150         }
151     }
152
153     public String JavaDoc getRawResponse() {
154         return this.rawResp;
155     }
156
157     private Vector splitResp(String JavaDoc r, String JavaDoc delim) {
158         int s1=0, s2=-1;
159         Vector out = new Vector(40);
160         out.addElement("empty");
161         while(true){
162             s2 = r.indexOf(delim, s1);
163             if(s2 != -1){
164                 out.addElement(r.substring(s1, s2));
165             }else{
166                 //the end part of the string (string not pattern terminated)
167
String JavaDoc _ = r.substring(s1);
168                 if(_ != null && !_.equals("")){
169                     out.addElement(_);
170                 }
171                 break;
172             }
173             s1 = s2;
174             s1 += delim.length();
175         }
176         return out;
177     }
178
179     public String JavaDoc toString() {
180         return response.toString();
181     }
182
183 }
184
Popular Tags