mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
♻️ Move OAuth logic from Listner to Auth-Manager
This commit is contained in:
parent
19a7986b56
commit
b3e766d08a
3 changed files with 55 additions and 40 deletions
|
@ -158,8 +158,8 @@ return array(
|
|||
|
||||
return new ConnectionListener(
|
||||
$page_id,
|
||||
$container->get( 'settings.data.common' ),
|
||||
$container->get( 'settings.service.onboarding-url-manager' ),
|
||||
$container->get( 'settings.service.authentication_manager' ),
|
||||
$container->get( 'woocommerce.logger.woocommerce' )
|
||||
);
|
||||
},
|
||||
|
|
|
@ -12,8 +12,7 @@ namespace WooCommerce\PayPalCommerce\Settings\Handler;
|
|||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\Settings\DTO\MerchantConnectionDTO;
|
||||
use WooCommerce\PayPalCommerce\Settings\Data\CommonSettings;
|
||||
use WooCommerce\PayPalCommerce\Settings\Service\AuthenticationManager;
|
||||
use WooCommerce\PayPalCommerce\Settings\Service\OnboardingUrlManager;
|
||||
use WooCommerce\WooCommerce\Logging\Logger\NullLogger;
|
||||
|
||||
|
@ -33,13 +32,6 @@ class ConnectionListener {
|
|||
*/
|
||||
private string $settings_page_id;
|
||||
|
||||
/**
|
||||
* Access to connection settings.
|
||||
*
|
||||
* @var CommonSettings
|
||||
*/
|
||||
private CommonSettings $settings;
|
||||
|
||||
/**
|
||||
* Access to the onboarding URL manager.
|
||||
*
|
||||
|
@ -47,6 +39,13 @@ class ConnectionListener {
|
|||
*/
|
||||
private OnboardingUrlManager $url_manager;
|
||||
|
||||
/**
|
||||
* Authentication manager service, responsible to update connection details.
|
||||
*
|
||||
* @var AuthenticationManager
|
||||
*/
|
||||
private AuthenticationManager $authentication_manager;
|
||||
|
||||
/**
|
||||
* Logger instance, mainly used for debugging purposes.
|
||||
*
|
||||
|
@ -64,16 +63,21 @@ class ConnectionListener {
|
|||
/**
|
||||
* Prepare the instance.
|
||||
*
|
||||
* @param string $settings_page_id Current plugin settings page ID.
|
||||
* @param CommonSettings $settings Access to saved connection details.
|
||||
* @param OnboardingUrlManager $url_manager Get OnboardingURL instances.
|
||||
* @param ?LoggerInterface $logger The logger, for debugging purposes.
|
||||
* @param string $settings_page_id Current plugin settings page ID.
|
||||
* @param OnboardingUrlManager $url_manager Get OnboardingURL instances.
|
||||
* @param AuthenticationManager $authentication_manager Authentication manager service.
|
||||
* @param ?LoggerInterface $logger The logger, for debugging purposes.
|
||||
*/
|
||||
public function __construct( string $settings_page_id, CommonSettings $settings, OnboardingUrlManager $url_manager, LoggerInterface $logger = null ) {
|
||||
$this->settings_page_id = $settings_page_id;
|
||||
$this->settings = $settings;
|
||||
$this->url_manager = $url_manager;
|
||||
$this->logger = $logger ?: new NullLogger();
|
||||
public function __construct(
|
||||
string $settings_page_id,
|
||||
OnboardingUrlManager $url_manager,
|
||||
AuthenticationManager $authentication_manager,
|
||||
LoggerInterface $logger = null
|
||||
) {
|
||||
$this->settings_page_id = $settings_page_id;
|
||||
$this->url_manager = $url_manager;
|
||||
$this->authentication_manager = $authentication_manager;
|
||||
$this->logger = $logger ?: new NullLogger();
|
||||
|
||||
// Initialize as "guest", the real ID is provided via process().
|
||||
$this->user_id = 0;
|
||||
|
@ -106,15 +110,11 @@ class ConnectionListener {
|
|||
|
||||
$this->logger->info( 'Found OAuth merchant data in request', $data );
|
||||
|
||||
$connection = $this->settings->get_merchant_data();
|
||||
|
||||
if ( $connection->merchant_id !== $data['merchant_id'] ) {
|
||||
throw new RuntimeException( 'Unexpected merchant ID in request' );
|
||||
try {
|
||||
$this->authentication_manager->finish_oauth_authentication( $data );
|
||||
} catch ( \Exception $e ) {
|
||||
$this->logger->error( 'Failed to complete authentication: ' . $e->getMessage() );
|
||||
}
|
||||
|
||||
$connection->merchant_email = $data['merchant_email'];
|
||||
|
||||
$this->store_data( $connection );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -168,24 +168,11 @@ class ConnectionListener {
|
|||
}
|
||||
|
||||
return array(
|
||||
'is_sandbox' => $this->settings->get_sandbox(),
|
||||
'merchant_id' => $merchant_id,
|
||||
'merchant_email' => $merchant_email,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist the merchant details to the database.
|
||||
*
|
||||
* @param MerchantConnectionDTO $connection Merchant connection details to store.
|
||||
*/
|
||||
protected function store_data( MerchantConnectionDTO $connection ) : void {
|
||||
$this->logger->info( 'Save merchant details to the DB', (array) $connection );
|
||||
|
||||
$this->settings->set_merchant_data( $connection );
|
||||
$this->settings->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sanitized connection token from the incoming request.
|
||||
*
|
||||
|
|
|
@ -248,6 +248,34 @@ class AuthenticationManager {
|
|||
$this->update_connection_details( $connection );
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the merchant details in the final OAuth redirect and extracts
|
||||
* missing credentials from the URL.
|
||||
*
|
||||
* @param array $request_data Array of request parameters to process.
|
||||
* @return void
|
||||
*
|
||||
* @throws RuntimeException Missing or invalid credentials.
|
||||
*/
|
||||
public function finish_oauth_authentication( array $request_data ) : void {
|
||||
$merchant_id = $request_data['merchant_id'];
|
||||
$merchant_email = $request_data['merchant_email'];
|
||||
|
||||
if ( empty( $merchant_id ) || empty( $merchant_email ) ) {
|
||||
throw new RuntimeException( 'Missing merchant ID or email in request' );
|
||||
}
|
||||
|
||||
$connection = $this->common_settings->get_merchant_data();
|
||||
|
||||
if ( $connection->merchant_id !== $merchant_id ) {
|
||||
throw new RuntimeException( 'Unexpected merchant ID in request' );
|
||||
}
|
||||
|
||||
$connection->merchant_email = $merchant_email;
|
||||
|
||||
$this->update_connection_details( $connection );
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Internal helper methods
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue