mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-30 05:00:51 +08:00
Merge pull request #3540 from woocommerce/PCP-5011-remove-exposure-of-data-sdk-client-token-when-not-needed
Remove data-sdk-client-token field when not needed (5011)
This commit is contained in:
commit
8e1307cae6
16 changed files with 244 additions and 233 deletions
|
@ -27,8 +27,38 @@ const usePayPalScript = ( namespace, ppcpConfig, isConfigLoaded ) => {
|
|||
useEffect( () => {
|
||||
const loadScript = async () => {
|
||||
if ( ! isPayPalLoaded && isConfigLoaded ) {
|
||||
const axoConfig = window.wc_ppcp_axo;
|
||||
|
||||
try {
|
||||
await loadPayPalScript( namespace, ppcpConfig );
|
||||
const res = await fetch(
|
||||
axoConfig.ajax.axo_script_attributes.endpoint,
|
||||
{
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify( {
|
||||
nonce: axoConfig.ajax.axo_script_attributes
|
||||
.nonce,
|
||||
} ),
|
||||
}
|
||||
);
|
||||
|
||||
const json = await res.json();
|
||||
if ( ! json.success ) {
|
||||
log(
|
||||
`Failed to load axo script attributes: ${ json.data.message }`,
|
||||
'error'
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await loadPayPalScript( namespace, {
|
||||
...ppcpConfig,
|
||||
script_attributes: {
|
||||
...ppcpConfig.script_attributes,
|
||||
'data-sdk-client-token': json.data.sdk_client_token,
|
||||
},
|
||||
} );
|
||||
setIsPayPalLoaded( true );
|
||||
} catch ( error ) {
|
||||
log(
|
||||
|
|
|
@ -67,24 +67,6 @@ class AxoBlockModule implements ServiceModule, ExtendingModule, ExecutableModule
|
|||
add_action(
|
||||
'wp_loaded',
|
||||
function () use ( $c ) {
|
||||
add_filter(
|
||||
'woocommerce_paypal_payments_localized_script_data',
|
||||
function( array $localized_script_data ) use ( $c ) {
|
||||
if ( ! $c->has( 'axo.available' ) || ! $c->get( 'axo.available' ) ) {
|
||||
return $localized_script_data;
|
||||
}
|
||||
|
||||
$module = $this;
|
||||
$api = $c->get( 'api.sdk-client-token' );
|
||||
assert( $api instanceof SdkClientToken );
|
||||
|
||||
$logger = $c->get( 'woocommerce.logger.woocommerce' );
|
||||
assert( $logger instanceof LoggerInterface );
|
||||
|
||||
return $module->add_sdk_client_token_to_script_data( $api, $logger, $localized_script_data );
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Param types removed to avoid third-party issues.
|
||||
*
|
||||
|
@ -146,37 +128,6 @@ class AxoBlockModule implements ServiceModule, ExtendingModule, ExecutableModule
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds id token to localized script data.
|
||||
*
|
||||
* @param SdkClientToken $api User id token api.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param array $localized_script_data The localized script data.
|
||||
* @return array
|
||||
*/
|
||||
private function add_sdk_client_token_to_script_data(
|
||||
SdkClientToken $api,
|
||||
LoggerInterface $logger,
|
||||
array $localized_script_data
|
||||
): array {
|
||||
try {
|
||||
$sdk_client_token = $api->sdk_client_token();
|
||||
$localized_script_data['axo'] = array(
|
||||
'sdk_client_token' => $sdk_client_token,
|
||||
);
|
||||
|
||||
} catch ( RuntimeException $exception ) {
|
||||
$error = $exception->getMessage();
|
||||
if ( is_a( $exception, PayPalApiException::class ) ) {
|
||||
$error = $exception->get_details( $error );
|
||||
}
|
||||
|
||||
$logger->error( $error );
|
||||
}
|
||||
|
||||
return $localized_script_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues PayPal Insights analytics script for the Checkout block.
|
||||
*
|
||||
|
|
|
@ -11,7 +11,8 @@ namespace WooCommerce\PayPalCommerce\AxoBlock;
|
|||
|
||||
use WC_Payment_Gateway;
|
||||
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
|
||||
use WooCommerce\PayPalCommerce\Axo\FrontendLoggerEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Axo\Endpoint\AxoScriptAttributes;
|
||||
use WooCommerce\PayPalCommerce\Axo\Endpoint\FrontendLogger;
|
||||
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Helper\Environment;
|
||||
use WooCommerce\PayPalCommerce\Axo\Gateway\AxoGateway;
|
||||
|
@ -257,9 +258,13 @@ class AxoBlockPaymentMethod extends AbstractPaymentMethodType {
|
|||
'icons_directory' => esc_url( $this->wcgateway_module_url ) . 'assets/images/axo/',
|
||||
'module_url' => untrailingslashit( $this->module_url ),
|
||||
'ajax' => array(
|
||||
'frontend_logger' => array(
|
||||
'endpoint' => \WC_AJAX::get_endpoint( FrontendLoggerEndpoint::ENDPOINT ),
|
||||
'nonce' => wp_create_nonce( FrontendLoggerEndpoint::nonce() ),
|
||||
'frontend_logger' => array(
|
||||
'endpoint' => \WC_AJAX::get_endpoint( FrontendLogger::ENDPOINT ),
|
||||
'nonce' => wp_create_nonce( FrontendLogger::nonce() ),
|
||||
),
|
||||
'axo_script_attributes' => array(
|
||||
'endpoint' => \WC_AJAX::get_endpoint( AxoScriptAttributes::ENDPOINT ),
|
||||
'nonce' => wp_create_nonce( AxoScriptAttributes::nonce() ),
|
||||
),
|
||||
),
|
||||
'logging_enabled' => $this->settings->has( 'logging_enabled' ) ? $this->settings->get( 'logging_enabled' ) : '',
|
||||
|
|
|
@ -2,20 +2,41 @@ import AxoManager from './AxoManager';
|
|||
import { loadPayPalScript } from '../../../ppcp-button/resources/js/modules/Helper/PayPalScriptLoading';
|
||||
import { log } from './Helper/Debug';
|
||||
|
||||
( function ( { axoConfig, ppcpConfig, jQuery } ) {
|
||||
( function ( { axoConfig, ppcpConfig } ) {
|
||||
const namespace = 'ppcpPaypalClassicAxo';
|
||||
const bootstrap = () => {
|
||||
new AxoManager( namespace, axoConfig, ppcpConfig );
|
||||
};
|
||||
|
||||
document.addEventListener( 'DOMContentLoaded', () => {
|
||||
document.addEventListener( 'DOMContentLoaded', async () => {
|
||||
if ( typeof PayPalCommerceGateway === 'undefined' ) {
|
||||
console.error( 'AXO could not be configured.' );
|
||||
return;
|
||||
}
|
||||
|
||||
// Load PayPal
|
||||
loadPayPalScript( namespace, ppcpConfig )
|
||||
const res = await fetch(
|
||||
axoConfig.ajax.axo_script_attributes.endpoint,
|
||||
{
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify( {
|
||||
nonce: axoConfig.ajax.axo_script_attributes.nonce,
|
||||
} ),
|
||||
}
|
||||
);
|
||||
|
||||
const json = await res.json();
|
||||
if ( ! json.success ) {
|
||||
throw new Error( json.data.message );
|
||||
}
|
||||
|
||||
loadPayPalScript( namespace, {
|
||||
...ppcpConfig,
|
||||
script_attributes: {
|
||||
...ppcpConfig.script_attributes,
|
||||
'data-sdk-client-token': json.data.sdk_client_token,
|
||||
},
|
||||
} )
|
||||
.then( () => {
|
||||
bootstrap();
|
||||
} )
|
||||
|
@ -26,5 +47,4 @@ import { log } from './Helper/Debug';
|
|||
} )( {
|
||||
axoConfig: window.wc_ppcp_axo,
|
||||
ppcpConfig: window.PayPalCommerceGateway,
|
||||
jQuery: window.jQuery,
|
||||
} );
|
||||
|
|
|
@ -10,8 +10,10 @@ declare(strict_types=1);
|
|||
namespace WooCommerce\PayPalCommerce\Axo;
|
||||
|
||||
use WooCommerce\PayPalCommerce\Axo\Assets\AxoManager;
|
||||
use WooCommerce\PayPalCommerce\Axo\Endpoint\AxoScriptAttributes;
|
||||
use WooCommerce\PayPalCommerce\Axo\Endpoint\FrontendLogger;
|
||||
use WooCommerce\PayPalCommerce\Axo\Gateway\AxoGateway;
|
||||
use WooCommerce\PayPalCommerce\Axo\Helper\ApmApplies;
|
||||
use WooCommerce\PayPalCommerce\Axo\Service\AxoApplies;
|
||||
use WooCommerce\PayPalCommerce\Axo\Helper\CompatibilityChecker;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
||||
|
@ -29,18 +31,20 @@ return array(
|
|||
return $eligibility_check();
|
||||
},
|
||||
'axo.eligibility.check' => static function ( ContainerInterface $container ): callable {
|
||||
$apm_applies = $container->get( 'axo.helpers.apm-applies' );
|
||||
assert( $apm_applies instanceof ApmApplies );
|
||||
$axo_applies = $container->get( 'axo.service.axo-applies' );
|
||||
assert( $axo_applies instanceof AxoApplies );
|
||||
|
||||
return static function () use ( $apm_applies ) : bool {
|
||||
return $apm_applies->for_country_currency() && $apm_applies->for_merchant();
|
||||
return static function () use ( $axo_applies ) : bool {
|
||||
return $axo_applies->for_country_currency() && $axo_applies->for_merchant();
|
||||
};
|
||||
},
|
||||
'axo.helpers.apm-applies' => static function ( ContainerInterface $container ) : ApmApplies {
|
||||
return new ApmApplies(
|
||||
'axo.service.axo-applies' => static function ( ContainerInterface $container ) : AxoApplies {
|
||||
return new AxoApplies(
|
||||
$container->get( 'axo.supported-country-currency-matrix' ),
|
||||
$container->get( 'api.shop.currency.getter' ),
|
||||
$container->get( 'api.shop.country' )
|
||||
$container->get( 'api.shop.country' ),
|
||||
$container->get( 'wcgateway.configuration.card-configuration' ),
|
||||
$container->get( 'wc-subscriptions.helper' )
|
||||
);
|
||||
},
|
||||
|
||||
|
@ -273,13 +277,22 @@ return array(
|
|||
return '<div class="ppcp-notice ppcp-notice-warning"><p>' . $notice_content . '</p></div>';
|
||||
},
|
||||
|
||||
'axo.endpoint.frontend-logger' => static function ( ContainerInterface $container ): FrontendLoggerEndpoint {
|
||||
return new FrontendLoggerEndpoint(
|
||||
'axo.endpoint.frontend-logger' => static function ( ContainerInterface $container ): FrontendLogger {
|
||||
return new FrontendLogger(
|
||||
$container->get( 'button.request-data' ),
|
||||
$container->get( 'woocommerce.logger.woocommerce' )
|
||||
);
|
||||
},
|
||||
|
||||
'axo.endpoint.script-attributes' => static function ( ContainerInterface $container ): AxoScriptAttributes {
|
||||
return new AxoScriptAttributes(
|
||||
$container->get( 'button.request-data' ),
|
||||
$container->get( 'woocommerce.logger.woocommerce' ),
|
||||
$container->get( 'api.sdk-client-token' ),
|
||||
$container->get( 'axo.eligible' )
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* The list of Fastlane incompatible plugins.
|
||||
*
|
||||
|
|
|
@ -11,7 +11,8 @@ namespace WooCommerce\PayPalCommerce\Axo\Assets;
|
|||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\CurrencyGetter;
|
||||
use WooCommerce\PayPalCommerce\Axo\FrontendLoggerEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Axo\Endpoint\AxoScriptAttributes;
|
||||
use WooCommerce\PayPalCommerce\Axo\Endpoint\FrontendLogger;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Helper\Environment;
|
||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
|
||||
|
@ -224,9 +225,13 @@ class AxoManager {
|
|||
'icons_directory' => esc_url( $this->wcgateway_module_url ) . 'assets/images/axo/',
|
||||
'module_url' => untrailingslashit( $this->module_url ),
|
||||
'ajax' => array(
|
||||
'frontend_logger' => array(
|
||||
'endpoint' => \WC_AJAX::get_endpoint( FrontendLoggerEndpoint::ENDPOINT ),
|
||||
'nonce' => wp_create_nonce( FrontendLoggerEndpoint::nonce() ),
|
||||
'frontend_logger' => array(
|
||||
'endpoint' => \WC_AJAX::get_endpoint( FrontendLogger::ENDPOINT ),
|
||||
'nonce' => wp_create_nonce( FrontendLogger::nonce() ),
|
||||
),
|
||||
'axo_script_attributes' => array(
|
||||
'endpoint' => \WC_AJAX::get_endpoint( AxoScriptAttributes::ENDPOINT ),
|
||||
'nonce' => wp_create_nonce( AxoScriptAttributes::nonce() ),
|
||||
),
|
||||
),
|
||||
'logging_enabled' => $this->settings->has( 'logging_enabled' ) ? $this->settings->get( 'logging_enabled' ) : '',
|
||||
|
|
|
@ -9,12 +9,11 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\Axo;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\Axo\Assets\AxoManager;
|
||||
use WooCommerce\PayPalCommerce\Axo\Endpoint\AxoScriptAttributes;
|
||||
use WooCommerce\PayPalCommerce\Axo\Endpoint\FrontendLogger;
|
||||
use WooCommerce\PayPalCommerce\Axo\Gateway\AxoGateway;
|
||||
use WooCommerce\PayPalCommerce\Axo\Service\AxoApplies;
|
||||
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
|
||||
use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingOptionsRenderer;
|
||||
|
@ -26,7 +25,6 @@ use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ServiceModule;
|
|||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Helper\CartCheckoutDetector;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsListener;
|
||||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||
use WC_Payment_Gateways;
|
||||
|
@ -211,7 +209,10 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
|
|||
$smart_button = $c->get( 'button.smart-button' );
|
||||
assert( $smart_button instanceof SmartButtonInterface );
|
||||
|
||||
if ( $this->should_render_fastlane( $c ) && $smart_button->should_load_ppcp_script() ) {
|
||||
$axo_applies = $c->get( 'axo.service.axo-applies' );
|
||||
assert( $axo_applies instanceof AxoApplies );
|
||||
|
||||
if ( $axo_applies->should_render_fastlane() && $smart_button->should_load_ppcp_script() ) {
|
||||
$manager->enqueue();
|
||||
}
|
||||
}
|
||||
|
@ -221,7 +222,10 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
|
|||
add_action(
|
||||
$manager->checkout_button_renderer_hook(),
|
||||
function () use ( $c, $manager ) {
|
||||
if ( $this->should_render_fastlane( $c ) ) {
|
||||
$axo_applies = $c->get( 'axo.service.axo-applies' );
|
||||
assert( $axo_applies instanceof AxoApplies );
|
||||
|
||||
if ( $axo_applies->should_render_fastlane() ) {
|
||||
$manager->render_checkout_button();
|
||||
}
|
||||
}
|
||||
|
@ -274,19 +278,6 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
|
|||
}
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'woocommerce_paypal_payments_localized_script_data',
|
||||
function( array $localized_script_data ) use ( $c ) {
|
||||
$api = $c->get( 'api.sdk-client-token' );
|
||||
assert( $api instanceof SdkClientToken );
|
||||
|
||||
$logger = $c->get( 'woocommerce.logger.woocommerce' );
|
||||
assert( $logger instanceof LoggerInterface );
|
||||
|
||||
return $this->add_sdk_client_token_to_script_data( $api, $logger, $localized_script_data );
|
||||
}
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'ppcp_onboarding_dcc_table_rows',
|
||||
/**
|
||||
|
@ -316,8 +307,10 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
|
|||
add_action(
|
||||
'template_redirect',
|
||||
function () use ( $c ) {
|
||||
$axo_applies = $c->get( 'axo.service.axo-applies' );
|
||||
assert( $axo_applies instanceof AxoApplies );
|
||||
|
||||
if ( $this->should_render_fastlane( $c ) ) {
|
||||
if ( $axo_applies->should_render_fastlane() ) {
|
||||
WC()->session->set( 'chosen_payment_method', AxoGateway::ID );
|
||||
}
|
||||
}
|
||||
|
@ -330,10 +323,10 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
|
|||
);
|
||||
|
||||
add_action(
|
||||
'wc_ajax_' . FrontendLoggerEndpoint::ENDPOINT,
|
||||
'wc_ajax_' . FrontendLogger::ENDPOINT,
|
||||
static function () use ( $c ) {
|
||||
$endpoint = $c->get( 'axo.endpoint.frontend-logger' );
|
||||
assert( $endpoint instanceof FrontendLoggerEndpoint );
|
||||
assert( $endpoint instanceof FrontendLogger );
|
||||
|
||||
$endpoint->handle_request();
|
||||
}
|
||||
|
@ -367,38 +360,17 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
|
|||
}
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
add_action(
|
||||
'wc_ajax_' . AxoScriptAttributes::ENDPOINT,
|
||||
static function () use ( $c ) {
|
||||
$endpoint = $c->get( 'axo.endpoint.script-attributes' );
|
||||
assert( $endpoint instanceof AxoScriptAttributes );
|
||||
|
||||
/**
|
||||
* Adds id token to localized script data.
|
||||
*
|
||||
* @param SdkClientToken $api User id token api.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param array $localized_script_data The localized script data.
|
||||
* @return array
|
||||
*/
|
||||
private function add_sdk_client_token_to_script_data(
|
||||
SdkClientToken $api,
|
||||
LoggerInterface $logger,
|
||||
array $localized_script_data
|
||||
): array {
|
||||
try {
|
||||
$sdk_client_token = $api->sdk_client_token();
|
||||
$localized_script_data['axo'] = array(
|
||||
'sdk_client_token' => $sdk_client_token,
|
||||
);
|
||||
|
||||
} catch ( RuntimeException $exception ) {
|
||||
$error = $exception->getMessage();
|
||||
if ( is_a( $exception, PayPalApiException::class ) ) {
|
||||
$error = $exception->get_details( $error );
|
||||
$endpoint->handle_request();
|
||||
}
|
||||
);
|
||||
|
||||
$logger->error( $error );
|
||||
}
|
||||
|
||||
return $localized_script_data;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -409,31 +381,10 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
|
|||
* @return bool
|
||||
*/
|
||||
private function hide_credit_card_when_using_fastlane( array $methods, ContainerInterface $c ): bool {
|
||||
return $this->should_render_fastlane( $c ) && isset( $methods[ CreditCardGateway::ID ] );
|
||||
}
|
||||
$axo_applies = $c->get( 'axo.service.axo-applies' );
|
||||
assert( $axo_applies instanceof AxoApplies );
|
||||
|
||||
/**
|
||||
* Condition to evaluate if Fastlane should be rendered.
|
||||
*
|
||||
* Fastlane should only render on the classic checkout, when Fastlane is enabled in the settings and also only for guest customers.
|
||||
*
|
||||
* @param ContainerInterface $c The container.
|
||||
* @return bool
|
||||
*/
|
||||
private function should_render_fastlane( ContainerInterface $c ): bool {
|
||||
|
||||
$dcc_configuration = $c->get( 'wcgateway.configuration.card-configuration' );
|
||||
assert( $dcc_configuration instanceof CardPaymentsConfiguration );
|
||||
|
||||
$subscription_helper = $c->get( 'wc-subscriptions.helper' );
|
||||
assert( $subscription_helper instanceof SubscriptionHelper );
|
||||
|
||||
return ! is_user_logged_in()
|
||||
&& CartCheckoutDetector::has_classic_checkout()
|
||||
&& $dcc_configuration->use_fastlane()
|
||||
&& ! $this->is_excluded_endpoint()
|
||||
&& is_checkout()
|
||||
&& ! $subscription_helper->cart_contains_subscription();
|
||||
return $axo_applies->should_render_fastlane() && isset( $methods[ CreditCardGateway::ID ] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -443,8 +394,10 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
|
|||
* @return void
|
||||
*/
|
||||
private function add_checkout_loader_markup( ContainerInterface $c ): void {
|
||||
$axo_applies = $c->get( 'axo.service.axo-applies' );
|
||||
assert( $axo_applies instanceof AxoApplies );
|
||||
|
||||
if ( $this->should_render_fastlane( $c ) ) {
|
||||
if ( $axo_applies->should_render_fastlane() ) {
|
||||
add_action(
|
||||
'woocommerce_checkout_before_customer_details',
|
||||
function () {
|
||||
|
@ -483,7 +436,6 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
|
|||
* @return bool
|
||||
*/
|
||||
private function is_excluded_endpoint(): bool {
|
||||
// Exclude the Order Pay and Order Received endpoints.
|
||||
return is_wc_endpoint_url( 'order-pay' ) || is_wc_endpoint_url( 'order-received' );
|
||||
}
|
||||
|
||||
|
|
69
modules/ppcp-axo/src/Endpoint/AxoScriptAttributes.php
Normal file
69
modules/ppcp-axo/src/Endpoint/AxoScriptAttributes.php
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
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;
|
||||
use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
|
||||
|
||||
/**
|
||||
* Handles the request for the PayPal Axo script attributes.
|
||||
*/
|
||||
class AxoScriptAttributes implements EndpointInterface {
|
||||
use ContextTrait;
|
||||
|
||||
const ENDPOINT = 'ppc-axo-script-attributes';
|
||||
|
||||
private RequestData $request_data;
|
||||
private LoggerInterface $logger;
|
||||
private SdkClientToken $sdk_client_token;
|
||||
private bool $axo_eligible;
|
||||
|
||||
public function __construct(
|
||||
RequestData $request_data,
|
||||
LoggerInterface $logger,
|
||||
SdkClientToken $sdk_client_token,
|
||||
bool $axo_eligible
|
||||
) {
|
||||
$this->request_data = $request_data;
|
||||
$this->logger = $logger;
|
||||
$this->sdk_client_token = $sdk_client_token;
|
||||
$this->axo_eligible = $axo_eligible;
|
||||
}
|
||||
|
||||
public static function nonce(): string {
|
||||
return self::ENDPOINT;
|
||||
}
|
||||
|
||||
public function handle_request(): bool {
|
||||
$this->request_data->read_request( $this->nonce() );
|
||||
|
||||
if (
|
||||
! $this->axo_eligible
|
||||
|| is_user_logged_in()
|
||||
|| $this->is_paypal_continuation()
|
||||
) {
|
||||
wp_send_json_error( 'Failed to load axo script attributes.' );
|
||||
return false;
|
||||
}
|
||||
|
||||
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' => $token,
|
||||
)
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Axo;
|
||||
namespace WooCommerce\PayPalCommerce\Axo\Endpoint;
|
||||
|
||||
use Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
@ -17,7 +17,7 @@ use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
|
|||
/**
|
||||
* Class FrontendLoggerEndpoint
|
||||
*/
|
||||
class FrontendLoggerEndpoint implements EndpointInterface {
|
||||
class FrontendLogger implements EndpointInterface {
|
||||
|
||||
const ENDPOINT = 'ppc-frontend-logger';
|
||||
|
|
@ -1,21 +1,21 @@
|
|||
<?php
|
||||
/**
|
||||
* ApmApplies helper.
|
||||
* AxoApplies helper.
|
||||
* Checks if AXO is available for a given country and currency.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Axo\Helper
|
||||
* @package WooCommerce\PayPalCommerce\Axo\Service
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Axo\Helper;
|
||||
namespace WooCommerce\PayPalCommerce\Axo\Service;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\CurrencyGetter;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Helper\CardPaymentsConfiguration;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Helper\CartCheckoutDetector;
|
||||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||
|
||||
/**
|
||||
* Class ApmApplies
|
||||
*/
|
||||
class ApmApplies {
|
||||
class AxoApplies {
|
||||
|
||||
/**
|
||||
* The matrix which countries and currency combinations can be used for AXO.
|
||||
|
@ -38,6 +38,9 @@ class ApmApplies {
|
|||
*/
|
||||
private $country;
|
||||
|
||||
private CardPaymentsConfiguration $dcc_configuration;
|
||||
private SubscriptionHelper $subscription_helper;
|
||||
|
||||
/**
|
||||
* DccApplies constructor.
|
||||
*
|
||||
|
@ -48,11 +51,15 @@ class ApmApplies {
|
|||
public function __construct(
|
||||
array $allowed_country_currency_matrix,
|
||||
CurrencyGetter $currency,
|
||||
string $country
|
||||
string $country,
|
||||
CardPaymentsConfiguration $dcc_configuration,
|
||||
SubscriptionHelper $subscription_helper
|
||||
) {
|
||||
$this->allowed_country_currency_matrix = $allowed_country_currency_matrix;
|
||||
$this->currency = $currency;
|
||||
$this->country = $country;
|
||||
$this->dcc_configuration = $dcc_configuration;
|
||||
$this->subscription_helper = $subscription_helper;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -79,4 +86,27 @@ class ApmApplies {
|
|||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if Fastlane should be rendered.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function should_render_fastlane(): bool {
|
||||
return ! is_user_logged_in()
|
||||
&& CartCheckoutDetector::has_classic_checkout()
|
||||
&& $this->dcc_configuration->use_fastlane()
|
||||
&& ! $this->is_excluded_endpoint()
|
||||
&& is_checkout()
|
||||
&& ! $this->subscription_helper->cart_contains_subscription();
|
||||
}
|
||||
|
||||
/**
|
||||
* Condition to evaluate if the current endpoint is excluded.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_excluded_endpoint(): bool {
|
||||
return is_wc_endpoint_url( 'order-pay' ) || is_wc_endpoint_url( 'order-received' );
|
||||
}
|
||||
}
|
|
@ -1,18 +1,6 @@
|
|||
import merge from 'deepmerge';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { keysToCamelCase } from './Utils';
|
||||
|
||||
const processAxoConfig = ( config ) => {
|
||||
const scriptOptions = {};
|
||||
const sdkClientToken = config?.axo?.sdk_client_token;
|
||||
const uuid = uuidv4().replace( /-/g, '' );
|
||||
if ( sdkClientToken && config?.user?.is_logged !== true ) {
|
||||
scriptOptions[ 'data-sdk-client-token' ] = sdkClientToken;
|
||||
scriptOptions[ 'data-client-metadata-id' ] = uuid;
|
||||
}
|
||||
return scriptOptions;
|
||||
};
|
||||
|
||||
const processUserIdToken = ( config ) => {
|
||||
const userIdToken = config?.save_payment_methods?.id_token;
|
||||
return userIdToken && config?.user?.is_logged === true
|
||||
|
@ -25,7 +13,8 @@ export const processConfig = ( config ) => {
|
|||
if ( config.script_attributes ) {
|
||||
scriptOptions = merge( scriptOptions, config.script_attributes );
|
||||
}
|
||||
const axoOptions = processAxoConfig( config );
|
||||
|
||||
const userIdTokenOptions = processUserIdToken( config );
|
||||
return merge.all( [ scriptOptions, axoOptions, userIdTokenOptions ] );
|
||||
|
||||
return merge.all( [ scriptOptions, userIdTokenOptions ] );
|
||||
};
|
||||
|
|
|
@ -1,31 +1,10 @@
|
|||
import { loadScript } from '@paypal/paypal-js';
|
||||
import dataClientIdAttributeHandler from '../DataClientIdAttributeHandler';
|
||||
import widgetBuilder from '../Renderer/WidgetBuilder';
|
||||
import { processConfig } from './ConfigProcessor';
|
||||
|
||||
const loadedScripts = new Map();
|
||||
const scriptPromises = new Map();
|
||||
|
||||
const handleDataClientIdAttribute = async ( scriptOptions, config ) => {
|
||||
if (
|
||||
config.data_client_id?.set_attribute &&
|
||||
config.vault_v3_enabled !== true
|
||||
) {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
dataClientIdAttributeHandler(
|
||||
scriptOptions,
|
||||
config.data_client_id,
|
||||
( paypal ) => {
|
||||
widgetBuilder.setPaypal( paypal );
|
||||
resolve( paypal );
|
||||
},
|
||||
reject
|
||||
);
|
||||
} );
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
export const loadPayPalScript = async ( namespace, config ) => {
|
||||
if ( ! namespace ) {
|
||||
throw new Error( 'Namespace is required' );
|
||||
|
@ -48,14 +27,6 @@ export const loadPayPalScript = async ( namespace, config ) => {
|
|||
'data-namespace': namespace,
|
||||
};
|
||||
|
||||
const dataClientIdResult = await handleDataClientIdAttribute(
|
||||
scriptOptions,
|
||||
config
|
||||
);
|
||||
if ( dataClientIdResult ) {
|
||||
return dataClientIdResult;
|
||||
}
|
||||
|
||||
const scriptPromise = new Promise( ( resolve, reject ) => {
|
||||
loadScript( scriptOptions )
|
||||
.then( ( script ) => {
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
import dataClientIdAttributeHandler from '../DataClientIdAttributeHandler';
|
||||
import { loadScript } from '@paypal/paypal-js';
|
||||
import widgetBuilder from '../Renderer/WidgetBuilder';
|
||||
import merge from 'deepmerge';
|
||||
import { keysToCamelCase } from './Utils';
|
||||
import { getCurrentPaymentMethod } from './CheckoutMethodState';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
// This component may be used by multiple modules. This assures that options are shared between all instances.
|
||||
const scriptOptionsMap = {};
|
||||
|
@ -72,28 +69,6 @@ export const loadPaypalScript = ( config, onLoaded, onError = null ) => {
|
|||
scriptOptions = merge( scriptOptions, config.script_attributes );
|
||||
}
|
||||
|
||||
// Axo SDK options
|
||||
const sdkClientToken = config?.axo?.sdk_client_token;
|
||||
const uuid = uuidv4().replace( /-/g, '' );
|
||||
if ( sdkClientToken && config?.user?.is_logged !== true ) {
|
||||
scriptOptions[ 'data-sdk-client-token' ] = sdkClientToken;
|
||||
scriptOptions[ 'data-client-metadata-id' ] = uuid;
|
||||
}
|
||||
|
||||
// Load PayPal script for special case with data-client-token
|
||||
if (
|
||||
config.data_client_id?.set_attribute &&
|
||||
config.vault_v3_enabled !== '1'
|
||||
) {
|
||||
dataClientIdAttributeHandler(
|
||||
scriptOptions,
|
||||
config.data_client_id,
|
||||
callback,
|
||||
errorCallback
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Adds data-user-id-token to script options.
|
||||
const userIdToken = config?.save_payment_methods?.id_token;
|
||||
if ( userIdToken && config?.user?.is_logged === true ) {
|
||||
|
|
|
@ -20,8 +20,7 @@ use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
|
|||
*/
|
||||
class CartScriptParamsEndpoint implements EndpointInterface {
|
||||
|
||||
|
||||
const ENDPOINT = 'ppc-cart-script-params';
|
||||
public const ENDPOINT = 'ppc-cart-script-params';
|
||||
|
||||
/**
|
||||
* The SmartButton.
|
||||
|
|
|
@ -248,7 +248,7 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
|
|||
public function process_payment( $order_id ) {
|
||||
$wc_order = wc_get_order( $order_id );
|
||||
// phpcs:disable WordPress.Security.NonceVerification
|
||||
$birth_date = wc_clean( wp_unslash( $_POST['billing_birth_date'] ?? '' ) );
|
||||
$birth_date = wc_clean( wp_unslash( $_POST['billing_birth_date'] ?? '' ) );
|
||||
$pay_for_order = wc_clean( wp_unslash( $_GET['pay_for_order'] ?? '' ) );
|
||||
if ( 'true' === $pay_for_order ) {
|
||||
if ( ! $this->checkout_helper->validate_birth_date( $birth_date ) ) {
|
||||
|
|
|
@ -51,11 +51,13 @@
|
|||
<exclude name="Squiz.Commenting.FileComment.Missing" />
|
||||
<exclude name="Squiz.Commenting.FileComment.MissingPackageTag" />
|
||||
</rule>
|
||||
<rule ref="Squiz.Commenting.ClassComment">
|
||||
<exclude name="Squiz.Commenting.ClassComment.Missing" />
|
||||
</rule>
|
||||
<rule ref="Generic.Commenting.DocComment">
|
||||
<exclude name="Generic.Commenting.DocComment.MissingShort" />
|
||||
</rule>
|
||||
|
||||
|
||||
<arg name="extensions" value="php"/>
|
||||
<file>api</file>
|
||||
<file>src</file>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue