mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
83 lines
2.3 KiB
PHP
83 lines
2.3 KiB
PHP
<?php
|
|
/**
|
|
* The RequestTrait wraps the wp_remote_get functionality for the API client.
|
|
*
|
|
* @package WooCommerce\PayPalCommerce\ApiClient\Endpoint
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
|
|
|
|
/**
|
|
* 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 ) {
|
|
|
|
$args['timeout'] = 30;
|
|
|
|
/**
|
|
* 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';
|
|
}
|
|
|
|
$response = wp_remote_get( $url, $args );
|
|
$this->logger->debug( $this->request_response_string( $url, $args, $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;
|
|
}
|
|
}
|