Skip to content

Commit

Permalink
Refactor Handler and add exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ovac committed Aug 14, 2017
1 parent 1724c37 commit a2380ba
Show file tree
Hide file tree
Showing 21 changed files with 950 additions and 99 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/docs
/.phpintel
composer.phar
couscous.phar


# -- Bower -- #
Expand Down
62 changes: 14 additions & 48 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use OVAC\HubtelPayment\Config;
use OVAC\HubtelPayment\ConfigInterface;
use OVAC\HubtelPayment\Exception\Handler;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use OVAC\HubtelPayment\Utility\HubtelHandler;

/**
* Api Class
Expand Down Expand Up @@ -73,7 +67,7 @@ public function injectConfig(Config $config)
/**
* Change the Default baseUrl defined by hubtel
*
* @param string $baseUrl [description]
* @param string $baseUrl The hubtel Resource Base URL
* @return self
*/
public function setBaseUrl($baseUrl)
Expand Down Expand Up @@ -142,6 +136,9 @@ public function _options($url = null, array $parameters = [])
}
/**
* {@inheritdoc}
*
* @throws \RuntimeException
* @throws \Handler
*/
public function execute($httpMethod, $url, array $parameters = [])
{
Expand All @@ -150,15 +147,14 @@ public function execute($httpMethod, $url, array $parameters = [])
$response = $this->getClient()->{$httpMethod}($url, ['query' => $parameters]);

return json_decode((string) $response->getBody(), true);
} catch (RequestInterface $e) {
throw new ClientException($e->getMessage(), $e);
} catch (ClientException $e) {
throw new Handler($e);
}

return;
}

throw new \RuntimeException('The API requires a configuration instance.');

}
/**
* Returns an Http client instance.
Expand All @@ -171,50 +167,20 @@ protected function getClient()

return new Client(
[
'base_uri' => $this->baseUrl . $config->getAccountNumber(), 'handler' => $this->createHandler(),
'base_uri' => $this->baseUrl . $config->getAccountNumber(),
'handler' => $this->createHandler($this->config),
]
);
}

/**
* Create the client handler.
*
* @return \GuzzleHttp\HandlerStack
* @SuppressWarnings(PHPMD.StaticAccess)
* @param \OVAC\HubtelPayment\Config $config
* @return \GuzzleHttp\HandlerStack
*/
protected function createHandler()
protected function createHandler(Config $config)
{
$stack = HandlerStack::create();

$stack->push(
Middleware::mapRequest(
function (RequestInterface $request) {
$config = $this->config;
$request = $request->withHeader('User-Agent', 'OVAC-Hubtel-Payment' . $config->getPackageVersion());
$request = $request->withHeader('Authorization', 'Basic ' . base64_encode($config->getClientId() . ':' . $config->getClientSecret()));

return $request;
}
)
);

$stack->push(
Middleware::retry(
function (
$retries,
RequestInterface $request,
ResponseInterface $response = null,
TransferException $exception = null
) {
return $retries < 3 && ($exception instanceof ConnectException || (
$response && $response->getStatusCode() >= 500
));
},
function ($retries) {
return (int) pow(2, $retries) * 1000;
}
)
);

return $stack;
return (new HubtelHandler($config))->createHandler();
}
}
5 changes: 3 additions & 2 deletions src/Api/Transaction/MassAssignable.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public function make($data = [])

