Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Oct 30, 2014
1 parent 014d76c commit 811931f
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/Payum/Silex/Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Payum\Silex\Controller;

use Payum\Core\Request\Authorize;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

class AuthorizeController extends PayumController
{
/**
* @param Request $request
*
* @return RedirectResponse
*/
public function doAction(Request $request)
{
$token = $this->httpRequestVerifier->verify($request);

$payment = $this->registry->getPayment($token->getPaymentName());
$payment->execute(new Authorize($token));

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

return new RedirectResponse($token->getAfterUrl());
}
}
26 changes: 26 additions & 0 deletions src/Payum/Silex/Controller/CaptureController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Payum\Silex\Controller;

use Payum\Core\Request\Capture;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

class CaptureController extends PayumController
{
/**
* @param Request $request
*
* @return RedirectResponse
*/
public function doAction(Request $request)
{
$token = $this->httpRequestVerifier->verify($request);

$payment = $this->registry->getPayment($token->getPaymentName());
$payment->execute(new Capture($token));

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

return new RedirectResponse($token->getAfterUrl());
}
}
25 changes: 25 additions & 0 deletions src/Payum/Silex/Controller/NotifyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace Payum\Silex\Controller;

use Payum\Core\Request\Notify;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class NotifyController extends PayumController
{
/**
* @param Request $request
*
* @return Response
*/
public function doAction(Request $request)
{
$token = $this->httpRequestVerifier->verify($request);

$payment = $this->registry->getPayment($token->getPaymentName());

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

return new Response('', 204);
}
}
39 changes: 39 additions & 0 deletions src/Payum/Silex/Controller/PayumController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace Payum\Silex\Controller;

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

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

/**
* @var RegistryInterface
*/
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;
}
}
26 changes: 26 additions & 0 deletions src/Payum/Silex/Controller/RefundController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Payum\Silex\Controller;

use Payum\Core\Request\Refund;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

class RefundController extends PayumController
{
/**
* @param Request $request
*
* @return RedirectResponse
*/
public function doAction(Request $request)
{
$token = $this->httpRequestVerifier->verify($request);

$payment = $this->registry->getPayment($token->getPaymentName());
$payment->execute(new Refund($token));

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

return new RedirectResponse($token->getAfterUrl());
}
}
142 changes: 140 additions & 2 deletions src/Payum/Silex/PayumProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,142 @@
<?php
class PayumProvider {
namespace Payum\Silex;

}
use Payum\Core\Bridge\Symfony\ReplyToSymfonyResponseConverter;
use Payum\Core\Bridge\Symfony\Security\HttpRequestVerifier;
use Payum\Core\Bridge\Symfony\Security\TokenFactory;
use Payum\Core\Registry\SimpleRegistry;
use Payum\Core\Reply\ReplyInterface;
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;

class PayumProvider implements ServiceProviderInterface
{
/**
* {@inheritDoc}
*/
public function register(Application $app)
{
$this->registerService($app);
$this->registerControllers($app);
$this->registerListeners($app);
}

/**
* {@inheritDoc}
*/
public function boot(Application $app)
{
}

/**
* @param Application $app
*/
protected function registerService(Application $app)
{
$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');
});

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

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

$app['payum.security.token_factory'] = $app->share(function($app) {
return new TokenFactory(
$app['url_generator'],
$app['payum.security.token_storage'],
$app['payum'],
'payum_capture_do',
'payum_notify_do',
'payum_authorize_do'
);
});

$app['payum.payments'] = $app->share(function () {
return [
// name => instance of PaymentInterface
];
});

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

$app['payum'] = $app->share(function($app) {
return new SimpleRegistry($app['payum.payments'], $app['payum.storages'], null, null);
});
}

/**
* @param 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']
);
});
$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']
);
});
$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']
);
});
$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']
);
});
$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');
}

/**
* @param Application $app
*/
protected function registerListeners(Application $app)
{
$app->error(function (\Exception $e, $code) use ($app) {
if (false == $e instanceof ReplyInterface) {
return;
}

/** @var ReplyToSymfonyResponseConverter $converter */
$converter = $app['payum.reply_to_symfony_response_converter'];

return $converter->convert($e);
}, $priority = 100);
}
}

0 comments on commit 811931f

Please sign in to comment.