Add ppcp_create_paypal_order_for_wc_order

This commit is contained in:
Alex P 2023-11-14 10:56:15 +02:00
parent a61e9303e9
commit b2ba72c06c
No known key found for this signature in database
GPG key ID: 54487A734A204D71
2 changed files with 66 additions and 0 deletions

View file

@ -21,6 +21,7 @@ use WooCommerce\PayPalCommerce\PPCP;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Helper\RefundFeesUpdater; use WooCommerce\PayPalCommerce\WcGateway\Helper\RefundFeesUpdater;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
/** /**
@ -47,6 +48,19 @@ function ppcp_get_paypal_order( $paypal_id_or_wc_order ): Order {
return $order_endpoint->order( $paypal_id_or_wc_order ); return $order_endpoint->order( $paypal_id_or_wc_order );
} }
/**
* Creates a PayPal order for the given WC order.
*
* @param WC_Order $wc_order The WC order.
* @throws Exception When the operation fails.
*/
function ppcp_create_paypal_order_for_wc_order( WC_Order $wc_order ): Order {
$order_processor = PPCP::container()->get( 'wcgateway.order-processor' );
assert( $order_processor instanceof OrderProcessor );
return $order_processor->create_order( $wc_order );
}
/** /**
* Captures the PayPal order. * Captures the PayPal order.
* *

View file

@ -0,0 +1,52 @@
<?php
namespace WooCommerce\PayPalCommerce\Api;
use Mockery;
use RuntimeException;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ModularTestCase;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
class CreateOrderForWcOrderTest extends ModularTestCase
{
private $orderProcesor;
public function setUp(): void {
parent::setUp();
$this->orderProcesor = Mockery::mock(OrderProcessor::class);
$this->bootstrapModule([
'wcgateway.order-processor' => function () {
return $this->orderProcesor;
},
]);
}
public function testSuccess(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$ret = Mockery::mock(Order::class);
$this->orderProcesor
->expects('create_order')
->with($wcOrder)
->andReturn($ret)
->once();
self::assertEquals($ret, ppcp_create_paypal_order_for_wc_order($wcOrder));
}
public function testFailure(): void {
$wcOrder = Mockery::mock(WC_Order::class);
$this->orderProcesor
->expects('create_order')
->with($wcOrder)
->andThrow(new RuntimeException())
->once();
$this->expectException(RuntimeException::class);
ppcp_create_paypal_order_for_wc_order($wcOrder);
}
}