2020-09-01 09:00:45 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* The RequestTrait wraps the wp_remote_get functionality for the API client.
|
|
|
|
*
|
2020-09-11 14:11:10 +03:00
|
|
|
* @package WooCommerce\PayPalCommerce\ApiClient\Endpoint
|
2020-09-01 09:00:45 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-09-11 14:11:10 +03:00
|
|
|
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
|
2020-09-01 09:00:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait RequestTrait
|
|
|
|
*/
|
|
|
|
trait RequestTrait {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a request
|
|
|
|
*
|
|
|
|
* @param string $url The URL to request.
|
|
|
|
* @param array $args The arguments by which to request.
|
|
|
|
*
|
|
|
|
* @return array|\WP_Error
|
|
|
|
*/
|
|
|
|
private function request( string $url, array $args ) {
|
|
|
|
|
2021-06-21 16:45:19 +02:00
|
|
|
$args['timeout'] = 30;
|
|
|
|
|
2020-09-01 09:00:45 +03:00
|
|
|
/**
|
|
|
|
* This filter can be used to alter the request args.
|
|
|
|
* For example, during testing, the PayPal-Mock-Response header could be
|
|
|
|
* added here.
|
|
|
|
*/
|
|
|
|
$args = apply_filters( 'ppcp_request_args', $args, $url );
|
|
|
|
if ( ! isset( $args['headers']['PayPal-Partner-Attribution-Id'] ) ) {
|
|
|
|
$args['headers']['PayPal-Partner-Attribution-Id'] = 'Woo_PPCP';
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:38:58 +02:00
|
|
|
$response = wp_remote_get( $url, $args );
|
|
|
|
|
|
|
|
$this->logger->log( 'info', '--------------------------------------------------------------------' );
|
|
|
|
$this->logger->log( 'info', 'URL: ' . wc_print_r( $url, true ) );
|
|
|
|
if ( isset( $args['method'] ) ) {
|
|
|
|
$this->logger->log( 'info', 'Method: ' . wc_print_r( $args['method'], true ) );
|
|
|
|
}
|
2021-09-13 15:38:34 +02:00
|
|
|
if ( isset( $args['body'] ) ) {
|
|
|
|
$this->logger->log( 'info', 'Request Body: ' . wc_print_r( $args['body'], true ) );
|
2021-09-13 13:38:58 +02:00
|
|
|
}
|
2021-09-13 15:38:34 +02:00
|
|
|
|
|
|
|
if ( ! is_wp_error( $response ) ) {
|
|
|
|
if ( isset( $response['headers']->getAll()['paypal-debug-id'] ) ) {
|
|
|
|
$this->logger->log( 'info', 'Response Debug ID: ' . wc_print_r( $response['headers']->getAll()['paypal-debug-id'], true ) );
|
|
|
|
}
|
|
|
|
if ( isset( $response['response'] ) ) {
|
|
|
|
$this->logger->log( 'info', 'Response: ' . wc_print_r( $response['response'], true ) );
|
|
|
|
}
|
|
|
|
if ( isset( $response['body'] ) ) {
|
|
|
|
$this->logger->log( 'info', 'Response Body: ' . wc_print_r( $response['body'], true ) );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->logger->log( 'error', 'WP Error: ' . wc_print_r( $response->get_error_code() . ' ' . $response->get_error_message(), true ) );
|
2021-09-13 13:38:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
2020-09-01 09:00:45 +03:00
|
|
|
}
|
|
|
|
}
|