| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | if ( ! defined( 'ABSPATH' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 	exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | if ( ! class_exists( 'WC_Stripe_Connect' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	 * Stripe Connect class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	class WC_Stripe_Connect { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 		const SETTINGS_OPTION = 'woocommerce_stripe_settings'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		 * Stripe connect api. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		 * @var object $api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		private $api; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		 * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		 * @param WC_Stripe_Connect_API $api stripe connect api. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		public function __construct( WC_Stripe_Connect_API $api ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			$this->api = $api; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 			add_action( 'admin_init', array( $this, 'maybe_handle_redirect' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		 * Gets the OAuth URL for Stripe onboarding flow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		 * @param  string $return_url url to return to after oauth flow. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		 * @return string|WP_Error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		public function get_oauth_url( $return_url = '' ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			if ( empty( $return_url ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 				$return_url = admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=stripe' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			if ( substr( $return_url, 0, 8 ) !== 'https://' ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 				return new WP_Error( 'invalid_url_protocol', __( 'Your site must be served over HTTPS in order to connect your Stripe account automatically.', 'woocommerce-gateway-stripe' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 			$result = $this->api->get_stripe_oauth_init( $return_url ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 			if ( is_wp_error( $result ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 				return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			return $result->oauthUrl; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		 * Initiate OAuth connection request to Connect Server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		 * @param  bool $state Stripe onboarding state. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		 * @param  int  $code  OAuth code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		 * @return string|WP_Error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		public function connect_oauth( $state, $code ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			$response = $this->api->get_stripe_oauth_keys( $code ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			if ( is_wp_error( $response ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 				return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 			return $this->save_stripe_keys( $response ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		 * Handle redirect back from oauth-init or credentials reset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		public function maybe_handle_redirect() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			if ( ! is_admin() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 				return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			// redirect from oauth-init | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			if ( isset( $_GET['wcs_stripe_code'], $_GET['wcs_stripe_state'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 				$response = $this->connect_oauth( $_GET['wcs_stripe_state'], $_GET['wcs_stripe_code'] ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 				wp_safe_redirect( remove_query_arg( array( 'wcs_stripe_state', 'wcs_stripe_code' ) ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 				exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			// redirect from credentials reset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			} elseif ( isset( $_GET['reset_stripe_api_credentials'], $_GET['_wpnonce'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 				if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'reset_stripe_api_credentials' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 					die( __( 'You are not authorized to clear Stripe account keys.', 'woocommerce-gateway-stripe' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 				$this->clear_stripe_keys(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 				wp_safe_redirect( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 					remove_query_arg( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 						array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 							'_wpnonce', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 							'reset_stripe_api_credentials', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 						) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 					) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 				); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 				exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		 * Saves stripe keys after OAuth response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		 * @param  array $result OAuth response result. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		 * @return array|WP_Error | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | 		 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  | 		private function save_stripe_keys( $result ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  | 			if ( ! isset( $result->publishableKey, $result->secretKey ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  | 				return new WP_Error( 'Invalid credentials received from WooCommerce Connect server' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  | 			$is_test                                = false !== strpos( $result->publishableKey, '_test_' ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  | 			$prefix                                 = $is_test ? 'test_' : ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  | 			$default_options                        = $this->get_default_stripe_config(); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  | 			$options                                = array_merge( $default_options, get_option( self::SETTINGS_OPTION, array() ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  | 			$options['enabled']                     = 'yes'; | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  | 			$options['testmode']                    = $is_test ? 'yes' : 'no'; | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  | 			$options[ $prefix . 'publishable_key' ] = $result->publishableKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  | 			$options[ $prefix . 'secret_key' ]      = $result->secretKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  | 			// While we are at it, let's also clear the account_id and | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  | 			// test_account_id if present. | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  | 			unset( $options['account_id'] ); | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  | 			unset( $options['test_account_id'] ); | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  | 			update_option( self::SETTINGS_OPTION, $options ); | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  | 			return $result; | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		 * Clears keys for test or production (whichever is presently enabled). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		private function clear_stripe_keys() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 			$options = get_option( self::SETTINGS_OPTION, array() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 			if ( 'yes' === $options['testmode'] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 				$options['test_publishable_key'] = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 				$options['test_secret_key']      = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 				// clear test_account_id if present | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 				unset( $options['test_account_id'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 				$options['publishable_key'] = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 				$options['secret_key']      = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 				// clear account_id if present | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 				unset( $options['account_id'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 			update_option( self::SETTINGS_OPTION, $options ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 		 * Gets default Stripe settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 		private function get_default_stripe_config() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 			$result = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 			$gateway = new WC_Gateway_Stripe(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 			foreach ( $gateway->form_fields as $key => $value ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 				if ( isset( $value['default'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 					$result[ $key ] = $value['default']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 			return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 		public function is_connected() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 			$options = get_option( self::SETTINGS_OPTION, array() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 			if ( isset( $options['testmode'] ) && 'yes' === $options['testmode'] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 				return isset( $options['test_publishable_key'], $options['test_secret_key'] ) && trim( $options['test_publishable_key'] ) && trim( $options['test_secret_key'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 				return isset( $options['publishable_key'], $options['secret_key'] ) && trim( $options['publishable_key'] ) && trim( $options['secret_key'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 196 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 197 |  |  |  | 
            
                        
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.