| Total Complexity | 44 |
| Total Lines | 308 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like GetPaid_Checkout 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_Checkout, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class GetPaid_Checkout { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var GetPaid_Payment_Form_Submission |
||
| 17 | */ |
||
| 18 | protected $payment_form_submission; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Class constructor. |
||
| 22 | * |
||
| 23 | * @param GetPaid_Payment_Form_Submission $submission |
||
| 24 | */ |
||
| 25 | public function __construct( $submission ) { |
||
| 26 | $this->payment_form_submission = $submission; |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Processes the checkout. |
||
| 31 | * |
||
| 32 | */ |
||
| 33 | public function process_checkout() { |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Validates the submission. |
||
| 54 | * |
||
| 55 | */ |
||
| 56 | protected function validate_submission() { |
||
| 57 | |||
| 58 | $submission = $this->payment_form_submission; |
||
| 59 | $data = $submission->get_data(); |
||
| 60 | |||
| 61 | // Do we have an error? |
||
| 62 | if ( ! empty( $submission->last_error ) ) { |
||
| 63 | wp_send_json_error( $submission->last_error ); |
||
| 64 | } |
||
| 65 | |||
| 66 | // We need a billing email. |
||
| 67 | if ( ! $submission->has_billing_email() || ! is_email( $submission->get_billing_email() ) ) { |
||
| 68 | wp_send_json_error( __( 'Provide a valid billing email.', 'invoicing' ) ); |
||
| 69 | } |
||
| 70 | |||
| 71 | // Non-recurring gateways should not be allowed to process recurring invoices. |
||
| 72 | if ( $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
||
| 73 | wp_send_json_error( __( 'The selected payment gateway does not support subscription payment.', 'invoicing' ) ); |
||
| 74 | } |
||
| 75 | |||
| 76 | // Ensure the gateway is active. |
||
| 77 | if ( ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
||
| 78 | wpinv_set_error( 'invalid_gateway', __( 'The selected payment gateway is not active', 'invoicing' ) ); |
||
| 79 | } |
||
| 80 | |||
| 81 | // Clear any existing errors. |
||
| 82 | wpinv_clear_errors(); |
||
| 83 | |||
| 84 | // Allow themes and plugins to hook to errors |
||
| 85 | do_action( 'getpaid_checkout_error_checks', $submission ); |
||
| 86 | |||
| 87 | // Do we have any errors? |
||
| 88 | if ( wpinv_get_errors() ) { |
||
| 89 | wp_send_json_error( getpaid_get_errors_html() ); |
||
| 90 | } |
||
| 91 | |||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Retrieves submission items. |
||
| 96 | * |
||
| 97 | * @return GetPaid_Form_Item[] |
||
| 98 | */ |
||
| 99 | protected function get_submission_items() { |
||
| 100 | |||
| 101 | $items = $this->payment_form_submission->get_items(); |
||
| 102 | |||
| 103 | // Ensure that we have items. |
||
| 104 | if ( empty( $items ) && 0 == count( $this->payment_form_submission->get_fees() ) ) { |
||
| 105 | wp_send_json_error( __( 'Please select at least one item.', 'invoicing' ) ); |
||
| 106 | } |
||
| 107 | |||
| 108 | return $items; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Retrieves submission invoice. |
||
| 113 | * |
||
| 114 | * @return WPInv_Invoice |
||
| 115 | */ |
||
| 116 | protected function get_submission_invoice() { |
||
| 117 | $submission = $this->payment_form_submission; |
||
| 118 | |||
| 119 | if ( ! $submission->has_invoice() ) { |
||
| 120 | return new WPInv_Invoice(); |
||
| 121 | } |
||
| 122 | |||
| 123 | $invoice = $submission->get_invoice(); |
||
| 124 | |||
| 125 | // Make sure that it is neither paid or refunded. |
||
| 126 | if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 127 | wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
||
| 128 | } |
||
| 129 | |||
| 130 | return $invoice; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Processes the submission invoice. |
||
| 135 | * |
||
| 136 | * @param WPInv_Invoice $invoice |
||
| 137 | * @param GetPaid_Form_Item[] $items |
||
| 138 | * @return WPInv_Invoice |
||
| 139 | */ |
||
| 140 | protected function process_submission_invoice( $invoice, $items ) { |
||
| 141 | |||
| 142 | $submission = $this->payment_form_submission; |
||
| 143 | $data = $submission->get_data(); |
||
| 144 | |||
| 145 | // Set-up the invoice details. |
||
| 146 | $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
||
| 147 | $invoice->set_user_id( $this->get_submission_customer() ); |
||
| 148 | $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
||
| 149 | $invoice->set_items( $items ); |
||
| 150 | $invoice->set_fees( $submission->get_fees() ); |
||
| 151 | $invoice->set_taxes( $submission->get_taxes() ); |
||
| 152 | $invoice->set_discounts( $submission->get_discounts() ); |
||
| 153 | $invoice->set_gateway( $data['wpi-gateway'] ); |
||
| 154 | |||
| 155 | if ( $submission->has_discount_code() ) { |
||
| 156 | $invoice->set_discount_code( $submission->get_discount_code() ); |
||
| 157 | } |
||
| 158 | |||
| 159 | getpaid_maybe_add_default_address( $invoice ); |
||
| 160 | return $invoice; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Retrieves the submission's customer. |
||
| 165 | * |
||
| 166 | * @return int The customer id. |
||
| 167 | */ |
||
| 168 | protected function get_submission_customer() { |
||
| 169 | $submission = $this->payment_form_submission; |
||
| 170 | |||
| 171 | // If this is an existing invoice... |
||
| 172 | if ( $submission->has_invoice() ) { |
||
| 173 | return $submission->get_invoice()->get_user_id(); |
||
| 174 | } |
||
| 175 | |||
| 176 | // (Maybe) create the user. |
||
| 177 | $user = get_current_user_id(); |
||
| 178 | |||
| 179 | if ( empty( $user ) ) { |
||
| 180 | $user = get_user_by( 'email', $submission->get_billing_email() ); |
||
| 181 | } |
||
| 182 | |||
| 183 | if ( empty( $user ) ) { |
||
| 184 | $user = wpinv_create_user( $submission->get_billing_email() ); |
||
| 185 | } |
||
| 186 | |||
| 187 | if ( is_wp_error( $user ) ) { |
||
| 188 | wp_send_json_error( $user->get_error_message() ); |
||
| 189 | } |
||
| 190 | |||
| 191 | if ( is_numeric( $user ) ) { |
||
| 192 | return $user; |
||
| 193 | } |
||
| 194 | |||
| 195 | return $user->ID; |
||
|
|
|||
| 196 | |||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Prepares submission data for saving to the database. |
||
| 201 | * |
||
| 202 | * @param WPInv_Invoice $invoice |
||
| 203 | */ |
||
| 204 | public function prepare_submission_data_for_saving( &$invoice ) { |
||
| 263 | |||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Confirms the submission is valid and send users to the gateway. |
||
| 268 | * |
||
| 269 | * @param WPInv_Invoice $invoice |
||
| 270 | * @param array $prepared_payment_form_data |
||
| 271 | */ |
||
| 272 | protected function post_process_submission( $invoice, $prepared_payment_form_data ) { |
||
| 273 | |||
| 274 | // Ensure the invoice exists. |
||
| 275 | if ( $invoice->get_id() == 0 ) { |
||
| 276 | wp_send_json_error( __( 'An error occured while saving your invoice.', 'invoicing' ) ); |
||
| 277 | } |
||
| 278 | |||
| 279 | // Was this invoice created via the payment form? |
||
| 280 | if ( ! $this->payment_form_submission->has_invoice() ) { |
||
| 281 | update_post_meta( $invoice->get_id(), 'wpinv_created_via', 'payment_form' ); |
||
| 282 | } |
||
| 283 | |||
| 284 | // Save payment form data. |
||
| 285 | if ( ! empty( $prepared_payment_form_data ) ) { |
||
| 286 | update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data ); |
||
| 287 | } |
||
| 288 | |||
| 289 | // Backwards compatibility. |
||
| 290 | add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); |
||
| 291 | add_action( 'wpinv_pre_send_back_to_checkout', array( $this, 'checkout_error' ) ); |
||
| 292 | |||
| 293 | wpinv_process_checkout( $invoice, $this->payment_form_submission ); |
||
| 294 | |||
| 295 | // If we are here, there was an error. |
||
| 296 | $this->checkout_error(); |
||
| 297 | |||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Sends a redrect response to payment details. |
||
| 302 | * |
||
| 303 | */ |
||
| 304 | public function send_redirect_response( $url ) { |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Fired when a checkout error occurs |
||
| 311 | * |
||
| 312 | */ |
||
| 313 | public function checkout_error() { |
||
| 321 | |||
| 322 | } |
||
| 323 | |||
| 325 |