Skip to content

Commit

Permalink
Added mocked api for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
MykolasVitkus committed Aug 27, 2019
1 parent 130efd6 commit 07ae2e7
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor
bin
/.idea/
5 changes: 3 additions & 2 deletions Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Payum\Core\Security\GenericTokenFactoryAwareInterface;
use Payum\Core\Security\GenericTokenFactoryAwareTrait;
use PTS\Paysera\Api;
use PTS\Paysera\MockedApi;
use WebToPay;

class CaptureAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
Expand All @@ -23,9 +24,9 @@ class CaptureAction implements ActionInterface, ApiAwareInterface, GatewayAwareI

use ApiAwareTrait;

public function __construct()
public function __construct($mocked = false)
{
$this->apiClass = Api::class;
$mocked ? $this->apiClass = MockedApi::class : $this->apiClass = Api::class;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions Action/NotifyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
use Payum\Core\Request\GetHttpRequest;
use Payum\Core\Request\Notify;
use PTS\Paysera\Api;
use PTS\Paysera\MockedApi;

class NotifyAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
{
use ApiAwareTrait;

use GatewayAwareTrait;

public function __construct()
public function __construct($mocked = false)
{
$this->apiClass = Api::class;
$mocked ? $this->apiClass = MockedApi::class : $this->apiClass = Api::class;
}

/**
Expand Down
61 changes: 61 additions & 0 deletions MockedApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace PTS\Paysera;

use Http\Message\MessageFactory;
use function League\Uri\create;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\Http\HttpException;
use Payum\Core\HttpClientInterface;
use Payum\Core\Reply\HttpPostRedirect;
use Payum\Core\Reply\HttpResponse;
use Payum\Core\Request\GetHttpRequest;
use Symfony\Component\HttpFoundation\Request;
use WebToPay;

class MockedApi
{
/**
* @var array
*/
protected $options = [];

/**
* Api constructor.
* @param array $options
*/
public function __construct(array $options)
{
$options = ArrayObject::ensureArrayObject($options);
$options->defaults($this->options);

$this->options = $options;
}

public function doPayment(array $fields)
{
$fields['projectid'] = $this->options['projectid'];
$fields['sign_password'] = $this->options['sign_password'];
$this->options['test'] ? $fields['test'] = 1 : $fields['test'] = 0;
$authorizeTokenUrl = $this->getApiEndpoint();
$data = WebToPay::buildRequest($fields);
throw new HttpPostRedirect($authorizeTokenUrl, $data);
}

public function doNotify(array $fields)
{
return true;
}

/**
* @return string
*/
protected function getApiEndpoint()
{
return WebToPay::PAY_URL;
}

public function getApiOptions()
{
return $this->options;
}
}
52 changes: 52 additions & 0 deletions MockedPayseraGatewayFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace PTS\Paysera;

use PTS\Paysera\Action\AuthorizeAction;
use PTS\Paysera\Action\CancelAction;
use PTS\Paysera\Action\ConvertPaymentAction;
use PTS\Paysera\Action\CaptureAction;
use PTS\Paysera\Action\NotifyAction;
use PTS\Paysera\Action\RefundAction;
use PTS\Paysera\Action\StatusAction;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\GatewayFactory;

class MockedPayseraGatewayFactory extends GatewayFactory
{
/**
* {@inheritDoc}
*/
protected function populateConfig(ArrayObject $config)
{
$config->defaults([
'payum.factory_name' => 'paysera',
'payum.factory_title' => 'paysera',
'payum.action.capture' => new CaptureAction($mocked = true),
'payum.action.authorize' => new AuthorizeAction(),
'payum.action.refund' => new RefundAction(),
'payum.action.cancel' => new CancelAction(),
'payum.action.notify' => new NotifyAction($mocked = true),
'payum.action.status' => new StatusAction(),
'payum.action.convert_payment' => new ConvertPaymentAction(),
]);

if (false == $config['payum.api']) {
$config['payum.default_options'] = array(
'projectid' => '',
'sign_password' => '',
'test' => true,
);
$config->defaults($config['payum.default_options']);
$config['payum.required_options'] = [
'projectid', 'sign_password'
];

$config['payum.api'] = function (ArrayObject $config) {
$config->validateNotEmpty($config['payum.required_options']);

return new MockedApi((array)$config);
};
}
}
}

0 comments on commit 07ae2e7

Please sign in to comment.