Skip to content

Commit 8852dbf

Browse files
authored
Implement re-connect flow using the Mkt App Oauth as provider (#3339)
* Add support for saving account keys from the Stripe App redirect flow * Fix lint warnings * Add app to valid OAuth connection types
1 parent 83aaeb4 commit 8852dbf

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

includes/connect/class-wc-stripe-connect-api.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,19 @@ public function get_stripe_oauth_init( $return_url, $mode = 'live' ) {
7171
* Send request to Connect Server for Stripe keys
7272
*
7373
* @param string $code OAuth server code.
74+
* @param string $type Optional. The type of the connection. 'connect' or 'app'. Default is 'connect'.
7475
* @param string $mode Optional. The mode to connect to. 'live' or 'test'. Default is 'live'.
7576
*
7677
* @return array
7778
*/
78-
public function get_stripe_oauth_keys( $code, $mode = 'live' ) {
79+
public function get_stripe_oauth_keys( $code, $type = 'connect', $mode = 'live' ) {
7980
$request = [ 'code' => $code ];
8081

82+
if ( 'app' === $type ) {
83+
$request['mode'] = $mode;
84+
return $this->request( 'POST', '/stripe/app-oauth-keys', $request );
85+
}
86+
8187
$path = 'test' === $mode ? '/stripe-sandbox/oauth-keys' : '/stripe/oauth-keys';
8288
return $this->request( 'POST', $path, $request );
8389
}

includes/connect/class-wc-stripe-connect.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,28 @@ public function get_oauth_url( $return_url = '', $mode = 'live' ) {
6666
*
6767
* @param string $state State token to prevent request forgery.
6868
* @param string $code OAuth code.
69+
* @param string $type Optional. The type of the connection. 'connect' or 'app'. Default is 'connect'.
6970
* @param string $mode Optional. The mode to connect to. 'live' or 'test'. Default is 'live'.
7071
*
7172
* @return string|WP_Error
7273
*/
73-
public function connect_oauth( $state, $code, $mode = 'live' ) {
74+
public function connect_oauth( $state, $code, $type = 'connect', $mode = 'live' ) {
7475
// The state parameter is used to protect against CSRF.
7576
// It's a unique, randomly generated, opaque, and non-guessable string that is sent when starting the
7677
// authentication request and validated when processing the response.
7778
if ( get_transient( 'wcs_stripe_connect_state_' . $mode ) !== $state ) {
7879
return new WP_Error( 'Invalid state received from Stripe server' );
7980
}
8081

81-
$response = $this->api->get_stripe_oauth_keys( $code, $mode );
82+
$response = $this->api->get_stripe_oauth_keys( $code, $type, $mode );
8283

8384
if ( is_wp_error( $response ) ) {
8485
return $response;
8586
}
8687

8788
delete_transient( 'wcs_stripe_connect_state_' . $mode );
8889

89-
return $this->save_stripe_keys( $response, $mode );
90+
return $this->save_stripe_keys( $response, $type, $mode );
9091
}
9192

9293
/**
@@ -111,31 +112,36 @@ public function maybe_handle_redirect() {
111112

112113
$state = wc_clean( wp_unslash( $_GET['wcs_stripe_state'] ) );
113114
$code = wc_clean( wp_unslash( $_GET['wcs_stripe_code'] ) );
115+
$type = isset( $_GET['wcs_stripe_type'] ) ? wc_clean( wp_unslash( $_GET['wcs_stripe_type'] ) ) : 'connect';
114116
$mode = isset( $_GET['wcs_stripe_mode'] ) ? wc_clean( wp_unslash( $_GET['wcs_stripe_mode'] ) ) : 'live';
115117

116-
$response = $this->connect_oauth( $state, $code, $mode );
118+
$response = $this->connect_oauth( $state, $code, $type, $mode );
117119

118120
$this->record_account_connect_track_event( is_wp_error( $response ) );
119121

120-
wp_safe_redirect( esc_url_raw( remove_query_arg( [ 'wcs_stripe_state', 'wcs_stripe_code', 'wcs_stripe_mode' ] ) ) );
122+
wp_safe_redirect( esc_url_raw( remove_query_arg( [ 'wcs_stripe_state', 'wcs_stripe_code', 'wcs_stripe_type', 'wcs_stripe_mode' ] ) ) );
121123
exit;
122124
}
123125
}
124126

125127
/**
126128
* Saves Stripe keys after OAuth response
127129
*
128-
* @param stdObject $result OAuth response result.
130+
* @param stdObject $result OAuth's response result.
131+
* @param string $type Optional. The type of the connection. 'connect' or 'app'. Default is 'connect'.
129132
* @param string $mode Optional. The mode to connect to. 'live' or 'test'. Default is 'live'.
130133
*
131-
* @return stdObject|WP_Error OAuth response result or WP_Error.
134+
* @return stdObject|WP_Error OAuth's response result or WP_Error.
132135
*/
133-
private function save_stripe_keys( $result, $mode = 'live' ) {
134-
136+
private function save_stripe_keys( $result, $type = 'connect', $mode = 'live' ) {
135137
if ( ! isset( $result->publishableKey, $result->secretKey ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
136138
return new WP_Error( 'Invalid credentials received from WooCommerce Connect server' );
137139
}
138140

141+
if ( 'app' === $type && ! isset( $result->refreshToken ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
142+
return new WP_Error( 'Invalid credentials received from WooCommerce Connect server' );
143+
}
144+
139145
$publishable_key = $result->publishableKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
140146
$secret_key = $result->secretKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
141147
$is_test = 'live' !== $mode;
@@ -147,7 +153,11 @@ private function save_stripe_keys( $result, $mode = 'live' ) {
147153
$options['upe_checkout_experience_enabled'] = $this->get_upe_checkout_experience_enabled();
148154
$options[ $prefix . 'publishable_key' ] = $publishable_key;
149155
$options[ $prefix . 'secret_key' ] = $secret_key;
150-
$options[ $prefix . 'connection_type' ] = 'connect';
156+
$options[ $prefix . 'connection_type' ] = $type;
157+
158+
if ( 'app' === $type ) {
159+
$options[ $prefix . 'refresh_token' ] = $result->refreshToken; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
160+
}
151161

152162
// While we are at it, let's also clear the account_id and
153163
// test_account_id if present.
@@ -193,7 +203,7 @@ private function get_default_stripe_config() {
193203
}
194204
}
195205

196-
$result['upe_checkout_experience_enabled'] = 'yes';
206+
$result['upe_checkout_experience_enabled'] = 'yes';
197207
$result['upe_checkout_experience_accepted_payments'][] = 'link';
198208

199209
return $result;
@@ -234,7 +244,7 @@ public function is_connected_via_oauth( $mode = 'live' ) {
234244
$options = get_option( self::SETTINGS_OPTION, [] );
235245
$key = 'test' === $mode ? 'test_connection_type' : 'connection_type';
236246

237-
return isset( $options[ $key ] ) && in_array( $options[ $key ], [ 'connect' ], true );
247+
return isset( $options[ $key ] ) && in_array( $options[ $key ], [ 'connect', 'app' ], true );
238248
}
239249

240250
/**

0 commit comments

Comments
 (0)