Complex classes like AuthorizeNetAIM often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AuthorizeNetAIM, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class AuthorizeNetAIM extends AuthorizeNetRequest |
||
|
|
|||
| 44 | { |
||
| 45 | |||
| 46 | const LIVE_URL = 'https://secure2.authorize.net/gateway/transact.dll'; |
||
| 47 | const SANDBOX_URL = 'https://test.authorize.net/gateway/transact.dll'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Holds all the x_* name/values that will be posted in the request. |
||
| 51 | * Default values are provided for best practice fields. |
||
| 52 | */ |
||
| 53 | protected $_x_post_fields = array( |
||
| 54 | "version" => "3.1", |
||
| 55 | "delim_char" => ",", |
||
| 56 | "delim_data" => "TRUE", |
||
| 57 | "relay_response" => "FALSE", |
||
| 58 | "encap_char" => "|", |
||
| 59 | ); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Only used if merchant wants to send multiple line items about the charge. |
||
| 63 | */ |
||
| 64 | private $_additional_line_items = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Only used if merchant wants to send custom fields. |
||
| 68 | */ |
||
| 69 | private $_custom_fields = array(); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Checks to make sure a field is actually in the API before setting. |
||
| 73 | * Set to false to skip this check. |
||
| 74 | */ |
||
| 75 | public $verify_x_fields = true; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * A list of all fields in the AIM API. |
||
| 79 | * Used to warn user if they try to set a field not offered in the API. |
||
| 80 | */ |
||
| 81 | private $_all_aim_fields = array("address","allow_partial_auth","amount", |
||
| 82 | "auth_code","authentication_indicator", "bank_aba_code","bank_acct_name", |
||
| 83 | "bank_acct_num","bank_acct_type","bank_check_number","bank_name", |
||
| 84 | "card_code","card_num","cardholder_authentication_value","city","company", |
||
| 85 | "country","cust_id","customer_ip","delim_char","delim_data","description", |
||
| 86 | "duplicate_window","duty","echeck_type","email","email_customer", |
||
| 87 | "encap_char","exp_date","fax","first_name","footer_email_receipt", |
||
| 88 | "freight","header_email_receipt","invoice_num","last_name","line_item", |
||
| 89 | "login","method","phone","po_num","recurring_billing","relay_response", |
||
| 90 | "ship_to_address","ship_to_city","ship_to_company","ship_to_country", |
||
| 91 | "ship_to_first_name","ship_to_last_name","ship_to_state","ship_to_zip", |
||
| 92 | "split_tender_id","state","tax","tax_exempt","test_request","tran_key", |
||
| 93 | "trans_id","type","version","zip" |
||
| 94 | ); |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Do an AUTH_CAPTURE transaction. |
||
| 98 | * |
||
| 99 | * Required "x_" fields: card_num, exp_date, amount |
||
| 100 | * |
||
| 101 | * @param string $amount The dollar amount to charge |
||
| 102 | * @param string $card_num The credit card number |
||
| 103 | * @param string $exp_date CC expiration date |
||
| 104 | * |
||
| 105 | * @return AuthorizeNetAIM_Response |
||
| 106 | */ |
||
| 107 | 24 | public function authorizeAndCapture($amount = false, $card_num = false, $exp_date = false) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Do a PRIOR_AUTH_CAPTURE transaction. |
||
| 118 | * |
||
| 119 | * Required "x_" field: trans_id(The transaction id of the prior auth, unless split |
||
| 120 | * tender, then set x_split_tender_id manually.) |
||
| 121 | * amount (only if lesser than original auth) |
||
| 122 | * |
||
| 123 | * @param string $trans_id Transaction id to charge |
||
| 124 | * @param string $amount Dollar amount to charge if lesser than auth |
||
| 125 | * |
||
| 126 | * @return AuthorizeNetAIM_Response |
||
| 127 | */ |
||
| 128 | 2 | public function priorAuthCapture($trans_id = false, $amount = false) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Do an AUTH_ONLY transaction. |
||
| 138 | * |
||
| 139 | * Required "x_" fields: card_num, exp_date, amount |
||
| 140 | * |
||
| 141 | * @param string $amount The dollar amount to charge |
||
| 142 | * @param string $card_num The credit card number |
||
| 143 | * @param string $exp_date CC expiration date |
||
| 144 | * |
||
| 145 | * @return AuthorizeNetAIM_Response |
||
| 146 | */ |
||
| 147 | 5 | public function authorizeOnly($amount = false, $card_num = false, $exp_date = false) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Do a VOID transaction. |
||
| 158 | * |
||
| 159 | * Required "x_" field: trans_id(The transaction id of the prior auth, unless split |
||
| 160 | * tender, then set x_split_tender_id manually.) |
||
| 161 | * |
||
| 162 | * @param string $trans_id Transaction id to void |
||
| 163 | * |
||
| 164 | * @return AuthorizeNetAIM_Response |
||
| 165 | */ |
||
| 166 | 4 | public function void($trans_id = false) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Do a CAPTURE_ONLY transaction. |
||
| 175 | * |
||
| 176 | * Required "x_" fields: auth_code, amount, card_num , exp_date |
||
| 177 | * |
||
| 178 | * @param string $auth_code The auth code |
||
| 179 | * @param string $amount The dollar amount to charge |
||
| 180 | * @param string $card_num The last 4 of credit card number |
||
| 181 | * @param string $exp_date CC expiration date |
||
| 182 | * |
||
| 183 | * @return AuthorizeNetAIM_Response |
||
| 184 | */ |
||
| 185 | public function captureOnly($auth_code = false, $amount = false, $card_num = false, $exp_date = false) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Do a CREDIT transaction. |
||
| 197 | * |
||
| 198 | * Required "x_" fields: trans_id, amount, card_num (just the last 4) |
||
| 199 | * |
||
| 200 | * @param string $trans_id Transaction id to credit |
||
| 201 | * @param string $amount The dollar amount to credit |
||
| 202 | * @param string $card_num The last 4 of credit card number |
||
| 203 | * |
||
| 204 | * @return AuthorizeNetAIM_Response |
||
| 205 | */ |
||
| 206 | public function credit($trans_id = false, $amount = false, $card_num = false) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Alternative syntax for setting x_ fields. |
||
| 217 | * |
||
| 218 | * Usage: $sale->method = "echeck"; |
||
| 219 | * |
||
| 220 | * @param string $name |
||
| 221 | * @param string $value |
||
| 222 | */ |
||
| 223 | 29 | public function __set($name, $value) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Quickly set multiple fields. |
||
| 230 | * |
||
| 231 | * Note: The prefix x_ will be added to all fields. If you want to set a |
||
| 232 | * custom field without the x_ prefix, use setCustomField or setCustomFields. |
||
| 233 | * |
||
| 234 | * @param array $fields Takes an array or object. |
||
| 235 | */ |
||
| 236 | 21 | public function setFields($fields) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Quickly set multiple custom fields. |
||
| 246 | * |
||
| 247 | * @param array $fields |
||
| 248 | */ |
||
| 249 | 1 | public function setCustomFields($fields) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Add a line item. |
||
| 259 | * |
||
| 260 | * @param string $item_id |
||
| 261 | * @param string $item_name |
||
| 262 | * @param string $item_description |
||
| 263 | * @param string $item_quantity |
||
| 264 | * @param string $item_unit_price |
||
| 265 | * @param string $item_taxable |
||
| 266 | */ |
||
| 267 | 2 | public function addLineItem($item_id, $item_name, $item_description, $item_quantity, $item_unit_price, $item_taxable) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Use ECHECK as payment type. |
||
| 280 | */ |
||
| 281 | 1 | public function setECheck($bank_aba_code, $bank_acct_num, $bank_acct_type, $bank_name, $bank_acct_name, $echeck_type = 'WEB') |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Set an individual name/value pair. This will append x_ to the name |
||
| 298 | * before posting. |
||
| 299 | * |
||
| 300 | * @param string $name |
||
| 301 | * @param string $value |
||
| 302 | */ |
||
| 303 | 30 | public function setField($name, $value) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Set a custom field. Note: the x_ prefix will not be added to |
||
| 319 | * your custom field if you use this method. |
||
| 320 | * |
||
| 321 | * @param string $name |
||
| 322 | * @param string $value |
||
| 323 | */ |
||
| 324 | 4 | public function setCustomField($name, $value) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Unset an x_ field. |
||
| 331 | * |
||
| 332 | * @param string $name Field to unset. |
||
| 333 | */ |
||
| 334 | 1 | public function unsetField($name) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * |
||
| 341 | * |
||
| 342 | * @param string $response |
||
| 343 | * |
||
| 344 | * @return AuthorizeNetAIM_Response |
||
| 345 | */ |
||
| 346 | 24 | protected function _handleResponse($response) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | 29 | protected function _getPostUrl() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Converts the x_post_fields array into a string suitable for posting. |
||
| 361 | */ |
||
| 362 | 29 | protected function _setPostString() |
|
| 380 | } |
||
| 381 | |||
| 502 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.