Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7979 from stefandoorn/configurable-payment-descri…
…ption Payment description configurable with translation and placeholders (#7971)
- Loading branch information
Showing
9 changed files
with
213 additions
and
5 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
53 changes: 53 additions & 0 deletions
53
src/Sylius/Bundle/PayumBundle/Provider/PaymentDescriptionProvider.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,53 @@ | ||
<?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. | ||
*/ | ||
|
||
namespace Sylius\Bundle\PayumBundle\Provider; | ||
|
||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
use Symfony\Component\Translation\TranslatorInterface; | ||
|
||
/** | ||
* @author Stefan Doorn <stefan@efectos.nl> | ||
*/ | ||
final class PaymentDescriptionProvider implements PaymentDescriptionProviderInterface | ||
{ | ||
/** | ||
* @var TranslatorInterface | ||
*/ | ||
private $translator; | ||
|
||
/** | ||
* @param TranslatorInterface $translator | ||
*/ | ||
public function __construct(TranslatorInterface $translator) | ||
{ | ||
$this->translator = $translator; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPaymentDescription(PaymentInterface $payment) | ||
{ | ||
/** @var OrderInterface $order */ | ||
$order = $payment->getOrder(); | ||
|
||
return $this->translator->transChoice( | ||
'sylius.payum_action.payment.description', | ||
$order->getItems()->count(), | ||
[ | ||
'%items%' => $order->getItems()->count(), | ||
'%total%' => round($order->getTotal() / 100, 2), | ||
] | ||
); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Sylius/Bundle/PayumBundle/Provider/PaymentDescriptionProviderInterface.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,27 @@ | ||
<?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. | ||
*/ | ||
|
||
namespace Sylius\Bundle\PayumBundle\Provider; | ||
|
||
use Sylius\Component\Core\Model\PaymentInterface; | ||
|
||
/** | ||
* @author Stefan Doorn <stefan@efectos.nl> | ||
*/ | ||
interface PaymentDescriptionProviderInterface | ||
{ | ||
/** | ||
* @param PaymentInterface $payment | ||
* | ||
* @return string | ||
*/ | ||
public function getPaymentDescription(PaymentInterface $payment); | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/Sylius/Bundle/PayumBundle/Resources/config/services/provider.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,12 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="sylius.payment_description_provider" class="Sylius\Bundle\PayumBundle\Provider\PaymentDescriptionProvider"> | ||
<argument type="service" id="translator" /> | ||
</service> | ||
</services> | ||
</container> |
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
59 changes: 59 additions & 0 deletions
59
src/Sylius/Bundle/PayumBundle/spec/Action/CapturePaymentActionSpec.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,59 @@ | ||
<?php | ||
|
||
namespace spec\Sylius\Bundle\PayumBundle\Action; | ||
|
||
use Payum\Core\Action\GatewayAwareAction; | ||
use Payum\Core\Bridge\Spl\ArrayObject; | ||
use Payum\Core\GatewayInterface; | ||
use Payum\Core\Request\Capture; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Sylius\Bundle\PayumBundle\Action\CapturePaymentAction; | ||
use Sylius\Bundle\PayumBundle\Provider\PaymentDescriptionProviderInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
|
||
/** | ||
* @author Stefan Doorn <stefan@efectos.nl> | ||
*/ | ||
final class CapturePaymentActionSpec extends ObjectBehavior | ||
{ | ||
function let(PaymentDescriptionProviderInterface $paymentDescriptionProvider) | ||
{ | ||
$this->beConstructedWith($paymentDescriptionProvider); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(CapturePaymentAction::class); | ||
} | ||
|
||
function it_extends_gateway_aware_action() | ||
{ | ||
$this->shouldHaveType(GatewayAwareAction::class); | ||
} | ||
|
||
function it_should_throw_exception_when_unsupported_request(Capture $capture) | ||
{ | ||
$this->shouldThrow('\Payum\Core\Exception\RequestNotSupportedException')->duringExecute($capture); | ||
} | ||
|
||
function it_should_perform_basic_capture( | ||
PaymentDescriptionProviderInterface $paymentDescriptionProvider, | ||
GatewayInterface $gateway, | ||
Capture $capture, | ||
PaymentInterface $payment, | ||
OrderInterface $order | ||
) { | ||
$this->setGateway($gateway); | ||
|
||
$payment->getOrder()->willReturn($order); | ||
$payment->getDetails()->willReturn([]); | ||
$capture->getModel()->willReturn($payment); | ||
|
||
$payment->setDetails([])->shouldBeCalled(); | ||
$capture->setModel(new ArrayObject())->shouldBeCalled(); | ||
|
||
$this->execute($capture); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Sylius/Bundle/PayumBundle/spec/Provider/PaymentDescriptionProviderSpec.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,42 @@ | ||
<?php | ||
|
||
namespace spec\Sylius\Bundle\PayumBundle\Provider; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Sylius\Bundle\PayumBundle\Provider\PaymentDescriptionProvider; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\OrderItem; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
use Symfony\Component\Translation\TranslatorInterface; | ||
|
||
/** | ||
* @author Stefan Doorn <stefan@efectos.nl> | ||
*/ | ||
final class PaymentDescriptionProviderSpec extends ObjectBehavior | ||
{ | ||
function let(TranslatorInterface $translator) | ||
{ | ||
$translator->transChoice('sylius.payum_action.payment.description', 2, [ | ||
'%items%' => 2, | ||
'%total%' => 100.00, | ||
])->willReturn('Payment contains 2 items for a total of 100'); | ||
|
||
$this->beConstructedWith($translator); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(PaymentDescriptionProvider::class); | ||
} | ||
|
||
function it_should_generate_a_description_string(PaymentInterface $payment, OrderInterface $order) | ||
{ | ||
$order->getItems()->willReturn(new ArrayCollection([new OrderItem(), new OrderItem()])); | ||
$order->getTotal()->willReturn(10000); | ||
$payment->getOrder()->willReturn($order); | ||
|
||
$this->getPaymentDescription($payment)->shouldReturn('Payment contains 2 items for a total of 100'); | ||
} | ||
} |