From 6f205ee730ac63af8a23eb19205af9d481f4717a Mon Sep 17 00:00:00 2001 From: "Alex P." Date: Thu, 31 Oct 2024 09:44:04 +0200 Subject: [PATCH] Add dummy connect_manual endpoint --- modules/ppcp-settings/services.php | 10 +- .../Endpoint/ConnectManualRestEndpoint.php | 92 +++++++++++++++++++ modules/ppcp-settings/src/SettingsModule.php | 7 ++ 3 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 modules/ppcp-settings/src/Endpoint/ConnectManualRestEndpoint.php diff --git a/modules/ppcp-settings/services.php b/modules/ppcp-settings/services.php index f34257f79..40c965cbb 100644 --- a/modules/ppcp-settings/services.php +++ b/modules/ppcp-settings/services.php @@ -9,12 +9,13 @@ declare( strict_types = 1 ); namespace WooCommerce\PayPalCommerce\Settings; +use WooCommerce\PayPalCommerce\Settings\Endpoint\ConnectManualRestEndpoint; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\Settings\Endpoint\OnboardingRestEndpoint; use WooCommerce\PayPalCommerce\Settings\Data\OnboardingProfile; return array( - 'settings.url' => static function ( ContainerInterface $container ) : string { + 'settings.url' => static function ( ContainerInterface $container ) : string { /** * The path cannot be false. * @@ -25,7 +26,7 @@ return array( dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php' ); }, - 'settings.data.onboarding' => static function ( ContainerInterface $container ) : OnboardingProfile { + 'settings.data.onboarding' => static function ( ContainerInterface $container ) : OnboardingProfile { $can_use_casual_selling = false; $can_use_vaulting = $container->has( 'save-payment-methods.eligible' ) && $container->get( 'save-payment-methods.eligible' ); $can_use_card_payments = $container->has( 'card-fields.eligible' ) && $container->get( 'card-fields.eligible' ); @@ -41,7 +42,10 @@ return array( $can_use_card_payments ); }, - 'settings.rest.onboarding' => static function ( ContainerInterface $container ) : OnboardingRestEndpoint { + 'settings.rest.onboarding' => static function ( ContainerInterface $container ) : OnboardingRestEndpoint { return new OnboardingRestEndpoint( $container->get( 'settings.data.onboarding' ) ); }, + 'settings.rest.connect_manual' => static function ( ContainerInterface $container ) : ConnectManualRestEndpoint { + return new ConnectManualRestEndpoint(); + }, ); diff --git a/modules/ppcp-settings/src/Endpoint/ConnectManualRestEndpoint.php b/modules/ppcp-settings/src/Endpoint/ConnectManualRestEndpoint.php new file mode 100644 index 000000000..904f415d4 --- /dev/null +++ b/modules/ppcp-settings/src/Endpoint/ConnectManualRestEndpoint.php @@ -0,0 +1,92 @@ + array( + 'js_name' => 'clientId', + 'sanitize' => 'sanitize_text_field', + ), + 'client_secret' => array( + 'js_name' => 'clientSecret', + 'sanitize' => 'sanitize_text_field', + ), + 'use_sandbox' => array( + 'js_name' => 'useSandbox', + 'sanitize' => 'to_boolean', + ), + ); + + /** + * Configure REST API routes. + */ + public function register_routes() { + register_rest_route( + $this->namespace, + '/' . $this->rest_base, + array( + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => array( $this, 'connect_manual' ), + 'permission_callback' => array( $this, 'check_permission' ), + ), + ) + ); + } + + /** + * Retrieves merchantId and email. + * + * @param WP_REST_Request $request Full data about the request. + */ + public function connect_manual( WP_REST_Request $request ) : WP_REST_Response { + $data = $this->sanitize_for_wordpress( + $request->get_params(), + $this->field_map + ); + + if ( ! isset( $data['client_id'] ) || empty( $data['client_id'] ) + || ! isset( $data['client_secret'] ) || empty( $data['client_secret'] ) ) { + return rest_ensure_response( + array( + 'success' => false, + ) + ); + } + + $result = array( + 'merchantId' => 'bt_us@woocommerce.com', + 'email' => 'AT45V2DGMKLRY', + 'success' => true, + ); + + return rest_ensure_response( $result ); + } +} diff --git a/modules/ppcp-settings/src/SettingsModule.php b/modules/ppcp-settings/src/SettingsModule.php index e2d556b19..ef7340fe1 100644 --- a/modules/ppcp-settings/src/SettingsModule.php +++ b/modules/ppcp-settings/src/SettingsModule.php @@ -9,6 +9,8 @@ declare( strict_types = 1 ); namespace WooCommerce\PayPalCommerce\Settings; +use WooCommerce\PayPalCommerce\Settings\Endpoint\ConnectManualRestEndpoint; +use WooCommerce\PayPalCommerce\Settings\Endpoint\OnboardingRestEndpoint; use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExecutableModule; use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameIdTrait; use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ServiceModule; @@ -107,7 +109,12 @@ class SettingsModule implements ServiceModule, ExecutableModule { 'rest_api_init', static function () use ( $container ) : void { $onboarding_endpoint = $container->get( 'settings.rest.onboarding' ); + assert( $onboarding_endpoint instanceof OnboardingRestEndpoint ); $onboarding_endpoint->register_routes(); + + $connect_manual_endpoint = $container->get( 'settings.rest.connect_manual' ); + assert( $connect_manual_endpoint instanceof ConnectManualRestEndpoint ); + $connect_manual_endpoint->register_routes(); } );