Skip to content

Commit

Permalink
[RefundPayment] Change order number to Order relation on RefundPayment
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed Jun 9, 2021
1 parent 7c0fb07 commit 91173ec
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 26 deletions.
10 changes: 7 additions & 3 deletions spec/Entity/RefundPaymentSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
namespace spec\Sylius\RefundPlugin\Entity;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\RefundPlugin\Entity\RefundPayment;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;

final class RefundPaymentSpec extends ObjectBehavior
{
public function let(PaymentMethodInterface $paymentMethod): void
public function let(OrderInterface $order, PaymentMethodInterface $paymentMethod): void
{
$this->beConstructedWith('000002', 100, 'USD', RefundPaymentInterface::STATE_NEW, $paymentMethod);
$this->beConstructedWith($order, 100, 'USD', RefundPaymentInterface::STATE_NEW, $paymentMethod);
}

public function it_is_initializable(): void
Expand All @@ -40,8 +41,11 @@ public function it_has_no_id_by_default(): void
$this->getId()->shouldReturn(null);
}

public function it_has_order_number(): void
public function it_has_an_order(OrderInterface $order): void
{
$order->getNumber()->willReturn('000002');

$this->getOrder()->shouldReturn($order);
$this->getOrderNumber()->shouldReturn('000002');
}

Expand Down
23 changes: 13 additions & 10 deletions spec/Factory/RefundPaymentFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace spec\Sylius\RefundPlugin\Factory;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Sylius\RefundPlugin\Entity\RefundPayment;
Expand All @@ -40,19 +41,21 @@ public function it_implements_refund_payment_factory_interface(): void

