| Total Complexity | 60 |
| Total Lines | 469 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Complex classes like GetPaid_Paypal_Gateway_IPN_Handler 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_Paypal_Gateway_IPN_Handler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class GetPaid_Paypal_Gateway_IPN_Handler { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Payment method id. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $id = 'paypal'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Payment method object. |
||
| 24 | * |
||
| 25 | * @var GetPaid_Paypal_Gateway |
||
| 26 | */ |
||
| 27 | protected $gateway; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Class constructor. |
||
| 31 | * |
||
| 32 | * @param GetPaid_Paypal_Gateway $gateway |
||
| 33 | */ |
||
| 34 | public function __construct( $gateway ) { |
||
| 35 | $this->gateway = $gateway; |
||
| 36 | $this->verify_ipn(); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Processes ipns and marks payments as complete. |
||
| 41 | * |
||
| 42 | * @return void |
||
| 43 | */ |
||
| 44 | public function verify_ipn() { |
||
| 77 | |||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Retrieves IPN Invoice. |
||
| 82 | * |
||
| 83 | * @param array $posted |
||
| 84 | * @return WPInv_Invoice |
||
| 85 | */ |
||
| 86 | protected function get_ipn_invoice( $posted ) { |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Check PayPal IPN validity. |
||
| 105 | */ |
||
| 106 | protected function validate_ipn() { |
||
| 107 | |||
| 108 | wpinv_error_log( 'Validating PayPal IPN response', false ); |
||
| 109 | |||
| 110 | // Retrieve the associated invoice. |
||
| 111 | $posted = wp_unslash( $_POST ); |
||
| 112 | $invoice = $this->get_ipn_invoice( $posted ); |
||
| 113 | |||
| 114 | if ( $this->gateway->is_sandbox( $invoice ) ) { |
||
| 115 | wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data', false ); |
||
| 116 | } |
||
| 117 | |||
| 118 | // Validate the IPN. |
||
| 119 | $posted['cmd'] = '_notify-validate'; |
||
| 120 | |||
| 121 | // Send back post vars to paypal. |
||
| 122 | $params = array( |
||
| 123 | 'body' => $posted, |
||
| 124 | 'timeout' => 60, |
||
| 125 | 'httpversion' => '1.1', |
||
| 126 | 'compress' => false, |
||
| 127 | 'decompress' => false, |
||
| 128 | 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
||
| 129 | ); |
||
| 130 | |||
| 131 | // Post back to get a response. |
||
| 132 | $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
||
| 133 | |||
| 134 | // Check to see if the request was valid. |
||
| 135 | if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
||
| 136 | wpinv_error_log( 'Received valid response from PayPal IPN: ' . $response['body'], false ); |
||
| 137 | return true; |
||
| 138 | } |
||
| 139 | |||
| 140 | if ( is_wp_error( $response ) ) { |
||
| 141 | wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
||
| 142 | return false; |
||
| 143 | } |
||
| 144 | |||
| 145 | wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
||
| 146 | return false; |
||
| 147 | |||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Check currency from IPN matches the invoice. |
||
| 152 | * |
||
| 153 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 154 | * @param string $currency currency to validate. |
||
| 155 | */ |
||
| 156 | protected function validate_ipn_currency( $invoice, $currency ) { |
||
| 157 | |||
| 158 | if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
||
| 159 | |||
| 160 | /* translators: %s: currency code. */ |
||
| 161 | $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
||
| 162 | |||
| 163 | wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
||
| 164 | } |
||
| 165 | |||
| 166 | wpinv_error_log( $currency, 'Validated IPN Currency', false ); |
||
|
|
|||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Check payment amount from IPN matches the invoice. |
||
| 171 | * |
||
| 172 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 173 | * @param float $amount amount to validate. |
||
| 174 | */ |
||
| 175 | protected function validate_ipn_amount( $invoice, $amount ) { |
||
| 176 | if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
||
| 177 | |||
| 178 | /* translators: %s: Amount. */ |
||
| 179 | $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
||
| 180 | |||
| 181 | wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
||
| 182 | } |
||
| 183 | |||
| 184 | wpinv_error_log( $amount, 'Validated IPN Amount', false ); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Verify receiver email from PayPal. |
||
| 189 | * |
||
| 190 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 191 | * @param string $receiver_email Email to validate. |
||
| 192 | */ |
||
| 193 | protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
||
| 194 | $paypal_email = wpinv_get_option( 'paypal_email' ); |
||
| 195 | |||
| 196 | if ( $receiver_email && strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
||
| 197 | wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
||
| 198 | |||
| 199 | /* translators: %s: email address . */ |
||
| 200 | $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
||
| 201 | |||
| 202 | return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
||
| 203 | } |
||
| 204 | |||
| 205 | wpinv_error_log( 'Validated PayPal Email', false ); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Handles one time payments. |
||
| 210 | * |
||
| 211 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 212 | * @param array $posted Posted data. |
||
| 213 | */ |
||
| 214 | protected function ipn_txn_web_accept( $invoice, $posted ) { |
||
| 215 | |||
| 216 | // Collect payment details |
||
| 217 | $payment_status = strtolower( $posted['payment_status'] ); |
||
| 218 | $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
||
| 219 | |||
| 220 | $this->validate_ipn_receiver_email( $invoice, $business_email ); |
||
| 221 | $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
||
| 222 | |||
| 223 | // Update the transaction id. |
||
| 224 | if ( ! empty( $posted['txn_id'] ) ) { |
||
| 225 | $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
||
| 226 | $invoice->save(); |
||
| 227 | } |
||
| 228 | |||
| 229 | $invoice->add_system_note( __( 'Processing invoice IPN', 'invoicing' ) ); |
||
| 230 | |||
| 231 | // Process a refund. |
||
| 232 | if ( 'refunded' === $payment_status || 'reversed' === $payment_status ) { |
||
| 233 | |||
| 234 | update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
||
| 235 | |||
| 236 | if ( ! $invoice->is_refunded() ) { |
||
| 237 | $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
||
| 238 | } |
||
| 239 | |||
| 240 | return wpinv_error_log( $posted['reason_code'], false ); |
||
| 241 | } |
||
| 242 | |||
| 243 | // Process payments. |
||
| 244 | if ( 'completed' === $payment_status ) { |
||
| 245 | |||
| 246 | if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
||
| 247 | return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
||
| 248 | } |
||
| 249 | |||
| 250 | $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
||
| 251 | |||
| 252 | $note = ''; |
||
| 253 | |||
| 254 | if ( ! empty( $posted['mc_fee'] ) ) { |
||
| 255 | $note = sprintf( __( 'PayPal Transaction Fee %s.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
||
| 256 | } |
||
| 257 | |||
| 258 | if ( ! empty( $posted['payer_status'] ) ) { |
||
| 259 | $note = ' ' . sprintf( __( 'Buyer status %s.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
||
| 260 | } |
||
| 261 | |||
| 262 | $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
||
| 263 | return wpinv_error_log( 'Invoice marked as paid.', false ); |
||
| 264 | |||
| 265 | } |
||
| 266 | |||
| 267 | // Pending payments. |
||
| 268 | if ( 'pending' === $payment_status ) { |
||
| 269 | |||
| 270 | /* translators: %s: pending reason. */ |
||
| 271 | $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
||
| 272 | |||
| 273 | return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
||
| 274 | } |
||
| 275 | |||
| 276 | /* translators: %s: payment status. */ |
||
| 277 | $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
||
| 278 | |||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Handles one time payments. |
||
| 283 | * |
||
| 284 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 285 | * @param array $posted Posted data. |
||
| 286 | */ |
||
| 287 | protected function ipn_txn_cart( $invoice, $posted ) { |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Handles subscription sign ups. |
||
| 293 | * |
||
| 294 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 295 | * @param array $posted Posted data. |
||
| 296 | */ |
||
| 297 | protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
||
| 298 | |||
| 299 | wpinv_error_log( 'Processing subscription signup', false ); |
||
| 300 | |||
| 301 | // Make sure the invoice has a subscription. |
||
| 302 | $subscription = getpaid_get_invoice_subscription( $invoice ); |
||
| 303 | |||
| 304 | if ( empty( $subscription ) ) { |
||
| 305 | return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
||
| 306 | } |
||
| 307 | |||
| 308 | wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
||
| 309 | |||
| 310 | // Validate the IPN. |
||
| 311 | $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
||
| 312 | $this->validate_ipn_receiver_email( $invoice, $business_email ); |
||
| 313 | $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
||
| 314 | |||
| 315 | // Activate the subscription. |
||
| 316 | $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
||
| 317 | $subscription->set_date_created( current_time( 'mysql' ) ); |
||
| 318 | $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
||
| 319 | $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
||
| 320 | $subscription->activate(); |
||
| 321 | |||
| 322 | // Set the transaction id. |
||
| 323 | if ( ! empty( $posted['txn_id'] ) ) { |
||
| 324 | $invoice->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), $posted['txn_id'] ), false, false, true ); |
||
| 325 | $invoice->set_transaction_id( $posted['txn_id'] ); |
||
| 326 | } |
||
| 327 | |||
| 328 | // Update the payment status. |
||
| 329 | $invoice->mark_paid(); |
||
| 330 | |||
| 331 | $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ), $posted['subscr_id'] ), false, false, true ); |
||
| 332 | |||
| 333 | wpinv_error_log( 'Subscription started.', false ); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Handles subscription renewals. |
||
| 338 | * |
||
| 339 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 340 | * @param array $posted Posted data. |
||
| 341 | */ |
||
| 342 | protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
||
| 343 | |||
| 344 | // Make sure the invoice has a subscription. |
||
| 345 | $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
||
| 346 | |||
| 347 | if ( empty( $subscription ) ) { |
||
| 348 | return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
||
| 349 | } |
||
| 350 | |||
| 351 | wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
||
| 352 | |||
| 353 | // PayPal sends a subscr_payment for the first payment too. |
||
| 354 | $date_completed = getpaid_format_date( $invoice->get_date_completed() ); |
||
| 355 | $date_created = getpaid_format_date( $invoice->get_date_created() ); |
||
| 356 | $today_date = getpaid_format_date( current_time( 'mysql' ) ); |
||
| 357 | $payment_date = getpaid_format_date( $posted['payment_date'] ); |
||
| 358 | $subscribe_date = getpaid_format_date( $subscription->get_date_created() ); |
||
| 359 | $dates = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) ); |
||
| 360 | |||
| 361 | foreach ( $dates as $date ) { |
||
| 362 | |||
| 363 | if ( $date !== $today_date && $date !== $payment_date ) { |
||
| 364 | continue; |
||
| 365 | } |
||
| 366 | |||
| 367 | if ( ! empty( $posted['txn_id'] ) ) { |
||
| 368 | $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
||
| 369 | $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), sanitize_text_field( $posted['txn_id'] ) ), false, false, true ); |
||
| 370 | } |
||
| 371 | |||
| 372 | return $invoice->mark_paid(); |
||
| 373 | |||
| 374 | } |
||
| 375 | |||
| 376 | wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
||
| 377 | |||
| 378 | // Abort if the payment is already recorded. |
||
| 379 | if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
||
| 380 | return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] . ' has already been processed', false ); |
||
| 381 | } |
||
| 382 | |||
| 383 | $args = array( |
||
| 384 | 'transaction_id' => $posted['txn_id'], |
||
| 385 | 'gateway' => $this->id, |
||
| 386 | ); |
||
| 387 | |||
| 388 | $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
||
| 389 | |||
| 390 | if ( empty( $invoice ) ) { |
||
| 391 | return; |
||
| 392 | } |
||
| 393 | |||
| 394 | $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), $posted['txn_id'] ), false, false, true ); |
||
| 395 | $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ), $posted['subscr_id'] ), false, false, true ); |
||
| 396 | |||
| 397 | $subscription->renew(); |
||
| 398 | wpinv_error_log( 'Subscription renewed.', false ); |
||
| 399 | |||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Handles subscription cancelations. |
||
| 404 | * |
||
| 405 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 406 | */ |
||
| 407 | protected function ipn_txn_subscr_cancel( $invoice ) { |
||
| 408 | |||
| 409 | // Make sure the invoice has a subscription. |
||
| 410 | $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
||
| 411 | |||
| 412 | if ( empty( $subscription ) ) { |
||
| 413 | return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
||
| 414 | } |
||
| 415 | |||
| 416 | wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
||
| 417 | $subscription->cancel(); |
||
| 418 | wpinv_error_log( 'Subscription cancelled.', false ); |
||
| 419 | |||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Handles subscription completions. |
||
| 424 | * |
||
| 425 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 426 | * @param array $posted Posted data. |
||
| 427 | */ |
||
| 428 | protected function ipn_txn_subscr_eot( $invoice ) { |
||
| 429 | |||
| 430 | // Make sure the invoice has a subscription. |
||
| 431 | $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
||
| 432 | |||
| 433 | if ( empty( $subscription ) ) { |
||
| 434 | return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
||
| 435 | } |
||
| 436 | |||
| 437 | wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
||
| 438 | $subscription->complete(); |
||
| 439 | wpinv_error_log( 'Subscription completed.', false ); |
||
| 440 | |||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Handles subscription fails. |
||
| 445 | * |
||
| 446 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 447 | * @param array $posted Posted data. |
||
| 448 | */ |
||
| 449 | protected function ipn_txn_subscr_failed( $invoice ) { |
||
| 461 | |||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Handles subscription suspensions. |
||
| 466 | * |
||
| 467 | * @param WPInv_Invoice $invoice Invoice object. |
||
| 468 | * @param array $posted Posted data. |
||
| 469 | */ |
||
| 470 | protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
||
| 482 | } |
||
| 483 | |||
| 484 | } |
||
| 485 |