Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add NotifyAction
  • Loading branch information
patrick477 committed Sep 4, 2017
1 parent faf8a36 commit fa6a3b4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 16 deletions.
68 changes: 55 additions & 13 deletions src/Action/NotifyAction.php
Expand Up @@ -10,37 +10,67 @@

namespace BitBag\PayUPlugin\Action;

use BitBag\PayUPlugin\SetPayU;
use BitBag\PayUPlugin\OpenPayUWrapper;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\Exception\UnsupportedApiException;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Request\GetHumanStatus;
use Payum\Core\Reply\HttpResponse;
use Payum\Core\Request\Notify;
use Sylius\Component\Core\Model\PaymentInterface;
use Webmozart\Assert\Assert;
use Payum\Core\ApiAwareInterface;

/**
* @author Mikołaj Król <mikolaj.krol@bitbag.pl>
*/
final class NotifyAction implements ActionInterface, GatewayAwareInterface
final class NotifyAction implements ActionInterface, ApiAwareInterface
{
use GatewayAwareTrait;

private $api = [];

/**
* @param mixed $request
*
* @throws \Payum\Core\Exception\RequestNotSupportedException if the action dose not support the request.
*/
public function execute($request)
{
/** @var $request Notify */
RequestNotSupportedException::assertSupports($this, $request);
$setPayU = new SetPayU($request->getToken());
$setPayU->setModel($request->getModel());;
$this->getGateway()->execute($setPayU);

$status = new GetHumanStatus($request->getToken());
$status->setModel($request->getModel());
$this->getGateway()->execute($status);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

/** @var $request Notify */
RequestNotSupportedException::assertSupports($this, $request);

/** @var PaymentInterface $payment */
$payment = $request->getFirstModel();
Assert::isInstanceOf($payment, PaymentInterface::class);

$body = file_get_contents('php://input');
$data = trim($body);

\OpenPayU_Configuration::setEnvironment($this->api['environment']);
\OpenPayU_Configuration::setMerchantPosId($this->api['pos_id']);
\OpenPayU_Configuration::setSignatureKey($this->api['signature_key']);

try {

$result = \OpenPayU_Order::consumeNotification($data);

if ($result->getResponse()->order->orderId) {

$order = \OpenPayU_Order::retrieve($result->getResponse()->order->orderId);

if($order->getStatus() === OpenPayUWrapper::SUCCESS_API_STATUS){

$payment->setState(PaymentInterface::STATE_COMPLETED);
}
}

} catch (\OpenPayU_Exception $e) {
throw new HttpResponse($e->getMessage());
}
}
}

/**
Expand All @@ -61,4 +91,16 @@ public function supports($request)
$request->getModel() instanceof \ArrayObject
;
}

/**
* {@inheritDoc}
*/
public function setApi($api)
{
if (!is_array($api)) {
throw new UnsupportedApiException('Not supported.');
}

$this->api = $api;
}
}
27 changes: 26 additions & 1 deletion src/Action/PayUAction.php
Expand Up @@ -23,6 +23,7 @@
use Payum\Core\Security\TokenInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Webmozart\Assert\Assert;
use Payum\Core\Payum;

/**
* @author Mikołaj Król <mikolaj.krol@bitbag.pl>
Expand All @@ -36,6 +37,11 @@ final class PayUAction implements ApiAwareInterface, ActionInterface
*/
private $openPayUWrapper;

/**
* @var Payum
*/
private $payum;

/**
* {@inheritDoc}
*/
Expand All @@ -50,9 +56,11 @@ public function setApi($api)

/**
* @param OpenPayUWrapperInterface $openPayUWrapper
* @param Payum $payum
*/
public function __construct(OpenPayUWrapperInterface $openPayUWrapper)
public function __construct(OpenPayUWrapperInterface $openPayUWrapper, Payum $payum)
{
$this->payum = $payum;
$this->setOpenPayUWrapper($openPayUWrapper);
}

Expand Down Expand Up @@ -132,8 +140,11 @@ public function setOpenPayUWrapper($openPayUWrapper)

private function prepareOrder(TokenInterface $token, $model, $posId)
{
$notifyToken = $this->createNotifyToken($token->getGatewayName(), $token->getDetails());

$order = [];
$order['continueUrl'] = $token->getTargetUrl();
$order['notifyUrl'] = $notifyToken->getTargetUrl();
$order['customerIp'] = $model['customerIp'];
$order['merchantPosId'] = $posId;
$order['description'] = $model['description'];
Expand Down Expand Up @@ -183,4 +194,18 @@ private function resolveProducts($model)

return $model['products'];
}

/**
* @param string $gatewayName
* @param object $model
*
* @return TokenInterface
*/
private function createNotifyToken($gatewayName, $model)
{
return $this->payum->getTokenFactory()->createNotifyToken(
$gatewayName,
$model
);
}
}
2 changes: 1 addition & 1 deletion src/Action/StatusAction.php
Expand Up @@ -39,7 +39,7 @@ public function execute($request)
}

if (OpenPayUWrapper::PENDING_API_STATUS === $status) {
$request->markPending();
$request->markCaptured();

return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.yml
Expand Up @@ -16,6 +16,6 @@ services:

bitbag.payu.payu_action:
class: BitBag\PayUPlugin\Action\PayUAction
arguments: ['@bitbag.payu.open_payu_wrapper']
arguments: ['@bitbag.payu.open_payu_wrapper', '@payum']
tags:
- { name: payum.action, factory: payu, alias: payum.action.set_payu }

0 comments on commit fa6a3b4

Please sign in to comment.