diff --git a/modules/ppcp-settings/services.php b/modules/ppcp-settings/services.php index 34fecd8ee..9db70b4c3 100644 --- a/modules/ppcp-settings/services.php +++ b/modules/ppcp-settings/services.php @@ -15,6 +15,7 @@ use WooCommerce\PayPalCommerce\Settings\Endpoint\ConnectManualRestEndpoint; use WooCommerce\PayPalCommerce\Settings\Endpoint\OnboardingRestEndpoint; use WooCommerce\PayPalCommerce\Settings\Service\ConnectionUrlGenerator; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; +use WooCommerce\PayPalCommerce\Settings\Endpoint\LoginLinkRestEndpoint; return array( 'settings.url' => static function ( ContainerInterface $container ) : string { @@ -55,6 +56,11 @@ return array( $container->get( 'woocommerce.logger.woocommerce' ) ); }, + 'settings.rest.login_link' => static function ( ContainerInterface $container ) : LoginLinkRestEndpoint { + return new LoginLinkRestEndpoint( + $container->get( 'settings.service.connection-url-generators' ), + ); + }, 'settings.casual-selling.supported-countries' => static function ( ContainerInterface $container ) : array { return array( 'AR', diff --git a/modules/ppcp-settings/src/Endpoint/LoginLinkRestEndpoint.php b/modules/ppcp-settings/src/Endpoint/LoginLinkRestEndpoint.php new file mode 100644 index 000000000..cb969c7ca --- /dev/null +++ b/modules/ppcp-settings/src/Endpoint/LoginLinkRestEndpoint.php @@ -0,0 +1,104 @@ +url_generators = $url_generators; + } + + /** + * Configure REST API routes. + */ + public function register_routes() { + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/(?P[\w]+)', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_login_url' ), + 'permission_callback' => array( $this, 'check_permission' ), + 'args' => array( + 'products' => array( + 'required' => true, + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + 'sanitize_callback' => function ( $products ) { + return array_map( 'sanitize_text_field', $products ); + }, + ), + ), + ), + ) + ); + } + + /** + * Returns the full login URL for the requested environment and products. + * + * @param WP_REST_Request $request The request object. + * + * @return WP_REST_Response The login URL or an error response. + */ + public function get_login_url( WP_REST_Request $request ) : WP_REST_Response { + $environment = $request->get_param( 'environment' ); + $products = $request->get_param( 'products' ); + + if ( ! isset( $this->url_generators[ $environment ] ) ) { + return new WP_REST_Response( + array( 'error' => 'Invalid environment specified.' ), + 400 + ); + } + + $url_generator = $this->url_generators[ $environment ]; + + try { + $url = $url_generator->generate( $products ); + + return rest_ensure_response( $url ); + } catch ( \Exception $e ) { + return new WP_REST_Response( + array( 'error' => $e->getMessage() ), + 500 + ); + } + } +} diff --git a/modules/ppcp-settings/src/SettingsModule.php b/modules/ppcp-settings/src/SettingsModule.php index 8c737f5ac..03a3a5e85 100644 --- a/modules/ppcp-settings/src/SettingsModule.php +++ b/modules/ppcp-settings/src/SettingsModule.php @@ -111,6 +111,7 @@ class SettingsModule implements ServiceModule, ExecutableModule { $endpoints = array( $container->get( 'settings.rest.onboarding' ), $container->get( 'settings.rest.connect_manual' ), + $container->get( 'settings.rest.login_link' ), ); foreach ( $endpoints as $endpoint ) {