commit package

This commit is contained in:
Kendall Arneaud 2024-06-19 13:09:04 +00:00
parent dbae007581
commit 5704b1303b
45 changed files with 5384 additions and 3 deletions

View File

@ -27,6 +27,6 @@
"install-path": "../karneaud/omnipay-rotessa" "install-path": "../karneaud/omnipay-rotessa"
} }
], ],
"dev": true, "dev": false,
"dev-package-names": [] "dev-package-names": []
} }

View File

@ -7,7 +7,7 @@
'type' => 'laravel-module', 'type' => 'laravel-module',
'install_path' => __DIR__ . '/../../', 'install_path' => __DIR__ . '/../../',
'aliases' => array(), 'aliases' => array(),
'dev' => true, 'dev' => false,
), ),
'versions' => array( 'versions' => array(
'karneaud/invoiceninja-rotessa' => array( 'karneaud/invoiceninja-rotessa' => array(

@ -1 +0,0 @@
Subproject commit c3107930d3f9d656ae3287241ebeded9a1daf8a9

View File

@ -0,0 +1,41 @@
{
"require-dev": {
"omnipay/tests":"*",
"oomphinc/composer-installers-extender": "*",
"http-interop/http-factory-guzzle": "dev-master",
"guzzlehttp/guzzle": "7.9.x-dev"
},
"name":"karneaud/omnipay-rotessa",
"minimum-stability": "dev",
"config": {
"allow-plugins": {
"php-http/discovery": true,
"composer/installers": true,
"oomphinc/composer-installers-extender": true
}
},
"require": {
"php": ">=8.0",
"php-http/discovery": "*",
"omnipay/common":"*"
},
"scripts": {
"generate": [
"generate"
],
"tests": [
"phpunit"
]
},
"autoload": {
"psr-4": {
"Omnipay\\Rotessa\\": "src/Omnipay/Rotessa/"
},
"classmap": ["src/Omnipay/Rotessa/Exception/Exceptions.php"]
},
"autoload-dev": {
"psr-4": {
"Omnipay\\Rotessa\\Test\\": "tests/"
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" verbose="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Suite">
<directory>./tests</directory>
</testsuite>
<testsuite name="Models">
<directory>./tests/Model</directory>
</testsuite>
<testsuite name="Requests">
<directory>./tests/Message/Request</directory>
</testsuite>
<testsuite name="Functionals">
<directory>./tests/Functional</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@ -0,0 +1,21 @@
<?php
namespace Omnipay\Rotessa;
use Omnipay\Common\AbstractGateway;
use Omnipay\Rotessa\ClientInterface;
use Omnipay\Rotessa\Message\RequestInterface;
abstract class AbstractClient extends AbstractGateway implements ClientInterface
{
protected $default_parameters = [];
public function getDefaultParameters() : array {
return $this->default_parameters;
}
public function setDefaultParameters(array $params) {
$this->default_parameters = $params;
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Omnipay\Rotessa;
use Omnipay\Rotessa\Message\Request\RequestInterface;
trait ApiTrait
{
public function getCustomers() : RequestInterface {
return $this->createRequest('GetCustomers', [] );
}
public function postCustomers(array $params) : RequestInterface {
return $this->createRequest('PostCustomers', $params );
}
public function getCustomersId(array $params) : RequestInterface {
return $this->createRequest('GetCustomersId', $params );
}
public function patchCustomersId(array $params) : RequestInterface {
return $this->createRequest('PatchCustomersId', $params );
}
public function postCustomersShowWithCustomIdentifier(array $params) : RequestInterface {
return $this->createRequest('PostCustomersShowWithCustomIdentifier', $params );
}
public function getTransactionSchedulesId(array $params) : RequestInterface {
return $this->createRequest('GetTransactionSchedulesId', $params );
}
public function deleteTransactionSchedulesId(array $params) : RequestInterface {
return $this->createRequest('DeleteTransactionSchedulesId', $params );
}
public function patchTransactionSchedulesId(array $params) : RequestInterface {
return $this->createRequest('PatchTransactionSchedulesId', $params );
}
public function postTransactionSchedules(array $params) : RequestInterface {
return $this->createRequest('PostTransactionSchedules', $params );
}
public function postTransactionSchedulesCreateWithCustomIdentifier(array $params) : RequestInterface {
return $this->createRequest('PostTransactionSchedulesCreateWithCustomIdentifier', $params );
}
public function postTransactionSchedulesUpdateViaPost(array $params) : RequestInterface {
return $this->createRequest('PostTransactionSchedulesUpdateViaPost', $params );
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace Omnipay\Rotessa;
use Omnipay\Common\GatewayInterface;
use Omnipay\Rotessa\Message\Request\RequestInterface;
interface ClientInterface extends GatewayInterface
{
public function getDefaultParameters(): array;
public function setDefaultParameters(array $data);
}

View File

@ -0,0 +1,43 @@
<?php
namespace Omnipay\Rotessa\Exception;
class BadRequestException extends \Exception {
protected $message = "Your request includes invalid parameters";
protected $code = 400;
}
class UnauthorizedException extends \Exception {
protected $message = "Your API key is not valid or is missing";
protected $code = 401;
}
class NotFoundException extends \Exception {
protected $message = "The specified resource could not be found";
protected $code = 404;
}
class NotAcceptableException extends \Exception {
protected $message = "You requested a format that isnt json";
protected $code = 406;
}
class UnprocessableEntityException extends \Exception {
protected $message = "Your request results in invalid data";
protected $code = 422;
}
class InternalServerErrorException extends \Exception {
protected $message = "We had a problem with our server. Try again later";
protected $code = 500;
}
class ServiceUnavailableException extends \Exception {
protected $message = "We're temporarily offline for maintenance. Please try again later";
protected $code = 503;
}
class ValidationException extends \Exception {
protected $message = "A validation error has occured";
protected $code = 600;
}

View File

@ -0,0 +1,74 @@
<?php
namespace Omnipay\Rotessa;
use Omnipay\Rotessa\ApiTrait;
use Omnipay\Rotessa\AbstractClient;
use Omnipay\Rotessa\ClientInterface;
use Omnipay\Rotessa\Message\Request\RequestInterface;
class Gateway extends AbstractClient implements ClientInterface {
use ApiTrait;
protected $default_parameters = ['api_key' => 1234567890 ];
protected $test_mode = true;
protected $api_key;
public function getName()
{
return 'Rotessa';
}
public function getDefaultParameters() : array
{
return array_merge($this->default_parameters, array('api_key' => $this->api_key, 'test_mode' => $this->test_mode ) );
}
public function setTestMode($value) {
$this->test_mode = $value;
}
public function getTestMode() {
return $this->test_mode;
}
protected function createRequest($class_name, ?array $parameters = [] ) :RequestInterface {
$class = null;
$class_name = "Omnipay\\Rotessa\\Message\\Request\\$class_name";
$parameters = $class_name::hasModel() ? (($parameters = ($class_name::getModel($parameters)))->validate() ? $parameters->jsonSerialize() : null ) : $parameters;
try {
$class = new $class_name($this->httpClient, $this->httpRequest, $this->getDefaultParameters() + $parameters );
} catch (\Throwable $th) {
throw $th;
}
return $class;
}
function setApiKey($value) {
$this->api_key = $value;
}
function getApiKey() {
return $this->api_key;
}
function authorize(array $options = []) : RequestInterface {
return $this->postCustomers($options);
}
function capture(array $options = []) : RequestInterface {
return array_key_exists('customer_id', $options)? $this->postTransactionSchedules($options) : $this->postTransactionSchedulesCreateWithCustomIdentifier($options) ;
}
function updateCustomer(array $options) : RequestInterface {
return $this->patchCustomersId($options);
}
function fetchTransaction($id = null) : RequestInterface {
return $this->getTransactionSchedulesId(compact('id'));
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace Omnipay\Rotessa\Http;
use Omnipay\Common\Http\Client as HttpClient;
use Omnipay\Common\Http\Exception\NetworkException;
use Omnipay\Common\Http\Exception\RequestException;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\RequestFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class Client extends HttpClient
{
/**
* The Http Client which implements `public function sendRequest(RequestInterface $request)`
* Note: Will be changed to PSR-18 when released
*
* @var HttpClient
*/
private $httpClient;
/**
* @var RequestFactory
*/
private $requestFactory;
public function __construct($httpClient = null, RequestFactory $requestFactory = null)
{
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
parent::__construct($httpClient, $requestFactory);
}
/**
* @param $method
* @param $uri
* @param array $headers
* @param string|array|resource|StreamInterface|null $body
* @param string $protocolVersion
* @return ResponseInterface
* @throws \Http\Client\Exception
*/
public function request(
$method,
$uri,
array $headers = [],
$body = null,
$protocolVersion = '1.1'
) {
return $this->sendRequest($method, $uri, $headers, $body, $protocolVersion);
}
/**
* @param RequestInterface $request
* @return ResponseInterface
* @throws \Http\Client\Exception
*/
private function sendRequest( $method,
$uri,
array $headers = [],
$body = null,
$protocolVersion = '1.1')
{
$response = null;
try {
if( method_exists($this->httpClient, 'sendRequest'))
$response = $this->httpClient->sendRequest( $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocolVersion));
else $response = $this->httpClient->request($method, $uri, compact('body','headers'));
} catch (\Http\Client\Exception\NetworkException $networkException) {
throw new NetworkException($networkException->getMessage(), $request, $networkException);
} catch (\Exception $exception) {
throw new RequestException($exception->getMessage(), $request, $exception);
}
return $response;
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace Omnipay\Rotessa\Http\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
class Response extends JsonResponse
{
protected $reason_phrase = '';
protected $reason_code = '';
public function __construct(mixed $data = null, int $status = 200, array $headers = [])
{
parent::__construct($data , $status, $headers, true);
if(array_key_exists('errors',$data = json_decode( $this->content, true) )) {
$data = $data['errors'][0];
$this->reason_phrase = $data['error_message'] ;
$this->reason_code = $data['error_message'] ;
}
}
public function getReasonPhrase() {
return $this->reason_phrase;
}
public function getReasonCode() {
return $this->reason_code;
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace Omnipay\Rotessa;
trait IsValidTypeTrait {
public static function isValid(string $value) {
return in_array($value, self::getTypes());
}
abstract public static function getTypes() : array;
}

View File

@ -0,0 +1,52 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
use Omnipay\Rotessa\Message\Request\RequestInterface;
use Omnipay\Common\Message\AbstractRequest as Request;
use Omnipay\Rotessa\Message\Response\ResponseInterface;
abstract class AbstractRequest extends Request implements RequestInterface
{
protected $test_mode = false;
protected $api_version;
protected $method = 'GET';
protected $endpoint;
protected $api_key;
public function setApiKey(string $value) {
$this->api_key = $value;
}
public function getData() {
try {
if(empty($this->api_key)) throw new \Exception('No Api Key Found!');
$this->validate( ...array_keys($data = $this->getParameters()));
} catch (\Throwable $th) {
throw new \Omnipay\Rotessa\Exception\ValidationException($th->getMessage() , 600, $th);
}
return (array) $data;
}
abstract public function sendData($data) : ResponseInterface;
abstract protected function sendRequest(string $method, string $endpoint, array $headers = [], array $data = [] );
abstract protected function createResponse(array $data) : ResponseInterface;
abstract public function getEndpointUrl(): string;
public function getEndpoint() : string {
return $this->endpoint;
}
public function getTestMode() {
return $this->test_mode;
}
public function setTestMode($mode) {
$this->test_mode = $mode;
}
}

View File

@ -0,0 +1,93 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
use Omnipay\Common\Http\ClientInterface;
use Omnipay\Rotessa\Http\Response\Response;
use Omnipay\Rotessa\Message\Response\BaseResponse;
use Omnipay\Rotessa\Message\Request\RequestInterface;
use Omnipay\Rotessa\Message\Response\ResponseInterface;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
class BaseRequest extends AbstractRequest implements RequestInterface
{
protected $base_url = 'rotessa.com';
protected $api_version = 1;
protected $endpoint = '';
const ENVIRONMENT_SANDBOX = 'sandbox-api';
const ENVIRONMENT_LIVE = 'api';
function __construct(ClientInterface $http_client = null, HttpRequest $http_request, $model ) {
parent::__construct($http_client, $http_request );
$this->initialize($model);
}
protected function sendRequest(string $method, string $endpoint, array $headers = [], array $data = [])
{
/**
* @param $method
* @param $uri
* @param array $headers
* @param string|resource|StreamInterface|null $body
* @param string $protocolVersion
* @return ResponseInterface
* @throws \Http\Client\Exception
*/
$response = $this->httpClient->request($method, $endpoint, $headers, json_encode($data) ) ;
$this->response = new Response ($response->getBody()->getContents(), $response->getStatusCode(), $response->getHeaders(), true);
}
protected function createResponse(array $data): ResponseInterface {
return new BaseResponse($this, $data, $this->response->getStatusCode(), $this->response->getReasonPhrase());
}
protected function replacePlaceholder($string, $array) {
$pattern = "/\{([^}]+)\}/";
$replacement = function($matches) use($array) {
$key = $matches[1];
if (array_key_exists($key, $array)) {
return $array[$key];
} else {
return $matches[0];
}
};
return preg_replace_callback($pattern, $replacement, $string);
}
public function sendData($data) :ResponseInterface {
$headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => "Token token={$this->api_key}"
];
$this->sendRequest(
$this->method,
$this->getEndpointUrl(),
$headers,
$data);
return $this->createResponse(json_decode($this->response->getContent(), true));
}
public function getEndpoint() : string {
return $this->replacePlaceholder($this->endpoint, $this->getParameters());
}
public function getEndpointUrl() : string {
return sprintf('https://%s.%s/v%d%s',$this->test_mode ? self::ENVIRONMENT_SANDBOX : self::ENVIRONMENT_LIVE ,$this->base_url, $this->api_version, $this->getEndpoint());
}
public static function hasModel() : bool {
return (bool) static::$model;
}
public static function getModel($parameters = []) {
$class_name = static::$model;
$class_name = "Omnipay\\Rotessa\\Model\\{$class_name}Model";
return new $class_name($parameters);
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
// You will need to create this BaseRequest class as abstracted from the AbstractRequest;
use Omnipay\Rotessa\Message\Request\BaseRequest;
use Omnipay\Rotessa\Message\Request\RequestInterface;
class DeleteTransactionSchedulesId extends BaseRequest implements RequestInterface
{
protected $endpoint = '/transaction_schedules/{id}';
protected $method = 'DELETE';
protected static $model = '';
public function setId(string $value) {
$this->setParameter('id',$value);
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
// You will need to create this BaseRequest class as abstracted from the AbstractRequest;
use Omnipay\Rotessa\Message\Request\BaseRequest;
use Omnipay\Rotessa\Message\Request\RequestInterface;
class GetCustomers extends BaseRequest implements RequestInterface
{
protected $endpoint = '/customers';
protected $method = 'GET';
protected static $model = '';
}

View File

@ -0,0 +1,19 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
// You will need to create this BaseRequest class as abstracted from the AbstractRequest;
use Omnipay\Rotessa\Message\Request\BaseRequest;
use Omnipay\Rotessa\Message\Request\RequestInterface;
class GetCustomersId extends BaseRequest implements RequestInterface
{
protected $endpoint = '/customers/{id}';
protected $method = 'GET';
protected static $model = '';
public function setId(int $value) {
$this->setParameter('id',$value);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
// You will need to create this BaseRequest class as abstracted from the AbstractRequest;
use Omnipay\Rotessa\Message\Request\BaseRequest;
use Omnipay\Rotessa\Message\Request\RequestInterface;
class GetTransactionSchedulesId extends BaseRequest implements RequestInterface
{
protected $endpoint = '/transaction_schedules/{id}';
protected $method = 'GET';
protected static $model = '';
public function setId(int $value) {
$this->setParameter('id',$value);
}
}

View File

@ -0,0 +1,65 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
// You will need to create this BaseRequest class as abstracted from the AbstractRequest;
use Omnipay\Rotessa\Message\Request\BaseRequest;
use Omnipay\Rotessa\Message\Request\RequestInterface;
class PatchCustomersId extends BaseRequest implements RequestInterface
{
protected $endpoint = '/customers/{id}';
protected $method = 'PATCH';
protected static $model = 'CustomerPatch';
public function setId(string $value) {
$this->setParameter('id',$value);
}
public function setCustomIdentifier(string $value) {
$this->setParameter('custom_identifier',$value);
}
public function setName(string $value) {
$this->setParameter('name',$value);
}
public function setEmail(string $value) {
$this->setParameter('email',$value);
}
public function setCustomerType(string $value) {
$this->setParameter('customer_type',$value);
}
public function setHomePhone(string $value) {
$this->setParameter('home_phone',$value);
}
public function setPhone(string $value) {
$this->setParameter('phone',$value);
}
public function setBankName(string $value) {
$this->setParameter('bank_name',$value);
}
public function setInstitutionNumber(string $value) {
$this->setParameter('institution_number',$value);
}
public function setTransitNumber(string $value) {
$this->setParameter('transit_number',$value);
}
public function setBankAccountType(string $value) {
$this->setParameter('bank_account_type',$value);
}
public function setAuthorizationType(string $value) {
$this->setParameter('authorization_type',$value);
}
public function setRoutingNumber(string $value) {
$this->setParameter('routing_number',$value);
}
public function setAccountNumber(string $value) {
$this->setParameter('account_number',$value);
}
public function setAddress(array $value) {
$this->setParameter('address',$value);
}
public function setTransactionSchedules(array $value) {
$this->setParameter('transaction_schedules',$value);
}
public function setFinancialTransactions(array $value) {
$this->setParameter('financial_transactions',$value);
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
// You will need to create this BaseRequest class as abstracted from the AbstractRequest;
use Omnipay\Rotessa\Message\Request\BaseRequest;
use Omnipay\Rotessa\Message\Request\RequestInterface;
class PatchTransactionSchedulesId extends BaseRequest implements RequestInterface
{
protected $endpoint = '/transaction_schedules/{id}';
protected $method = 'PATCH';
public function setId(int $value) {
$this->setParameter('id',$value);
}
public function setAmount(int $value) {
$this->setParameter('amount',$value);
}
public function setComment(string $value) {
$this->setParameter('comment',$value);
}
}

View File

@ -0,0 +1,60 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
// You will need to create this BaseRequest class as abstracted from the AbstractRequest;
use Omnipay\Rotessa\Message\Request\BaseRequest;
use Omnipay\Rotessa\Message\Request\RequestInterface;
class PostCustomers extends BaseRequest implements RequestInterface
{
protected $endpoint = '/customers';
protected $method = 'POST';
protected static $model = 'Customer';
public function setId(string $value) {
$this->setParameter('id',$value);
}
public function setCustomIdentifier(string $value) {
$this->setParameter('custom_identifier',$value);
}
public function setName(string $value) {
$this->setParameter('name',$value);
}
public function setEmail(string $value) {
$this->setParameter('email',$value);
}
public function setCustomerType(string $value) {
$this->setParameter('customer_type',$value);
}
public function setHomePhone(string $value) {
$this->setParameter('home_phone',$value);
}
public function setPhone(string $value) {
$this->setParameter('phone',$value);
}
public function setBankName(string $value) {
$this->setParameter('bank_name',$value);
}
public function setInstitutionNumber(string $value = '') {
$this->setParameter('institution_number',$value);
}
public function setTransitNumber(string $value = '') {
$this->setParameter('transit_number',$value);
}
public function setBankAccountType(string $value) {
$this->setParameter('bank_account_type',$value);
}
public function setAuthorizationType(string $value = '') {
$this->setParameter('authorization_type',$value);
}
public function setRoutingNumber(string $value = '') {
$this->setParameter('routing_number',$value);
}
public function setAccountNumber(string $value) {
$this->setParameter('account_number',$value);
}
public function setAddress(array $value) {
$this->setParameter('address',$value);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
// You will need to create this BaseRequest class as abstracted from the AbstractRequest;
use Omnipay\Rotessa\Message\Request\BaseRequest;
use Omnipay\Rotessa\Message\Request\RequestInterface;
class PostCustomersShowWithCustomIdentifier extends BaseRequest implements RequestInterface
{
protected $endpoint = '/customers/show_with_custom_identifier';
protected $method = 'POST';
protected static $model = null;
public function setCustomIdentifier(string $value) {
$this->setParameter('custom_identifier',$value);
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
// You will need to create this BaseRequest class as abstracted from the AbstractRequest;
use Omnipay\Rotessa\Message\Request\BaseRequest;
use Omnipay\Rotessa\Message\Request\RequestInterface;
class PostTransactionSchedules extends BaseRequest implements RequestInterface
{
protected $endpoint = '/transaction_schedules';
protected $method = 'POST';
protected static $model = 'TransactionSchedule';
public function setCustomerId(string $value) {
$this->setParameter('customer_id',$value);
}
public function setProcessDate(string $value) {
$this->setParameter('process_date',$value);
}
public function setFrequency(string $value) {
$this->setParameter('frequency',$value);
}
public function setInstallments(int $value) {
$this->setParameter('installments',$value);
}
public function setComment(string $value) {
$this->setParameter('comment',$value);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
use Omnipay\Rotessa\Message\Request\RequestInterface;
class PostTransactionSchedulesCreateWithCustomIdentifier extends PostTransactionSchedules implements RequestInterface
{
protected $endpoint = '/transaction_schedules/create_with_custom_identifier';
protected $method = 'POST';
public function setCustomIdentifier(string $value) {
$this->setParameter('custom_identifier',$value);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
// You will need to create this BaseRequest class as abstracted from the AbstractRequest;
use Omnipay\Rotessa\Message\Request\BaseRequest;
use Omnipay\Rotessa\Message\Request\RequestInterface;
class PostTransactionSchedulesUpdateViaPost extends BaseRequest implements RequestInterface
{
protected $endpoint = '/transaction_schedules/update_via_post';
protected $method = 'POST';
public function setId(int $value) {
$this->setParameter('id',$value);
}
public function setAmount(int $value) {
$this->setParameter('amount',$value);
}
public function setComment(string $value) {
$this->setParameter('comment',$value);
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace Omnipay\Rotessa\Message\Request;
use Omnipay\Rotessa\Message\Response\ResponseInterface;
use Omnipay\Common\Message\RequestInterface as MessageInterface;
interface RequestInterface extends MessageInterface
{
}

View File

@ -0,0 +1,16 @@
<?php
namespace Omnipay\Rotessa\Message\Response;
use Omnipay\Common\Message\AbstractResponse as Response;
abstract class AbstractResponse extends Response implements ResponseInterface
{
abstract public function getData();
abstract public function getCode();
abstract public function getMessage();
abstract public function getParameter(string $key);
}

View File

@ -0,0 +1,44 @@
<?php
namespace Omnipay\Rotessa\Message\Response;
use Omnipay\Rotessa\Message\Request\RequestInterface;
use Omnipay\Rotessa\Message\Response\ResponseInterface;
use Omnipay\Common\Message\AbstractResponse as Response;
class BaseResponse extends Response implements ResponseInterface
{
protected $code = 0;
protected $message = null;
function __construct(RequestInterface $request, array $data = [], int $code = 200, string $message = null ) {
parent::__construct($request, $data);
$this->code = $code;
$this->message = $message;
}
public function getData() {
return $this->getParameters();
}
public function getCode() {
return (int) $this->code;
}
public function isSuccessful() {
return $this->code < 300;
}
public function getMessage() {
return $this->message;
}
protected function getParameters() {
return $this->data;
}
public function getParameter(string $key) {
return $this->getParameters()[$key];
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace Omnipay\Rotessa\Message\Response;
use Omnipay\Common\Message\ResponseInterface as MessageInterface;
interface ResponseInterface extends MessageInterface
{
public function getParameter(string $key) ;
}

View File

@ -0,0 +1,63 @@
<?php
namespace Omnipay\Rotessa\Model;
use Omnipay\Common\ParametersTrait;
use Omnipay\Rotessa\Model\ModelInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Omnipay\Rotessa\Exception\ValidationException;
abstract class AbstractModel implements ModelInterface {
use ParametersTrait;
abstract public function jsonSerialize() : array;
public function validate() : bool {
$required = array_diff_key( array_flip($this->required), array_filter($this->getParameters()) );
if(!empty($required)) throw new ValidationException("Could not validate " . implode(",", array_keys($required)) );
return true;
}
public function __get($key) {
return array_key_exists($key, $this->attributes) ? $this->getParameter($key) : null;
}
public function __set($key, $value) {
if(array_key_exists($key, $this->attributes)) $this->setParameter($key, $value);
}
public function __toString() : string {
return json_encode($this);
}
public function toString() : string {
return $this->__toString();
}
public function __toArray() : array {
return $this->getParameters();
}
public function toArray() : array {
return $this->__toArray();
}
public function initialize(array $params = []) {
$this->parameters = new ParameterBag;
$parameters = array_merge($this->defaults, $params);
if ($parameters) {
foreach ($this->attributes as $param => $type) {
$value = @$parameters[$param];
if($value){
settype($value, $type);
$this->setParameter($param, $value);
}
}
}
return $this;
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Omnipay\Rotessa\Model;
use \DateTime;
use Omnipay\Rotessa\Model\AbstractModel;
use Omnipay\Rotessa\Model\ModelInterface;
class BaseModel extends AbstractModel implements ModelInterface {
protected $attributes = [
"id" => "string"
];
protected $required = ['id'];
protected $defaults = ['id' => 0 ];
public function __construct($parameters = array()) {
$this->initialize($parameters);
}
public function jsonSerialize() : array {
return array_intersect_key($this->toArray(), array_flip($this->required) );
}
}

View File

@ -0,0 +1,94 @@
<?php
namespace Omnipay\Rotessa\Model;
use Omnipay\Rotessa\Object\Country;
use Omnipay\Rotessa\Object\Address;
use Omnipay\Rotessa\Model\BaseModel;
use Omnipay\Rotessa\Object\CustomerType;
use Omnipay\Rotessa\Model\ModelInterface;
use Omnipay\Rotessa\Object\BankAccountType;
use Omnipay\Rotessa\Object\AuthorizationType;
use Omnipay\Rotessa\Exception\ValidationException;
class CustomerModel extends BaseModel implements ModelInterface {
protected $attributes = [
"id" => "string",
"custom_identifier" => "string",
"name" => "string",
"email" => "string",
"customer_type" => "string",
"home_phone" => "string",
"phone" => "string",
"bank_name" => "string",
"institution_number" => "string",
"transit_number" => "string",
"bank_account_type" => "string",
"authorization_type" => "string",
"routing_number" => "string",
"account_number" => "string",
"address" => "object",
"transaction_schedules" => "array",
"financial_transactions" => "array",
"active" => "bool"
];
protected $defaults = ["active" => false,"customer_type" =>'Business',"bank_account_type" =>'Savings',"authorization_type" =>'Online',];
protected $required = ["name","email","customer_type","home_phone","phone","bank_name","institution_number","transit_number","bank_account_type","authorization_type","routing_number","account_number","address",'custom_identifier'];
public function validate() : bool {
try {
$country = $this->address->country;
if(!self::isValidCountry($country)) throw new \Exception("Invalid country!");
$this->required = array_diff($this->required, Country::isAmerican($country) ? ["institution_number", "transit_number"] : ["bank_account_type", "routing_number"]);
parent::validate();
if(Country::isCanadian($country) ) {
if(!self::isValidTransitNumber($this->getParameter('transit_number'))) throw new \Exception("Invalid transit number!");
if(!self::isValidInstitutionNumber($this->getParameter('institution_number'))) throw new \Exception("Invalid institution number!");
}
if(!self::isValidCustomerType($this->getParameter('customer_type'))) throw new \Exception("Invalid customer type!");
if(!self::isValidBankAccountType($this->getParameter('bank_account_type'))) throw new \Exception("Invalid bank account type!");
if(!self::isValidAuthorizationType($this->getParameter('authorization_type'))) throw new \Exception("Invalid authorization type!");
} catch (\Throwable $th) {
throw new ValidationException($th->getMessage());
}
return true;
}
public static function isValidCountry(string $country ) : bool {
return Country::isValidCountryCode($country) || Country::isValidCountryName($country);
}
public static function isValidTransitNumber(string $value ) : bool {
return strlen($value) == 5;
}
public static function isValidInstitutionNumber(string $value ) : bool {
return strlen($value) == 3;
}
public static function isValidCustomerType(string $value ) : bool {
return CustomerType::isValid($value);
}
public static function isValidBankAccountType(string $value ) : bool {
return BankAccountType::isValid($value);
}
public static function isValidAuthorizationType(string $value ) : bool {
return AuthorizationType::isValid($value);
}
public function toArray() : array {
return [ 'address' => (array) $this->getParameter('address') ] + parent::toArray();
}
public function jsonSerialize() : array {
$address = (array) $this->getParameter('address');
unset($address['country']);
return compact('address') + parent::jsonSerialize();
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Omnipay\Rotessa\Model;
use Omnipay\Rotessa\Object\Country;
use Omnipay\Rotessa\Object\Address;
use Omnipay\Rotessa\Object\CustomerType;
use Omnipay\Rotessa\Model\ModelInterface;
use Omnipay\Rotessa\Object\BankAccountType;
use Omnipay\Rotessa\Object\AuthorizationType;
use Omnipay\Rotessa\Exception\ValidationException;
class CustomerPatchModel extends CustomerModel implements ModelInterface {
protected $required = ["id","custom_identifier","name","email","customer_type","home_phone","phone","bank_name","institution_number","transit_number","bank_account_type","authorization_type","routing_number","account_number","address"];
}

View File

@ -0,0 +1,8 @@
<?php
namespace Omnipay\Rotessa\Model;
interface ModelInterface extends \JsonSerializable
{
public function __toArray();
public function __toString();
}

View File

@ -0,0 +1,84 @@
<?php
namespace Omnipay\Rotessa\Model;
use \DateTime;
use Omnipay\Rotessa\Model\BaseModel;
use Omnipay\Rotessa\Object\Frequency;
use Omnipay\Rotessa\Model\ModelInterface;
use Omnipay\Rotessa\Exception\ValidationException;
class TransactionScheduleModel extends BaseModel implements ModelInterface {
protected $properties;
protected $attributes = [
"id" => "string",
"amount" => "float",
"comment" => "string",
"created_at" => "date",
"financial_transactions" => "array",
"frequency" => "string",
"installments" => "integer",
"next_process_date" => "date",
"process_date" => "date",
"updated_at" => "date",
"customer_id" => "string",
"custom_identifier" => "string",
];
public const DATE_FORMAT = 'F j, Y';
protected $defaults = ["amount" =>0.00,"comment" =>' ',"financial_transactions" =>0,"frequency" =>'Once',"installments" =>1];
protected $required = ["amount","comment","frequency","installments","process_date"];
public function validate() : bool {
try {
parent::validate();
if(!self::isValidDate($this->process_date)) throw new \Exception("Could not validate date ");
if(!self::isValidFrequency($this->frequency)) throw new \Exception("Invalid frequency");
if(is_null($this->customer_id) && is_null($this->custom_identifier)) throw new \Exception("customer id or custom identifier is invalid");
} catch (\Throwable $th) {
throw new ValidationException($th->getMessage());
}
return true;
}
public function jsonSerialize() : array {
return ['customer_id' => $this->getParameter('customer_id'), 'custom_identifier' => $this->getParameter('custom_identifier') ] + parent::jsonSerialize() ;
}
public function __toArray() : array {
return parent::__toArray() ;
}
public function initialize(array $params = [] ) {
$o_params = array_intersect_key(
$params = array_intersect_key($params, $this->attributes),
($attr = array_filter($this->attributes, fn($p) => $p != "date"))
);
parent::initialize($o_params);
$d_params = array_diff_key($params, $attr);
array_walk($d_params, function($v,$k) {
$this->setParameter($k, self::formatDate( $v) );
}, );
return $this;
}
public static function isValidDate($date) : bool {
$d = DateTime::createFromFormat(self::DATE_FORMAT, $date);
// Check if the date is valid and matches the format
return $d && $d->format(self::DATE_FORMAT) === $date;
}
public static function isValidFrequency($value) : bool {
return Frequency::isValid($value);
}
protected static function formatDate($date) : string {
$d = new DateTime($date);
return $d->format(self::DATE_FORMAT);
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Omnipay\Rotessa\Model;
use Omnipay\Rotessa\Model\BaseModel;
use Omnipay\Rotessa\Model\ModelInterface;
class TransactionSchedulesIdBodyModel extends BaseModel implements ModelInterface {
protected $properties;
protected $attributes = [
"amount" => "int",
"comment" => "string",
];
public const DATE_FORMAT = 'Y-m-d H:i:s';
private $_is_error = false;
protected $defaults = ["amount" =>0,"comment" =>'0',];
protected $required = ["amount","comment",];
}

View File

@ -0,0 +1,24 @@
<?php
namespace Omnipay\Rotessa\Model;
use Omnipay\Rotessa\Model\BaseModel;
use Omnipay\Rotessa\Model\ModelInterface;
class TransactionSchedulesUpdateViaPostBodyModel extends BaseModel implements ModelInterface {
protected $properties;
protected $attributes = [
"id" => "int",
"amount" => "int",
"comment" => "string",
];
public const DATE_FORMAT = 'Y-m-d H:i:s';
private $_is_error = false;
protected $defaults = ["amount" =>0,"comment" =>'0',];
protected $required = ["amount","comment",];
}

View File

@ -0,0 +1,53 @@
<?php
namespace Omnipay\Rotessa\Object;
use Omnipay\Common\ParametersTrait;
final class Address implements \JsonSerializable {
use ParametersTrait;
protected $attributes = [
"address_1" => "string",
"address_2" => "string",
"city" => "string",
"id" => "int",
"postal_code" => "string",
"province_code" => "string",
"country" => "string"
];
protected $required = ["address_1","address_2","city","postal_code","province_code",];
public function jsonSerialize() {
return array_intersect_key($this->getParameters(), array_flip($this->required));
}
public function getCountry() : string {
return $this->getParameter('country');
}
public function initialize(array $parameters) {
foreach($this->attributes as $param => $type) {
$value = @$parameters[$param] ;
settype($value, $type);
$value = $value ?? null;
$this->parameters->set($param, $value);
}
}
public function __toArray() : array {
return $this->getParameters();
}
public function __toString() : string {
return $this->getFullAddress();
}
public function getFullAddress() :string {
$full_address = $this->getParameters();
extract($full_address);
return "$address_1 $address_2, $city, $postal_code $province_code, $country";
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Omnipay\Rotessa\Object;
use Omnipay\Rotessa\IsValidTypeTrait;
final class AuthorizationType {
use isValidTypeTrait;
const IN_PERSON = "In Person";
const ONLINE = "Online";
public static function isInPerson($value) {
return $value === self::IN_PERSON;
}
public static function isOnline($value) {
return $value === self::ONLINE;
}
public static function getTypes() : array {
return [
self::IN_PERSON,
self::ONLINE
];
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Omnipay\Rotessa\Object;
use Omnipay\Rotessa\IsValidTypeTrait;
final class BankAccountType {
use IsValidTypeTrait;
const SAVINGS = "Savings";
const CHECKING = "Checking";
public static function isSavings($value) {
return $value === self::SAVINGS;
}
public static function isChecking($value) {
return $value === self::Checking;
}
public static function getTypes() : array {
return [
self::SAVINGS,
self::CHECKING
];
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace Omnipay\Rotessa\Object;
use Omnipay\Rotessa\IsValidTypeTrait;
final class Country {
use IsValidTypeTrait;
protected static $codes = ['CA','US'];
protected static $names = ['United States', 'Canada'];
public static function isValidCountryName(string $value) {
return in_array($value, self::$names);
}
public static function isValidCountryCode(string $value) {
return in_array($value, self::$codes);
}
public static function isAmerican(string $value) : bool {
return $value == 'US' || $value == 'United States';
}
public static function isCanadian(string $value) : bool {
return $value == 'CA' || $value == 'Canada';
}
public static function getTypes() : array {
return $codes + $names;
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Omnipay\Rotessa\Object;
use Omnipay\Rotessa\IsValidTypeTrait;
final class CustomerType {
use IsValidTypeTrait;
const PERSONAL = "Personal";
const BUSINESS = "Business";
public static function isPersonal($value) {
return $value === self::PERSONAL;
}
public static function isBusiness($value) {
return $value === self::BUSINESS;
}
public static function getTypes() : array {
return [
self::PERSONAL,
self::BUSINESS
];
}
}

View File

@ -0,0 +1,64 @@
<?php
namespace Omnipay\Rotessa\Object;
use Omnipay\Rotessa\IsValidTypeTrait;
final class Frequency {
use IsValidTypeTrait;
const ONCE = "Once";
const WEEKLY = "Weekly";
const OTHER_WEEK = "Every Other Week";
const MONTHLY= "Monthly";
const OTHER_MONTH = "Every Other Month";
const QUARTERLY = "Quarterly";
const SEMI_ANNUALLY = "Semi-Annually";
const YEARLY = "Yearly";
public static function isOnce($value) {
return $value === self::ONCE;
}
public static function isWeekly($value) {
return $value === self::WEEKLY;
}
public static function isOtherWeek($value) {
return $value === self::OTHER_WEEK;
}
public static function isMonthly($value) {
return $value === self::MONTHLY;
}
public static function isOtherMonth($value) {
return $value === self::OTHER_MONTH;
}
public static function isQuarterly($value) {
return $value === self::QUARTERLY;
}
public static function isSemiAnnually($value) {
return $value === self::SEMI_ANNUALLY;
}
public static function isYearly($value) {
return $value === self::YEARLY;
}
public static function getTypes() : array {
return [
self::ONCE,
self::WEEKLY,
self::OTHER_WEEK,
self::MONTHLY,
self::OTHER_MONTH,
self::QUARTERLY,
self::SEMI_ANNUALLY,
self::YEARLY
];
}
}