@@ -66,27 +66,28 @@ public function get_oauth_url( $return_url = '', $mode = 'live' ) {
66
66
*
67
67
* @param string $state State token to prevent request forgery.
68
68
* @param string $code OAuth code.
69
+ * @param string $type Optional. The type of the connection. 'connect' or 'app'. Default is 'connect'.
69
70
* @param string $mode Optional. The mode to connect to. 'live' or 'test'. Default is 'live'.
70
71
*
71
72
* @return string|WP_Error
72
73
*/
73
- public function connect_oauth ( $ state , $ code , $ mode = 'live ' ) {
74
+ public function connect_oauth ( $ state , $ code , $ type = ' connect ' , $ mode = 'live ' ) {
74
75
// The state parameter is used to protect against CSRF.
75
76
// It's a unique, randomly generated, opaque, and non-guessable string that is sent when starting the
76
77
// authentication request and validated when processing the response.
77
78
if ( get_transient ( 'wcs_stripe_connect_state_ ' . $ mode ) !== $ state ) {
78
79
return new WP_Error ( 'Invalid state received from Stripe server ' );
79
80
}
80
81
81
- $ response = $ this ->api ->get_stripe_oauth_keys ( $ code , $ mode );
82
+ $ response = $ this ->api ->get_stripe_oauth_keys ( $ code , $ type , $ mode );
82
83
83
84
if ( is_wp_error ( $ response ) ) {
84
85
return $ response ;
85
86
}
86
87
87
88
delete_transient ( 'wcs_stripe_connect_state_ ' . $ mode );
88
89
89
- return $ this ->save_stripe_keys ( $ response , $ mode );
90
+ return $ this ->save_stripe_keys ( $ response , $ type , $ mode );
90
91
}
91
92
92
93
/**
@@ -111,31 +112,36 @@ public function maybe_handle_redirect() {
111
112
112
113
$ state = wc_clean ( wp_unslash ( $ _GET ['wcs_stripe_state ' ] ) );
113
114
$ 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 ' ;
114
116
$ mode = isset ( $ _GET ['wcs_stripe_mode ' ] ) ? wc_clean ( wp_unslash ( $ _GET ['wcs_stripe_mode ' ] ) ) : 'live ' ;
115
117
116
- $ response = $ this ->connect_oauth ( $ state , $ code , $ mode );
118
+ $ response = $ this ->connect_oauth ( $ state , $ code , $ type , $ mode );
117
119
118
120
$ this ->record_account_connect_track_event ( is_wp_error ( $ response ) );
119
121
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 ' ] ) ) );
121
123
exit ;
122
124
}
123
125
}
124
126
125
127
/**
126
128
* Saves Stripe keys after OAuth response
127
129
*
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'.
129
132
* @param string $mode Optional. The mode to connect to. 'live' or 'test'. Default is 'live'.
130
133
*
131
- * @return stdObject|WP_Error OAuth response result or WP_Error.
134
+ * @return stdObject|WP_Error OAuth's response result or WP_Error.
132
135
*/
133
- private function save_stripe_keys ( $ result , $ mode = 'live ' ) {
134
-
136
+ private function save_stripe_keys ( $ result , $ type = 'connect ' , $ mode = 'live ' ) {
135
137
if ( ! isset ( $ result ->publishableKey , $ result ->secretKey ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
136
138
return new WP_Error ( 'Invalid credentials received from WooCommerce Connect server ' );
137
139
}
138
140
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
+
139
145
$ publishable_key = $ result ->publishableKey ; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
140
146
$ secret_key = $ result ->secretKey ; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
141
147
$ is_test = 'live ' !== $ mode ;
@@ -147,7 +153,11 @@ private function save_stripe_keys( $result, $mode = 'live' ) {
147
153
$ options ['upe_checkout_experience_enabled ' ] = $ this ->get_upe_checkout_experience_enabled ();
148
154
$ options [ $ prefix . 'publishable_key ' ] = $ publishable_key ;
149
155
$ 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
+ }
151
161
152
162
// While we are at it, let's also clear the account_id and
153
163
// test_account_id if present.
@@ -193,7 +203,7 @@ private function get_default_stripe_config() {
193
203
}
194
204
}
195
205
196
- $ result ['upe_checkout_experience_enabled ' ] = 'yes ' ;
206
+ $ result ['upe_checkout_experience_enabled ' ] = 'yes ' ;
197
207
$ result ['upe_checkout_experience_accepted_payments ' ][] = 'link ' ;
198
208
199
209
return $ result ;
@@ -234,7 +244,7 @@ public function is_connected_via_oauth( $mode = 'live' ) {
234
244
$ options = get_option ( self ::SETTINGS_OPTION , [] );
235
245
$ key = 'test ' === $ mode ? 'test_connection_type ' : 'connection_type ' ;
236
246
237
- return isset ( $ options [ $ key ] ) && in_array ( $ options [ $ key ], [ 'connect ' ], true );
247
+ return isset ( $ options [ $ key ] ) && in_array ( $ options [ $ key ], [ 'connect ' , ' app ' ], true );
238
248
}
239
249
240
250
/**
0 commit comments