From f396870610b8c631561d0f1307d5b6b98e6c53b1 Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 26 Apr 2023 16:41:47 +0300 Subject: [PATCH] Refactor patch request --- .../src/Endpoint/OrderEndpoint.php | 37 ++++++++++--------- modules/ppcp-api-client/src/Entity/Patch.php | 2 +- .../src/Factory/PatchCollectionFactory.php | 10 ++++- .../ApiClient/Endpoint/OrderEndpointTest.php | 4 +- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php index c08b463bb..c0179d8e7 100644 --- a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php @@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\AuthorizationStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; +use WooCommerce\PayPalCommerce\ApiClient\Entity\PatchCollection; use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentMethod; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken; @@ -514,13 +515,22 @@ class OrderEndpoint { 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(); - 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). @@ -528,7 +538,7 @@ class OrderEndpoint { $patches_array = apply_filters( 'ppcp_patch_order_request_body_data', $patches_array ); $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( 'method' => 'PATCH', 'headers' => array( @@ -544,11 +554,8 @@ class OrderEndpoint { $response = $this->request( $url, $args ); if ( is_wp_error( $response ) ) { - $error = new RuntimeException( - __( 'Could not retrieve order.', 'woocommerce-paypal-payments' ) - ); - $this->logger->log( - 'warning', + $error = new RuntimeException( 'Could not patch order.' ); + $this->logger->warning( $error->getMessage(), array( 'args' => $args, @@ -564,8 +571,7 @@ class OrderEndpoint { $json, $status_code ); - $this->logger->log( - 'warning', + $this->logger->warning( $error->getMessage(), array( 'args' => $args, @@ -574,9 +580,6 @@ class OrderEndpoint { ); throw $error; } - - $new_order = $this->order( $order_to_update->id() ); - return $new_order; } /** diff --git a/modules/ppcp-api-client/src/Entity/Patch.php b/modules/ppcp-api-client/src/Entity/Patch.php index 4350565cf..8814532ea 100644 --- a/modules/ppcp-api-client/src/Entity/Patch.php +++ b/modules/ppcp-api-client/src/Entity/Patch.php @@ -83,8 +83,8 @@ class Patch { public function to_array(): array { return array( 'op' => $this->op(), - 'value' => $this->value(), 'path' => $this->path(), + 'value' => $this->value(), ); } diff --git a/modules/ppcp-api-client/src/Factory/PatchCollectionFactory.php b/modules/ppcp-api-client/src/Factory/PatchCollectionFactory.php index 3be240393..d6ae8b7fc 100644 --- a/modules/ppcp-api-client/src/Factory/PatchCollectionFactory.php +++ b/modules/ppcp-api-client/src/Factory/PatchCollectionFactory.php @@ -71,7 +71,15 @@ class PatchCollectionFactory { ); $operation = $purchase_unit_from ? 'replace' : 'add'; $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, $path . "/@reference_id=='" . $purchase_unit_to->reference_id() . "'", $value diff --git a/tests/PHPUnit/ApiClient/Endpoint/OrderEndpointTest.php b/tests/PHPUnit/ApiClient/Endpoint/OrderEndpointTest.php index fee542879..5041bf16a 100644 --- a/tests/PHPUnit/ApiClient/Endpoint/OrderEndpointTest.php +++ b/tests/PHPUnit/ApiClient/Endpoint/OrderEndpointTest.php @@ -647,7 +647,7 @@ class OrderEndpointTest extends TestCase $intent = 'CAPTURE'; $logger = Mockery::mock(LoggerInterface::class); - $logger->shouldReceive('log'); + $logger->shouldReceive('warning'); $logger->shouldReceive('debug'); $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $subscription_helper = Mockery::mock(SubscriptionHelper::class); @@ -742,7 +742,7 @@ class OrderEndpointTest extends TestCase $intent = 'CAPTURE'; $logger = Mockery::mock(LoggerInterface::class); - $logger->shouldReceive('log'); + $logger->shouldReceive('warning'); $logger->shouldReceive('debug'); $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class);