mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-30 05:00:51 +08:00
✨ Add LoginLinkRestEndpoint for merchant login
This commit is contained in:
parent
f6b1d7a3cb
commit
fe7dd9a653
3 changed files with 111 additions and 0 deletions
|
@ -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',
|
||||
|
|
104
modules/ppcp-settings/src/Endpoint/LoginLinkRestEndpoint.php
Normal file
104
modules/ppcp-settings/src/Endpoint/LoginLinkRestEndpoint.php
Normal 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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 ) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue