Skip to content

Commit

Permalink
Merge pull request #7979 from stefandoorn/configurable-payment-descri…
Browse files Browse the repository at this point in the history
…ption

Payment description configurable with translation and placeholders (#7971)
  • Loading branch information
lchrusciel committed May 9, 2017
2 parents f5f0a4f + 8722716 commit 73fb801
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/Sylius/Bundle/PayumBundle/Action/CapturePaymentAction.php
Expand Up @@ -17,12 +17,26 @@
use Payum\Core\Model\Payment as PayumPayment;
use Payum\Core\Request\Capture;
use Payum\Core\Request\Convert;
use Sylius\Bundle\PayumBundle\Provider\PaymentDescriptionProviderInterface;
use Sylius\Bundle\PayumBundle\Request\GetStatus;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface as SyliusPaymentInterface;

final class CapturePaymentAction extends GatewayAwareAction
{
/**
* @var PaymentDescriptionProviderInterface
*/
private $paymentDescriptionProvider;

/**
* @param PaymentDescriptionProviderInterface $paymentDescriptionProvider
*/
public function __construct(PaymentDescriptionProviderInterface $paymentDescriptionProvider)
{
$this->paymentDescriptionProvider = $paymentDescriptionProvider;
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -51,11 +65,7 @@ public function execute($request)
$payumPayment->setCurrencyCode($order->getCurrencyCode());
$payumPayment->setClientEmail($order->getCustomer()->getEmail());
$payumPayment->setClientId($order->getCustomer()->getId());
$payumPayment->setDescription(sprintf(
'Payment contains %d items for a total of %01.2f',
$order->getItems()->count(),
round($totalAmount / 100, 2)
));
$payumPayment->setDescription($this->paymentDescriptionProvider->getPaymentDescription($payment));
$payumPayment->setDetails($payment->getDetails());

$this->gateway->execute($convert = new Convert($payumPayment, 'array', $request->getToken()));
Expand Down
@@ -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),
]
);
}
}
@@ -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);
}
Expand Up @@ -21,6 +21,7 @@
<import resource="services/controller.xml" />
<import resource="services/extension.xml" />
<import resource="services/form.xml" />
<import resource="services/provider.xml" />
</imports>

<services>
Expand Down
Expand Up @@ -7,6 +7,7 @@
<services>
<!-- Generic -->
<service id="sylius.payum_action.capture_payment" class="Sylius\Bundle\PayumBundle\Action\CapturePaymentAction">
<argument type="service" id="sylius.payment_description_provider" />
<tag name="payum.action" all="true" alias="sylius.capture_payment" />
</service>
<service id="sylius.payum_action.execute_same_request_with_payment_details" class="Sylius\Bundle\PayumBundle\Action\ExecuteSameRequestWithPaymentDetailsAction">
Expand Down
@@ -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>
Expand Up @@ -15,3 +15,6 @@ sylius:
offline: Offline
paypal_express_checkout: Paypal express checkout
stripe_checkout: Stripe checkout
payum_action:
payment:
description: Payment contains %items% item for a total of %total%|Payment contains %items% items for a total of %total%
@@ -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);
}
}
@@ -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');
}
}

0 comments on commit 73fb801

Please sign in to comment.