diff --git a/modules/ppcp-api-client/src/ApiModule.php b/modules/ppcp-api-client/src/ApiModule.php index 7a07be123..62e18373f 100644 --- a/modules/ppcp-api-client/src/ApiModule.php +++ b/modules/ppcp-api-client/src/ApiModule.php @@ -43,6 +43,20 @@ class ApiModule implements ModuleInterface { WC()->session->set( 'ppcp_fees', $fees ); } ); + add_filter( + 'ppcp_create_order_request_body_data', + function( array $data ) use ( $c ) { + + foreach ( $data['purchase_units'] as $purchase_unit_index => $purchase_unit ) { + foreach ( $purchase_unit['items'] as $item_index => $item ) { + $data['purchase_units'][ $purchase_unit_index ]['items'][ $item_index ]['name'] = + apply_filters( 'woocommerce_paypal_payments_cart_line_item_name', $item['name'], $item['cart_item_key'] ); + } + } + + return $data; + } + ); add_action( 'woocommerce_paypal_payments_paypal_order_created', function ( Order $order ) use ( $c ) { diff --git a/modules/ppcp-api-client/src/Endpoint/WebhookEndpoint.php b/modules/ppcp-api-client/src/Endpoint/WebhookEndpoint.php index 6ae411cfa..3df4c3def 100644 --- a/modules/ppcp-api-client/src/Endpoint/WebhookEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/WebhookEndpoint.php @@ -202,7 +202,15 @@ class WebhookEndpoint { $status_code = (int) wp_remote_retrieve_response_code( $response ); if ( 204 !== $status_code ) { - $json = json_decode( $response['body'] ) ?? null; + $json = null; + /** + * Use in array as consistency check. + * + * @psalm-suppress RedundantConditionGivenDocblockType + */ + if ( is_array( $response ) ) { + $json = json_decode( $response['body'] ); + } throw new PayPalApiException( $json, $status_code diff --git a/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php b/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php index 443fd1cbd..1fb10ed8c 100644 --- a/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php +++ b/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php @@ -116,6 +116,13 @@ class OrderProcessor { */ private $order_helper; + /** + * Array to store temporary order data changes to restore after processing. + * + * @var array + */ + private $restore_order_data = array(); + /** * OrderProcessor constructor. * @@ -292,8 +299,12 @@ class OrderProcessor { * @return Order */ public function patch_order( \WC_Order $wc_order, Order $order ): Order { + $this->apply_outbound_order_filters( $wc_order ); $updated_order = $this->order_factory->from_wc_order( $wc_order, $order ); - $order = $this->order_endpoint->patch_order_with( $order, $updated_order ); + $this->restore_order_from_filters( $wc_order ); + + $order = $this->order_endpoint->patch_order_with( $order, $updated_order ); + return $order; } @@ -323,4 +334,48 @@ class OrderProcessor { true ); } + + /** + * Applies filters to the WC_Order, so they are reflected only on PayPal Order. + * + * @param WC_Order $wc_order The WoocOmmerce Order. + * @return void + */ + private function apply_outbound_order_filters( WC_Order $wc_order ): void { + $items = $wc_order->get_items(); + + $this->restore_order_data['names'] = array(); + + foreach ( $items as $item ) { + if ( ! $item instanceof \WC_Order_Item ) { + continue; + } + + $original_name = $item->get_name(); + $new_name = apply_filters( 'woocommerce_paypal_payments_order_line_item_name', $original_name, $item->get_id(), $wc_order->get_id() ); + + if ( $new_name !== $original_name ) { + $this->restore_order_data['names'][ $item->get_id() ] = $original_name; + $item->set_name( $new_name ); + } + } + } + + /** + * Restores the WC_Order to it's state before filters. + * + * @param WC_Order $wc_order The WooCommerce Order. + * @return void + */ + private function restore_order_from_filters( WC_Order $wc_order ): void { + if ( is_array( $this->restore_order_data['names'] ?? null ) ) { + foreach ( $this->restore_order_data['names'] as $wc_item_id => $original_name ) { + $wc_item = $wc_order->get_item( $wc_item_id, false ); + + if ( $wc_item ) { + $wc_item->set_name( $original_name ); + } + } + } + } } diff --git a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php index 32a2366ca..97ceca9b5 100644 --- a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php +++ b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php @@ -57,6 +57,7 @@ class OrderProcessorTest extends TestCase ->andReturn($payments); $wcOrder = Mockery::mock(\WC_Order::class); + $wcOrder->expects('get_items')->andReturn([]); $wcOrder->expects('update_meta_data') ->with(PayPalGateway::ORDER_PAYMENT_MODE_META_KEY, 'live'); $wcOrder->shouldReceive('get_id')->andReturn(1); @@ -193,7 +194,8 @@ class OrderProcessorTest extends TestCase ->andReturn($payments); $wcOrder = Mockery::mock(\WC_Order::class); - $orderStatus = Mockery::mock(OrderStatus::class); + $wcOrder->expects('get_items')->andReturn([]); + $orderStatus = Mockery::mock(OrderStatus::class); $orderStatus ->shouldReceive('is') ->with(OrderStatus::APPROVED)