mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-30 05:00:51 +08:00
Add MoneyFactory
This commit is contained in:
parent
7f7ae4c700
commit
5cbd84a050
4 changed files with 80 additions and 36 deletions
|
@ -27,6 +27,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\ApplicationContextFactory;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Factory\AuthorizationFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\CaptureFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\ItemFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\MoneyFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PatchCollectionFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayeeFactory;
|
||||
|
@ -280,9 +281,13 @@ return array(
|
|||
$item_factory = $container->get( 'api.factory.item' );
|
||||
return new AmountFactory(
|
||||
$item_factory,
|
||||
$container->get( 'api.factory.money' ),
|
||||
$container->get( 'api.shop.currency' )
|
||||
);
|
||||
},
|
||||
'api.factory.money' => static function ( ContainerInterface $container ): MoneyFactory {
|
||||
return new MoneyFactory();
|
||||
},
|
||||
'api.factory.payer' => static function ( ContainerInterface $container ): PayerFactory {
|
||||
$address_factory = $container->get( 'api.factory.address' );
|
||||
return new PayerFactory( $address_factory );
|
||||
|
|
|
@ -28,6 +28,13 @@ class AmountFactory {
|
|||
*/
|
||||
private $item_factory;
|
||||
|
||||
/**
|
||||
* The Money factory.
|
||||
*
|
||||
* @var MoneyFactory
|
||||
*/
|
||||
private $money_factory;
|
||||
|
||||
/**
|
||||
* 3-letter currency code of the shop.
|
||||
*
|
||||
|
@ -38,12 +45,14 @@ class AmountFactory {
|
|||
/**
|
||||
* AmountFactory constructor.
|
||||
*
|
||||
* @param ItemFactory $item_factory The Item factory.
|
||||
* @param string $currency 3-letter currency code of the shop.
|
||||
* @param ItemFactory $item_factory The Item factory.
|
||||
* @param MoneyFactory $money_factory The Money factory.
|
||||
* @param string $currency 3-letter currency code of the shop.
|
||||
*/
|
||||
public function __construct( ItemFactory $item_factory, string $currency ) {
|
||||
$this->item_factory = $item_factory;
|
||||
$this->currency = $currency;
|
||||
public function __construct( ItemFactory $item_factory, MoneyFactory $money_factory, string $currency ) {
|
||||
$this->item_factory = $item_factory;
|
||||
$this->money_factory = $money_factory;
|
||||
$this->currency = $currency;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -169,16 +178,7 @@ class AmountFactory {
|
|||
* @throws RuntimeException When JSON object is malformed.
|
||||
*/
|
||||
public function from_paypal_response( \stdClass $data ): Amount {
|
||||
if ( ! isset( $data->value ) || ! is_numeric( $data->value ) ) {
|
||||
throw new RuntimeException( __( 'No value given', 'woocommerce-paypal-payments' ) );
|
||||
}
|
||||
if ( ! isset( $data->currency_code ) ) {
|
||||
throw new RuntimeException(
|
||||
__( 'No currency given', 'woocommerce-paypal-payments' )
|
||||
);
|
||||
}
|
||||
|
||||
$money = new Money( (float) $data->value, $data->currency_code );
|
||||
$money = $this->money_factory->from_paypal_response( $data );
|
||||
$breakdown = ( isset( $data->breakdown ) ) ? $this->break_down( $data->breakdown ) : null;
|
||||
return new Amount( $money, $breakdown );
|
||||
}
|
||||
|
|
39
modules/ppcp-api-client/src/Factory/MoneyFactory.php
Normal file
39
modules/ppcp-api-client/src/Factory/MoneyFactory.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
/**
|
||||
* The Money factory.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use stdClass;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Class MoneyFactory
|
||||
*/
|
||||
class MoneyFactory {
|
||||
|
||||
/**
|
||||
* Returns a Money object based off a PayPal Response.
|
||||
*
|
||||
* @param stdClass $data The JSON object.
|
||||
*
|
||||
* @return Money
|
||||
* @throws RuntimeException When JSON object is malformed.
|
||||
*/
|
||||
public function from_paypal_response( stdClass $data ): Money {
|
||||
if ( ! isset( $data->value ) || ! is_numeric( $data->value ) ) {
|
||||
throw new RuntimeException( 'No money value given' );
|
||||
}
|
||||
if ( ! isset( $data->currency_code ) ) {
|
||||
throw new RuntimeException( 'No currency given' );
|
||||
}
|
||||
|
||||
return new Money( (float) $data->value, $data->currency_code );
|
||||
}
|
||||
}
|
|
@ -15,11 +15,21 @@ class AmountFactoryTest extends TestCase
|
|||
{
|
||||
private $currency = 'EUR';
|
||||
|
||||
public function testFromWcCartDefault()
|
||||
{
|
||||
$itemFactory = Mockery::mock(ItemFactory::class);
|
||||
$testee = new AmountFactory($itemFactory, $this->currency);
|
||||
private $itemFactory;
|
||||
private $moneyFactory;
|
||||
private $testee;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->itemFactory = Mockery::mock(ItemFactory::class);
|
||||
$this->moneyFactory = new MoneyFactory();
|
||||
$this->testee = new AmountFactory($this->itemFactory, $this->moneyFactory, $this->currency);
|
||||
}
|
||||
|
||||
public function testFromWcCartDefault()
|
||||
{
|
||||
$cart = Mockery::mock(\WC_Cart::class);
|
||||
$cart
|
||||
->shouldReceive('get_total')
|
||||
|
@ -53,7 +63,7 @@ class AmountFactoryTest extends TestCase
|
|||
$woocommerce->session = $session;
|
||||
$session->shouldReceive('get')->andReturn([]);
|
||||
|
||||
$result = $testee->from_wc_cart($cart);
|
||||
$result = $this->testee->from_wc_cart($cart);
|
||||
$this->assertEquals($this->currency, $result->currency_code());
|
||||
$this->assertEquals((float) 1, $result->value());
|
||||
$this->assertEquals((float) 10, $result->breakdown()->discount()->value());
|
||||
|
@ -68,10 +78,6 @@ class AmountFactoryTest extends TestCase
|
|||
|
||||
public function testFromWcCartNoDiscount()
|
||||
{
|
||||
$itemFactory = Mockery::mock(ItemFactory::class);
|
||||
$testee = new AmountFactory($itemFactory, $this->currency);
|
||||
|
||||
$expectedCurrency = 'EUR';
|
||||
$expectedTotal = 1;
|
||||
$cart = Mockery::mock(\WC_Cart::class);
|
||||
$cart
|
||||
|
@ -105,13 +111,12 @@ class AmountFactoryTest extends TestCase
|
|||
when('WC')->justReturn($woocommerce);
|
||||
$woocommerce->session = $session;
|
||||
$session->shouldReceive('get')->andReturn([]);
|
||||
$result = $testee->from_wc_cart($cart);
|
||||
$result = $this->testee->from_wc_cart($cart);
|
||||
$this->assertNull($result->breakdown()->discount());
|
||||
}
|
||||
|
||||
public function testFromWcOrderDefault()
|
||||
{
|
||||
$itemFactory = Mockery::mock(ItemFactory::class);
|
||||
$order = Mockery::mock(\WC_Order::class);
|
||||
$unitAmount = Mockery::mock(Money::class);
|
||||
$unitAmount
|
||||
|
@ -131,11 +136,10 @@ class AmountFactoryTest extends TestCase
|
|||
$item
|
||||
->shouldReceive('tax')
|
||||
->andReturn($tax);
|
||||
$itemFactory
|
||||
$this->itemFactory
|
||||
->expects('from_wc_order')
|
||||
->with($order)
|
||||
->andReturn([$item]);
|
||||
$testee = new AmountFactory($itemFactory, $this->currency);
|
||||
|
||||
$order
|
||||
->shouldReceive('get_total')
|
||||
|
@ -154,7 +158,7 @@ class AmountFactoryTest extends TestCase
|
|||
->with(false)
|
||||
->andReturn(3);
|
||||
|
||||
$result = $testee->from_wc_order($order);
|
||||
$result = $this->testee->from_wc_order($order);
|
||||
$this->assertEquals((float) 3, $result->breakdown()->discount()->value());
|
||||
$this->assertEquals((float) 6, $result->breakdown()->item_total()->value());
|
||||
$this->assertEquals((float) 1.5, $result->breakdown()->shipping()->value());
|
||||
|
@ -169,7 +173,6 @@ class AmountFactoryTest extends TestCase
|
|||
|
||||
public function testFromWcOrderDiscountIsNull()
|
||||
{
|
||||
$itemFactory = Mockery::mock(ItemFactory::class);
|
||||
$order = Mockery::mock(\WC_Order::class);
|
||||
$unitAmount = Mockery::mock(Money::class);
|
||||
$unitAmount
|
||||
|
@ -189,11 +192,10 @@ class AmountFactoryTest extends TestCase
|
|||
$item
|
||||
->shouldReceive('tax')
|
||||
->andReturn($tax);
|
||||
$itemFactory
|
||||
$this->itemFactory
|
||||
->expects('from_wc_order')
|
||||
->with($order)
|
||||
->andReturn([$item]);
|
||||
$testee = new AmountFactory($itemFactory, $this->currency);
|
||||
|
||||
$order
|
||||
->shouldReceive('get_total')
|
||||
|
@ -212,7 +214,7 @@ class AmountFactoryTest extends TestCase
|
|||
->with(false)
|
||||
->andReturn(0);
|
||||
|
||||
$result = $testee->from_wc_order($order);
|
||||
$result = $this->testee->from_wc_order($order);
|
||||
$this->assertNull($result->breakdown()->discount());
|
||||
}
|
||||
|
||||
|
@ -222,12 +224,10 @@ class AmountFactoryTest extends TestCase
|
|||
*/
|
||||
public function testFromPayPalResponse($response, $expectsException)
|
||||
{
|
||||
$itemFactory = Mockery::mock(ItemFactory::class);
|
||||
$testee = new AmountFactory($itemFactory, $this->currency);
|
||||
if ($expectsException) {
|
||||
$this->expectException(RuntimeException::class);
|
||||
}
|
||||
$result = $testee->from_paypal_response($response);
|
||||
$result = $this->testee->from_paypal_response($response);
|
||||
if ($expectsException) {
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue