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 WC_Stripe_Apple_Pay 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 WC_Stripe_Apple_Pay, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class WC_Stripe_Apple_Pay extends WC_Gateway_Stripe { |
||
| 12 | /** |
||
| 13 | * This Instance. |
||
| 14 | * |
||
| 15 | * @var |
||
| 16 | */ |
||
| 17 | private static $_this; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Gateway. |
||
| 21 | * |
||
| 22 | * @var |
||
| 23 | */ |
||
| 24 | private $_gateway; |
||
|
|
|||
| 25 | |||
| 26 | /** |
||
| 27 | * Statement Description |
||
| 28 | * |
||
| 29 | * @var |
||
| 30 | */ |
||
| 31 | public $statement_descriptor; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Gateway settings. |
||
| 35 | * |
||
| 36 | * @var |
||
| 37 | */ |
||
| 38 | private $_gateway_settings; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Constructor. |
||
| 42 | * |
||
| 43 | * @access public |
||
| 44 | * @since 3.1.0 |
||
| 45 | * @version 3.1.0 |
||
| 46 | */ |
||
| 47 | public function __construct() { |
||
| 56 | |||
| 57 | public function instance() { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Initialize. |
||
| 63 | * |
||
| 64 | * @access public |
||
| 65 | * @since 3.1.0 |
||
| 66 | * @version 3.1.0 |
||
| 67 | */ |
||
| 68 | public function init() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Filters the gateway title to reflect Apple Pay. |
||
| 86 | * |
||
| 87 | */ |
||
| 88 | public function filter_gateway_title( $title, $id ) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Enqueue JS scripts and styles for single product page. |
||
| 106 | * |
||
| 107 | * @since 3.1.0 |
||
| 108 | * @version 3.1.0 |
||
| 109 | */ |
||
| 110 | public function single_scripts() { |
||
| 111 | if ( ! is_single() ) { |
||
| 112 | return; |
||
| 113 | } |
||
| 114 | |||
| 115 | global $post; |
||
| 116 | |||
| 117 | $product = wc_get_product( $post->ID ); |
||
| 118 | |||
| 119 | if ( ! in_array( ( version_compare( WC_VERSION, '2.7.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) { |
||
| 120 | return; |
||
| 121 | } |
||
| 122 | |||
| 123 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
||
| 124 | |||
| 125 | wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION ); |
||
| 126 | |||
| 127 | wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
||
| 128 | wp_enqueue_script( 'woocommerce_stripe_apple_pay_single', plugins_url( 'assets/js/stripe-apple-pay-single' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'stripe' ), WC_STRIPE_VERSION, true ); |
||
| 129 | |||
| 130 | $publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key']; |
||
| 131 | |||
| 132 | $stripe_params = array( |
||
| 133 | 'key' => $publishable_key, |
||
| 134 | 'currency_code' => get_woocommerce_currency(), |
||
| 135 | 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), |
||
| 136 | 'label' => $this->statement_descriptor, |
||
| 137 | 'ajaxurl' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
||
| 138 | 'stripe_apple_pay_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ), |
||
| 139 | 'stripe_apple_pay_cart_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ), |
||
| 140 | 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ), |
||
| 141 | 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ), |
||
| 142 | 'needs_shipping' => WC()->cart->needs_shipping() ? 'yes' : 'no', |
||
| 143 | ); |
||
| 144 | |||
| 145 | wp_localize_script( 'woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters( 'wc_stripe_apple_pay_single_params', $stripe_params ) ); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Enqueue JS scripts and styles for the cart/checkout. |
||
| 150 | * |
||
| 151 | * @since 3.1.0 |
||
| 152 | * @version 3.1.0 |
||
| 153 | */ |
||
| 154 | public function cart_scripts() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Checks to make sure product type is supported by Apple Pay. |
||
| 187 | * |
||
| 188 | */ |
||
| 189 | public function supported_product_types() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Display Apple Pay button on the cart page |
||
| 198 | * |
||
| 199 | * @since 3.1.0 |
||
| 200 | * @version 3.1.0 |
||
| 201 | */ |
||
| 202 | public function display_apple_pay_button() { |
||
| 203 | $gateways = WC()->payment_gateways->get_available_payment_gateways(); |
||
| 204 | |||
| 205 | /** |
||
| 206 | * In order for the Apple Pay button to show on cart page, |
||
| 207 | * Apple Pay must be enabled and Stripe gateway must be enabled. |
||
| 208 | */ |
||
| 209 | if ( |
||
| 210 | 'yes' !== $this->_gateway_settings['apple_pay'] |
||
| 211 | || ! isset( $gateways['stripe'] ) |
||
| 212 | ) { |
||
| 213 | return; |
||
| 214 | } |
||
| 215 | |||
| 216 | View Code Duplication | if ( is_single() ) { |
|
| 217 | global $post; |
||
| 218 | |||
| 219 | $product = wc_get_product( $post->ID ); |
||
| 220 | |||
| 221 | if ( ! in_array( ( version_compare( WC_VERSION, '2.7.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) { |
||
| 222 | return; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | $apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black'; |
||
| 227 | $button_lang = ! empty( $this->_gateway_settings['apple_pay_button_lang'] ) ? strtolower( $this->_gateway_settings['apple_pay_button_lang'] ) : 'en'; |
||
| 228 | ?> |
||
| 229 | <button class="apple-pay-button" lang="<?php echo esc_attr( $button_lang ); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr( $apple_pay_button ); ?>;"></button> |
||
| 230 | <?php |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Display Apple Pay button on the cart page |
||
| 235 | * |
||
| 236 | * @since 3.1.0 |
||
| 237 | * @version 3.1.0 |
||
| 238 | */ |
||
| 239 | public function display_apple_pay_separator_html() { |
||
| 240 | $gateways = WC()->payment_gateways->get_available_payment_gateways(); |
||
| 241 | |||
| 242 | /** |
||
| 243 | * In order for the Apple Pay button to show on cart page, |
||
| 244 | * Apple Pay must be enabled and Stripe gateway must be enabled. |
||
| 245 | */ |
||
| 246 | if ( |
||
| 247 | 'yes' !== $this->_gateway_settings['apple_pay'] |
||
| 248 | || ! isset( $gateways['stripe'] ) |
||
| 249 | ) { |
||
| 250 | return; |
||
| 251 | } |
||
| 252 | |||
| 253 | View Code Duplication | if ( is_single() ) { |
|
| 254 | global $post; |
||
| 255 | |||
| 256 | $product = wc_get_product( $post->ID ); |
||
| 257 | |||
| 258 | if ( ! in_array( ( version_compare( WC_VERSION, '2.7.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) { |
||
| 259 | return; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | ?> |
||
| 263 | <p class="apple-pay-button-checkout-separator">- <?php esc_html_e( 'Or', 'woocommerce-gateway-stripe' ); ?> -</p> |
||
| 264 | <?php |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Generates the Apple Pay single cart. |
||
| 269 | * |
||
| 270 | * @since 3.1.0 |
||
| 271 | * @version 3.1.0 |
||
| 272 | */ |
||
| 273 | public function generate_apple_pay_single() { |
||
| 274 | if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) { |
||
| 275 | wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
||
| 276 | } |
||
| 277 | |||
| 278 | if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
||
| 279 | define( 'WOOCOMMERCE_CART', true ); |
||
| 280 | } |
||
| 281 | |||
| 282 | global $post; |
||
| 283 | |||
| 284 | $product = wc_get_product( $post->ID ); |
||
| 285 | $qty = absint( $_POST['qty'] ); |
||
| 286 | |||
| 287 | /** |
||
| 288 | * If this page is single product page, we need to simulate |
||
| 289 | * adding the product to the cart taken account if it is a |
||
| 290 | * simple or variable product. |
||
| 291 | */ |
||
| 292 | if ( is_single() ) { |
||
| 293 | // First empty the cart to prevent wrong calculation. |
||
| 294 | WC()->cart->empty_cart(); |
||
| 295 | |||
| 296 | if ( 'variable' === ( version_compare( WC_VERSION, '2.7.0', '<' ) ? $product->product_type : $product->get_type() ) && isset( $_POST['attributes'] ) ) { |
||
| 297 | $attributes = array_map( 'wc_clean', $_POST['attributes'] ); |
||
| 298 | |||
| 299 | $variation_id = $product->get_matching_variation( $attributes ); |
||
| 300 | |||
| 301 | WC()->cart->add_to_cart( $product->get_id(), $qty, $variation_id, $attributes ); |
||
| 302 | } |
||
| 303 | |||
| 304 | if ( 'simple' === ( version_compare( WC_VERSION, '2.7.0', '<' ) ? $product->product_type : $product->get_type() ) ) { |
||
| 305 | WC()->cart->add_to_cart( $product->get_id(), $qty ); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | WC()->cart->calculate_totals(); |
||
| 310 | |||
| 311 | wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Generates the Apple Pay cart. |
||
| 316 | * |
||
| 317 | * @since 3.1.0 |
||
| 318 | * @version 3.1.0 |
||
| 319 | */ |
||
| 320 | public function generate_apple_pay_cart() { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Calculate and set shipping method. |
||
| 330 | * |
||
| 331 | * @since 3.1.0 |
||
| 332 | * @version 3.1.0 |
||
| 333 | * @param array $address |
||
| 334 | */ |
||
| 335 | public function calculate_shipping( $address = array() ) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Gets shipping for Apple Pay Payment sheet. |
||
| 394 | * |
||
| 395 | * @since 3.1.0 |
||
| 396 | * @version 3.1.0 |
||
| 397 | */ |
||
| 398 | public function get_shipping_methods() { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Updates shipping method on cart session. |
||
| 453 | * |
||
| 454 | * @since 3.1.0 |
||
| 455 | * @version 3.1.0 |
||
| 456 | */ |
||
| 457 | public function update_shipping_method() { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Handles the Apple Pay processing via AJAX |
||
| 504 | * |
||
| 505 | * @access public |
||
| 506 | * @since 3.1.0 |
||
| 507 | * @version 3.1.0 |
||
| 508 | */ |
||
| 509 | public function process_apple_pay() { |
||
| 510 | if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) { |
||
| 511 | wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
||
| 512 | } |
||
| 513 | |||
| 514 | try { |
||
| 515 | $result = array_map( 'wc_clean', $_POST['result'] ); |
||
| 516 | |||
| 517 | $order = $this->create_order( $result ); |
||
| 518 | |||
| 519 | $order_id = version_compare( WC_VERSION, '2.7.0', '<' ) ? $order->id : $order->get_id(); |
||
| 520 | |||
| 521 | // Handle payment. |
||
| 522 | if ( $order->get_total() > 0 ) { |
||
| 523 | |||
| 524 | View Code Duplication | if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) { |
|
| 525 | return new WP_Error( 'stripe_error', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe::get_minimum_amount() / 100 ) ) ); |
||
| 526 | } |
||
| 527 | |||
| 528 | WC_Stripe::log( "Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}" ); |
||
| 529 | |||
| 530 | // Make the request. |
||
| 531 | $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) ); |
||
| 532 | |||
| 533 | if ( is_wp_error( $response ) ) { |
||
| 534 | $localized_messages = $this->get_localized_messages(); |
||
| 535 | |||
| 536 | throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) ); |
||
| 537 | } |
||
| 538 | |||
| 539 | // Process valid response. |
||
| 540 | $this->process_response( $response, $order ); |
||
| 541 | } else { |
||
| 542 | $order->payment_complete(); |
||
| 543 | } |
||
| 544 | |||
| 545 | // Remove cart. |
||
| 546 | WC()->cart->empty_cart(); |
||
| 547 | |||
| 548 | update_post_meta( $order_id, '_customer_user', get_current_user_id() ); |
||
| 549 | update_post_meta( $order_id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) ); |
||
| 550 | |||
| 551 | // Return thank you page redirect. |
||
| 552 | wp_send_json( array( |
||
| 553 | 'success' => 'true', |
||
| 554 | 'redirect' => $this->get_return_url( $order ), |
||
| 555 | ) ); |
||
| 556 | |||
| 557 | } catch ( Exception $e ) { |
||
| 558 | WC()->session->set( 'refresh_totals', true ); |
||
| 559 | WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) ); |
||
| 560 | |||
| 561 | if ( is_object( $order ) && isset( $order_id ) && $order->has_status( array( 'pending', 'failed' ) ) ) { |
||
| 562 | $this->send_failed_order_email( $order_id ); |
||
| 563 | } |
||
| 564 | |||
| 565 | wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) ); |
||
| 566 | } |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Generate the request for the payment. |
||
| 571 | * @param WC_Order $order |
||
| 572 | * @param string $source token |
||
| 573 | * @return array() |
||
| 574 | */ |
||
| 575 | protected function generate_payment_request( $order, $source ) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Builds the shippings methods to pass to Apple Pay. |
||
| 604 | * |
||
| 605 | * @since 3.1.0 |
||
| 606 | * @version 3.1.0 |
||
| 607 | */ |
||
| 608 | public function build_shipping_methods( $shipping_methods ) { |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Builds the line items to pass to Apple Pay. |
||
| 629 | * |
||
| 630 | * @since 3.1.0 |
||
| 631 | * @version 3.1.0 |
||
| 632 | */ |
||
| 633 | public function build_line_items() { |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Create order programatically. |
||
| 702 | * |
||
| 703 | * @since 3.1.0 |
||
| 704 | * @version 3.1.0 |
||
| 705 | * @param array $data |
||
| 706 | * @return object $order |
||
| 707 | */ |
||
| 708 | public function create_order( $data = array() ) { |
||
| 851 | } |
||
| 852 | |||
| 854 |
This check marks private properties in classes that are never used. Those properties can be removed.