-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Api] Change payment method after checkout
- Loading branch information
Showing
10 changed files
with
256 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/Sylius/Bundle/ApiBundle/Command/ChangePaymentMethod.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Command; | ||
|
||
/** @experimental */ | ||
class ChangePaymentMethod implements OrderTokenValueAwareInterface, SubresourceIdAwareInterface | ||
{ | ||
/** @var string|null */ | ||
public $orderTokenValue; | ||
|
||
/** | ||
* @psalm-immutable | ||
* | ||
* @var string | ||
*/ | ||
public $paymentId; | ||
|
||
/** | ||
* @psalm-immutable | ||
* | ||
* @var string | ||
*/ | ||
public $paymentMethodCode; | ||
|
||
public function __construct(string $paymentMethodCode) | ||
{ | ||
$this->paymentMethodCode = $paymentMethodCode; | ||
} | ||
|
||
public function getOrderTokenValue(): ?string | ||
{ | ||
return $this->orderTokenValue; | ||
} | ||
|
||
public function setOrderTokenValue(?string $orderTokenValue): void | ||
{ | ||
$this->orderTokenValue = $orderTokenValue; | ||
} | ||
|
||
public function getSubresourceId(): ?string | ||
{ | ||
return $this->paymentId; | ||
} | ||
|
||
public function setSubresourceId(?string $subresourceId): void | ||
{ | ||
$this->paymentId = $subresourceId; | ||
} | ||
|
||
public function getSubresourceIdAttributeKey(): string | ||
{ | ||
return 'paymentId'; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/Sylius/Bundle/ApiBundle/CommandHandler/ChangePaymentMethodHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\CommandHandler; | ||
|
||
use SM\Factory\FactoryInterface; | ||
use Sylius\Bundle\ApiBundle\Command\ChangePaymentMethod; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\Component\Core\OrderCheckoutTransitions; | ||
use Sylius\Component\Core\Repository\OrderRepositoryInterface; | ||
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface; | ||
use Sylius\Component\Core\Repository\PaymentRepositoryInterface; | ||
use Sylius\Component\Payment\Model\PaymentInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** @experimental */ | ||
final class ChangePaymentMethodHandler | ||
{ | ||
/** @var OrderRepositoryInterface */ | ||
private $orderRepository; | ||
|
||
/** @var PaymentMethodRepositoryInterface */ | ||
private $paymentMethodRepository; | ||
|
||
/** @var PaymentRepositoryInterface */ | ||
private $paymentRepository; | ||
|
||
/** @var FactoryInterface */ | ||
private $stateMachineFactory; | ||
|
||
public function __construct( | ||
OrderRepositoryInterface $orderRepository, | ||
PaymentMethodRepositoryInterface $paymentMethodRepository, | ||
PaymentRepositoryInterface $paymentRepository, | ||
FactoryInterface $stateMachineFactory | ||
) { | ||
$this->orderRepository = $orderRepository; | ||
$this->paymentMethodRepository = $paymentMethodRepository; | ||
$this->paymentRepository = $paymentRepository; | ||
$this->stateMachineFactory = $stateMachineFactory; | ||
} | ||
|
||
public function __invoke(ChangePaymentMethod $changePaymentMethod): OrderInterface | ||
{ | ||
/** @var OrderInterface|null $order */ | ||
$order = $this->orderRepository->findOneBy(['tokenValue' => $changePaymentMethod->orderTokenValue]); | ||
|
||
Assert::notNull($order, 'Order has not been found.'); | ||
|
||
/** @var PaymentMethodInterface|null $paymentMethod */ | ||
$paymentMethod = $this->paymentMethodRepository->findOneBy([ | ||
'code' => $changePaymentMethod->paymentMethodCode, | ||
]); | ||
Assert::notNull($paymentMethod, 'Payment method has not been found'); | ||
|
||
$payment = $this->paymentRepository->findOneByOrderId($changePaymentMethod->paymentId, $order->getId()); | ||
Assert::notNull($payment, 'Can not find payment with given identifier.'); | ||
if ($payment->getState() === PaymentInterface::STATE_NEW) { | ||
$payment->setMethod($paymentMethod); | ||
} | ||
|
||
return $order; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Commands/ChoosePaymentMethod.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<!-- | ||
This file is part of the Sylius package. | ||
(c) Paweł Jędrzejewski | ||
For the full copyright and license information, please view the LICENSE | ||
file that was distributed with this source code. | ||
--> | ||
|
||
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd" | ||
> | ||
<class name="Sylius\Bundle\ApiBundle\Command\ChangePaymentMethod"> | ||
<attribute name="paymentMethodCode"> | ||
<group>order:change_payment_method</group> | ||
</attribute> | ||
</class> | ||
</serializer> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters