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

WC_Stripe_Connect::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
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
29
			$this->api = $api;
30
		}
31
32
		/**
33
		 * Gets the OAuth URL for Stripe onboarding flow
34
		 *
35
		 * @param  string $return_url url to return to after oauth flow.
36
		 *
37
		 * @return string|WP_Error
38
		 */
39
		public function get_oauth_url( $return_url = '' ) {
40
41
			if ( empty( $return_url ) ) {
42
				$return_url = admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe' );
43
			}
44
45
			if ( substr( $return_url, 0, 8 ) !== 'https://' ) {
46
				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' ) );
47
			}
48
49
			$result = $this->api->get_stripe_oauth_init( $return_url );
50
51
			if ( is_wp_error( $result ) ) {
52
				return $result;
53
			}
54
55
			update_option( 'stripe_state', $result->state );
56
57
			return $result->oauthUrl; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
58
		}
59
60
		/**
61
		 * Deauthorize existing Stripe account
62
		 *
63
		 * @return array|WP_Error
64
		 */
65
		public function deauthorize_account() {
66
67
			$response = $this->api->deauthorize_stripe_account();
68
69
			if ( is_wp_error( $response ) ) {
70
				return $response;
71
			}
72
73
			$this->clear_stripe_keys();
74
75
			return $response;
76
		}
77
78
		/**
79
		 * Initiate OAuth connection request to Connect Server
80
		 *
81
		 * @param  bool $state Stripe onboarding state.
82
		 * @param  int  $code  OAuth code.
83
		 *
84
		 * @return string|WP_Error
85
		 */
86
		public function connect_oauth( $state, $code ) {
87
88
			if ( get_option( 'stripe_state', false ) !== $state ) {
89
				return new WP_Error( 'Invalid stripe state' );
90
			}
91
92
			$response = $this->api->get_stripe_oauth_keys( $code );
93
94
			if ( is_wp_error( $response ) ) {
95
				return $response;
96
			}
97
98
			delete_option( 'stripe_state' );
99
100
			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 100 which is incompatible with the return type documented by WC_Stripe_Connect::connect_oauth of type string|WP_Error.
Loading history...
101
		}
102
103
		/**
104
		 * Handle redirect back from oauth-init
105
		 */
106
		public function maybe_connect_oauth() {
107
108
			if ( isset( $_GET['wcs_stripe_code'], $_GET['wcs_stripe_state'] ) ) {
109
				$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...
110
111
				wp_safe_redirect( remove_query_arg( array( 'wcs_stripe_state', 'wcs_stripe_code' ) ) );
112
				exit;
113
			}
114
		}
115
116
117
		/**
118
		 * Saves stripe keys after OAuth response
119
		 *
120
		 * @param  array $result OAuth response result.
121
		 *
122
		 * @return array|WP_Error
123
		 */
124
		private function save_stripe_keys( $result ) {
125
126
			if ( ! isset( $result->publishableKey, $result->secretKey ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
127
				return new WP_Error( 'Invalid credentials received from WooCommerce Connect server' );
128
			}
129
130
			$is_test         = false !== strpos( $result->publishableKey, '_test_' ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
131
			$prefix          = $is_test ? 'test_' : '';
132
			$default_options = array();
133
134
			$options                                = array_merge( $default_options, get_option( self::SETTINGS_OPTION, array() ) );
135
			$options['enabled']                     = 'yes';
136
			$options['testmode']                    = $is_test ? 'yes' : 'no';
137
			$options[ $prefix . 'publishable_key' ] = $result->publishableKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
138
			$options[ $prefix . 'secret_key' ]      = $result->secretKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
139
140
			// While we are at it, let's also clear the account_id and
141
			// test_account_id if present.
142
			unset( $options['account_id'] );
143
			unset( $options['test_account_id'] );
144
145
			update_option( self::SETTINGS_OPTION, $options );
146
147
			return $result;
148
		}
149
150
		/**
151
		 * Clears keys for test or production (whichever is presently enabled).
152
		 */
153
		private function clear_stripe_keys() {
154
155
			$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...
156
			$options         = array_merge( $default_options, get_option( self::SETTINGS_OPTION, array() ) );
157
158
			if ( 'yes' === $options['testmode'] ) {
159
				$options['test_publishable_key'] = '';
160
				$options['test_secret_key']      = '';
161
			} else {
162
				$options['publishable_key'] = '';
163
				$options['secret_key']      = '';
164
			}
165
166
			// While we are at it, let's also clear the account_id and
167
			// test_account_id if present.
168
			unset( $options['account_id'] );
169
			unset( $options['test_account_id'] );
170
171
			update_option( self::SETTINGS_OPTION, $options );
172
173
		}
174
	}
175
}
176