diff --git a/src/class-convertkit-api-v4.php b/src/class-convertkit-api-v4.php index f542eda..8bd7ebe 100644 --- a/src/class-convertkit-api-v4.php +++ b/src/class-convertkit-api-v4.php @@ -319,7 +319,7 @@ private function delete_code_verifier() { * @param string $str String to encode. * @return Encoded string. */ - private function base64_urlencode( $str ) { + public function base64_urlencode( $str ) { // Encode to Base64 string. $str = base64_encode( $str ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions @@ -339,10 +339,10 @@ private function base64_urlencode( $str ) { * * @since 2.0.0 * - * @param string $state State. - * @return string OAuth URL + * @param string $return_url Return URL. + * @return string OAuth URL */ - public function get_oauth_url( $state = false ) { + public function get_oauth_url( $return_url = false ) { // Generate and store code verifier and challenge. $code_verifier = $this->generate_and_store_code_verifier(); @@ -357,11 +357,11 @@ public function get_oauth_url( $state = false ) { 'code_challenge_method' => 'S256', ); - if ( $state ) { + if ( $return_url ) { $args['state'] = $this->base64_urlencode( wp_json_encode( array( - 'return_to' => $state, + 'return_to' => $return_url, 'client_id' => $this->client_id, ) ) diff --git a/tests/wpunit/APITest.php b/tests/wpunit/APITest.php index 4316f86..f14af87 100644 --- a/tests/wpunit/APITest.php +++ b/tests/wpunit/APITest.php @@ -351,7 +351,7 @@ public function testGetOAuthURLWithState() { // Confirm the OAuth URL returned is correct. $this->assertEquals( - $this->api->get_oauth_url( 'an-example-state' ), + $this->api->get_oauth_url( 'https://example.com' ), 'https://app.convertkit.com/oauth/authorize?' . http_build_query( [ 'client_id' => $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], @@ -359,7 +359,14 @@ public function testGetOAuthURLWithState() 'redirect_uri' => $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], 'code_challenge' => $this->api->generate_code_challenge( $this->api->get_code_verifier() ), 'code_challenge_method' => 'S256', - 'state' => 'an-example-state', + 'state' => $this->api->base64_urlencode( + wp_json_encode( + array( + 'return_to' => 'https://example.com', + 'client_id' => $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + ) + ) + ), ] ) );