mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Add API for refunding and voiding orders
This commit is contained in:
parent
70d9cfbea0
commit
5dce62fa78
4 changed files with 300 additions and 41 deletions
88
tests/PHPUnit/Api/OrderRefundTest.php
Normal file
88
tests/PHPUnit/Api/OrderRefundTest.php
Normal file
|
@ -0,0 +1,88 @@
|
|||
<?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;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
|
||||
|
||||
class OrderRefundTest extends ModularTestCase
|
||||
{
|
||||
private $refundProcessor;
|
||||
|
||||
private $orderEndpoint;
|
||||
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->refundProcessor = Mockery::mock(RefundProcessor::class);
|
||||
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
|
||||
|
||||
$this->bootstrapModule([
|
||||
'wcgateway.processor.refunds' => function () {
|
||||
return $this->refundProcessor;
|
||||
},
|
||||
'api.endpoint.order' => function () {
|
||||
return $this->orderEndpoint;
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
public function testSuccess(): 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();
|
||||
|
||||
$this->refundProcessor
|
||||
->expects('refund')
|
||||
->once();
|
||||
|
||||
ppcp_refund_order($wcOrder, 42.0, 'reason');
|
||||
}
|
||||
|
||||
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_refund_order($wcOrder, 42.0, 'reason');
|
||||
}
|
||||
|
||||
public function testFailure(): 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();
|
||||
|
||||
$this->refundProcessor
|
||||
->expects('refund')
|
||||
->andThrow(new RuntimeException())
|
||||
->once();
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
ppcp_refund_order($wcOrder, 42.0, 'reason');
|
||||
}
|
||||
}
|
88
tests/PHPUnit/Api/OrderVoidTest.php
Normal file
88
tests/PHPUnit/Api/OrderVoidTest.php
Normal file
|
@ -0,0 +1,88 @@
|
|||
<?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;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
|
||||
|
||||
class OrderVoidTest extends ModularTestCase
|
||||
{
|
||||
private $refundProcessor;
|
||||
|
||||
private $orderEndpoint;
|
||||
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->refundProcessor = Mockery::mock(RefundProcessor::class);
|
||||
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
|
||||
|
||||
$this->bootstrapModule([
|
||||
'wcgateway.processor.refunds' => function () {
|
||||
return $this->refundProcessor;
|
||||
},
|
||||
'api.endpoint.order' => function () {
|
||||
return $this->orderEndpoint;
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
public function testSuccess(): 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();
|
||||
|
||||
$this->refundProcessor
|
||||
->expects('void')
|
||||
->once();
|
||||
|
||||
ppcp_void_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_void_order($wcOrder);
|
||||
}
|
||||
|
||||
public function testFailure(): 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();
|
||||
|
||||
$this->refundProcessor
|
||||
->expects('void')
|
||||
->andThrow(new RuntimeException())
|
||||
->once();
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
ppcp_void_order($wcOrder);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue