Completed
Pull Request — master (#1267)
by
unknown
01:43
created

WC_Stripe_Connect::is_connected()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 6
nop 0
dl 0
loc 10
rs 8.8333
c 0
b 0
f 0
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&section=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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $state is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 );
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 75 which is incompatible with the return type documented by WC_Stripe_Connect::connect_oauth of type string|WP_Error.
Loading history...
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'] );
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...
90
				wp_safe_redirect( remove_query_arg( array( 'wcs_stripe_state', 'wcs_stripe_code' ) ) );
91
				exit;
92
			// redirect from credentials reset
93
			} elseif ( isset( $_GET['reset_stripe_api_credentials'] ) ) {
94
95
				$this->clear_stripe_keys();
96
				wp_safe_redirect( remove_query_arg( array( 'reset_stripe_api_credentials' ) ) );
97
				exit;
98
			}
99
100
		}
101
102
		/**
103
		 * Saves stripe keys after OAuth response
104
		 *
105
		 * @param  array $result OAuth response result.
106
		 *
107
		 * @return array|WP_Error
108
		 */
109
		private function save_stripe_keys( $result ) {
110
111
			if ( ! isset( $result->publishableKey, $result->secretKey ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
112
				return new WP_Error( 'Invalid credentials received from WooCommerce Connect server' );
113
			}
114
115
			$is_test                                = false !== strpos( $result->publishableKey, '_test_' ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
116
			$prefix                                 = $is_test ? 'test_' : '';
117
			$default_options                        = $this->get_default_stripe_config();
118
			$options                                = array_merge( $default_options, get_option( self::SETTINGS_OPTION, array() ) );
119
			$options['enabled']                     = 'yes';
120
			$options['testmode']                    = $is_test ? 'yes' : 'no';
121
			$options[ $prefix . 'publishable_key' ] = $result->publishableKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
122
			$options[ $prefix . 'secret_key' ]      = $result->secretKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
123
124
			// While we are at it, let's also clear the account_id and
125
			// test_account_id if present.
126
			unset( $options['account_id'] );
127
			unset( $options['test_account_id'] );
128
129
			update_option( self::SETTINGS_OPTION, $options );
130
131
			return $result;
132
		}
133
134
		/**
135
		 * Clears keys for test or production (whichever is presently enabled).
136
		 */
137
		private function clear_stripe_keys() {
138
139
			$options = get_option( self::SETTINGS_OPTION, array() );
140
141
			if ( 'yes' === $options['testmode'] ) {
142
				$options['test_publishable_key'] = '';
143
				$options['test_secret_key']      = '';
144
			} else {
145
				$options['publishable_key'] = '';
146
				$options['secret_key']      = '';
147
			}
148
149
			// While we are at it, let's also clear the account_id and
150
			// test_account_id if present.
151
			unset( $options['account_id'] );
152
			unset( $options['test_account_id'] );
153
154
			update_option( self::SETTINGS_OPTION, $options );
155
156
		}
157
158
		/**
159
		 * Gets default Stripe settings
160
		 */
161
		private function get_default_stripe_config() {
162
163
			$result = array();
164
			$gateway = new WC_Gateway_Stripe();
165
			foreach ( $gateway->form_fields as $key => $value ) {
166
				if ( isset( $value['default'] ) ) {
167
					$result[ $key ] = $value['default'];
168
				}
169
			}
170
171
			return $result;
172
		}
173
174
		public function is_connected() {
175
176
			$options = get_option( self::SETTINGS_OPTION, array() );
177
178
			if ( isset( $options['testmode'] ) && 'yes' === $options['testmode'] ) {
179
				return isset( $options['test_publishable_key'], $options['test_secret_key'] ) && $options['test_publishable_key'] && $options['test_secret_key'];
180
			} else {
181
				return isset( $options['publishable_key'], $options['secret_key'] ) && $options['publishable_key'] && $options['secret_key'];
182
			}
183
		}
184
	}
185
}
186