mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Use client credentials
This commit is contained in:
parent
a152027772
commit
bea17db588
4 changed files with 75 additions and 32 deletions
|
@ -9,6 +9,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace WooCommerce\PayPalCommerce\ApiClient;
|
namespace WooCommerce\PayPalCommerce\ApiClient;
|
||||||
|
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Authentication\ClientCredentials;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken;
|
use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
|
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentMethodTokensEndpoint;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentMethodTokensEndpoint;
|
||||||
|
@ -1656,18 +1657,23 @@ return array(
|
||||||
return new PurchaseUnitSanitizer( $behavior, $line_name );
|
return new PurchaseUnitSanitizer( $behavior, $line_name );
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
|
'api.client-credentials' => static function(ContainerInterface $container): ClientCredentials {
|
||||||
|
return new ClientCredentials(
|
||||||
|
$container->get( 'wcgateway.settings' )
|
||||||
|
);
|
||||||
|
},
|
||||||
'api.user-id-token' => static function( ContainerInterface $container ): UserIdToken {
|
'api.user-id-token' => static function( ContainerInterface $container ): UserIdToken {
|
||||||
return new UserIdToken(
|
return new UserIdToken(
|
||||||
$container->get( 'api.host' ),
|
$container->get( 'api.host' ),
|
||||||
$container->get( 'api.bearer' ),
|
$container->get( 'woocommerce.logger.woocommerce' ),
|
||||||
$container->get( 'woocommerce.logger.woocommerce' )
|
$container->get( 'api.client-credentials' )
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
'api.sdk-client-token' => static function( ContainerInterface $container ): SdkClientToken {
|
'api.sdk-client-token' => static function( ContainerInterface $container ): SdkClientToken {
|
||||||
return new SdkClientToken(
|
return new SdkClientToken(
|
||||||
$container->get( 'api.host' ),
|
$container->get( 'api.host' ),
|
||||||
$container->get( 'api.bearer' ),
|
$container->get( 'woocommerce.logger.woocommerce' ),
|
||||||
$container->get( 'woocommerce.logger.woocommerce' )
|
$container->get( 'api.client-credentials' )
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* The client credentials.
|
||||||
|
*
|
||||||
|
* @package WooCommerce\PayPalCommerce\ApiClient\Authentication
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace WooCommerce\PayPalCommerce\ApiClient\Authentication;
|
||||||
|
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ClientCredentials
|
||||||
|
*/
|
||||||
|
class ClientCredentials {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The settings.
|
||||||
|
*
|
||||||
|
* @var Settings
|
||||||
|
*/
|
||||||
|
protected $settings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ClientCredentials constructor.
|
||||||
|
*
|
||||||
|
* @param Settings $settings The settings.
|
||||||
|
*/
|
||||||
|
public function __construct(Settings $settings) {
|
||||||
|
$this->settings = $settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function credentials(): string {
|
||||||
|
$client_id = $this->settings->has( 'client_id' ) ? $this->settings->get( 'client_id' ) : '';
|
||||||
|
$client_secret = $this->settings->has( 'client_secret' ) ? $this->settings->get( 'client_secret' ) : '';
|
||||||
|
|
||||||
|
return 'Basic ' . base64_encode($client_id . ':' . $client_secret);
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,13 +27,6 @@ class SdkClientToken {
|
||||||
*/
|
*/
|
||||||
private $host;
|
private $host;
|
||||||
|
|
||||||
/**
|
|
||||||
* The bearer.
|
|
||||||
*
|
|
||||||
* @var Bearer
|
|
||||||
*/
|
|
||||||
private $bearer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The logger.
|
* The logger.
|
||||||
*
|
*
|
||||||
|
@ -41,21 +34,28 @@ class SdkClientToken {
|
||||||
*/
|
*/
|
||||||
private $logger;
|
private $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The client credentials.
|
||||||
|
*
|
||||||
|
* @var ClientCredentials
|
||||||
|
*/
|
||||||
|
private $client_credentials;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SdkClientToken constructor.
|
* SdkClientToken constructor.
|
||||||
*
|
*
|
||||||
* @param string $host The host.
|
* @param string $host The host.
|
||||||
* @param Bearer $bearer The bearer.
|
|
||||||
* @param LoggerInterface $logger The logger.
|
* @param LoggerInterface $logger The logger.
|
||||||
|
* @param ClientCredentials $client_credentials The client credentials.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
string $host,
|
string $host,
|
||||||
Bearer $bearer,
|
LoggerInterface $logger,
|
||||||
LoggerInterface $logger
|
ClientCredentials $client_credentials
|
||||||
) {
|
) {
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
$this->bearer = $bearer;
|
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
$this->client_credentials = $client_credentials;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,8 +69,6 @@ class SdkClientToken {
|
||||||
* @throws RuntimeException If something unexpected happens.
|
* @throws RuntimeException If something unexpected happens.
|
||||||
*/
|
*/
|
||||||
public function sdk_client_token( string $target_customer_id = '' ): string {
|
public function sdk_client_token( string $target_customer_id = '' ): string {
|
||||||
$bearer = $this->bearer->bearer();
|
|
||||||
|
|
||||||
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||||
$domain = wp_unslash( $_SERVER['HTTP_HOST'] ?? '' );
|
$domain = wp_unslash( $_SERVER['HTTP_HOST'] ?? '' );
|
||||||
$domain = preg_replace( '/^www\./', '', $domain );
|
$domain = preg_replace( '/^www\./', '', $domain );
|
||||||
|
@ -89,7 +87,7 @@ class SdkClientToken {
|
||||||
$args = array(
|
$args = array(
|
||||||
'method' => 'POST',
|
'method' => 'POST',
|
||||||
'headers' => array(
|
'headers' => array(
|
||||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
'Authorization' => $this->client_credentials->credentials(),
|
||||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
|
@ -27,13 +27,6 @@ class UserIdToken {
|
||||||
*/
|
*/
|
||||||
private $host;
|
private $host;
|
||||||
|
|
||||||
/**
|
|
||||||
* The bearer.
|
|
||||||
*
|
|
||||||
* @var Bearer
|
|
||||||
*/
|
|
||||||
private $bearer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The logger.
|
* The logger.
|
||||||
*
|
*
|
||||||
|
@ -41,21 +34,28 @@ class UserIdToken {
|
||||||
*/
|
*/
|
||||||
private $logger;
|
private $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The client credentials.
|
||||||
|
*
|
||||||
|
* @var ClientCredentials
|
||||||
|
*/
|
||||||
|
private $client_credentials;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UserIdToken constructor.
|
* UserIdToken constructor.
|
||||||
*
|
*
|
||||||
* @param string $host The host.
|
* @param string $host The host.
|
||||||
* @param Bearer $bearer The bearer.
|
|
||||||
* @param LoggerInterface $logger The logger.
|
* @param LoggerInterface $logger The logger.
|
||||||
|
* @param ClientCredentials $client_credentials The client credentials.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
string $host,
|
string $host,
|
||||||
Bearer $bearer,
|
LoggerInterface $logger,
|
||||||
LoggerInterface $logger
|
ClientCredentials $client_credentials
|
||||||
) {
|
) {
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
$this->bearer = $bearer;
|
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
$this->client_credentials = $client_credentials;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,8 +69,6 @@ class UserIdToken {
|
||||||
* @throws RuntimeException If something unexpected happens.
|
* @throws RuntimeException If something unexpected happens.
|
||||||
*/
|
*/
|
||||||
public function id_token( string $target_customer_id = '' ): string {
|
public function id_token( string $target_customer_id = '' ): string {
|
||||||
$bearer = $this->bearer->bearer();
|
|
||||||
|
|
||||||
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token';
|
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token';
|
||||||
if ( $target_customer_id ) {
|
if ( $target_customer_id ) {
|
||||||
$url = add_query_arg(
|
$url = add_query_arg(
|
||||||
|
@ -84,7 +82,7 @@ class UserIdToken {
|
||||||
$args = array(
|
$args = array(
|
||||||
'method' => 'POST',
|
'method' => 'POST',
|
||||||
'headers' => array(
|
'headers' => array(
|
||||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
'Authorization' => $this->client_credentials->credentials(),
|
||||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue