Skip to content

Commit

Permalink
Making the extension to test with
Browse files Browse the repository at this point in the history
It is unknown at this point if Sylius will even accept this..
  • Loading branch information
alexrowesoap authored Feb 24, 2018
1 parent 09852fa commit 245d508
Show file tree
Hide file tree
Showing 22 changed files with 2,538 additions and 21 deletions.
20 changes: 20 additions & 0 deletions Action/Api/BaseApiAwareAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Alexrowesoap\Payumsagepay\Action\Api;

use Payum\Core\Action\ActionInterface;
use Payum\Core\ApiAwareInterface;
use Payum\Core\ApiAwareTrait;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Alexrowesoap\Payumsagepay\Api;

abstract class BaseApiAwareAction implements ActionInterface, GatewayAwareInterface, ApiAwareInterface
{
use GatewayAwareTrait;
use ApiAwareTrait;

public function __construct()
{
$this->apiClass = Api::class;
}
}
39 changes: 39 additions & 0 deletions Action/AuthorizeAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace Alexrowesoap\Payumsagepay\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Request\Authorize;
use Payum\Core\Exception\RequestNotSupportedException;

class AuthorizeAction implements ActionInterface, GatewayAwareInterface
{
use GatewayAwareTrait;

/**
* {@inheritDoc}
*
* @param Authorize $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

$model = ArrayObject::ensureArrayObject($request->getModel());

throw new \LogicException('Not implemented');
}

/**
* {@inheritDoc}
*/
public function supports($request)
{
return
$request instanceof Authorize &&
$request->getModel() instanceof \ArrayAccess
;
}
}
38 changes: 38 additions & 0 deletions Action/CancelAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace Soapmedia\Payumsagepay\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Request\Cancel;

class CancelAction implements ActionInterface
{
use GatewayAwareTrait;

/**
* {@inheritDoc}
*
* @param Cancel $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

$model = ArrayObject::ensureArrayObject($request->getModel());

throw new \LogicException('Not implemented');
}

/**
* {@inheritDoc}
*/
public function supports($request)
{
return
$request instanceof Cancel &&
$request->getModel() instanceof \ArrayAccess
;
}
}
108 changes: 108 additions & 0 deletions Action/CaptureAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
namespace Alexrowesoap\Payumsagepay\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\LogicException;
use Payum\Core\Exception\UnsupportedApiException;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Reply\HttpRedirect;
use Payum\Core\Request\Capture;
use Payum\Core\Exception\RequestNotSupportedException;
use Alexrowesoap\Payumsagepay\Api;
use Alexrowesoap\Payumsagepay\Api\State\StateInterface;

class CaptureAction implements ActionInterface
{
use GatewayAwareTrait;

/**
* @var Api
*/
protected $api;

/**
* {@inheritDoc}
*/
public function setApi($api)
{
if (false === $api instanceof Api) {
throw new UnsupportedApiException('Not supported.');
}

$this->api = $api;
}

/**
* {@inheritDoc}
*
* @param Capture $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

$model = ArrayObject::ensureArrayObject($request->getModel());
$token = $request->getToken();

if (isset($model['Status'])) {
return;
}

$details = $this->api->prepareOffsiteDetails($model->toUnsafeArray()); // filter model details for request

$missing = $this->api->getMissingDetails($details); // sagepay's api is wayward we should check for presence of required minimum

if (count($missing) > 0) {
throw new LogicException(
sprintf(
'Missing: %s details are mandatory for current payment request!',
implode(", ", array_keys($missing))
)
);
}

$model['state'] = StateInterface::STATE_WAITING;

// we do not need any sync action
// but payum by default do not set any afterUrl to notify token
if ($token) {
$model['afterUrl'] = $token->getAfterUrl();
}

$response = $this->api->createOffsitePurchase($details);

$responseArr = json_decode( $response->getBody()->getContents() );
$model['state'] = $responseArr['Status'] ==
Api::STATUS_OK || $responseArr['Status'] == Api::STATUS_OK_REPEATED ?
StateInterface::STATE_REPLIED :
StateInterface::STATE_ERROR;

/*$model->replace(
array_merge(
(array) $model->toUnsafeArray(),
(array) $response->toArray()
)
);*/

if ($responseArr['Status'] == Api::STATUS_OK ||
$responseArr['Status'] == Api::STATUS_OK_REPEATED
) {

throw new HttpRedirect(
$responseArr['NextURL'] . '=' . $responseArr['VPSTxId']
);
}
}

