mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-30 05:00:51 +08:00
Add API for retrieving paypal order
This commit is contained in:
parent
9ba93c9e11
commit
70d9cfbea0
2 changed files with 109 additions and 0 deletions
|
@ -15,10 +15,36 @@ use Exception;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
use WC_Order;
|
use WC_Order;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
||||||
use WooCommerce\PayPalCommerce\PPCP;
|
use WooCommerce\PayPalCommerce\PPCP;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
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.
|
* Captures the PayPal order.
|
||||||
*
|
*
|
||||||
|
|
83
tests/PHPUnit/Api/GetOrderTest.php
Normal file
83
tests/PHPUnit/Api/GetOrderTest.php
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace WooCommerce\PayPalCommerce\Api;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Mockery;
|
||||||
|
use RuntimeException;
|
||||||
|
use WC_Order;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
||||||
|
use WooCommerce\PayPalCommerce\ModularTestCase;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||||
|
|
||||||
|
class GetOrderTest extends ModularTestCase
|
||||||
|
{
|
||||||
|
private $orderEndpoint;
|
||||||
|
|
||||||
|
public function setUp(): void {
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue