| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | if ( ! defined( 'ABSPATH' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 	exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | include_once( 'class-wc-gateway-paypal-response.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Handle PDT Responses from PayPal. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class WC_Gateway_Paypal_PDT_Handler extends WC_Gateway_Paypal_Response { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	/** @var string identity_token for PDT support */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	protected $identity_token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * @param bool $sandbox | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @param string $identity_token | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | 	public function __construct( $sandbox = false, $identity_token = '' ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 		add_action( 'woocommerce_thankyou_paypal', array( $this, 'check_response' ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 		$this->identity_token = $identity_token; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 		$this->sandbox        = $sandbox; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 * Validate a PDT transaction to ensure its authentic. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * @param  string $transaction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * @return bool|array False or result array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	protected function validate_transaction( $transaction ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		$pdt = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 			'body' 			=> array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 				'cmd' => '_notify-synch', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 				'tx'  => $transaction, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 				'at'  => $this->identity_token, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			'timeout' 		=> 60, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 			'httpversion'   => '1.1', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			'user-agent'	=> 'WooCommerce/' . WC_VERSION | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		// Post back to get a response. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$response = wp_safe_remote_post( $this->sandbox ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $pdt ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		if ( is_wp_error( $response ) || ! strpos( $response['body'], "SUCCESS" ) === 0 ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		// Parse transaction result data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		$transaction_result  = array_map( 'wc_clean', array_map( 'urldecode', explode( "\n", $response['body'] ) ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$transaction_results = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		foreach ( $transaction_result as $line ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			$line                            = explode( "=", $line ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			$transaction_results[ $line[0] ] = isset( $line[1] ) ? $line[1] : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		if ( ! empty( $transaction_results['charset'] ) && function_exists( 'iconv' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			foreach ( $transaction_results as $key => $value ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 				$transaction_results[ $key ] = iconv( $transaction_results['charset'], 'utf-8', $value ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		return $transaction_results; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * Check Response for PDT. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	public function check_response() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		if ( empty( $_REQUEST['cm'] ) || empty( $_REQUEST['tx'] ) || empty( $_REQUEST['st'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$order_id    = wc_clean( stripslashes( $_REQUEST['cm'] ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		$status      = wc_clean( strtolower( stripslashes( $_REQUEST['st'] ) ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		$amount      = wc_clean( stripslashes( $_REQUEST['amt'] ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$transaction = wc_clean( stripslashes( $_REQUEST['tx'] ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		if ( ! ( $order = $this->get_paypal_order( $order_id ) ) || ! $order->has_status( 'pending' ) ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		$transaction_result = $this->validate_transaction( $transaction ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		if ( $transaction_result && 'completed' === $status ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			if ( $order->get_total() != $amount ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 				WC_Gateway_Paypal::log( 'Payment error: Amounts do not match (amt ' . $amount . ')' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 				$this->payment_on_hold( $order, sprintf( __( 'Validation error: PayPal amounts do not match (amt %s).', 'woocommerce' ), $amount ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 				$this->payment_complete( $order, $transaction,  __( 'PDT payment completed', 'woocommerce' ) ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 				// Log paypal transaction fee and other meta data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 				if ( ! empty( $transaction_result['mc_fee'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 					update_post_meta( $order->id, 'PayPal Transaction Fee', $transaction_result['mc_fee'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 				if ( ! empty( $transaction_result['payer_email'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 					update_post_meta( $order->id, 'Payer PayPal address', $transaction_result['payer_email'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 				if ( ! empty( $transaction_result['first_name'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 					update_post_meta( $order->id, 'Payer first name', $transaction_result['first_name'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 				if ( ! empty( $transaction_result['last_name'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 					update_post_meta( $order->id, 'Payer last name', $transaction_result['last_name'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 				if ( ! empty( $transaction_result['payment_type'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 					update_post_meta( $order->id, 'Payment type', $transaction_result['payment_type'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.