Add LoginLinkRestEndpoint for merchant login

This commit is contained in:
Philipp Stracker 2024-11-15 14:05:52 +01:00
parent f6b1d7a3cb
commit fe7dd9a653
No known key found for this signature in database
3 changed files with 111 additions and 0 deletions

View file

@ -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',

View file

@ -0,0 +1,104 @@
<?php
/**
* REST endpoint to manage the onboarding module.
*
* @package WooCommerce\PayPalCommerce\Settings\Endpoint
*/
declare( strict_types = 1 );
namespace WooCommerce\PayPalCommerce\Settings\Endpoint;
use WP_REST_Server;
use WP_REST_Response;
use WP_REST_Request;
use WooCommerce\PayPalCommerce\Settings\Service\ConnectionUrlGenerator;
/**
* REST controller that generates merchant login URLs.
*/
class LoginLinkRestEndpoint extends RestEndpoint {
/**
* The base path for this REST controller.
*
* @var string
*/
protected $rest_base = 'login_link';
/**
* Link generator list, with environment name as array key.
*
* @var ConnectionUrlGenerator[]
*/
protected array $url_generators;
/**
* Constructor.
*
* @param ConnectionUrlGenerator[] $url_generators Array of environment-specific URL generators.
*/
public function __construct( array $url_generators ) {
$this->url_generators = $url_generators;
}
/**
* Configure REST API routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<environment>[\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
);
}
}
}

View file

@ -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 ) {