Skip to content

Commit

Permalink
Merge pull request #19 from alexandernst/token-factory
Browse files Browse the repository at this point in the history
Token factory
  • Loading branch information
makasim committed Jul 21, 2014
2 parents ecca8c1 + fa3269a commit 2442acc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
34 changes: 9 additions & 25 deletions docs/get-it-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,37 +85,21 @@ class PaypalController extends CController

$payum = $this->getPayum();

$tokenStorage = $payum->getTokenStorage();
$storage = $payum->getRegistry()->getStorage(
'PaymentDetails',
$paymentName
);

$paymentDetails = $storage->createModel();
$paymentDetails['PAYMENTREQUEST_0_CURRENCYCODE'] = 'USD';
$paymentDetails['PAYMENTREQUEST_0_AMT'] = 1.23;
$storage->updateModel($paymentDetails);
$details = $storage->createModel();
$details['PAYMENTREQUEST_0_CURRENCYCODE'] = 'USD';
$details['PAYMENTREQUEST_0_AMT'] = 1.23;
$storage->updateModel($details);

$captureToken = $payum->getTokenFactory()->createCaptureToken($paymentName, $details, 'paypal/done');

$doneToken = $tokenStorage->createModel();
$doneToken->setPaymentName($paymentName);
$doneToken->setDetails($storage->getIdentificator($paymentDetails));
$doneToken->setTargetUrl(
$this->createAbsoluteUrl('paypal/done', array('payum_token' => $doneToken->getHash()))
);
$tokenStorage->updateModel($doneToken);

$captureToken = $tokenStorage->createModel();
$captureToken->setPaymentName('paypal');
$captureToken->setDetails($storage->getIdentificator($paymentDetails));
$captureToken->setTargetUrl(
$this->createAbsoluteUrl('payment/capture', array('payum_token' => $captureToken->getHash()))
);
$captureToken->setAfterUrl($doneToken->getTargetUrl());
$tokenStorage->updateModel($captureToken);

$paymentDetails['RETURNURL'] = $captureToken->getTargetUrl();
$paymentDetails['CANCELURL'] = $captureToken->getTargetUrl();
$storage->updateModel($paymentDetails);
$details['RETURNURL'] = $captureToken->getTargetUrl();
$details['CANCELURL'] = $captureToken->getTargetUrl();
$storage->updateModel($details);

$this->redirect($captureToken->getTargetUrl());
}
Expand Down
12 changes: 6 additions & 6 deletions src/Payum/YiiExtension/PaymentController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Payum\YiiExtension;

use Payum\Core\Request\BinaryMaskStatusRequest;
use Payum\Core\Request\InteractiveRequestInterface;
use Payum\Core\Request\RedirectUrlInteractiveRequest;
//use Payum\Core\Request\Http\RedirectUrlInteractiveRequest; // see issue #17
Expand All @@ -22,17 +21,18 @@ public function actionCapture()
$token = $this->getPayum()->getHttpRequestVerifier()->verify($_REQUEST);
$payment = $this->getPayum()->getRegistry()->getPayment($token->getPaymentName());

$status = new BinaryMaskStatusRequest($token);
$payment->execute($status);

$capture = new SecuredCaptureRequest($token);
$payment->execute($capture);
$payment->execute($capture = new SecuredCaptureRequest($token));

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

$this->redirect($token->getAfterUrl());
}

public function actionNotify()
{
throw new \LogicException('Not Implemented');
}

public function handleException(\CExceptionEvent $event)
{
if (false == $event->exception instanceof InteractiveRequestInterface) {
Expand Down
17 changes: 17 additions & 0 deletions src/Payum/YiiExtension/PayumComponent.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php
namespace Payum\YiiExtension;

\Yii::import('Payum\YiiExtension\TokenFactory', true);

use Payum\Core\PaymentInterface;
use Payum\Core\Registry\RegistryInterface;
use Payum\Core\Registry\SimpleRegistry;
use Payum\Core\Security\GenericTokenFactoryInterface;
use Payum\Core\Security\HttpRequestVerifierInterface;
use Payum\Core\Security\PlainHttpRequestVerifier;
use Payum\Core\Storage\StorageInterface;
Expand All @@ -25,6 +28,11 @@ class PayumComponent extends \CApplicationComponent
*/
public $tokenStorage;

/**
* @var GenericTokenFactoryInterface
*/
public $tokenFactory;

/**
* @var HttpRequestVerifierInterface
*/
Expand All @@ -40,6 +48,7 @@ public function init()
$this->registry = new SimpleRegistry($this->payments, $this->storages, null);

$this->httpRequestVerifier = new PlainHttpRequestVerifier($this->tokenStorage);
$this->tokenFactory = new TokenFactory($this->tokenStorage, $this->registry, 'payment/capture', 'payment/notify');
}

/**
Expand All @@ -50,6 +59,14 @@ public function getTokenStorage()
return $this->tokenStorage;
}

/**
* @return GenericTokenFactoryInterface
*/
public function getTokenFactory()
{
return $this->tokenFactory;
}

/**
* @return HttpRequestVerifierInterface
*/
Expand Down
25 changes: 25 additions & 0 deletions src/Payum/YiiExtension/TokenFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace Payum\YiiExtension;

use Payum\Core\Security\AbstractGenericTokenFactory;

class TokenFactory extends AbstractGenericTokenFactory
{

/**
* @param string $path
* @param array $parameters
*
* @return string
*/
protected function generateUrl($path, array $parameters = array())
{
$ampersand = '&';
$schema = '';

return
\Yii::app()->getRequest()->getHostInfo($schema).
\Yii::app()->createUrl(trim($path,'/'),$parameters, $ampersand)
;
}
}

0 comments on commit 2442acc

Please sign in to comment.