Merge branch 'refs/heads/trunk' into modularity-module-migration

# Conflicts:
#	modules/ppcp-applepay/src/ApplepayModule.php
#	modules/ppcp-compat/src/CompatModule.php
This commit is contained in:
Moritz Meißelbach 2024-08-23 15:50:49 +02:00
commit 789d979c80
No known key found for this signature in database
GPG key ID: 9FDCE7BEB31FA3E5
113 changed files with 11616 additions and 2360 deletions

View file

@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\ClientCredentials;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Orders;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentMethodTokensEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentTokensEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\CardAuthenticationResult;
@ -240,6 +241,13 @@ return array(
$bn_code
);
},
'api.endpoint.orders' => static function ( ContainerInterface $container ): Orders {
return new Orders(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'api.endpoint.billing-agreements' => static function ( ContainerInterface $container ): BillingAgreementsEndpoint {
return new BillingAgreementsEndpoint(
$container->get( 'api.host' ),
@ -1444,7 +1452,6 @@ return array(
'CN' => array(
'mastercard' => array(),
'visa' => array(),
'amex' => array(),
),
'CY' => array(
'mastercard' => array(),

View file

@ -0,0 +1,108 @@
<?php
/**
* Orders API endpoints.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Endpoint
* @link https://developer.paypal.com/docs/api/orders/v2/ Orders API documentation.
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Psr\Log\LoggerInterface;
use RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WP_Error;
/**
* Class Orders
*/
class Orders {
use RequestTrait;
/**
* The host.
*
* @var string
*/
private $host;
/**
* The bearer.
*
* @var Bearer
*/
private $bearer;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
/**
* Orders constructor.
*
* @param string $host The host.
* @param Bearer $bearer The bearer.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
string $host,
Bearer $bearer,
LoggerInterface $logger
) {
$this->host = $host;
$this->bearer = $bearer;
$this->logger = $logger;
}
/**
* Creates a PayPal order.
*
* @param array $request_body The request body.
* @param array $headers The request headers.
* @return array
* @throws RuntimeException If something went wrong with the request.
* @throws PayPalApiException If something went wrong with the PayPal API request.
*/
public function create( array $request_body, array $headers = array() ): array {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v2/checkout/orders';
$default_headers = array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
'PayPal-Request-Id' => uniqid( 'ppcp-', true ),
);
$headers = array_merge(
$default_headers,
$headers
);
$args = array(
'method' => 'POST',
'headers' => $headers,
'body' => wp_json_encode( $request_body ),
);
$response = $this->request( $url, $args );
if ( $response instanceof WP_Error ) {
throw new RuntimeException( $response->get_error_message() );
}
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( $status_code !== 200 ) {
throw new PayPalApiException(
json_decode( $response['body'] ),
$status_code
);
}
return $response;
}
}