/**
* This method is used to mass assign the properties required by the Hubtel ReceiveMoney and SendMoney Api
* @param array $data
*
* @param array $data
* @example ['amount' => 10, 'customer' => ['name' => 'victor', ...], 'clientReference' => 123, 'callbackOnSuccess' => 'url', 'amount' => 10, 'description' => 'some description']
* @return self
* @return self
*/
protected function massAssign($data = [])
{
Expand Down
43 changes: 35 additions & 8 deletions src/Api/Transaction/ReceiveMoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use OVAC\HubtelPayment\Api\Transaction\MassAssignable;
use OVAC\HubtelPayment\Api\Transaction\Transaction;
use OVAC\HubtelPayment\Utility\CanCleanParameters;

/**
* Class ReceiveMoney
Expand All @@ -29,6 +30,7 @@ class ReceiveMoney extends Transaction
{
use MassAssignable;
use Transactable;
use CanCleanParameters;

/**
* The 6 digit unique token required to debit a Vodafone
Expand All @@ -48,8 +50,30 @@ class ReceiveMoney extends Transaction
* @var boolean
*/
protected $feesOnCustomer;
/**
* {@inheritdoc}
*/
protected $parametersRequired = [
'CustomerName',
'CustomerMsisdn',
'Amount',
'PrimaryCallbackURL',
'Description',
'Channel',
];
/**
* {@inheritdoc}
*/
protected $parametersOptional = [
'CustomerEmail',
'SecondaryCallbackURL',
'ClientReference',
'FeesOnCustomer',
'Token',
];
/**
* Construct for creating a new instance of the ReceiveMoney Api class
*
* @param array $data An array with configurations for the receive money class
*/
public function __construct($data = [])
Expand Down Expand Up @@ -130,14 +154,6 @@ public function feesOnCustomer($feesOnCustomer)
return $this->setFeesOnCustomer($feesOnCustomer);
}

/**
* [run description]
* @return [type] [description]
*/
public function run()
{
}

/**
* @return string
*/
Expand Down Expand Up @@ -177,4 +193,15 @@ public function setFeesOnCustomer($feesOnCustomer)

return $this;
}
/**
* The method runs the transaction
*
* @return
*/
public function run()
{
if ($this->propertiesPassRequired()) {
$this->_get('/receive/mobilemoney', $this->propertiesToArray());
}
}
}
1 change: 1 addition & 0 deletions src/Api/Transaction/SendMoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SendMoney extends Transaction

/**
* Construct for creating a new instance of the SendMoney Api class
*
* @param array $data An array with configurations for the send money class
*/
public function __construct($data = [])
Expand Down
27 changes: 26 additions & 1 deletion src/Api/Transaction/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ class Transaction extends Api
*/
protected $description;

/**
* Defines an array of required properties
*
* Names must correspond with properties/parameters on the class
* and the properties must accessors defined on the
* instance
*
* @var array
*/
protected $parametersRequired = [];
/**
* Defines an array of the names of optional properties/parameters names
*
* Names must correspond with properties on the class
* and the properties must accessors defined on the
* instance
*
* @var array
*/
protected $parametersOptional = [];

/**
* returnes the name of the give customer
*
Expand Down Expand Up @@ -161,6 +182,7 @@ public function setCustomerMsisdn($customerMsisdn)

/**
* returns the transaction channel (Mobile Network)
*
* @return string
*/
public function getChannel()
Expand Down Expand Up @@ -281,6 +303,7 @@ public function setClientReference($clientReference)

/**
* gets the description of the transaction.
*
* @return string
*/
public function getDescription()
Expand Down Expand Up @@ -335,6 +358,7 @@ public function setCustomer($data = [])
}
/**
* This method sests a single callback for both the success and Error
*
* @param string $primaryCallbackURL A url for callbacks from the hubtel server
* @return self
*/
Expand All @@ -350,7 +374,8 @@ public function callback($primaryCallbackURL)

/**
* This method sets the callbacks for the Hubtel Payments
* @param array|string $data
*
* @param array|string $data
* @return self
*/
public function setCallback($data = [])
Expand Down
2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Config implements ConfigInterface
*
* @var string
*/
protected $accoutNumber;
protected $accountNumber;
/**
* The Hubtel Developer Applicaton Client Id.
*
Expand Down
23 changes: 23 additions & 0 deletions src/Exception/BadRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* @package OVAC/Hubtel-Payment
* @version 1.0.0
* @link https://github.com/ovac/hubtel-payment
*
* @author Ariama O. Victor (OVAC) <contact@ovac4u.com>
* @link http://ovac4u.com
*
* @license https://github.com/ovac/hubtel-payment/blob/master/LICENSE
* @copyright (c) 2017, Rescope Inc
*/

namespace OVAC\HubtelPayment\Exception;

/**
* Class BadRequestException
* throws OVAC\HubtelPayment\Exception\BadRequestException
*/
class BadRequestException extends HubtelException
{
}

0 comments on commit a2380ba

Please sign in to comment.