/**
* {@inheritDoc}
*/
public function supports($request)
{
return
$request instanceof Capture &&
$request->getModel() instanceof \ArrayAccess
;
}
}
53 changes: 53 additions & 0 deletions Action/ConvertPaymentAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace Alexrowesoap\Payumsagepay\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Model\PaymentInterface;
use Payum\Core\Request\Convert;
use Payum\Core\Request\GetCurrency;

class ConvertPaymentAction implements ActionInterface
{
use GatewayAwareTrait;

/**
* {@inheritDoc}
*
* @param Convert $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

/** @var PaymentInterface $payment */
$payment = $request->getSource();

$this->gateway->execute($currency = new GetCurrency($payment->getCurrencyCode()));
$divisor = pow(10, $currency->exp);

$details = ArrayObject::ensureArrayObject($payment->getDetails());

$details['VendorTxCode'] = $payment->getNumber();
$details['Amount'] = $payment->getTotalAmount() / $divisor;
$details['Currency'] = $payment->getCurrencyCode();
$details['Description'] = $payment->getDescription();
$details['CustomerEMail'] = $payment->getClientEmail();

$request->setResult((array) $details);
}

/**
* {@inheritDoc}
*/
public function supports($request)
{
return
$request instanceof Convert &&
$request->getSource() instanceof PaymentInterface &&
$request->getTo() == 'array'
;
}
}
122 changes: 122 additions & 0 deletions Action/NotifyAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
namespace Alexrowesoap\Payumsagepay\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Exception\UnsupportedApiException;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Request\GetHttpRequest;
use Payum\Core\Request\Notify;
use Alexrowesoap\Payumsagepay\Api;
use Alexrowesoap\Payumsagepay\Api\Reply\NotifyResponse;
use Alexrowesoap\Payumsagepay\Api\State\StateInterface;

class NotifyAction implements ActionInterface
{
use GatewayAwareTrait;

/**
* @var Api
*/
protected $api;

/**
* {@inheritDoc}
*/
public function setApi($api)
{
if (false === $api instanceof Api) {
throw new UnsupportedApiException('Not supported.');
}

$this->api = $api;
}

/**
* {@inheritDoc}
*
* @param Notify $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

$model = ArrayObject::ensureArrayObject($request->getModel());

if (!isset($model['state']) ||
!in_array(
$model['state'],
array(
StateInterface::STATE_REPLIED,
StateInterface::STATE_NOTIFIED,
StateInterface::STATE_CONFIRMED,
)
)
) {
throw new RequestNotSupportedException("process only replied and notified payments");
}

$httpRequest = new GetHttpRequest;
$this->gateway->execute($httpRequest);

if ($httpRequest->method != 'POST') {
return;
}

$status = Api::STATUS_OK;
$model['state'] = StateInterface::STATE_NOTIFIED;
$notification = $httpRequest->request;
$redirectUrl = $model['afterUrl'];
// check signature hash

if ($this->api->tamperingDetected((array) $notification, (array) $model->toUnsafeArray())) {
$status = Api::STATUS_INVALID;
$statusDetails = "Tampering detected. Wrong hash.";
} else {
if ($notification['Status'] == Api::STATUS_OK) {
$model['state'] = StateInterface::STATE_CONFIRMED;
} elseif ($notification['Status'] == Api::STATUS_PENDING) {
$model['state'] = StateInterface::STATE_REPLIED;
} else {
$model['state'] = StateInterface::STATE_ERROR;
}

$statusDetails = 'Transaction processed';

if ($notification['Status'] == Api::STATUS_ERROR
&& isset($notification['Vendor'])
&& isset($notification['VendorTxCode'])
&& isset($notification['StatusDetail'])
) {
$status = Api::STATUS_ERROR;
$statusDetails = 'Status of ERROR is seen, together with your Vendor, VendorTxCode and the StatusDetail.';
}

}

$model['notification'] = (array) $notification;
$model->replace(
$model->toUnsafeArray()
);

$params = array(
'Status' => $status,
'StatusDetails' => $statusDetails,
'RedirectURL' => $redirectUrl,
);

throw new NotifyResponse($params);
}

/**
* {@inheritDoc}
*/
public function supports($request)
{
return
$request instanceof Notify &&
$request->getModel() instanceof \ArrayAccess
;
}
}
Loading

0 comments on commit 245d508

Please sign in to comment.