mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Merge branch 'trunk' into PCP-1877-improve-line-item-mismatch-handling
This commit is contained in:
commit
fcaf4d495e
121 changed files with 6058 additions and 498 deletions
130
tests/PHPUnit/Api/OrderRefundFeesUpdateTest.php
Normal file
130
tests/PHPUnit/Api/OrderRefundFeesUpdateTest.php
Normal 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);
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
);
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -58,6 +58,7 @@ class OnboardingUrlTest extends TestCase
|
|||
// Expectations
|
||||
$this->cache->shouldReceive('has')->once()->andReturn(true);
|
||||
$this->cache->shouldReceive('get')->once()->andReturn($cacheData);
|
||||
$this->cache->shouldReceive('set')->once();
|
||||
$this->cache->shouldReceive('delete')->once();
|
||||
|
||||
$this->assertTrue(
|
||||
|
|
|
@ -25,6 +25,7 @@ use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
|
|||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
use function Brain\Monkey\Functions\when;
|
||||
|
||||
class RenewalHandlerTest extends TestCase
|
||||
{
|
||||
|
@ -115,6 +116,9 @@ class RenewalHandlerTest extends TestCase
|
|||
->shouldReceive('payment_source')
|
||||
->andReturn(null);
|
||||
|
||||
$wcOrder
|
||||
->shouldReceive('get_meta')
|
||||
->andReturn('');
|
||||
$wcOrder
|
||||
->shouldReceive('get_id')
|
||||
->andReturn(1);
|
||||
|
@ -154,6 +158,8 @@ class RenewalHandlerTest extends TestCase
|
|||
->with([$purchaseUnit], 'no_shipping', $payer, $token)
|
||||
->andReturn($order);
|
||||
|
||||
when('wcs_get_subscriptions_for_order')->justReturn(array());
|
||||
|
||||
$wcOrder->shouldReceive('update_status');
|
||||
$wcOrder->shouldReceive('save');
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@ class TestCase extends \PHPUnit\Framework\TestCase
|
|||
when('wc_clean')->returnArg();
|
||||
when('get_transient')->returnArg();
|
||||
when('delete_transient')->returnArg();
|
||||
when('wcs_get_subscription')->returnArg();
|
||||
|
||||
setUp();
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue