Get sdk token from paypal

This commit is contained in:
Emili Castells Guasch 2025-07-17 17:36:06 +02:00
parent 450e4eb6aa
commit 57e36ed954
No known key found for this signature in database
2 changed files with 21 additions and 4 deletions

View file

@ -292,7 +292,8 @@ return array(
'axo.endpoint.script-attributes' => static function ( ContainerInterface $container ): AxoScriptAttributes {
return new AxoScriptAttributes(
$container->get( 'button.request-data' ),
$container->get( 'woocommerce.logger.woocommerce' )
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get('api.sdk-client-token')
);
},

View file

@ -3,6 +3,8 @@
namespace WooCommerce\PayPalCommerce\Axo\Endpoint;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
@ -12,10 +14,16 @@ class AxoScriptAttributes implements EndpointInterface {
private RequestData $request_data;
private LoggerInterface $logger;
private SdkClientToken $sdk_client_token;
public function __construct( RequestData $request_data, LoggerInterface $logger ) {
public function __construct(
RequestData $request_data,
LoggerInterface $logger,
SdkClientToken $sdk_client_token
) {
$this->request_data = $request_data;
$this->logger = $logger;
$this->sdk_client_token = $sdk_client_token;
}
public static function nonce(): string {
@ -25,10 +33,18 @@ class AxoScriptAttributes implements EndpointInterface {
public function handle_request(): bool {
$this->request_data->read_request( $this->nonce() );
try {
$token = $this->sdk_client_token->sdk_client_token();
} catch (PayPalApiException $exception) {
$this->logger->error($exception->getMessage());
wp_send_json_error($exception->getMessage());
return false;
}
wp_send_json_success(
array(
'sdk_client_token' => 'abc123',
'client_metadata_id' => 'xyz789',
'sdk_client_token' => $token,
'client_metadata_id' => str_replace('-', '', wp_generate_uuid4()),
)
);