Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Oct 29, 2015
1 parent e07cf75 commit b0e417e
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 171 deletions.
6 changes: 3 additions & 3 deletions src/Payum/Silex/Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class AuthorizeController extends PayumController
*/
public function doAction(Request $request)
{
$token = $this->httpRequestVerifier->verify($request);
$token = $this->payum->getHttpRequestVerifier()->verify($request);

$gateway = $this->registry->getGateway($token->getGatewayName());
$gateway = $this->payum->getGateway($token->getGatewayName());
$gateway->execute(new Authorize($token));

$this->httpRequestVerifier->invalidate($token);
$this->payum->getHttpRequestVerifier()->invalidate($token);

return new RedirectResponse($token->getAfterUrl());
}
Expand Down
6 changes: 3 additions & 3 deletions src/Payum/Silex/Controller/CaptureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class CaptureController extends PayumController
*/
public function doAction(Request $request)
{
$token = $this->httpRequestVerifier->verify($request);
$token = $this->payum->getHttpRequestVerifier()->verify($request);

$gateway = $this->registry->getGateway($token->getGatewayName());
$gateway = $this->payum->getGateway($token->getGatewayName());
$gateway->execute(new Capture($token));

$this->httpRequestVerifier->invalidate($token);
$this->payum->getHttpRequestVerifier()->invalidate($token);

return new RedirectResponse($token->getAfterUrl());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Payum/Silex/Controller/NotifyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class NotifyController extends PayumController
*/
public function doAction(Request $request)
{
$token = $this->httpRequestVerifier->verify($request);
$token = $this->payum->getHttpRequestVerifier()->verify($request);

$gateway = $this->registry->getGateway($token->getGatewayName());
$gateway = $this->payum->getGateway($token->getGatewayName());

$gateway->execute(new Notify($token));

Expand Down
33 changes: 7 additions & 26 deletions src/Payum/Silex/Controller/PayumController.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
<?php
namespace Payum\Silex\Controller;

use Payum\Core\Registry\RegistryInterface;
use Payum\Core\Security\GenericTokenFactoryInterface;
use Payum\Core\Security\HttpRequestVerifierInterface;
use Payum\Core\Payum;

abstract class PayumController
{
/**
* @var GenericTokenFactoryInterface
* @var Payum
*/
protected $tokenFactory;
protected $payum;

/**
* @var RegistryInterface
* @param Payum $payum
*/
protected $registry;

/**
* @var HttpRequestVerifierInterface
*/
protected $httpRequestVerifier;

/**
* @param GenericTokenFactoryInterface $tokenFactory
* @param HttpRequestVerifierInterface $httpRequestVerifier
* @param RegistryInterface $registry
*/
public function __construct(
GenericTokenFactoryInterface $tokenFactory,
HttpRequestVerifierInterface $httpRequestVerifier,
RegistryInterface $registry
) {
$this->tokenFactory = $tokenFactory;
$this->registry = $registry;
$this->httpRequestVerifier = $httpRequestVerifier;
public function __construct(Payum $payum)
{
$this->payum = $payum;
}
}
6 changes: 3 additions & 3 deletions src/Payum/Silex/Controller/RefundController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class RefundController extends PayumController
*/
public function doAction(Request $request)
{
$token = $this->httpRequestVerifier->verify($request);
$token = $this->payum->getHttpRequestVerifier()->verify($request);

$gateway = $this->registry->getGateway($token->getGatewayName());
$gateway = $this->payum->getGateway($token->getGatewayName());
$gateway->execute(new Refund($token));

$this->httpRequestVerifier->invalidate($token);
$this->payum->getHttpRequestVerifier()->invalidate($token);

return new RedirectResponse($token->getAfterUrl());
}
Expand Down
31 changes: 0 additions & 31 deletions src/Payum/Silex/DynamicRegistry.php

This file was deleted.

166 changes: 63 additions & 103 deletions src/Payum/Silex/PayumProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
use Payum\Core\Bridge\Symfony\Security\TokenFactory;
use Payum\Core\Bridge\Twig\TwigFactory;
use Payum\Core\GatewayFactory;
use Payum\Core\Payum;
use Payum\Core\PayumBuilder;
use Payum\Core\Registry\StorageRegistryInterface;
use Payum\Core\Reply\ReplyInterface;
use Payum\Core\Security\GenericTokenFactory;
use Payum\Core\Storage\StorageInterface;
use Payum\Silex\Controller\AuthorizeController;
use Payum\Silex\Controller\CaptureController;
use Payum\Silex\Controller\NotifyController;
use Payum\Silex\Controller\RefundController;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;

class PayumProvider implements ServiceProviderInterface
{
Expand All @@ -45,59 +50,65 @@ public function boot(Application $app)
*/
protected function registerService(Application $app)
{
$app['payum.template.layout'] = '@PayumCore/layout.html.twig';
$app['payum.template.obtain_credit_card'] = '@PayumSymfonyBridge/obtainCreditCard.html.twig';
$app['payum.builder'] = $app->share(function($app) {
$builder = new PayumBuilder();

$app['twig.loader.filesystem'] = $app->share($app->extend('twig.loader.filesystem', function($loader, $app) {
/** @var \Twig_Loader_Filesystem $loader */
$builder->addDefaultStorages();

foreach (TwigFactory::createGenericPaths() as $path => $name) {
$loader->addPath($path, $name);
}
$builder->setCoreGatewayFactoryConfig([
'twig.env' => $app['twig'],

return $loader;
}));
'payum.action.get_http_request' => function() use ($app) {
$action = new GetHttpRequestAction();
$action->setHttpRequest($app['request']);

$app['payum.action.get_http_request'] = $app->share(function($app) {
$action = new GetHttpRequestAction();
$action->setHttpRequest($app['request']);
return $action;
},
'payum.action.obtain_credit_card' => function() use($app) {
$action = new ObtainCreditCardAction($app['form.factory'], $app['payum.template.obtain_credit_card']);
$action->setRequest($app['request']);

return $action;
});
return $action;
},
]);

$app['payum.action.obtain_credit_card'] = $app->share(function($app) {
$action = new ObtainCreditCardAction($app['form.factory'], $app['payum.template.obtain_credit_card']);
$action->setRequest($app['request']);
$builder->setGenericTokenFactoryPaths([
'capture' => 'payum_capture_do',
'notify' => 'payum_notify_do',
'authorize' => 'payum_authorize_do',
'refund' => 'payum_refund_do'
]);

return $action;
});
$builder->setTokenFactory(function(StorageInterface $tokenStorage, StorageRegistryInterface $registry) use ($app) {
return new TokenFactory($tokenStorage, $registry, $app['url_generator']);
});

$app['payum.gateway_config_storage'] = $app->share(function($app) {
return null;
});
$builder->setHttpRequestVerifier(function(StorageInterface $tokenStorage) {
return new HttpRequestVerifier($tokenStorage);
});

$app['payum.security.token_storage'] = $app->share(function() {
throw new \LogicException('This service has to be overwritten. Check the example in the doc at payum.org');
return $builder;
});

$app['payum.reply_to_symfony_response_converter'] = $app->share(function($app) {
return new ReplyToSymfonyResponseConverter();
});
$app['payum'] = $app->share(function($app) {
/** @var PayumBuilder $builder */
$builder = $app['payum.builder'];

$app['payum.security.http_request_verifier'] = $app->share(function($app) {
return new HttpRequestVerifier($app['payum.security.token_storage']);
return $builder->getPayum();
});

$app['payum.security.token_factory'] = $app->share(function($app) {
return new GenericTokenFactory(
new TokenFactory($app['payum.security.token_storage'], $app['payum'], $app['url_generator']),
array(
'capture' => 'payum_capture_do',
'notify' => 'payum_notify_do',
'authorize' => 'payum_authorize_do',
'refund' => 'payum_refund_do'
)
);
$app['twig.loader.filesystem'] = $app->share($app->extend('twig.loader.filesystem', function($loader, $app) {
/** @var \Twig_Loader_Filesystem $loader */

foreach (TwigFactory::createGenericPaths() as $path => $name) {
$loader->addPath($path, $name);
}

return $loader;
}));

$app['payum.reply_to_symfony_response_converter'] = $app->share(function($app) {
return new ReplyToSymfonyResponseConverter();
});

$app['form.types'] = $app->share($app->extend('form.types', function ($types) use ($app) {
Expand All @@ -110,54 +121,19 @@ protected function registerService(Application $app)
}));

$app['payum.gateway_choices'] = $app->share(function ($app) {
return array();
});

$app['payum.core_gateway_factory_config'] = $app->share(function ($app) {
return [
'twig.env' => $app['twig'],
'payum.template.layout' => $app['payum.template.layout'],
/** @var Payum $payum */
$payum = $app['payum'];

'payum.action.get_http_request' => $app['payum.action.get_http_request'],
'payum.action.obtain_credit_card' => $app['payum.action.get_http_request'],
];
});

$app['payum.core_gateway_factory'] = $app->share(function ($app) {
return new GatewayFactory($app['payum.core_gateway_factory_config']);
});

$app['payum.gateway_factories'] = $app->share(function () {
return [
// name => instance of GatewayFactoryInterface or service id
];
});
$choices = [];
foreach ($payum->getGatewayFactories() as $name => $factory) {
if (in_array($name, ['omnipay', 'omnipay_direct', 'omnipay_offsite'])) {
continue;
}

$app['payum.gateways'] = $app->share(function () {
return [
// name => instance of GatewayInterface or service id
];
});

$app['payum.storages'] = $app->share(function ($app) {
return [
// modelClass => instance of StorageInterface or service id
];
});

$app['payum'] = $app->share(function($app) {
$registry = new PimpleAwareRegistry(
$app['payum.gateways'],
$app['payum.storages'],
$app['payum.gateway_factories']
);
$registry->setPimple($app);

if ($configStorage = $app['payum.gateway_config_storage']) {
$registry = new DynamicRegistry($configStorage, $registry);
$choices[$name] = ucwords(str_replace(['_', 'omnipay'], ' ', $name));
}

return $registry;
return $choices;
});
}

Expand All @@ -167,41 +143,25 @@ protected function registerService(Application $app)
protected function registerControllers(Application $app)
{
$app['payum.controller.authorize'] = $app->share(function() use ($app) {
return new AuthorizeController(
$app['payum.security.token_factory'],
$app['payum.security.http_request_verifier'],
$app['payum']
);
return new AuthorizeController($app['payum']);
});
$app->get('/payment/authorize/{payum_token}', 'payum.controller.authorize:doAction')->bind('payum_authorize_do');
$app->post('/payment/authorize/{payum_token}', 'payum.controller.authorize:doAction')->bind('payum_authorize_do_post');

$app['payum.controller.capture'] = $app->share(function() use ($app) {
return new CaptureController(
$app['payum.security.token_factory'],
$app['payum.security.http_request_verifier'],
$app['payum']
);
return new CaptureController($app['payum']);
});
$app->get('/payment/capture/{payum_token}', 'payum.controller.capture:doAction')->bind('payum_capture_do');
$app->post('/payment/capture/{payum_token}', 'payum.controller.capture:doAction')->bind('payum_capture_do_post');

$app['payum.controller.notify'] = $app->share(function() use ($app) {
return new NotifyController(
$app['payum.security.token_factory'],
$app['payum.security.http_request_verifier'],
$app['payum']
);
return new NotifyController($app['payum']);
});
$app->get('/payment/notify/{payum_token}', 'payum.controller.notify:doAction')->bind('payum_notify_do');
$app->post('/payment/notify/{payum_token}', 'payum.controller.notify:doAction')->bind('payum_notify_do_post');

$app['payum.controller.refund'] = $app->share(function() use ($app) {
return new RefundController(
$app['payum.security.token_factory'],
$app['payum.security.http_request_verifier'],
$app['payum']
);
return new RefundController($app['payum']);
});
$app->get('/payment/refund/{payum_token}', 'payum.controller.refund:doAction')->bind('payum_refund_do');
$app->post('/payment/refund/{payum_token}', 'payum.controller.refund:doAction')->bind('payum_refund_do_post');
Expand Down

0 comments on commit b0e417e

Please sign in to comment.