| Total Complexity | 48 |
| Total Lines | 535 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like GetPaid_Payment_Gateway 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.
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 GetPaid_Payment_Gateway, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class GetPaid_Payment_Gateway { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Set if the place checkout button should be renamed on selection. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | public $checkout_button_text; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Boolean whether the method is enabled. |
||
| 25 | * |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | public $enabled = true; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Payment method id. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | public $id; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Payment method order. |
||
| 39 | * |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | public $order = 10; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Payment method title for the frontend. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | public $title; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Payment method description for the frontend. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | public $description; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Gateway title. |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | public $method_title = ''; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Gateway description. |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | public $method_description = ''; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Countries this gateway is allowed for. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | public $countries; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Currencies this gateway is allowed for. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | public $currencies; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Currencies this gateway is not allowed for. |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | public $exclude_currencies; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Maximum transaction amount, zero does not define a maximum. |
||
| 95 | * |
||
| 96 | * @var int |
||
| 97 | */ |
||
| 98 | public $max_amount = 0; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Optional URL to view a transaction. |
||
| 102 | * |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | public $view_transaction_url = ''; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Optional URL to view a subscription. |
||
| 109 | * |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | public $view_subscription_url = ''; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Optional label to show for "new payment method" in the payment |
||
| 116 | * method/token selection radio selection. |
||
| 117 | * |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | public $new_method_label = ''; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Contains a users saved tokens for this gateway. |
||
| 124 | * |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | protected $tokens = array(); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * An array of features that this gateway supports. |
||
| 131 | * |
||
| 132 | * @var array |
||
| 133 | */ |
||
| 134 | protected $supports = array(); |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Class constructor. |
||
| 138 | */ |
||
| 139 | public function __construct() { |
||
| 140 | $this->enabled = wpinv_is_gateway_active( $this->id ); |
||
| 141 | |||
| 142 | // Register gateway. |
||
| 143 | add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) ); |
||
| 144 | |||
| 145 | // Enable Subscriptions. |
||
| 146 | if ( $this->supports( 'subscription' ) ) { |
||
| 147 | add_filter( "wpinv_{$this->id}_support_subscription", '__return_true' ); |
||
| 148 | } |
||
| 149 | |||
| 150 | // Gateway settings. |
||
| 151 | add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 ); |
||
| 152 | |||
| 153 | // Process payment. |
||
| 154 | add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 ); |
||
| 155 | |||
| 156 | // Change the checkout button text. |
||
| 157 | if ( ! empty( $this->checkout_button_text ) ) { |
||
| 158 | add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) ); |
||
| 159 | } |
||
| 160 | |||
| 161 | // Check if a gateway is valid for a given currency. |
||
| 162 | add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 ); |
||
| 163 | |||
| 164 | // Generate the transaction url. |
||
| 165 | add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 ); |
||
| 166 | |||
| 167 | // Generate the subscription url. |
||
| 168 | add_filter( "getpaid_gateway_{$this->id}_subscription_url", array( $this, 'filter_subscription_url' ), 10, 2 ); |
||
| 169 | |||
| 170 | // Confirm payments. |
||
| 171 | add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 ); |
||
| 172 | |||
| 173 | // Verify IPNs. |
||
| 174 | add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) ); |
||
| 175 | |||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Checks if this gateway is a given gateway. |
||
| 180 | * |
||
| 181 | * @since 1.0.19 |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | public function is( $gateway ) { |
||
| 185 | return $gateway == $this->id; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns a users saved tokens for this gateway. |
||
| 190 | * |
||
| 191 | * @since 1.0.19 |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | public function get_tokens() { |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Return the title for admin screens. |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function get_method_title() { |
||
| 218 | return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this ); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Return the description for admin screens. |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function get_method_description() { |
||
| 227 | return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this ); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get the success url. |
||
| 232 | * |
||
| 233 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function get_return_url( $invoice ) { |
||
| 237 | |||
| 238 | // Payment success url |
||
| 239 | $return_url = add_query_arg( |
||
| 240 | array( |
||
| 241 | 'payment-confirm' => $this->id, |
||
| 242 | 'invoice_key' => $invoice->get_key(), |
||
| 243 | 'utm_nooverride' => 1 |
||
| 244 | ), |
||
| 245 | wpinv_get_success_page_uri() |
||
| 246 | ); |
||
| 247 | |||
| 248 | return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this ); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Confirms payments when rendering the success page. |
||
| 253 | * |
||
| 254 | * @param string $content Success page content. |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function get_confirm_payment( $content ) { |
||
| 258 | |||
| 259 | // Retrieve the invoice. |
||
| 260 | $invoice_id = getpaid_get_current_invoice_id(); |
||
| 261 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 262 | |||
| 263 | // Ensure that it exists and that it is pending payment. |
||
| 264 | if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) { |
||
| 265 | return $content; |
||
| 266 | } |
||
| 267 | |||
| 268 | // Can the user view this invoice?? |
||
| 269 | if ( ! wpinv_user_can_view_invoice( $invoice ) ) { |
||
| 270 | return $content; |
||
| 271 | } |
||
| 272 | |||
| 273 | // Show payment processing indicator. |
||
| 274 | return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) ); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Processes ipns and marks payments as complete. |
||
| 279 | * |
||
| 280 | * @return void |
||
| 281 | */ |
||
| 282 | public function verify_ipn() {} |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get a link to the transaction on the 3rd party gateway site (if applicable). |
||
| 286 | * |
||
| 287 | * @param string $transaction_url transaction url. |
||
| 288 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 289 | * @return string transaction URL, or empty string. |
||
| 290 | */ |
||
| 291 | public function filter_transaction_url( $transaction_url, $invoice ) { |
||
| 292 | |||
| 293 | $transaction_id = $invoice->get_transaction_id(); |
||
| 294 | |||
| 295 | if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
||
| 296 | $transaction_url = sprintf( $this->view_transaction_url, $transaction_id ); |
||
| 297 | $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
||
| 298 | $transaction_url = str_replace( '{sandbox}', $replace, $transaction_url ); |
||
| 299 | } |
||
| 300 | |||
| 301 | return $transaction_url; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get a link to the subscription on the 3rd party gateway site (if applicable). |
||
| 306 | * |
||
| 307 | * @param string $subscription_url transaction url. |
||
| 308 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 309 | * @return string subscription URL, or empty string. |
||
| 310 | */ |
||
| 311 | public function filter_subscription_url( $subscription_url, $invoice ) { |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Check if the gateway is available for use. |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | public function is_available() { |
||
| 332 | return ! empty( $this->enabled ); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Return the gateway's title. |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function get_title() { |
||
| 341 | return apply_filters( 'getpaid_gateway_title', $this->title, $this ); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Return the gateway's description. |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function get_description() { |
||
| 350 | return apply_filters( 'getpaid_gateway_description', $this->description, $this ); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Process Payment. |
||
| 355 | * |
||
| 356 | * |
||
| 357 | * @param WPInv_Invoice $invoice Invoice. |
||
| 358 | * @param array $submission_data Posted checkout fields. |
||
| 359 | * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | public function process_payment( $invoice, $submission_data, $submission ) { |
||
| 363 | // Process the payment then either redirect to the success page or the gateway. |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Process refund. |
||
| 368 | * |
||
| 369 | * If the gateway declares 'refunds' support, this will allow it to refund. |
||
| 370 | * a passed in amount. |
||
| 371 | * |
||
| 372 | * @param WPInv_Invoice $invoice Invoice. |
||
| 373 | * @param float $amount Refund amount. |
||
| 374 | * @param string $reason Refund reason. |
||
| 375 | * @return WP_Error|bool True or false based on success, or a WP_Error object. |
||
| 376 | */ |
||
| 377 | public function process_refund( $invoice, $amount = null, $reason = '' ) { |
||
| 378 | return false; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Displays the payment fields, credit cards etc. |
||
| 383 | * |
||
| 384 | * @param int $invoice_id 0 or invoice id. |
||
| 385 | * @param GetPaid_Payment_Form $form Current payment form. |
||
| 386 | */ |
||
| 387 | public function payment_fields( $invoice_id, $form ) {} |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Check if a gateway supports a given feature. |
||
| 391 | * |
||
| 392 | * Gateways should override this to declare support (or lack of support) for a feature. |
||
| 393 | * For backward compatibility, gateways support 'products' by default, but nothing else. |
||
| 394 | * |
||
| 395 | * @param string $feature string The name of a feature to test support for. |
||
| 396 | * @return bool True if the gateway supports the feature, false otherwise. |
||
| 397 | * @since 1.0.19 |
||
| 398 | */ |
||
| 399 | public function supports( $feature ) { |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Grab and display our saved payment methods. |
||
| 405 | * |
||
| 406 | * @since 1.0.19 |
||
| 407 | */ |
||
| 408 | public function saved_payment_methods() { |
||
| 409 | $html = '<ul class="getpaid-saved-payment-methods" data-count="' . esc_attr( count( $this->get_tokens() ) ) . '">'; |
||
| 410 | |||
| 411 | foreach ( $this->get_tokens() as $token ) { |
||
| 412 | $html .= $this->get_saved_payment_method_option_html( $token ); |
||
| 413 | } |
||
| 414 | |||
| 415 | $html .= $this->get_new_payment_method_option_html(); |
||
| 416 | $html .= '</ul>'; |
||
| 417 | |||
| 418 | echo apply_filters( 'getpaid_payment_gateway_form_saved_payment_methods_html', $html, $this ); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Gets saved payment method HTML from a token. |
||
| 423 | * |
||
| 424 | * @since 1.0.19 |
||
| 425 | * @param array $token Payment Token. |
||
| 426 | * @return string Generated payment method HTML |
||
| 427 | */ |
||
| 428 | public function get_saved_payment_method_option_html( $token ) { |
||
| 441 | ); |
||
| 442 | |||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. |
||
| 447 | * |
||
| 448 | * @since 1.0.19 |
||
| 449 | */ |
||
| 450 | public function get_new_payment_method_option_html() { |
||
| 463 | ); |
||
| 464 | |||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Outputs a checkbox for saving a new payment method to the database. |
||
| 469 | * |
||
| 470 | * @since 1.0.19 |
||
| 471 | */ |
||
| 472 | public function save_payment_method_checkbox() { |
||
| 483 | ); |
||
| 484 | |||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Registers the gateway. |
||
| 489 | * |
||
| 490 | * @return array |
||
| 491 | */ |
||
| 492 | public function register_gateway( $gateways ) { |
||
| 493 | |||
| 494 | $gateways[ $this->id ] = array( |
||
| 495 | |||
| 496 | 'admin_label' => $this->method_title, |
||
| 497 | 'checkout_label' => $this->title, |
||
| 498 | 'ordering' => $this->order, |
||
| 499 | |||
| 500 | ); |
||
| 501 | |||
| 502 | return $gateways; |
||
| 503 | |||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Checks whether or not this is a sandbox request. |
||
| 508 | * |
||
| 509 | * @param WPInv_Invoice|null $invoice Invoice object or null. |
||
| 510 | * @return bool |
||
| 511 | */ |
||
| 512 | public function is_sandbox( $invoice = null ) { |
||
| 513 | |||
| 514 | if ( ! empty( $invoice ) && ! $invoice->needs_payment() ) { |
||
| 515 | return $invoice->get_mode() == 'test'; |
||
| 516 | } |
||
| 517 | |||
| 518 | return wpinv_is_test_mode( $this->id ); |
||
| 519 | |||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Renames the checkout button |
||
| 524 | * |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | public function rename_checkout_button() { |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Validate gateway currency |
||
| 533 | * |
||
| 534 | * @return bool |
||
| 535 | */ |
||
| 536 | public function validate_currency( $validation, $currency ) { |
||
| 537 | |||
| 538 | // Required currencies. |
||
| 539 | if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) { |
||
| 552 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.