Skip to content

Commit

Permalink
Moved response conversion to method on PayumController
Browse files Browse the repository at this point in the history
Catch response of execute for conversion
Update PayumServiceProvder.php for Laravel 5.1

HttpRedirect is a child of HttpResponse moved the reply conversion for HttpRedirect above HttpResponse so redirects are handled as intended
  • Loading branch information
Christopher Scott committed Jun 18, 2015
1 parent 73d5b50 commit 553d389
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 39 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
],
"require": {
"php": ">=5.3.2",
"illuminate/container": "~4.0",
"illuminate/routing": "~4.0",
"illuminate/support": "~4.0",
"illuminate/container": "~5.1",
"illuminate/routing": "~5.1",
"illuminate/support": "~5.1",
"payum/core": "1.0.*@dev"
},
"suggest": {
Expand Down
5 changes: 4 additions & 1 deletion src/Payum/LaravelPackage/Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public function doAction($payum_token)

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

$gateway->execute(new Authorize($token));
$response = $this->convertReply($gateway->execute(new Authorize($token), true));

if($response)
return $response;

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

Expand Down
6 changes: 5 additions & 1 deletion src/Payum/LaravelPackage/Controller/CaptureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ public function doAction($payum_token)

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

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

$response = $this->convertReply($gateway->execute(new Capture($token), true));

if($response)
return $response;

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

Expand Down
10 changes: 8 additions & 2 deletions src/Payum/LaravelPackage/Controller/NotifyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ public function doUnsafeAction(Request $request)
{
$gateway = $this->getPayum()->getGateway($request->get('gateway_name'));

$gateway->execute(new Notify(null));
$response = $this->convertReply($gateway->execute(new Notify(null), true));

if($response)
return $response;

return \Response::make(null, 204);
}
Expand All @@ -21,7 +24,10 @@ public function doAction(Request $request)

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

$gateway->execute(new Notify($token));
$response = $this->convertReply($gateway->execute(new Notify($token), true));

if($response)
return $response;

return \Response::make(null, 204);
}
Expand Down
37 changes: 35 additions & 2 deletions src/Payum/LaravelPackage/Controller/PayumController.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<?php
namespace Payum\LaravelPackage\Controller;

use Illuminate\Routing\Controllers\Controller;
use Illuminate\Routing\Controller;
use Payum\Core\Registry\RegistryInterface;
use Payum\Core\Security\HttpRequestVerifierInterface;

abstract class PayumController extends \Controller
use Payum\Core\Exception\LogicException;
use Payum\Core\Reply\HttpRedirect;
use Payum\Core\Reply\HttpResponse;
use Payum\Core\Reply\ReplyInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

abstract class PayumController extends Controller
{
/**
* @return RegistryInterface
Expand All @@ -22,4 +29,30 @@ protected function getHttpRequestVerifier()
{
return \App::make('payum.security.http_request_verifier');
}

public function convertReply($reply)
{
if(!$reply instanceof ReplyInterface)
return;

$response = null;

if ($reply instanceof SymfonyHttpResponse) {
$response = $reply->getResponse();
} elseif ($reply instanceof HttpRedirect) {
$response = new RedirectResponse($reply->getUrl());
} elseif ($reply instanceof HttpResponse) {
$response = new Response($reply->getContent());
}
if ($response) {
return $response;
}

$ro = new \ReflectionObject($reply);
throw new LogicException(
sprintf('Cannot convert reply %s to Laravel response.', $ro->getShortName()),
null,
$reply
);
}
}
5 changes: 4 additions & 1 deletion src/Payum/LaravelPackage/Controller/RefundController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public function doAction($payum_token)

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

$gateway->execute(new Refund($token));
$response = $this->convertReply($gateway->execute(new Refund($token), true));

if($response)
return $response;

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

Expand Down
40 changes: 11 additions & 29 deletions src/Payum/LaravelPackage/PayumServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,11 @@ class PayumServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->package('payum/payum-laravel-package');
\View::addNamespace('payum/payum', __DIR__.'/../../views');

$this->app->error(function(ReplyInterface $reply)
{
$response = null;

if ($reply instanceof SymfonyHttpResponse) {
$response = $reply->getResponse();
} elseif ($reply instanceof HttpResponse) {
$response = new Response($reply->getContent());
} elseif ($reply instanceof HttpRedirect) {
$response = new RedirectResponse($reply->getUrl());
}

if ($response) {
return $response;
}

$ro = new \ReflectionObject($reply);
throw new LogicException(
sprintf('Cannot convert reply %s to Laravel response.', $ro->getShortName()),
null,
$reply
);
});
$this->publishes([
__DIR__.'/../../config/config.php' => config_path('payum-laravel-package.php'),
]);

$this->loadViewsFrom(__DIR__.'/../../views', 'payum/payum');

\Route::any('/payment/authorize/{payum_token}', array(
'as' => 'payum_authorize_do',
Expand Down Expand Up @@ -77,8 +56,8 @@ public function boot()
//TODO add exceptions if invalid gateways and storages options set.

$payum = new ContainerAwareRegistry(
\Config::get('payum-laravel-package::gateways'),
\Config::get('payum-laravel-package::storages')
\Config::get('payum-laravel-package.gateways'),
\Config::get('payum-laravel-package.storages')
);

$payum->setContainer($app);
Expand All @@ -89,7 +68,7 @@ public function boot()
$this->app['payum.security.token_storage'] = $this->app->share(function($app) {
//TODO add exceptions if invalid gateways and storages options set.

$tokenStorage = \Config::get('payum-laravel-package::token_storage');
$tokenStorage = \Config::get('payum-laravel-package.token_storage');

return is_object($tokenStorage) ? $tokenStorage : $app[$tokenStorage];
});
Expand All @@ -116,6 +95,9 @@ public function boot()
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/../../config/config.php', 'payum-laravel-package'
);
}

/**
Expand Down

0 comments on commit 553d389

Please sign in to comment.