mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
♻️ Use new connection manager in REST endpoint
This commit is contained in:
parent
a2221e5233
commit
45db5abb34
2 changed files with 24 additions and 142 deletions
|
@ -81,10 +81,7 @@ return array(
|
||||||
},
|
},
|
||||||
'settings.rest.connect_manual' => static function ( ContainerInterface $container ) : ConnectManualRestEndpoint {
|
'settings.rest.connect_manual' => static function ( ContainerInterface $container ) : ConnectManualRestEndpoint {
|
||||||
return new ConnectManualRestEndpoint(
|
return new ConnectManualRestEndpoint(
|
||||||
$container->get( 'api.paypal-host-production' ),
|
$container->get( 'settings.service.connection_manager' ),
|
||||||
$container->get( 'api.paypal-host-sandbox' ),
|
|
||||||
$container->get( 'woocommerce.logger.woocommerce' ),
|
|
||||||
$container->get( 'settings.data.general' )
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
'settings.rest.login_link' => static function ( ContainerInterface $container ) : LoginLinkRestEndpoint {
|
'settings.rest.login_link' => static function ( ContainerInterface $container ) : LoginLinkRestEndpoint {
|
||||||
|
|
|
@ -20,33 +20,12 @@ use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Orders;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Orders;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\InMemoryCache;
|
use WooCommerce\PayPalCommerce\ApiClient\Helper\InMemoryCache;
|
||||||
use WooCommerce\PayPalCommerce\Settings\Data\GeneralSettings;
|
use WooCommerce\PayPalCommerce\Settings\Data\GeneralSettings;
|
||||||
|
use WooCommerce\PayPalCommerce\Settings\Service\ConnectionManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST controller for connection via manual credentials input.
|
* REST controller for connection via manual credentials input.
|
||||||
*/
|
*/
|
||||||
class ConnectManualRestEndpoint extends RestEndpoint {
|
class ConnectManualRestEndpoint extends RestEndpoint {
|
||||||
|
|
||||||
/**
|
|
||||||
* The API host for the live mode.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private string $live_host;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The API host for the sandbox mode.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private string $sandbox_host;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The logger.
|
|
||||||
*
|
|
||||||
* @var LoggerInterface
|
|
||||||
*/
|
|
||||||
private $logger;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base path for this REST controller.
|
* The base path for this REST controller.
|
||||||
*
|
*
|
||||||
|
@ -54,13 +33,6 @@ class ConnectManualRestEndpoint extends RestEndpoint {
|
||||||
*/
|
*/
|
||||||
protected $rest_base = 'connect_manual';
|
protected $rest_base = 'connect_manual';
|
||||||
|
|
||||||
/**
|
|
||||||
* Settings instance.
|
|
||||||
*
|
|
||||||
* @var GeneralSettings
|
|
||||||
*/
|
|
||||||
private $settings = null;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Field mapping for request.
|
* Field mapping for request.
|
||||||
*
|
*
|
||||||
|
@ -81,24 +53,27 @@ class ConnectManualRestEndpoint extends RestEndpoint {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines the JSON response format (when connection was successful).
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private array $response_map = array(
|
||||||
|
'merchant_id' => array(
|
||||||
|
'js_name' => 'merchantId',
|
||||||
|
),
|
||||||
|
'merchant_email' => array(
|
||||||
|
'js_name' => 'email',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ConnectManualRestEndpoint constructor.
|
* ConnectManualRestEndpoint constructor.
|
||||||
*
|
*
|
||||||
* @param string $live_host The API host for the live mode.
|
* @param ConnectionManager $connection_manager The connection manager.
|
||||||
* @param string $sandbox_host The API host for the sandbox mode.
|
|
||||||
* @param LoggerInterface $logger The logger.
|
|
||||||
* @param GeneralSettings $settings Settings instance.
|
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct( ConnectionManager $connection_manager ) {
|
||||||
string $live_host,
|
$this->connection_manager = $connection_manager;
|
||||||
string $sandbox_host,
|
|
||||||
LoggerInterface $logger,
|
|
||||||
GeneralSettings $settings
|
|
||||||
) {
|
|
||||||
$this->live_host = $live_host;
|
|
||||||
$this->sandbox_host = $sandbox_host;
|
|
||||||
$this->logger = $logger;
|
|
||||||
$this->settings = $settings;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -133,106 +108,16 @@ class ConnectManualRestEndpoint extends RestEndpoint {
|
||||||
$client_secret = $data['client_secret'] ?? '';
|
$client_secret = $data['client_secret'] ?? '';
|
||||||
$use_sandbox = (bool) ( $data['use_sandbox'] ?? false );
|
$use_sandbox = (bool) ( $data['use_sandbox'] ?? false );
|
||||||
|
|
||||||
if ( empty( $client_id ) || empty( $client_secret ) ) {
|
|
||||||
return $this->return_error( 'No client ID or secret provided.' );
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$payee = $this->request_payee( $client_id, $client_secret, $use_sandbox );
|
$this->connection_manager->validate_id_and_secret( $client_id, $client_secret );
|
||||||
|
$this->connection_manager->connect_via_secret( $use_sandbox, $client_id, $client_secret );
|
||||||
} catch ( Exception $exception ) {
|
} catch ( Exception $exception ) {
|
||||||
return $this->return_error( $exception->getMessage() );
|
return $this->return_error( $exception->getMessage() );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $use_sandbox ) {
|
$account = $this->connection_manager->get_account_details();
|
||||||
$this->settings->set_is_sandbox( true );
|
$response = $this->sanitize_for_javascript( $this->response_map, $account );
|
||||||
$this->settings->set_sandbox_client_id( $client_id );
|
|
||||||
$this->settings->set_sandbox_client_secret( $client_secret );
|
|
||||||
$this->settings->set_sandbox_merchant_id( $payee->merchant_id );
|
|
||||||
$this->settings->set_sandbox_merchant_email( $payee->email_address );
|
|
||||||
} else {
|
|
||||||
$this->settings->set_is_sandbox( false );
|
|
||||||
$this->settings->set_live_client_id( $client_id );
|
|
||||||
$this->settings->set_live_client_secret( $client_secret );
|
|
||||||
$this->settings->set_live_merchant_id( $payee->merchant_id );
|
|
||||||
$this->settings->set_live_merchant_email( $payee->email_address );
|
|
||||||
}
|
|
||||||
$this->settings->save();
|
|
||||||
|
|
||||||
return $this->return_success(
|
return $this->return_success( $response );
|
||||||
array(
|
|
||||||
'merchantId' => $payee->merchant_id,
|
|
||||||
'email' => $payee->email_address,
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the payee object with the merchant data
|
|
||||||
* by creating a minimal PayPal order.
|
|
||||||
*
|
|
||||||
* @throws Exception When failed to retrieve payee.
|
|
||||||
*
|
|
||||||
* phpcs:disable Squiz.Commenting
|
|
||||||
* phpcs:disable Generic.Commenting
|
|
||||||
*
|
|
||||||
* @param string $client_secret The client secret.
|
|
||||||
* @param bool $use_sandbox Whether to use the sandbox mode.
|
|
||||||
* @param string $client_id The client ID.
|
|
||||||
*
|
|
||||||
* @return stdClass The payee object.
|
|
||||||
*/
|
|
||||||
private function request_payee(
|
|
||||||
string $client_id,
|
|
||||||
string $client_secret,
|
|
||||||
bool $use_sandbox
|
|
||||||
) : stdClass {
|
|
||||||
|
|
||||||
$host = $use_sandbox ? $this->sandbox_host : $this->live_host;
|
|
||||||
|
|
||||||
$bearer = new PayPalBearer(
|
|
||||||
new InMemoryCache(),
|
|
||||||
$host,
|
|
||||||
$client_id,
|
|
||||||
$client_secret,
|
|
||||||
$this->logger,
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
$orders = new Orders(
|
|
||||||
$host,
|
|
||||||
$bearer,
|
|
||||||
$this->logger
|
|
||||||
);
|
|
||||||
|
|
||||||
$request_body = array(
|
|
||||||
'intent' => 'CAPTURE',
|
|
||||||
'purchase_units' => array(
|
|
||||||
array(
|
|
||||||
'amount' => array(
|
|
||||||
'currency_code' => 'USD',
|
|
||||||
'value' => 1.0,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
$response = $orders->create( $request_body );
|
|
||||||
$body = json_decode( $response['body'] );
|
|
||||||
|
|
||||||
$order_id = $body->id;
|
|
||||||
|
|
||||||
$order_response = $orders->order( $order_id );
|
|
||||||
$order_body = json_decode( $order_response['body'] );
|
|
||||||
|
|
||||||
$pu = $order_body->purchase_units[0];
|
|
||||||
$payee = $pu->payee;
|
|
||||||
if ( ! is_object( $payee ) ) {
|
|
||||||
throw new RuntimeException( 'Payee not found.' );
|
|
||||||
}
|
|
||||||
if ( ! isset( $payee->merchant_id ) || ! isset( $payee->email_address ) ) {
|
|
||||||
throw new RuntimeException( 'Payee info not found.' );
|
|
||||||
}
|
|
||||||
|
|
||||||
return $payee;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue