Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store gateway config in payment. #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/Payum/Server/Api/ApiControllerProvider.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
<?php
namespace Payum\Server\Api;

use Payum\Core\Bridge\Symfony\Reply\HttpResponse;
use Payum\Core\Reply\ReplyInterface;
use Payum\Server\Api\Controller\GatewayController;
use Payum\Server\Api\Controller\GatewayMetaController;
use Payum\Server\Api\Controller\PaymentController;
use Payum\Server\Api\Controller\RootController;
use Payum\Server\Api\Controller\TokenController;
use Payum\Server\ReplyToJsonResponseConverter;
use Silex\Application as SilexApplication;
use Silex\ControllerCollection;
use Silex\ServiceProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ApiControllerProvider implements ServiceProviderInterface
{
Expand All @@ -33,7 +27,8 @@ public function register(SilexApplication $app)
$app['api.view.payment_to_json_converter'],
$app['form.factory'],
$app['api.view.form_to_json_converter'],
$app['url_generator']
$app['url_generator'],
$app['payum.gateway_config_storage']
);
});

Expand Down
2 changes: 1 addition & 1 deletion src/Payum/Server/Api/ApiProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ApiProvider implements ServiceProviderInterface
public function register(SilexApplication $app)
{
$app['api.view.payment_to_json_converter'] = function() use ($app) {
return new PaymentToJsonConverter($app['payum']);
return new PaymentToJsonConverter($app['payum'], $app['api.view.gateway_config_to_json_converter']);
};

$app['api.view.token_to_json_converter'] = function() use ($app) {
Expand Down
50 changes: 19 additions & 31 deletions src/Payum/Server/Api/Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Payum;
use Payum\Core\Registry\RegistryInterface;
use Payum\Core\Request\Convert;
use Payum\Core\Security\Util\Random;
use Payum\Core\Storage\StorageInterface;
use Payum\Server\Api\View\FormToJsonConverter;
Expand Down Expand Up @@ -46,25 +45,33 @@ class PaymentController
*/
private $urlGenerator;

/**
* @var StorageInterface
*/
private $gatewayConfigStorage;

/**
* @param Payum $payum
* @param PaymentToJsonConverter $paymentToJsonConverter
* @param FormFactoryInterface $formFactory
* @param FormToJsonConverter $formToJsonConverter
* @param UrlGeneratorInterface $urlGenerator
* @param StorageInterface $gatewayConfigStorage
*/
public function __construct(
Payum $payum,
PaymentToJsonConverter $paymentToJsonConverter,
FormFactoryInterface $formFactory,
FormToJsonConverter $formToJsonConverter,
UrlGeneratorInterface $urlGenerator
UrlGeneratorInterface $urlGenerator,
StorageInterface $gatewayConfigStorage
) {
$this->payum = $payum;
$this->formFactory = $formFactory;
$this->formToJsonConverter = $formToJsonConverter;
$this->paymentToJsonConverter = $paymentToJsonConverter;
$this->urlGenerator = $urlGenerator;
$this->gatewayConfigStorage = $gatewayConfigStorage;
}

/**
Expand All @@ -87,42 +94,20 @@ public function createAction($content, Request $request)
/** @var Payment $payment */
$payment = $form->getData();
$payment->setId(Random::generateToken());

$storage = $this->payum->getStorage($payment);
$storage->update($payment);

$payment->setNumber($payment->getNumber() ?: date('Ymd-'.mt_rand(10000, 99999)));

$storage->update($payment);

// TODO
$payment->setValue('links', 'done', 'http://dev.payum-server.com/client/index.html');

$payment->setValue('links', 'self', $this->urlGenerator->generate('payment_get', ['id' => $payment->getId()], true));

$token = $this->payum->getTokenFactory()->createAuthorizeToken($payment->getGatewayName(), $payment, $payment->getValue('links', 'done'), [
'payum_token' => null,
'payment' => $payment->getId(),
]);
$payment->setValue('links', 'authorize', $token->getTargetUrl());

$token = $this->payum->getTokenFactory()->createCaptureToken($payment->getGatewayName(), $payment, $payment->getValue('links', 'done'), [
'payum_token' => null,
'payment' => $payment->getId(),
]);
$payment->setValue('links', 'capture', $token->getTargetUrl());

$token = $this->payum->getTokenFactory()->createNotifyToken($payment->getGatewayName(), $payment);
$payment->setValue('links', 'notify', $token->getTargetUrl());
if ($payment->getGatewayConfig()->getGatewayName()) {
$payment->setGatewayConfig($this->gatewayConfigStorage->find($payment->getGatewayConfig()->getGatewayName()));
}

$storage->update($payment);
$this->payum->getStorage($payment)->update($payment);

return new JsonResponse(
array(
'payment' => $this->paymentToJsonConverter->convert($payment),
),
201,
array('Location' => $payment->getValue('links', 'self'))
array('Location' => $this->urlGenerator->generate('payment_get', ['id' => $payment->getId()], true))
);
}

Expand All @@ -148,8 +133,11 @@ public function updateAction($content, Request $request)
/** @var Payment $payment */
$payment = $form->getData();

$storage = $this->payum->getStorage($payment);
$storage->update($payment);
if ($payment->getGatewayConfig()->getGatewayName()) {
$payment->setGatewayConfig($this->gatewayConfigStorage->find($payment->getGatewayConfig()->getGatewayName()));
}

$this->payum->getStorage($payment)->update($payment);

return new JsonResponse(array(
'payment' => $this->paymentToJsonConverter->convert($payment),
Expand Down
23 changes: 10 additions & 13 deletions src/Payum/Server/Api/View/PaymentToJsonConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Payum\Server\Api\View;

use Payum\Core\Registry\GatewayRegistryInterface;
use Payum\Core\Request\GetHumanStatus;
use Payum\Server\Model\Payment;

class PaymentToJsonConverter
Expand All @@ -12,12 +11,19 @@ class PaymentToJsonConverter
*/
private $registry;

/**
* @var GatewayConfigToJsonConverter
*/
private $gatewayConfigToJsonConverter;

/**
* @param GatewayRegistryInterface $registry
* @param GatewayConfigToJsonConverter $gatewayConfigToJsonConverter
*/
public function __construct(GatewayRegistryInterface $registry)
public function __construct(GatewayRegistryInterface $registry, GatewayConfigToJsonConverter $gatewayConfigToJsonConverter)
{
$this->registry = $registry;
$this->gatewayConfigToJsonConverter = $gatewayConfigToJsonConverter;
}

/**
Expand All @@ -27,26 +33,17 @@ public function __construct(GatewayRegistryInterface $registry)
*/
public function convert(Payment $payment)
{
$normalizedPayment = [
return [
'id' => $payment->getId(),
'status' => $payment->getStatus(),
'gatewayName' => $payment->getGatewayName(),
'number' => $payment->getNumber(),
'totalAmount' => $payment->getTotalAmount(),
'currencyCode' => $payment->getCurrencyCode(),
'clientEmail' => $payment->getClientEmail(),
'clientId' => $payment->getClientId(),
'description' => $payment->getDescription(),
'details' => $payment->getDetails(),
'_links' => [],
'gateway' => $this->gatewayConfigToJsonConverter->convert($payment->getGatewayConfig()),
];

foreach (['self', 'done', 'capture', 'authorize', 'notify'] as $name) {
if ($link = $payment->getValue('links', $name)) {
$normalizedPayment['_links'][$name] = ['href' => $link];
}
}

return $normalizedPayment;
}
}
42 changes: 42 additions & 0 deletions src/Payum/Server/Form/Type/CreatePaymentGatewayConfigType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace Payum\Server\Form\Type;

use Payum\Server\Model\GatewayConfig;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CreatePaymentGatewayConfigType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('gatewayName', 'payum_gateways_choice', [
'required' => false,
'empty_value' => '',
])
;
}

/**
* {@inheritDoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => GatewayConfig::class,
'allow_extra_fields' => true,
));
}

/**
* {@inheritDoc}
*/
public function getName()
{
return 'create_payment_gateway_config';
}
}
5 changes: 1 addition & 4 deletions src/Payum/Server/Form/Type/CreatePaymentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ class CreatePaymentType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('gatewayName', 'payum_gateways_choice', [
'required' => false,
'empty_value' => '',
])
->add('gatewayConfig', 'create_payment_gateway_config')
->add('totalAmount', 'number', array(
'label' => 'Amount',
'constraints' => array(new NotBlank(), new Type(['type' => 'numeric']))
Expand Down
32 changes: 18 additions & 14 deletions src/Payum/Server/Model/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,30 +180,34 @@ public function setCreditCard(CreditCardInterface $creditCard)
}

/**
* @return string
* @return Payer
*/
public function getGatewayName()
public function getPayer()
{
return $this->getSelfValue('gatewayName');
if (false == $this->getValue('self', 'payer')) {
$this->setObject('self', 'payer', new Payer());
}

return $this->getObject('self', 'payer', Payer::class);
}

/**
* @param string $gatewayName
* @return GatewayConfig
*/
public function setGatewayName($gatewayName)
public function getGatewayConfig()
{
$this->setSelfValue('gatewayName', $gatewayName);
if (false == $this->getValue('self', 'gatewayConfig')) {
$this->setObject('self', 'gatewayConfig', new GatewayConfig());
}

return $this->getObject('self', 'gatewayConfig', GatewayConfig::class);
}

/**
* @return Payer
* @param GatewayConfig $gatewayConfig
*/
public function getPayer()
public function setGatewayConfig(GatewayConfig $gatewayConfig)
{
if (false == $this->getValue('self', 'payer')) {
$this->setObject('self', 'payer', new Payer());
}

return $this->getObject('self', 'payer', Payer::class);
$this->setObject('self', 'gatewayConfig', $gatewayConfig);
}
}
}
2 changes: 1 addition & 1 deletion src/Payum/Server/Model/SecurityToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function getGatewayName()
{
$payment = $this->getPayment();

return $payment ? $payment->getGatewayName() : null;
return $payment ? $payment->getGatewayConfig()->getGatewayName() : null;
}

/**
Expand Down
13 changes: 7 additions & 6 deletions src/Payum/Server/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@
use Doctrine\MongoDB\Database;
use Payum\Core\Bridge\Symfony\Reply\HttpResponse;
use Payum\Core\Bridge\Twig\TwigFactory;
use Payum\Core\Exception\LogicException;
use Payum\Core\Model\GatewayConfigInterface;
use Payum\Core\Payum;
use Payum\Core\PayumBuilder;
use Payum\Core\Reply\ReplyInterface;
use Payum\Core\Security\TokenInterface;
use Payum\Core\Storage\StorageInterface;
use Payum\Server\Action\AuthorizePaymentAction;
use Payum\Server\Action\CapturePaymentAction;
use Payum\Server\Action\ExecuteSameRequestWithPaymentDetailsAction;
use Payum\Server\Action\ObtainMissingDetailsAction;
use Payum\Server\Action\ObtainMissingDetailsForBe2BillAction;
use Payum\Server\Controller\AuthorizeController;
use Payum\Server\Controller\CaptureController;
use Payum\Server\Extension\UpdatePaymentStatusExtension;
use Payum\Server\Form\Type\ChooseGatewayType;
use Payum\Server\Form\Type\CreatePaymentGatewayConfigType;
use Payum\Server\Form\Type\CreatePaymentType;
use Payum\Server\Form\Type\CreateTokenType;
use Payum\Server\Form\Type\UpdatePaymentType;
Expand All @@ -35,7 +32,6 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Constraints\NotBlank;

class ServiceProvider implements ServiceProviderInterface
{
Expand Down Expand Up @@ -102,6 +98,7 @@ public function register(SilexApplication $app)
$app['form.types'] = $app->share($app->extend('form.types', function ($types) use ($app) {
$types[] = new CreatePaymentType();
$types[] = new UpdatePaymentType();
$types[] = new CreatePaymentGatewayConfigType();
$types[] = new CreateTokenType();
$types[] = new ChooseGatewayType();

Expand Down Expand Up @@ -156,12 +153,16 @@ public function register(SilexApplication $app)
/** @var FormFactoryInterface $formFactory */
$formFactory = $app['form.factory'];

$form = $formFactory->createNamed('', 'choose_gateway', $payment, [
$form = $formFactory->createNamed('', 'choose_gateway', [
'action' => $token->getTargetUrl(),
]);

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var StorageInterface $gatewayConfigStorage */
$gatewayConfigStorage = $app['payum.gateway_config_storage'];
$payment->setGatewayConfig($gatewayConfigStorage->find($form->getData()['gatewayName']));

$payum->getStorage($payment)->update($payment);
} else {
/** @var \Twig_Environment $twig */
Expand Down
Loading