mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 09:08:09 +08:00
Move request response information from logger to request trait and return it as string
This commit is contained in:
parent
9f348ed411
commit
5ca2f9d69f
2 changed files with 43 additions and 51 deletions
|
@ -10,6 +10,7 @@ declare(strict_types=1);
|
||||||
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
|
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
|
||||||
|
|
||||||
use WooCommerce\WooCommerce\Logging\Logger\WooCommerceLogger;
|
use WooCommerce\WooCommerce\Logging\Logger\WooCommerceLogger;
|
||||||
|
use WP_Error;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Trait RequestTrait
|
* Trait RequestTrait
|
||||||
|
@ -40,12 +41,52 @@ trait RequestTrait {
|
||||||
|
|
||||||
$response = wp_remote_get( $url, $args );
|
$response = wp_remote_get( $url, $args );
|
||||||
|
|
||||||
if ( $this->logger instanceof WooCommerceLogger ) {
|
if ( is_wp_error( $response ) ) {
|
||||||
$this->logger->logRequestResponse( $url, $args, $response, $this->host );
|
$this->logger->error( $response->get_error_code() . ' ' . $response->get_error_message() );
|
||||||
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->logger->info( $this->request_response_string( $url, $args, $response ) );
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns request and response information as string.
|
||||||
|
*
|
||||||
|
* @param string $url The request URL.
|
||||||
|
* @param array $args The request arguments.
|
||||||
|
* @param array $response The response.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function request_response_string( string $url, array $args, array $response ): string {
|
||||||
|
$method = $args['method'] ?? '';
|
||||||
|
$output = $method . ' ' . $url . "\n";
|
||||||
|
if ( isset( $args['body'] ) ) {
|
||||||
|
if ( ! in_array(
|
||||||
|
$url,
|
||||||
|
array(
|
||||||
|
trailingslashit( $this->host ) . 'v1/oauth2/token/',
|
||||||
|
trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials',
|
||||||
|
),
|
||||||
|
true
|
||||||
|
) ) {
|
||||||
|
$output .= 'Request Body: ' . wc_print_r( $args['body'], true ) . "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( isset( $response['headers']->getAll()['paypal-debug-id'] ) ) {
|
||||||
|
$output .= 'Response Debug ID: ' . $response['headers']->getAll()['paypal-debug-id'] . "\n";
|
||||||
|
}
|
||||||
|
if ( isset( $response['response'] ) ) {
|
||||||
|
$output .= 'Response: ' . wc_print_r( $response['response'], true ) . "\n";
|
||||||
|
|
||||||
|
if ( isset( $response['body'] )
|
||||||
|
&& isset( $response['response']['code'] )
|
||||||
|
&& ! in_array( $response['response']['code'], array( 200, 201, 202, 204 ), true ) ) {
|
||||||
|
$output .= 'Response Body: ' . wc_print_r( $response['body'], true ) . "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,53 +62,4 @@ class WooCommerceLogger implements LoggerInterface {
|
||||||
}
|
}
|
||||||
$this->wc_logger->log( $level, $message, $context );
|
$this->wc_logger->log( $level, $message, $context );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Logs request and response information.
|
|
||||||
*
|
|
||||||
* @param string $url The request URL.
|
|
||||||
* @param array $args The request arguments.
|
|
||||||
* @param array|WP_Error $response The response or WP_Error on failure.
|
|
||||||
* @param string $host The host.
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function logRequestResponse( string $url, array $args, $response, string $host ) {
|
|
||||||
|
|
||||||
if ( is_wp_error( $response ) ) {
|
|
||||||
$this->error( $response->get_error_code() . ' ' . $response->get_error_message() );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$method = $args['method'] ?? '';
|
|
||||||
$output = $method . ' ' . $url . "\n";
|
|
||||||
if ( isset( $args['body'] ) ) {
|
|
||||||
if ( ! in_array(
|
|
||||||
$url,
|
|
||||||
array(
|
|
||||||
trailingslashit( $host ) . 'v1/oauth2/token/',
|
|
||||||
trailingslashit( $host ) . 'v1/oauth2/token?grant_type=client_credentials',
|
|
||||||
),
|
|
||||||
true
|
|
||||||
) ) {
|
|
||||||
$output .= 'Request Body: ' . wc_print_r( $args['body'], true ) . "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( is_array( $response ) ) {
|
|
||||||
if ( isset( $response['headers']->getAll()['paypal-debug-id'] ) ) {
|
|
||||||
$output .= 'Response Debug ID: ' . $response['headers']->getAll()['paypal-debug-id'] . "\n";
|
|
||||||
}
|
|
||||||
if ( isset( $response['response'] ) ) {
|
|
||||||
$output .= 'Response: ' . wc_print_r( $response['response'], true ) . "\n";
|
|
||||||
|
|
||||||
if ( isset( $response['body'] )
|
|
||||||
&& isset( $response['response']['code'] )
|
|
||||||
&& ! in_array( $response['response']['code'], array( 200, 201, 202, 204 ), true ) ) {
|
|
||||||
$output .= 'Response Body: ' . wc_print_r( $response['body'], true ) . "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->info( $output );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue