Refactor patch request

This commit is contained in:
Alex P 2023-04-26 16:41:47 +03:00
parent 47d6933288
commit f396870610
No known key found for this signature in database
GPG key ID: 54487A734A204D71
4 changed files with 32 additions and 21 deletions

View file

@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\AuthorizationStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PatchCollection;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer; use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentMethod; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentMethod;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
@ -514,13 +515,22 @@ class OrderEndpoint {
return $order_to_update; return $order_to_update;
} }
$this->patch( $order_to_update->id(), $patches );
$new_order = $this->order( $order_to_update->id() );
return $new_order;
}
/**
* Patches an order.
*
* @param string $order_id The PayPal order ID.
* @param PatchCollection $patches The patches.
*
* @throws RuntimeException If the request fails.
*/
public function patch( string $order_id, PatchCollection $patches ): void {
$patches_array = $patches->to_array(); $patches_array = $patches->to_array();
if ( ! isset( $patches_array[0]['value']['shipping'] ) ) {
$shipping = isset( $order_to_update->purchase_units()[0] ) && null !== $order_to_update->purchase_units()[0]->shipping() ? $order_to_update->purchase_units()[0]->shipping() : null;
if ( $shipping ) {
$patches_array[0]['value']['shipping'] = $shipping->to_array();
}
}
/** /**
* The filter can be used to modify the order patching request body data (the final prices, items). * The filter can be used to modify the order patching request body data (the final prices, items).
@ -528,7 +538,7 @@ class OrderEndpoint {
$patches_array = apply_filters( 'ppcp_patch_order_request_body_data', $patches_array ); $patches_array = apply_filters( 'ppcp_patch_order_request_body_data', $patches_array );
$bearer = $this->bearer->bearer(); $bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v2/checkout/orders/' . $order_to_update->id(); $url = trailingslashit( $this->host ) . 'v2/checkout/orders/' . $order_id;
$args = array( $args = array(
'method' => 'PATCH', 'method' => 'PATCH',
'headers' => array( 'headers' => array(
@ -544,11 +554,8 @@ class OrderEndpoint {
$response = $this->request( $url, $args ); $response = $this->request( $url, $args );
if ( is_wp_error( $response ) ) { if ( is_wp_error( $response ) ) {
$error = new RuntimeException( $error = new RuntimeException( 'Could not patch order.' );
__( 'Could not retrieve order.', 'woocommerce-paypal-payments' ) $this->logger->warning(
);
$this->logger->log(
'warning',
$error->getMessage(), $error->getMessage(),
array( array(
'args' => $args, 'args' => $args,
@ -564,8 +571,7 @@ class OrderEndpoint {
$json, $json,
$status_code $status_code
); );
$this->logger->log( $this->logger->warning(
'warning',
$error->getMessage(), $error->getMessage(),
array( array(
'args' => $args, 'args' => $args,
@ -574,9 +580,6 @@ class OrderEndpoint {
); );
throw $error; throw $error;
} }
$new_order = $this->order( $order_to_update->id() );
return $new_order;
} }
/** /**

View file

@ -83,8 +83,8 @@ class Patch {
public function to_array(): array { public function to_array(): array {
return array( return array(
'op' => $this->op(), 'op' => $this->op(),
'value' => $this->value(),
'path' => $this->path(), 'path' => $this->path(),
'value' => $this->value(),
); );
} }

View file

@ -71,7 +71,15 @@ class PatchCollectionFactory {
); );
$operation = $purchase_unit_from ? 'replace' : 'add'; $operation = $purchase_unit_from ? 'replace' : 'add';
$value = $purchase_unit_to->to_array(); $value = $purchase_unit_to->to_array();
$patches[] = new Patch(
if ( ! isset( $value['shipping'] ) ) {
$shipping = $purchase_unit_from && null !== $purchase_unit_from->shipping() ? $purchase_unit_from->shipping() : null;
if ( $shipping ) {
$value['shipping'] = $shipping->to_array();
}
}
$patches[] = new Patch(
$operation, $operation,
$path . "/@reference_id=='" . $purchase_unit_to->reference_id() . "'", $path . "/@reference_id=='" . $purchase_unit_to->reference_id() . "'",
$value $value

View file

@ -647,7 +647,7 @@ class OrderEndpointTest extends TestCase
$intent = 'CAPTURE'; $intent = 'CAPTURE';
$logger = Mockery::mock(LoggerInterface::class); $logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log'); $logger->shouldReceive('warning');
$logger->shouldReceive('debug'); $logger->shouldReceive('debug');
$applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class);
$subscription_helper = Mockery::mock(SubscriptionHelper::class); $subscription_helper = Mockery::mock(SubscriptionHelper::class);
@ -742,7 +742,7 @@ class OrderEndpointTest extends TestCase
$intent = 'CAPTURE'; $intent = 'CAPTURE';
$logger = Mockery::mock(LoggerInterface::class); $logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log'); $logger->shouldReceive('warning');
$logger->shouldReceive('debug'); $logger->shouldReceive('debug');
$applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class);