Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EEG_Paypal_Express 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 EEG_Paypal_Express, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined( 'EVENT_ESPRESSO_VERSION' )) { exit('NO direct script access allowed'); } |
||
15 | class EEG_Paypal_Express extends EE_Offsite_Gateway { |
||
16 | |||
17 | /** |
||
18 | * Merchant API Username. |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $_api_username = null; |
||
22 | |||
23 | /** |
||
24 | * Merchant API Password. |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $_api_password = null; |
||
28 | |||
29 | /** |
||
30 | * API Signature. |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $_api_signature = null; |
||
34 | |||
35 | /** |
||
36 | * Request Shipping address on PP checkout page. |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $_request_shipping_addr = null; |
||
40 | |||
41 | /** |
||
42 | * Business/personal logo. |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $_image_url = null; |
||
46 | |||
47 | /** |
||
48 | * All the currencies supported by this gateway. |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $_currencies_supported = array( |
||
52 | 'USD', |
||
53 | 'AUD', |
||
54 | 'BRL', |
||
55 | 'CAD', |
||
56 | 'CZK', |
||
57 | 'DKK', |
||
58 | 'EUR', |
||
59 | 'HKD', |
||
60 | 'HUF', |
||
61 | 'ILS', |
||
62 | 'JPY', |
||
63 | 'MYR', |
||
64 | 'MXN', |
||
65 | 'NOK', |
||
66 | 'NZD', |
||
67 | 'PHP', |
||
68 | 'PLN', |
||
69 | 'GBP', |
||
70 | 'RUB', |
||
71 | 'SGD', |
||
72 | 'SEK', |
||
73 | 'CHF', |
||
74 | 'TWD', |
||
75 | 'THB', |
||
76 | 'TRY' |
||
77 | ); |
||
78 | |||
79 | |||
80 | /** |
||
81 | * Sets the gateway URL variable based on whether debug mode is enabled or not. |
||
82 | * |
||
83 | * @param type $settings_array |
||
84 | */ |
||
85 | public function set_settings( $settings_array ) { |
||
94 | |||
95 | |||
96 | /** |
||
97 | * |
||
98 | * @param EEI_Payment $payment |
||
99 | * @param type $billing_info |
||
100 | * @param type $return_url |
||
101 | * @param type $cancel_url |
||
102 | */ |
||
103 | public function set_redirection_info( $payment, $billing_info = array(), $return_url = NULL, $notify_url = NULL, $cancel_url = NULL ) { |
||
104 | View Code Duplication | if ( ! $payment instanceof EEI_Payment ) { |
|
105 | $payment->set_gateway_response( __( 'Error. No associated payment was found.', 'event_espresso' ) ); |
||
106 | $payment->set_status( $this->_pay_model->failed_status() ); |
||
107 | return $payment; |
||
108 | } |
||
109 | $transaction = $payment->transaction(); |
||
110 | View Code Duplication | if ( ! $transaction instanceof EEI_Transaction ) { |
|
111 | $payment->set_gateway_response( __( 'Could not process this payment because it has no associated transaction.', 'event_espresso' ) ); |
||
112 | $payment->set_status( $this->_pay_model->failed_status() ); |
||
113 | return $payment; |
||
114 | } |
||
115 | $order_description = substr( $this->_format_order_description($payment), 0, 127 ); |
||
116 | $primary_registration = $transaction->primary_registration(); |
||
117 | $primary_attendee = $primary_registration instanceof EE_Registration ? $primary_registration->attendee() : false; |
||
118 | $locale = explode('-', get_bloginfo('language')); |
||
119 | |||
120 | // Gather request parameters. |
||
121 | $token_request_dtls = array( |
||
122 | 'METHOD' => 'SetExpressCheckout', |
||
123 | 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
||
124 | 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
||
125 | 'PAYMENTREQUEST_0_DESC' => $order_description, |
||
126 | 'RETURNURL' => $return_url, |
||
127 | 'CANCELURL' => $cancel_url, |
||
128 | 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
||
129 | 'SOLUTIONTYPE' => 'Sole', // Buyer does not need to create a PayPal account to check out. This is referred to as PayPal Account Optional. |
||
130 | 'BUTTONSOURCE' => 'EventEspresso_SP',//EE will blow up if you change this |
||
131 | 'LOCALECODE' => $locale[1] // Locale of the pages displayed by PayPal during Express Checkout. |
||
132 | ); |
||
133 | |||
134 | // Show itemized list. |
||
135 | if ( EEH_Money::compare_floats( $payment->amount(), $transaction->total(), '==' ) ) { |
||
136 | $item_num = 0; |
||
137 | $itemized_sum = 0; |
||
138 | $total_line_items = $transaction->total_line_item(); |
||
139 | // Go through each item in the list. |
||
140 | foreach ( $total_line_items->get_items() as $line_item ) { |
||
141 | if ( $line_item instanceof EE_Line_Item ) { |
||
142 | // PayPal doesn't like line items with 0.00 amount, so we may skip those. |
||
143 | if ( EEH_Money::compare_floats( $line_item->total(), '0.00', '==' ) ) { |
||
144 | continue; |
||
145 | } |
||
146 | |||
147 | $unit_price = $line_item->unit_price(); |
||
148 | $line_item_quantity = $line_item->quantity(); |
||
149 | // This is a discount. |
||
150 | if ( $line_item->is_percent() ) { |
||
151 | $unit_price = $line_item->total(); |
||
152 | $line_item_quantity = 1; |
||
153 | } |
||
154 | // Item Name. |
||
155 | $token_request_dtls['L_PAYMENTREQUEST_0_NAME'.$item_num] = substr($line_item->name(), 0, 127); |
||
156 | // Item description. |
||
157 | $token_request_dtls['L_PAYMENTREQUEST_0_DESC'.$item_num] = substr($line_item->desc(), 0, 127); |
||
158 | // Cost of individual item. |
||
159 | $token_request_dtls['L_PAYMENTREQUEST_0_AMT'.$item_num] = $this->format_currency( $unit_price ); |
||
160 | // Item Number. |
||
161 | $token_request_dtls['L_PAYMENTREQUEST_0_NUMBER'.$item_num] = $item_num + 1; |
||
162 | // Item quantity. |
||
163 | $token_request_dtls['L_PAYMENTREQUEST_0_QTY'.$item_num] = $line_item_quantity; |
||
164 | // Digital item is sold. |
||
165 | $token_request_dtls['L_PAYMENTREQUEST_0_ITEMCATEGORY'.$item_num] = 'Physical'; |
||
166 | $itemized_sum += $line_item->total(); |
||
167 | ++$item_num; |
||
168 | } |
||
169 | } |
||
170 | // Item's sales S/H and tax amount. |
||
171 | $token_request_dtls['PAYMENTREQUEST_0_ITEMAMT'] = $total_line_items->get_items_total(); |
||
172 | $token_request_dtls['PAYMENTREQUEST_0_TAXAMT'] = $total_line_items->get_total_tax(); |
||
173 | $token_request_dtls['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
||
174 | $token_request_dtls['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
||
175 | |||
176 | $itemized_sum_diff_from_txn_total = round( $transaction->total() - $itemized_sum - $total_line_items->get_total_tax(), 2 ); |
||
177 | // If we were not able to recognize some item like promotion, surcharge or cancellation, |
||
178 | // add the difference as an extra line item. |
||
179 | if ( $itemized_sum_diff_from_txn_total != 0 ) { |
||
180 | // Item Name. |
||
181 | $token_request_dtls['L_PAYMENTREQUEST_0_NAME'.$item_num] = substr( __( 'Other (promotion/surcharge/cancellation)', 'event_espresso' ), 0, 127 ); |
||
182 | // Item description. |
||
183 | $token_request_dtls['L_PAYMENTREQUEST_0_DESC'.$item_num] = substr( $line_item->desc(), 0, 127 ); |
||
184 | // Cost of individual item. |
||
185 | $token_request_dtls['L_PAYMENTREQUEST_0_AMT'.$item_num] = $this->format_currency( $itemized_sum_diff_from_txn_total ); |
||
186 | // Item Number. |
||
187 | $token_request_dtls['L_PAYMENTREQUEST_0_NUMBER'.$item_num] = $item_num + 1; |
||
188 | // Item quantity. |
||
189 | $token_request_dtls['L_PAYMENTREQUEST_0_QTY'.$item_num] = 1; |
||
190 | ++$item_num; |
||
191 | } |
||
192 | } else { |
||
193 | // Just one Item. |
||
194 | // Item Name. |
||
195 | $token_request_dtls['L_PAYMENTREQUEST_0_NAME0'] = substr( sprintf( __('Payment for %1$s', 'event_espresso'), $primary_registration->reg_code() ), 0, 127 ); |
||
196 | // Item description. |
||
197 | $token_request_dtls['L_PAYMENTREQUEST_0_DESC0'] = substr( sprintf( __('Payment of %1$s for %2$s', 'event_espresso'), $payment->amount(), $primary_registration->reg_code() ), 0, 127 ); |
||
198 | // Cost of individual item. |
||
199 | $token_request_dtls['L_PAYMENTREQUEST_0_AMT0'] = $this->format_currency( $payment->amount() ); |
||
200 | // Item Number. |
||
201 | $token_request_dtls['L_PAYMENTREQUEST_0_NUMBER0'] = 1; |
||
202 | // Item quantity. |
||
203 | $token_request_dtls['L_PAYMENTREQUEST_0_QTY0'] = 1; |
||
204 | // Digital item is sold. |
||
205 | $token_request_dtls['L_PAYMENTREQUEST_0_ITEMCATEGORY0'] = 'Physical'; |
||
206 | // Item's sales S/H and tax amount. |
||
207 | $token_request_dtls['PAYMENTREQUEST_0_ITEMAMT'] = $this->format_currency( $payment->amount() ); |
||
208 | $token_request_dtls['PAYMENTREQUEST_0_TAXAMT'] = '0'; |
||
209 | $token_request_dtls['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
||
210 | $token_request_dtls['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
||
211 | } |
||
212 | // Automatically filling out shipping and contact information. |
||
213 | if ( $this->_request_shipping_addr && $primary_attendee instanceof EEI_Attendee ) { |
||
214 | $token_request_dtls['NOSHIPPING'] = '2'; // If you do not pass the shipping address, PayPal obtains it from the buyer's account profile. |
||
215 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET'] = $primary_attendee->address(); |
||
216 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET2'] = $primary_attendee->address2(); |
||
217 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCITY'] = $primary_attendee->city(); |
||
218 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTATE'] = $primary_attendee->state_abbrev(); |
||
219 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $primary_attendee->country_ID(); |
||
220 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOZIP'] = $primary_attendee->zip(); |
||
221 | $token_request_dtls['PAYMENTREQUEST_0_EMAIL'] = $primary_attendee->email(); |
||
222 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOPHONENUM'] = $primary_attendee->phone(); |
||
223 | } elseif ( ! $this->_request_shipping_addr ) { |
||
224 | // Do not request shipping details on the PP Checkout page. |
||
225 | $token_request_dtls['NOSHIPPING'] = '1'; |
||
226 | $token_request_dtls['REQCONFIRMSHIPPING'] = '0'; |
||
227 | |||
228 | } |
||
229 | // Used a business/personal logo on the PayPal page. |
||
230 | if ( ! empty($this->_image_url) ) { |
||
231 | $token_request_dtls['LOGOIMG'] = $this->_image_url; |
||
232 | } |
||
233 | // Request PayPal token. |
||
234 | $token_request_response = $this->_ppExpress_request( $token_request_dtls, 'Payment Token', $payment ); |
||
235 | $token_rstatus = $this->_ppExpress_check_response( $token_request_response ); |
||
236 | $response_args = ( isset($token_rstatus['args']) && is_array($token_rstatus['args']) ) ? $token_rstatus['args'] : array(); |
||
237 | if ( $token_rstatus['status'] ) { |
||
238 | // We got the Token so we may continue with the payment and redirect the client. |
||
239 | $payment->set_details( $response_args ); |
||
240 | |||
241 | $gateway_url = ( $this->_debug_mode ) ? 'https://www.sandbox.paypal.com' : 'https://www.paypal.com'; |
||
242 | $payment->set_redirect_url( $gateway_url . '/checkoutnow?useraction=commit&cmd=_express-checkout&token=' . $response_args['TOKEN'] ); |
||
243 | View Code Duplication | } else { |
|
244 | if ( isset($response_args['L_ERRORCODE']) ) { |
||
245 | $payment->set_gateway_response( $response_args['L_ERRORCODE'] . '; ' . $response_args['L_SHORTMESSAGE'] ); |
||
246 | } else { |
||
247 | $payment->set_gateway_response( __( 'Error occurred while trying to setup the Express Checkout.', 'event_espresso' ) ); |
||
248 | } |
||
249 | $payment->set_details( $response_args ); |
||
250 | $payment->set_status( $this->_pay_model->failed_status() ); |
||
251 | } |
||
252 | |||
253 | return $payment; |
||
254 | } |
||
255 | |||
256 | |||
257 | /** |
||
258 | * |
||
259 | * @param array $update_info { |
||
260 | * @type string $gateway_txn_id |
||
261 | * @type string status an EEMI_Payment status |
||
262 | * } |
||
263 | * @param type $transaction |
||
264 | * @return EEI_Payment |
||
265 | */ |
||
266 | public function handle_payment_update( $update_info, $transaction ) { |
||
267 | $payment = ( $transaction instanceof EEI_Transaction ) ? $transaction->last_payment() : null; |
||
268 | |||
269 | if ( $payment instanceof EEI_Payment ) { |
||
270 | $this->log( array( 'Return from Authorization' => $update_info ), $payment ); |
||
271 | |||
272 | $payment_details = $payment->details(); |
||
273 | $transaction = $payment->transaction(); |
||
274 | View Code Duplication | if ( ! $transaction instanceof EEI_Transaction ) { |
|
275 | $payment->set_gateway_response( __( 'Could not process this payment because it has no associated transaction.', 'event_espresso' ) ); |
||
276 | $payment->set_status( $this->_pay_model->failed_status() ); |
||
277 | return $payment; |
||
278 | } |
||
279 | $primary_registrant = $transaction->primary_registration(); |
||
280 | |||
281 | // Check if we still have the token. |
||
282 | if ( ! isset($payment_details['TOKEN']) || empty($payment_details['TOKEN']) ) { |
||
283 | $payment->set_status( $this->_pay_model->failed_status() ); |
||
284 | return $payment; |
||
285 | } |
||
286 | |||
287 | $cdetails_request_dtls = array( |
||
288 | 'METHOD' => 'GetExpressCheckoutDetails', |
||
289 | 'TOKEN' => $payment_details['TOKEN'] |
||
290 | ); |
||
291 | // Request Customer Details. |
||
292 | $cdetails_request_response = $this->_ppExpress_request( $cdetails_request_dtls, 'Customer Details', $payment ); |
||
293 | $cdetails_rstatus = $this->_ppExpress_check_response( $cdetails_request_response ); |
||
294 | $cdata_response_args = ( isset($cdetails_rstatus['args']) && is_array($cdetails_rstatus['args']) ) ? $cdetails_rstatus['args'] : array(); |
||
295 | if ( $cdetails_rstatus['status'] ) { |
||
296 | // We got the PayerID so now we can Complete the transaction. |
||
297 | $docheckout_request_dtls = array( |
||
298 | 'METHOD' => 'DoExpressCheckoutPayment', |
||
299 | 'PAYERID' => $cdata_response_args['PAYERID'], |
||
300 | 'TOKEN' => $payment_details['TOKEN'], |
||
301 | 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
||
302 | 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
||
303 | 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code() |
||
304 | ); |
||
305 | // Payment Checkout/Capture. |
||
306 | $docheckout_request_response = $this->_ppExpress_request( $docheckout_request_dtls, 'Do Payment', $payment ); |
||
307 | $docheckout_rstatus = $this->_ppExpress_check_response( $docheckout_request_response ); |
||
308 | $docheckout_response_args = ( isset($docheckout_rstatus['args']) && is_array($docheckout_rstatus['args']) ) ? $docheckout_rstatus['args'] : array(); |
||
309 | if ( $docheckout_rstatus['status'] ) { |
||
310 | // All is well, payment approved. |
||
311 | $primary_registration_code = $primary_registrant instanceof EE_Registration ? $primary_registrant->reg_code() : ''; |
||
312 | $payment->set_extra_accntng( $primary_registration_code ); |
||
313 | $payment->set_amount( isset($docheckout_response_args['PAYMENTINFO_0_AMT']) ? floatval( $docheckout_response_args['PAYMENTINFO_0_AMT'] ) : 0 ); |
||
314 | $payment->set_txn_id_chq_nmbr( isset( $docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID'] ) ? $docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID'] : null ); |
||
315 | $payment->set_details( $cdata_response_args ); |
||
316 | $payment->set_gateway_response( isset( $docheckout_response_args['PAYMENTINFO_0_ACK'] ) ? $docheckout_response_args['PAYMENTINFO_0_ACK'] : '' ); |
||
317 | $payment->set_status( $this->_pay_model->approved_status() ); |
||
318 | View Code Duplication | } else { |
|
319 | if ( isset($docheckout_response_args['L_ERRORCODE']) ) { |
||
320 | $payment->set_gateway_response( $docheckout_response_args['L_ERRORCODE'] . '; ' . $docheckout_response_args['L_SHORTMESSAGE'] ); |
||
321 | } else { |
||
322 | $payment->set_gateway_response( __( 'Error occurred while trying to Capture the funds.', 'event_espresso' ) ); |
||
323 | } |
||
324 | $payment->set_details( $docheckout_response_args ); |
||
325 | $payment->set_status( $this->_pay_model->declined_status() ); |
||
326 | } |
||
327 | View Code Duplication | } else { |
|
328 | if ( isset($cdata_response_args['L_ERRORCODE']) ) { |
||
329 | $payment->set_gateway_response( $cdata_response_args['L_ERRORCODE'] . '; ' . $cdata_response_args['L_SHORTMESSAGE'] ); |
||
330 | } else { |
||
331 | $payment->set_gateway_response( __( 'Error occurred while trying to get payment Details from PayPal.', 'event_espresso' ) ); |
||
332 | } |
||
333 | $payment->set_details( $cdata_response_args ); |
||
334 | $payment->set_status( $this->_pay_model->failed_status() ); |
||
335 | } |
||
336 | } else { |
||
337 | $payment->set_gateway_response( __( 'Error occurred while trying to process the payment.', 'event_espresso' ) ); |
||
338 | $payment->set_status( $this->_pay_model->failed_status() ); |
||
339 | } |
||
340 | |||
341 | return $payment; |
||
342 | } |
||
343 | |||
344 | |||
345 | /** |
||
346 | * Make the Express checkout request. |
||
347 | * |
||
348 | * @param array $request_params |
||
349 | * @param string $request_text |
||
350 | * @param EEI_Payment $payment |
||
351 | * @return mixed |
||
352 | */ |
||
353 | public function _ppExpress_request( $request_params, $request_text, $payment ) { |
||
380 | |||
381 | |||
382 | /** |
||
383 | * Check the response status. |
||
384 | * |
||
385 | * @param mixed $request_response |
||
386 | * @return array |
||
387 | */ |
||
388 | public function _ppExpress_check_response( $request_response ) { |
||
415 | |||
416 | |||
417 | /** |
||
418 | * Log a "Cleared" request. |
||
419 | * |
||
420 | * @param array $response |
||
421 | * @param EEI_Payment $payment |
||
422 | * @param string $info |
||
423 | * @return void |
||
424 | */ |
||
425 | private function _log_clean_request( $request_prms, $payment, $info ) { |
||
432 | |||
433 | |||
434 | /** |
||
435 | * Get error from the response data. |
||
436 | * |
||
437 | * @param array $data_array |
||
438 | * @return array |
||
439 | */ |
||
440 | private function _get_errors( $data_array ) { |
||
468 | |||
469 | } |
||
470 | // End of file EEG_Paypal_Express.gateway.php |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: