Adding frontend logger (WIP)

This commit is contained in:
Emili Castells Guasch 2024-05-24 17:13:43 +02:00
parent 1d83b0ce85
commit f6d07d9225
5 changed files with 103 additions and 0 deletions

View file

@ -88,6 +88,20 @@ class AxoManager {
this.triggerGatewayChange(); this.triggerGatewayChange();
} }
async log(message, level = 'info') {
await fetch(axoConfig.ajax.frontend_logger.endpoint, {
method: 'POST',
credentials: 'same-origin',
body: JSON.stringify({
nonce: axoConfig.ajax.frontend_logger.nonce,
log: {
message,
level,
}
})
});
}
registerEventHandlers() { registerEventHandlers() {
this.$(document).on('change', 'input[name=payment_method]', (ev) => { this.$(document).on('change', 'input[name=payment_method]', (ev) => {
@ -697,6 +711,8 @@ class AxoManager {
this.ensureBillingPhoneNumber(data); this.ensureBillingPhoneNumber(data);
this.log(`Ryan flow - submitted nonce: ${this.data.card.id}` )
this.submit(this.data.card.id, data); this.submit(this.data.card.id, data);
} else { // Gary flow } else { // Gary flow
@ -706,6 +722,7 @@ class AxoManager {
this.cardComponent.getPaymentToken( this.cardComponent.getPaymentToken(
this.tokenizeData() this.tokenizeData()
).then((response) => { ).then((response) => {
this.log(`Gary flow - submitted nonce: ${response.id}` )
this.submit(response.id); this.submit(response.id);
}); });
} catch (e) { } catch (e) {

View file

@ -225,4 +225,10 @@ return array(
return '<div class="ppcp-notice ppcp-notice-warning"><p>' . $notice_content . '</p></div>'; 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(
$container->get( 'button.request-data' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
); );

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Axo\Assets; namespace WooCommerce\PayPalCommerce\Axo\Assets;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\Axo\FrontendLoggerEndpoint;
use WooCommerce\PayPalCommerce\Onboarding\Environment; use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus; use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
@ -209,6 +210,12 @@ class AxoManager {
), ),
'icons_directory' => esc_url( $this->wcgateway_module_url ) . 'assets/images/axo/', 'icons_directory' => esc_url( $this->wcgateway_module_url ) . 'assets/images/axo/',
'module_url' => untrailingslashit( $this->module_url ), 'module_url' => untrailingslashit( $this->module_url ),
'ajax' => array(
'frontend_logger' => array(
'endpoint' => \WC_AJAX::get_endpoint( FrontendLoggerEndpoint::ENDPOINT ),
'nonce' => wp_create_nonce( FrontendLoggerEndpoint::nonce() ),
),
),
); );
} }

View file

@ -239,6 +239,16 @@ class AxoModule implements ModuleInterface {
1 1
); );
add_action(
'wc_ajax_' . FrontendLoggerEndpoint::ENDPOINT,
static function () use ( $c ) {
$endpoint = $c->get( 'axo.endpoint.frontend-logger' );
assert( $endpoint instanceof FrontendLoggerEndpoint );
$endpoint->handle_request();
}
);
// Add the markup necessary for displaying overlays and loaders for Axo on the checkout page. // Add the markup necessary for displaying overlays and loaders for Axo on the checkout page.
$this->add_checkout_loader_markup( $c ); $this->add_checkout_loader_markup( $c );
} }

View file

@ -0,0 +1,63 @@
<?php
/**
* The endpoint to log entries from frontend.
*
* @package WooCommerce\PayPalCommerce\Button\Endpoint
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Axo;
use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
class FrontendLoggerEndpoint implements EndpointInterface {
const ENDPOINT = 'ppc-frontend-logger';
/**
* The request data helper.
*
* @var RequestData
*/
private $request_data;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
public function __construct(RequestData $request_data, LoggerInterface $logger){
$this->request_data = $request_data;
$this->logger = $logger;
}
/**
* Returns the nonce.
*
* @return string
*/
public static function nonce(): string {
return self::ENDPOINT;
}
/**
* Handles the request.
*
* @return bool
* @throws Exception On Error.
*/
public function handle_request(): bool {
$data = $this->request_data->read_request( $this->nonce() );
$this->logger->info("[AXO] " . $data['log']['message']);
wp_send_json_success();
return true;
}
}