Skip to content

Commit

Permalink
Payone Payum Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
dpfaffenbauer committed Mar 6, 2019
0 parents commit 85be1fe
Show file tree
Hide file tree
Showing 23 changed files with 1,444 additions and 0 deletions.
121 changes: 121 additions & 0 deletions Action/AbstractPurchaseAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2017 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Payum\Payone\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Request\Authorize;
use Payum\Core\Request\Capture;
use Payum\Core\Request\Generic;
use Payum\Core\Request\GetHttpRequest;
use Payum\Core\Security\GenericTokenFactoryAwareInterface;
use Payum\Core\Security\GenericTokenFactoryAwareTrait;

abstract class AbstractPurchaseAction implements ActionInterface, GenericTokenFactoryAwareInterface, GatewayAwareInterface
{
use GatewayAwareTrait;
use GenericTokenFactoryAwareTrait;

/**
* @param Authorize|Capture $request
*
* @return ArrayObject
*/
protected function preExecute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

return ArrayObject::ensureArrayObject($request->getModel());
}

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

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

if ($this->getCompletedStatus() === $model['status']) {
return;
}

$model['completed_status'] = $this->getCompletedStatus();

$model['redirect'] = [
'success' => $request->getToken()->getAfterUrl(),
'error' => $request->getToken()->getAfterUrl().'?canceled=1',
'back' => $request->getToken()->getAfterUrl().'?canceled=1',
];

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

if (isset($httpRequest->query['canceled'])) {
$model['status'] = 'canceled';

return;
}

if (in_array($model['status'], ['canceled', 'failed', 'refunded'], true)) {
return;
}

// if (PaymentTypes::PAYMENT_METHOD_DIRECT_DEBIT_SEPA === $model[Api::FIELD_PAYMENT_METHOD]) {
// $this->gateway->execute($mandate = new GetSepaMandate($model));
// }
// if (Api::PAYMENT_METHOD_CREDIT_CARD_PPAN === $model[Api::FIELD_PAYMENT_METHOD]) {
// $this->gateway->execute($ppan = new GetPseudoCardPan($model));
// }

if ($model['status'] === 'failed') {
return;
}

if (false === $model->get('param', false) && $request->getToken() && $this->tokenFactory) {
$notifyToken = $this->tokenFactory->createNotifyToken(
$request->getToken()->getGatewayName(),
$request->getToken()->getDetails()
);

$model['param'] = $notifyToken->getHash();
}

if ('pending' !== $model['status']) {
$this->gateway->execute($this->createApiRequest($model));
}

if (in_array($model['status'], ['authorized', 'captured', 'failed'], true)) {
return;
}

// if notification is needed the payment will be completed in the NotifyAction
}

/**
* @param $model
*
* @return Generic
*/
abstract protected function createApiRequest($model);

/**
* @return string
*/
abstract protected function getCompletedStatus();
}
81 changes: 81 additions & 0 deletions Action/Api/AuthorizeAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2017 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Payum\Payone\Action\Api;

use ArvPayoneApi\Response\GenericResponse;
use ArvPayoneApi\Response\Status;
use CoreShop\Payum\Payone\Request\Api\Authorize;
use CoreShop\Payum\Payone\Request\Api\OnSitePayment;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\LogicException;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Reply\HttpRedirect;

class AuthorizeAction extends BaseApiAwareAction implements GatewayAwareInterface
{
use GatewayAwareTrait;

/**
* @param Authorize $request
* @throws \Exception
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

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

if ($this->api->isOnsite()) {
$this->gateway->execute(new OnSitePayment($this->api->getPaymentType(), $model));
}

$response = $this->api->authorize($model->toUnsafeArray());

if ($response->getSuccess()) {
$model['txid'] = $response->getTransactionID();

$model['status'] = 'pending';
$model['message'] = $response->getErrorMessage();

if ($response->getStatus() === Status::REDIRECT) {
if ($response instanceof GenericResponse) {
$model['responseData'] = $response->getResponseData();

throw new HttpRedirect($model['responseData']['redirecturl']);
}
}

if ($response->getStatus() === Status::APPROVED) {
$model['status'] = 'authorized';
}

return;
}

throw new LogicException('Unknown status: '.$response->getStatus());
}

/**
* @param mixed $request
*
* @return boolean
*/
public function supports($request)
{
return
$request instanceof Authorize &&
$request->getModel() instanceof \ArrayAccess;
}
}
43 changes: 43 additions & 0 deletions Action/Api/BaseApiAwareAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2017 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Payum\Payone\Action\Api;

use CoreShop\Payum\Payone\Api;
use Payum\Core\Action\ActionInterface;
use Payum\Core\ApiAwareInterface;
use Payum\Core\Exception\UnsupportedApiException;

abstract class BaseApiAwareAction implements ActionInterface, ApiAwareInterface
{
/**
* @var Api
*/
protected $api;

/**
* {@inheritDoc}
*/
public function setApi($api)
{
if (false === $api instanceof Api) {
throw new UnsupportedApiException(
sprintf(
'Not supported. Expected %s instance to be set as api.',
Api::class
)
);
}

$this->api = $api;
}
}
54 changes: 54 additions & 0 deletions Action/Api/CaptureAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2017 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Payum\Payone\Action\Api;

use ArvPayoneApi\Response\Status;
use CoreShop\Payum\Payone\Request\Api\Capture;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;

class CaptureAction extends BaseApiAwareAction implements GatewayAwareInterface
{
use GatewayAwareTrait;

/**
* @param Capture $request
* @throws \Exception
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

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

$response = $this->api->capture($model->toUnsafeArray());

if ($response->getStatus() === Status::APPROVED) {
$model['status'] = 'captured';
}
}

/**
* @param mixed $request
*
* @return boolean
*/
public function supports($request)
{
return
$request instanceof Capture &&
$request->getModel() instanceof \ArrayAccess;
}
}
Loading

0 comments on commit 85be1fe

Please sign in to comment.