public function it_creates_new_refund_payment(
PaymentMethodRepositoryInterface $paymentMethodRepository,
PaymentMethodInterface $paymentMethod
PaymentMethodInterface $paymentMethod,
OrderInterface $order
): void {
$paymentMethodRepository->find(1)->willReturn($paymentMethod);

$this->createWithData(
'0002',
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
1
)->shouldBeLike(
new RefundPayment(
'0002',
$this
->createWithData(
$order,
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
1
)
->shouldBeLike(new RefundPayment(
$order->getWrappedObject(),
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
Expand Down
14 changes: 11 additions & 3 deletions spec/ProcessManager/RefundPaymentProcessManagerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use Doctrine\ORM\EntityManagerInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;
use Sylius\RefundPlugin\Event\RefundPaymentGenerated;
use Sylius\RefundPlugin\Event\UnitsRefunded;
Expand All @@ -33,13 +35,15 @@ public function let(
OrderFullyRefundedStateResolverInterface $orderFullyRefundedStateResolver,
RelatedPaymentIdProviderInterface $relatedPaymentIdProvider,
RefundPaymentFactoryInterface $refundPaymentFactory,
OrderRepositoryInterface $orderRepository,
EntityManagerInterface $entityManager,
MessageBusInterface $eventBus
): void {
$this->beConstructedWith(
$orderFullyRefundedStateResolver,
$relatedPaymentIdProvider,
$refundPaymentFactory,
$orderRepository,
$entityManager,
$eventBus
);
Expand All @@ -54,12 +58,16 @@ public function it_reacts_on_units_refunded_event_and_creates_refund_payment(
OrderFullyRefundedStateResolverInterface $orderFullyRefundedStateResolver,
RelatedPaymentIdProviderInterface $relatedPaymentIdProvider,
RefundPaymentFactoryInterface $refundPaymentFactory,
OrderRepositoryInterface $orderRepository,
EntityManagerInterface $entityManager,
MessageBusInterface $eventBus,
RefundPaymentInterface $refundPayment,
MessageBusInterface $eventBus
OrderInterface $order
): void {
$orderRepository->findOneByNumber('000222')->willReturn($order);

$refundPaymentFactory->createWithData(
'000222',
$order,
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
Expand All @@ -72,7 +80,7 @@ public function it_reacts_on_units_refunded_event_and_creates_refund_payment(
$orderFullyRefundedStateResolver->resolve('000222')->shouldBeCalled();

$refundPayment->getId()->willReturn(10);
$refundPayment->getOrderNumber()->willReturn('000222');
$refundPayment->getOrder()->willReturn($order);
$refundPayment->getAmount()->willReturn(1000);

$relatedPaymentIdProvider->getForRefundPayment($refundPayment)->willReturn(3);
Expand Down
16 changes: 11 additions & 5 deletions src/Entity/RefundPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@

namespace Sylius\RefundPlugin\Entity;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;

class RefundPayment implements RefundPaymentInterface
{
/** @var int|null */
protected $id;

/** @var string */
protected $orderNumber;
/** @var OrderInterface */
protected $order;

/** @var int */
protected $amount;
Expand All @@ -36,13 +37,13 @@ class RefundPayment implements RefundPaymentInterface
protected $paymentMethod;

public function __construct(
string $orderNumber,
OrderInterface $order,
int $amount,
string $currencyCode,
string $state,
PaymentMethodInterface $paymentMethod
) {
$this->orderNumber = $orderNumber;
$this->order = $order;
$this->amount = $amount;
$this->currencyCode = $currencyCode;
$this->state = $state;
Expand All @@ -54,9 +55,14 @@ public function getId(): ?int
return $this->id;
}

public function getOrder(): OrderInterface
{
return $this->order;
}

public function getOrderNumber(): string
{
return $this->orderNumber;
return $this->getOrder()->getNumber();
}

public function getAmount(): int
Expand Down
4 changes: 4 additions & 0 deletions src/Entity/RefundPaymentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\RefundPlugin\Entity;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Resource\Model\ResourceInterface;

Expand All @@ -22,6 +23,9 @@ interface RefundPaymentInterface extends ResourceInterface

public const STATE_COMPLETED = 'completed';

public function getOrder(): OrderInterface;

/** @deprecated this function is deprecated and will be removed in v1.0. Use RefundPaymentInterface::getOrder() instead */
public function getOrderNumber(): string;

public function getAmount(): int;
Expand Down
5 changes: 3 additions & 2 deletions src/Factory/RefundPaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\RefundPlugin\Factory;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Sylius\RefundPlugin\Entity\RefundPayment;
Expand All @@ -29,7 +30,7 @@ public function __construct(PaymentMethodRepositoryInterface $paymentMethodRepos
}

public function createWithData(
string $orderNumber,
OrderInterface $order,
int $amount,
string $currencyCode,
string $state,
Expand All @@ -38,6 +39,6 @@ public function createWithData(
/** @var PaymentMethodInterface $paymentMethod */
$paymentMethod = $this->paymentMethodRepository->find($paymentMethodId);

return new RefundPayment($orderNumber, $amount, $currencyCode, $state, $paymentMethod);
return new RefundPayment($order, $amount, $currencyCode, $state, $paymentMethod);
}
}
3 changes: 2 additions & 1 deletion src/Factory/RefundPaymentFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

namespace Sylius\RefundPlugin\Factory;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;

interface RefundPaymentFactoryInterface
{
public function createWithData(
string $orderNumber,
OrderInterface $order,
int $amount,
string $currencyCode,
string $state,
Expand Down
14 changes: 13 additions & 1 deletion src/ProcessManager/RefundPaymentProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
namespace Sylius\RefundPlugin\ProcessManager;

use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;
use Sylius\RefundPlugin\Event\RefundPaymentGenerated;
use Sylius\RefundPlugin\Event\UnitsRefunded;
use Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface;
use Sylius\RefundPlugin\Provider\RelatedPaymentIdProviderInterface;
use Sylius\RefundPlugin\StateResolver\OrderFullyRefundedStateResolverInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Webmozart\Assert\Assert;

final class RefundPaymentProcessManager implements UnitsRefundedProcessStepInterface
{
Expand All @@ -33,6 +36,9 @@ final class RefundPaymentProcessManager implements UnitsRefundedProcessStepInter
/** @var RefundPaymentFactoryInterface */
private $refundPaymentFactory;

/** @var OrderRepositoryInterface */
private $orderRepository;

/** @var EntityManagerInterface */
private $entityManager;

Expand All @@ -43,20 +49,26 @@ public function __construct(
OrderFullyRefundedStateResolverInterface $orderFullyRefundedStateResolver,
RelatedPaymentIdProviderInterface $relatedPaymentIdProvider,
RefundPaymentFactoryInterface $refundPaymentFactory,
OrderRepositoryInterface $orderRepository,
EntityManagerInterface $entityManager,
MessageBusInterface $eventBus
) {
$this->orderFullyRefundedStateResolver = $orderFullyRefundedStateResolver;
$this->relatedPaymentIdProvider = $relatedPaymentIdProvider;
$this->refundPaymentFactory = $refundPaymentFactory;
$this->orderRepository = $orderRepository;
$this->entityManager = $entityManager;
$this->eventBus = $eventBus;
}

public function next(UnitsRefunded $unitsRefunded): void
{
/** @var OrderInterface|null $order */
$order = $this->orderRepository->findOneByNumber($unitsRefunded->orderNumber());
Assert::notNull($order);

$refundPayment = $this->refundPaymentFactory->createWithData(
$unitsRefunded->orderNumber(),
$order,
$unitsRefunded->amount(),
$unitsRefunded->currencyCode(),
RefundPaymentInterface::STATE_NEW,
Expand Down
5 changes: 4 additions & 1 deletion src/Resources/config/doctrine/RefundPayment.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
<generator strategy="AUTO" />
</id>

<field name="orderNumber" column="order_number" type="string" />
<field name="amount" column="amount" type="integer" />
<field name="currencyCode" column="currency_code" type="string" />
<field name="state" column="state" type="string" />

<many-to-one field="paymentMethod" target-entity="Sylius\Component\Payment\Model\PaymentMethodInterface">
<join-column name="payment_method_id" referenced-column-name="id" nullable="true" />
</many-to-one>

<many-to-one field="order" target-entity="Sylius\Component\Order\Model\OrderInterface">
<join-column name="order_id" />
</many-to-one>
</mapped-superclass>
</doctrine-mapping>
1 change: 1 addition & 0 deletions src/Resources/config/services/event_bus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<argument type="service" id="Sylius\RefundPlugin\StateResolver\OrderFullyRefundedStateResolverInterface" />
<argument type="service" id="Sylius\RefundPlugin\Provider\RelatedPaymentIdProviderInterface" />
<argument type="service" id="Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface" />
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="doctrine.orm.default_entity_manager" />
<argument type="service" id="sylius.event_bus" />
<tag name="sylius_refund.units_refunded.process_step" priority="50" />
Expand Down

0 comments on commit 91173ec

Please sign in to comment.