From 70d9cfbea0c3983006e480a01b7c40ed758413dc Mon Sep 17 00:00:00 2001 From: Alex P Date: Fri, 17 Mar 2023 10:23:34 +0200 Subject: [PATCH] Add API for retrieving paypal order --- api/order-functions.php | 26 ++++++++++ tests/PHPUnit/Api/GetOrderTest.php | 83 ++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 tests/PHPUnit/Api/GetOrderTest.php diff --git a/api/order-functions.php b/api/order-functions.php index cb15478de..9a8ee4ecd 100644 --- a/api/order-functions.php +++ b/api/order-functions.php @@ -15,10 +15,36 @@ use Exception; use InvalidArgumentException; use RuntimeException; use WC_Order; +use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; +use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\PPCP; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; +/** + * Returns the PayPal order. + * + * @param string|WC_Order $paypal_id_or_wc_order The ID of PayPal order or a WC order (with the ID in meta). + * @throws InvalidArgumentException When the argument cannot be used for retrieving the order. + * @throws Exception When the operation fails. + */ +function ppcp_get_paypal_order( $paypal_id_or_wc_order ): Order { + if ( $paypal_id_or_wc_order instanceof WC_Order ) { + $paypal_id_or_wc_order = $paypal_id_or_wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY ); + if ( ! $paypal_id_or_wc_order ) { + throw new InvalidArgumentException( 'PayPal order ID not found in meta.' ); + } + } + if ( ! is_string( $paypal_id_or_wc_order ) ) { + throw new InvalidArgumentException( 'Invalid PayPal order ID, string expected.' ); + } + + $order_endpoint = PPCP::container()->get( 'api.endpoint.order' ); + assert( $order_endpoint instanceof OrderEndpoint ); + + return $order_endpoint->order( $paypal_id_or_wc_order ); +} + /** * Captures the PayPal order. * diff --git a/tests/PHPUnit/Api/GetOrderTest.php b/tests/PHPUnit/Api/GetOrderTest.php new file mode 100644 index 000000000..362276249 --- /dev/null +++ b/tests/PHPUnit/Api/GetOrderTest.php @@ -0,0 +1,83 @@ +orderEndpoint = Mockery::mock(OrderEndpoint::class); + + $this->bootstrapModule([ + 'api.endpoint.order' => function () { + return $this->orderEndpoint; + }, + ]); + } + + public function testSuccess(): void { + $this->orderEndpoint + ->expects('order') + ->with('123abc') + ->andReturn(Mockery::mock(Order::class)) + ->once(); + + ppcp_get_paypal_order('123abc'); + } + + public function testSuccessWithOrder(): void { + $wcOrder = Mockery::mock(WC_Order::class); + $wcOrder->expects('get_meta') + ->with(PayPalGateway::ORDER_ID_META_KEY) + ->andReturn('123abc'); + + $this->orderEndpoint + ->expects('order') + ->with('123abc') + ->andReturn(Mockery::mock(Order::class)) + ->once(); + + ppcp_get_paypal_order($wcOrder); + } + + public function testOrderWithoutId(): void { + $wcOrder = Mockery::mock(WC_Order::class); + $wcOrder->expects('get_meta') + ->with(PayPalGateway::ORDER_ID_META_KEY) + ->andReturn(false); + + $this->expectException(InvalidArgumentException::class); + + ppcp_get_paypal_order($wcOrder); + } + + public function testFailure(): void { + $this->orderEndpoint + ->expects('order') + ->with('123abc') + ->andThrow(new RuntimeException()) + ->once(); + + $this->expectException(RuntimeException::class); + + ppcp_get_paypal_order('123abc'); + } + + public function testInvalidId(): void { + $this->expectException(InvalidArgumentException::class); + + ppcp_get_paypal_order(123); + } +}