Completed
Pull Request — master (#1267)
by
unknown
02:08 queued 17s
created

WC_Stripe_Connect   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 184
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A get_oauth_url() 0 20 4
A deauthorize_account() 0 12 2
A connect_oauth() 0 16 3
A maybe_connect_oauth() 0 9 2
A save_stripe_keys() 0 25 4
A clear_stripe_keys() 0 21 2
A wc_ajax_oauth_init() 0 14 4
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
29
			$this->api = $api;
30
31
			add_action( 'wc_ajax_wc_stripe_oauth_init', array( $this, 'wc_ajax_oauth_init' ) );
32
		}
33
34
		/**
35
		 * Gets the OAuth URL for Stripe onboarding flow
36
		 *
37
		 * @param  string $return_url url to return to after oauth flow.
38
		 *
39
		 * @return string|WP_Error
40
		 */
41
		public function get_oauth_url( $return_url = '' ) {
42
43
			if ( empty( $return_url ) ) {
44
				$return_url = admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe' );
45
			}
46
47
			if ( substr( $return_url, 0, 8 ) !== 'https://' ) {
48
				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' ) );
49
			}
50
51
			$result = $this->api->get_stripe_oauth_init( $return_url );
52
53
			if ( is_wp_error( $result ) ) {
54
				return $result;
55
			}
56
57
			update_option( 'stripe_state', $result->state );
58
59
			return $result->oauthUrl; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
60
		}
61
62
		/**
63
		 * Deauthorize existing Stripe account
64
		 *
65
		 * @return array|WP_Error
66
		 */
67
		public function deauthorize_account() {
68
69
			$response = $this->api->deauthorize_stripe_account();
70
71
			if ( is_wp_error( $response ) ) {
72
				return $response;
73
			}
74
75
			$this->clear_stripe_keys();
76
77
			return $response;
78
		}
79
80
		/**
81
		 * Initiate OAuth connection request to Connect Server
82
		 *
83
		 * @param  bool $state Stripe onboarding state.
84
		 * @param  int  $code  OAuth code.
85
		 *
86
		 * @return string|WP_Error
87
		 */
88
		public function connect_oauth( $state, $code ) {
89
90
			if ( get_option( 'stripe_state', false ) !== $state ) {
91
				return new WP_Error( 'Invalid stripe state' );
92
			}
93
94
			$response = $this->api->get_stripe_oauth_keys( $code );
95
96
			if ( is_wp_error( $response ) ) {
97
				return $response;
98
			}
99
100
			delete_option( 'stripe_state' );
101
102
			return $this->save_stripe_keys( $response );
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->save_stripe_keys($response); of type WP_Error|array adds the type array to the return on line 102 which is incompatible with the return type documented by WC_Stripe_Connect::connect_oauth of type string|WP_Error.
Loading history...
103
		}
104
105
		/**
106
		 * Handle redirect back from oauth-init
107
		 */
108
		public function maybe_connect_oauth() {
109
110
			if ( isset( $_GET['wcs_stripe_code'], $_GET['wcs_stripe_state'] ) ) {
111
				$response = $this->connect_oauth( $_GET['wcs_stripe_state'], $_GET['wcs_stripe_code'] );
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
112
113
				wp_safe_redirect( remove_query_arg( array( 'wcs_stripe_state', 'wcs_stripe_code' ) ) );
114
				exit;
115
			}
116
		}
117
118
119
		/**
120
		 * Saves stripe keys after OAuth response
121
		 *
122
		 * @param  array $result OAuth response result.
123
		 *
124
		 * @return array|WP_Error
125
		 */
126
		private function save_stripe_keys( $result ) {
127
128
			if ( ! isset( $result->publishableKey, $result->secretKey ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
129
				return new WP_Error( 'Invalid credentials received from WooCommerce Connect server' );
130
			}
131
132
			$is_test         = false !== strpos( $result->publishableKey, '_test_' ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
133
			$prefix          = $is_test ? 'test_' : '';
134
			$default_options = array();
135
136
			$options                                = array_merge( $default_options, get_option( self::SETTINGS_OPTION, array() ) );
137
			$options['enabled']                     = 'yes';
138
			$options['testmode']                    = $is_test ? 'yes' : 'no';
139
			$options[ $prefix . 'publishable_key' ] = $result->publishableKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
140
			$options[ $prefix . 'secret_key' ]      = $result->secretKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
141
142
			// While we are at it, let's also clear the account_id and
143
			// test_account_id if present.
144
			unset( $options['account_id'] );
145
			unset( $options['test_account_id'] );
146
147
			update_option( self::SETTINGS_OPTION, $options );
148
149
			return $result;
150
		}
151
152
		/**
153
		 * Clears keys for test or production (whichever is presently enabled).
154
		 */
155
		private function clear_stripe_keys() {
156
157
			$default_options = $this->get_default_config();
0 ignored issues
show
Bug introduced by
The method get_default_config() does not seem to exist on object<WC_Stripe_Connect>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
158
			$options         = array_merge( $default_options, get_option( self::SETTINGS_OPTION, array() ) );
159
160
			if ( 'yes' === $options['testmode'] ) {
161
				$options['test_publishable_key'] = '';
162
				$options['test_secret_key']      = '';
163
			} else {
164
				$options['publishable_key'] = '';
165
				$options['secret_key']      = '';
166
			}
167
168
			// While we are at it, let's also clear the account_id and
169
			// test_account_id if present.
170
			unset( $options['account_id'] );
171
			unset( $options['test_account_id'] );
172
173
			update_option( self::SETTINGS_OPTION, $options );
174
175
		}
176
177
		/**
178
		 * Gets Stripe Connect Oauth url and redirects to Stripe.
179
		 */
180
		public function wc_ajax_oauth_init() {
181
182
			if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_oauth_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.ValidatedSanitizedInput.MissingUnslash
183
				wp_die( __( 'You are not authorized to automatically copy Stripe keys.', 'woocommerce-gateway-stripe' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
184
			}
185
186
			$oauth_url = $this->get_oauth_url();
187
188
			if ( is_wp_error( $oauth_url ) ) {
189
				wp_send_json_error( $oauth_url->get_error_message() );
190
			}
191
192
			wp_send_json_success( $oauth_url );
193
		}
194
	}
195
}
196