Merge pull request #1578 from woocommerce/PCP-1512-pay-pal-fee-and-pay-pal-payout-do-not-change-on-order-if-we-do-partial-refund

PayPal fee and PayPal Payout do not change on order if we do partial refund (1512)
This commit is contained in:
Emili Castells 2023-08-30 11:05:34 +02:00 committed by GitHub
commit 2571d31ad8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 1508 additions and 81 deletions

View file

@ -0,0 +1,130 @@
<?php
namespace WooCommerce\PayPalCommerce\Api;
use Mockery;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ModularTestCase;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Helper\RefundFeesUpdater;
use Psr\Log\LoggerInterface;
use WC_Order;
use function Brain\Monkey\Functions\when;
class OrderRefundFeesUpdateTest extends ModularTestCase
{
private $order_endpoint;
private $logger;
private $refundFeesUpdater;
public function setUp(): void
{
$this->order_endpoint = $this->createMock(OrderEndpoint::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->refundFeesUpdater = new RefundFeesUpdater($this->order_endpoint, $this->logger);
}
public function testUpdateWithoutPaypalOrderId(): void
{
$wc_order_id = 123;
$wc_order = Mockery::mock(WC_Order::class);
$wc_order->expects('get_meta')
->with(PayPalGateway::ORDER_ID_META_KEY)
->andReturn(null);
$wc_order->expects('get_id')->andReturn($wc_order_id);
$this->logger->expects($this->once())
->method('error');
$this->refundFeesUpdater->update($wc_order);
}
public function testUpdateWithValidData(): void
{
$wc_order_id = 123;
$paypal_order_id = 'test_order_id';
$refund_id = 'XYZ123';
$meta_data = [
'gross_amount' => ['value' => 10.0, 'currency_code' => 'USD'],
'paypal_fee' => ['value' => 7.0, 'currency_code' => 'USD'],
'net_amount' => ['value' => 3.0, 'currency_code' => 'USD'],
];
when('get_comments')->justReturn([]);
$wc_order = Mockery::mock(WC_Order::class);
$wc_order->expects('get_meta')
->with(PayPalGateway::ORDER_ID_META_KEY)
->andReturn($paypal_order_id);
$wc_order->expects('get_id')
->times(3)
->andReturn($wc_order_id);
$wc_order->expects('update_meta_data')
->once()
->with('_ppcp_paypal_refund_fees', $meta_data);
$wc_order->expects('add_order_note')
->once()
->withArgs(function ($arg) use ($refund_id) {
return strpos($arg, $refund_id) !== false;
});
$wc_order->expects('save')->once();
$moneyGross = Mockery::mock(Money::class);
$moneyGross->expects('value')->once()->andReturn($meta_data['gross_amount']['value']);
$moneyGross->expects('currency_code')->once()->andReturn($meta_data['gross_amount']['currency_code']);
$moneyFee = Mockery::mock(Money::class);
$moneyFee->expects('value')->once()->andReturn($meta_data['paypal_fee']['value']);
$moneyFee->expects('currency_code')->once()->andReturn($meta_data['paypal_fee']['currency_code']);
$moneyNet = Mockery::mock(Money::class);
$moneyNet->expects('value')->once()->andReturn($meta_data['net_amount']['value']);
$moneyNet->expects('currency_code')->once()->andReturn($meta_data['net_amount']['currency_code']);
$breakdown = $this->getMockBuilder(\stdClass::class)
->addMethods(['gross_amount', 'paypal_fee', 'net_amount'])
->getMock();
$breakdown->method('gross_amount')->willReturn($moneyGross);
$breakdown->method('paypal_fee')->willReturn($moneyFee);
$breakdown->method('net_amount')->willReturn($moneyNet);
$refund = $this->getMockBuilder(\stdClass::class)
->addMethods(['id', 'seller_payable_breakdown'])
->getMock();
$refund->method('id')->willReturn($refund_id);
$refund->method('seller_payable_breakdown')->willReturn($breakdown);
$payments = $this->getMockBuilder(\stdClass::class)
->addMethods(['refunds'])
->getMock();
$payments->method('refunds')->willReturn([$refund]);
$purchase_unit = $this->getMockBuilder(\stdClass::class)
->addMethods(['payments'])
->getMock();
$purchase_unit->method('payments')->willReturn($payments);
$paypal_order = Mockery::mock(Order::class);
$paypal_order->expects('purchase_units')->andReturn([$purchase_unit]);
$this->order_endpoint->method('order')->with($paypal_order_id)->willReturn($paypal_order);
$this->logger->expects($this->exactly(2))
->method('debug')
->withConsecutive(
[$this->stringContains('Updating order paypal refund fees.')],
[$this->stringContains('Updated order paypal refund fees.')]
);
$this->refundFeesUpdater->update($wc_order);
}
}

View file

@ -43,10 +43,19 @@ class PaymentsTest extends TestCase
'status' => 'CREATED',
]
);
$captures = [$capture];
$authorizations = [$authorization];
$refund = \Mockery::mock(Refund::class);
$refund->shouldReceive('to_array')->andReturn(
[
'id' => 'refund',
'status' => 'CREATED',
]
);
$testee = new Payments($authorizations, $captures);
$authorizations = [$authorization];
$captures = [$capture];
$refunds = [$refund];
$testee = new Payments($authorizations, $captures, $refunds);
$this->assertEquals(
[
@ -62,6 +71,12 @@ class PaymentsTest extends TestCase
'status' => 'CREATED',
],
],
'refunds' => [
[
'id' => 'refund',
'status' => 'CREATED',
],
],
],
$testee->to_array()
);

