1 | <?php |
---|
2 | // Example usage |
---|
3 | // require_once CODE_BASE . '/lib/AuthorizeNet.inc.php'; |
---|
4 | // |
---|
5 | // $authorizenet = new AuthorizeNet(); |
---|
6 | // $authorizenet->setParam(array( |
---|
7 | // 'x_Login' => 'myaccount', |
---|
8 | // 'x_Test_Request' => 'TRUE', |
---|
9 | // 'x_First_Name' => 'John', |
---|
10 | // 'x_Last_Name' => 'Doe', |
---|
11 | // 'x_Amount' => '1.20', |
---|
12 | // 'x_Card_Num' => '4111111111111111', |
---|
13 | // 'x_Card_Code' => '123', |
---|
14 | // 'x_Exp_Date' => '042008', |
---|
15 | // 'x_Invoice_Num' => '100', |
---|
16 | // 'x_Address' => '10 rue Levouv', |
---|
17 | // 'x_City' => 'SomeCity', |
---|
18 | // 'x_State' => 'CA', |
---|
19 | // 'x_Zip' => '75010', |
---|
20 | // )); |
---|
21 | // |
---|
22 | // $result_code = $authorizenet->process(); // Returns one of: false = error, 1 = accepted, 2 = declined, 3 = error |
---|
23 | // $result_array = $authorizenet->getResults(); |
---|
24 | // |
---|
25 | // foreach ($result_array as $key => $value) { |
---|
26 | // print "$key: $value<br>\n"; |
---|
27 | // } |
---|
28 | |
---|
29 | /** |
---|
30 | * The AuthorizeNet class provides an abstract interface for communicating |
---|
31 | * with authorize.net's AIM interface. Supports Auth.Net v3.1 |
---|
32 | * |
---|
33 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
34 | * @version 1.0 |
---|
35 | * @date 2004-04-06 |
---|
36 | */ |
---|
37 | class AuthorizeNet |
---|
38 | { |
---|
39 | var $post_url = ''; // The URL to post data to. |
---|
40 | var $md5_hash_value = ','; // A custom value for the response delimination character. |
---|
41 | var $_results = array(); |
---|
42 | var $_params = array(); |
---|
43 | var $_default_params = array( |
---|
44 | 'x_version' => '3.1', |
---|
45 | 'x_relay_response' => 'FALSE', |
---|
46 | 'x_delim_data' => 'TRUE', |
---|
47 | 'x_echo_data' => 'TRUE', |
---|
48 | 'x_adc_url' => 'FALSE', |
---|
49 | 'x_type' => 'AUTH_CAPTURE', |
---|
50 | 'x_method' => 'CC', |
---|
51 | 'x_login' => '', |
---|
52 | 'x_tran_key' => '', |
---|
53 | 'x_delim_char' => ',', |
---|
54 | 'x_encap_char' => '', |
---|
55 | ); |
---|
56 | |
---|
57 | // Array of response names. Used in the results array. |
---|
58 | var $_result_fields = Array( |
---|
59 | 'x_response_code', |
---|
60 | 'x_response_subcode', |
---|
61 | 'x_response_reason_code', |
---|
62 | 'x_response_reason_text', |
---|
63 | 'x_auth_code', |
---|
64 | 'x_avs_code', |
---|
65 | 'x_trans_id', |
---|
66 | 'x_invoice_num', |
---|
67 | 'x_description', |
---|
68 | 'x_amount', |
---|
69 | 'x_method', |
---|
70 | 'x_type', |
---|
71 | 'x_cust_id', |
---|
72 | 'x_first_name', |
---|
73 | 'x_last_name', |
---|
74 | 'x_company', |
---|
75 | 'x_address', |
---|
76 | 'x_city', |
---|
77 | 'x_state', |
---|
78 | 'x_zip', |
---|
79 | 'x_country', |
---|
80 | 'x_phone', |
---|
81 | 'x_fax', |
---|
82 | 'x_email', |
---|
83 | 'x_ship_to_first_name', |
---|
84 | 'x_ship_to_last_name', |
---|
85 | 'x_ship_to_company', |
---|
86 | 'x_ship_to_address', |
---|
87 | 'x_ship_to_city', |
---|
88 | 'x_ship_to_state', |
---|
89 | 'x_ship_to_zip', |
---|
90 | 'x_ship_to_country', |
---|
91 | 'x_tax', |
---|
92 | 'x_duty', |
---|
93 | 'x_freight', |
---|
94 | 'x_tax_exempt', |
---|
95 | 'x_po_num', |
---|
96 | 'x_md5_hash', |
---|
97 | 'x_card_code' |
---|
98 | ); |
---|
99 | |
---|
100 | /** |
---|
101 | * Constructs a new authentication object. |
---|
102 | * |
---|
103 | * @access public |
---|
104 | * |
---|
105 | * @param optional array $_params A hash containing parameters. |
---|
106 | */ |
---|
107 | function AuthorizeNet($params = array()) |
---|
108 | { |
---|
109 | if (!function_exists('curl_init')) { |
---|
110 | trigger_error('AuthorizeNet error: curl not installed.', E_USER_ERROR); |
---|
111 | } |
---|
112 | |
---|
113 | // The authorize.net url to post to. |
---|
114 | $this->post_url = isset($params['post_url']) ? $params['post_url'] : 'https://secure.authorize.net/gateway/transact.dll'; |
---|
115 | |
---|
116 | // A custom value for the response delimination character. |
---|
117 | $this->md5_hash_value = isset($params['md5_hash_value']) ? $params['md5_hash_value'] : ''; |
---|
118 | |
---|
119 | // Set default parameters. |
---|
120 | $this->_params = $this->_default_params; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Set parameters. |
---|
125 | * |
---|
126 | * @access public |
---|
127 | * @param array $array_merge Associative array of parameters. |
---|
128 | */ |
---|
129 | function setParam($params) |
---|
130 | { |
---|
131 | $this->_params = array_merge($this->_params, $params); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Returns a specified value from a registered parameter. |
---|
136 | * |
---|
137 | * @access public |
---|
138 | * @param string $key Which value to return. |
---|
139 | * @param array $array_merge Associative array of parameters. |
---|
140 | */ |
---|
141 | function getParam($key) |
---|
142 | { |
---|
143 | if (isset($this->_params[$key])) { |
---|
144 | return $this->_params[$key]; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * Submit parameters to gateway. |
---|
150 | * |
---|
151 | * @access public |
---|
152 | * |
---|
153 | * @return mixed False or x_response_code: false = error, 1 = accepted, 2 = declined, 3 = error |
---|
154 | */ |
---|
155 | function process() |
---|
156 | { |
---|
157 | if (empty($this->_params['x_login'])) { |
---|
158 | $this->_results['x_response_reason_text'] = _("Transaction gateway temporarily not available. Please try again later."); |
---|
159 | logMsg(sprintf('x_login not specified.', null), LOG_ERROR, __FILE__, __LINE__); |
---|
160 | return false; |
---|
161 | } |
---|
162 | if (empty($this->_params['x_card_num'])) { |
---|
163 | $this->_results['x_response_reason_text'] = _("Transaction gateway temporarily not available. Please try again later."); |
---|
164 | logMsg(sprintf('x_card_num not specified.', null), LOG_ERROR, __FILE__, __LINE__); |
---|
165 | return false; |
---|
166 | } |
---|
167 | |
---|
168 | // Generate query string from params. |
---|
169 | $q = ''; |
---|
170 | $delim = ''; |
---|
171 | foreach ($this->_params as $key=>$val) { |
---|
172 | $q .= $delim . $key . '=' . urlencode($val); |
---|
173 | $delim = '&'; |
---|
174 | } |
---|
175 | |
---|
176 | // Setup curl and execute request. |
---|
177 | $ch = curl_init(); |
---|
178 | curl_setopt($ch, CURLOPT_URL, $this->post_url); |
---|
179 | curl_setopt($ch, CURLOPT_HEADER, 0); |
---|
180 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
---|
181 | curl_setopt($ch, CURLOPT_POST, 1); |
---|
182 | curl_setopt($ch, CURLOPT_POSTFIELDS, $q); |
---|
183 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
---|
184 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); |
---|
185 | $result = curl_exec($ch); |
---|
186 | curl_close($ch); |
---|
187 | |
---|
188 | if (!$result) { |
---|
189 | return false; |
---|
190 | } |
---|
191 | return $this->_processResult($result); |
---|
192 | } |
---|
193 | |
---|
194 | |
---|
195 | /** |
---|
196 | * Returns the results array. Returns a specific element if $key is provided. |
---|
197 | * |
---|
198 | * @access public |
---|
199 | * |
---|
200 | * @return array Returns the results array. |
---|
201 | */ |
---|
202 | function getResult($key=null) |
---|
203 | { |
---|
204 | if (isset($key)) { |
---|
205 | return $this->_results[$key]; |
---|
206 | } else { |
---|
207 | return $this->_results; |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * Tests a returned md5 hash value with a locally computated one. |
---|
213 | * |
---|
214 | * @access public |
---|
215 | * |
---|
216 | * @return bool True if the hash is valid, false otherwise. |
---|
217 | */ |
---|
218 | function validMD5Hash() |
---|
219 | { |
---|
220 | return ( |
---|
221 | strtolower($this->getResult('x_md5_hash')) == strtolower(md5( |
---|
222 | $this->md5_hash_value . |
---|
223 | $this->getParam('x_login') . |
---|
224 | $this->getResult('x_trans_id') . |
---|
225 | $this->getResult('x_amount')) |
---|
226 | ) |
---|
227 | ); |
---|
228 | } |
---|
229 | |
---|
230 | /** |
---|
231 | * Reset all variables. Call before beginning a new transaction. |
---|
232 | * |
---|
233 | * @access public |
---|
234 | */ |
---|
235 | function reset() |
---|
236 | { |
---|
237 | $this->_results = Array(); |
---|
238 | $this->_params = $this->_default_params; |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * Process the result from the curl execution to create an associative array of returned data. |
---|
243 | * |
---|
244 | * @access private. |
---|
245 | * |
---|
246 | * @param mixed $result The result from the curl execution. |
---|
247 | * |
---|
248 | * @return integer Transaction result code. |
---|
249 | */ |
---|
250 | function _processResult($result) |
---|
251 | { |
---|
252 | $this->_results = Array(); |
---|
253 | |
---|
254 | $results = explode($this->getParam('x_delim_char'), $result); |
---|
255 | |
---|
256 | $num = sizeof($this->_result_fields); |
---|
257 | for ($i=0, $j=0; $i<$num; $i++) { |
---|
258 | if (isset($this->_result_fields[$i])) { |
---|
259 | $this->_results[$this->_result_fields[$i]] = $results[$i]; |
---|
260 | } else { |
---|
261 | $j++; |
---|
262 | $this->_results["x_custom_$j"] = $results[$i]; |
---|
263 | } |
---|
264 | } |
---|
265 | return $this->_results['x_response_code']; |
---|
266 | } |
---|
267 | } |
---|
268 | ?> |
---|