View file

@ -7,6 +7,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payments;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Refund;
use WooCommerce\PayPalCommerce\TestCase;
use Mockery;
@ -18,11 +19,15 @@ class PaymentsFactoryTest extends TestCase
$authorization->shouldReceive('to_array')->andReturn(['id' => 'foo', 'status' => 'CREATED']);
$capture = Mockery::mock(Capture::class);
$capture->shouldReceive('to_array')->andReturn(['id' => 'capture', 'status' => 'CREATED']);
$refund = Mockery::mock(Refund::class);
$refund->shouldReceive('to_array')->andReturn(['id' => 'refund', 'status' => 'CREATED']);
$authorizationsFactory = Mockery::mock(AuthorizationFactory::class);
$authorizationsFactory->shouldReceive('from_paypal_response')->andReturn($authorization);
$captureFactory = Mockery::mock(CaptureFactory::class);
$captureFactory->shouldReceive('from_paypal_response')->andReturn($capture);
$refundFactory = Mockery::mock(RefundFactory::class);
$refundFactory->shouldReceive('from_paypal_response')->andReturn($refund);
$response = (object)[
'authorizations' => [
(object)['id' => 'foo', 'status' => 'CREATED'],
@ -30,9 +35,12 @@ class PaymentsFactoryTest extends TestCase
'captures' => [
(object)['id' => 'capture', 'status' => 'CREATED'],
],
'refunds' => [
(object)['id' => 'refund', 'status' => 'CREATED'],
],
];
$testee = new PaymentsFactory($authorizationsFactory, $captureFactory);
$testee = new PaymentsFactory($authorizationsFactory, $captureFactory, $refundFactory);
$result = $testee->from_paypal_response($response);
$this->assertInstanceOf(Payments::class, $result);
@ -44,6 +52,9 @@ class PaymentsFactoryTest extends TestCase
'captures' => [
['id' => 'capture', 'status' => 'CREATED'],
],
'refunds' => [
['id' => 'refund', 'status' => 'CREATED'],
],
];
$this->assertEquals($expectedToArray, $result->to_array());
}

View file

@ -43,11 +43,32 @@ class FeesRendererTest extends TestCase
],
]);
$wcOrder->expects('get_meta')
->with(PayPalGateway::REFUND_FEES_META_KEY)
->andReturn([
'gross_amount' => [
'currency_code' => 'USD',
'value' => '20.52',
],
'paypal_fee' => [
'currency_code' => 'USD',
'value' => '0.51',
],
'net_amount' => [
'currency_code' => 'USD',
'value' => '50.01',
],
]);
$result = $this->renderer->render($wcOrder);
$this->assertStringContainsString('Fee', $result);
$this->assertStringContainsString('0.41', $result);
$this->assertStringContainsString('Payout', $result);
$this->assertStringContainsString('10.01', $result);
$this->assertStringContainsString('PayPal Refund Fee', $result);
$this->assertStringContainsString('0.51', $result);
$this->assertStringContainsString('PayPal Refund', $result);
$this->assertStringContainsString('50.01', $result);
}
public function testRenderWithoutNet() {
@ -62,6 +83,10 @@ class FeesRendererTest extends TestCase
],
]);
$wcOrder->expects('get_meta')
->with(PayPalGateway::REFUND_FEES_META_KEY)
->andReturn([]);
$result = $this->renderer->render($wcOrder);
$this->assertStringContainsString('Fee', $result);
$this->assertStringContainsString('0.41', $result);
@ -78,6 +103,10 @@ class FeesRendererTest extends TestCase
->with(PayPalGateway::FEES_META_KEY)
->andReturn($meta);
$wcOrder->expects('get_meta')
->with(PayPalGateway::REFUND_FEES_META_KEY)
->andReturn([]);
$this->assertSame('', $this->renderer->render($wcOrder));
}