Skip to content

Commit

Permalink
cleaned up Subaccount naming and others
Browse files Browse the repository at this point in the history
  • Loading branch information
rukykf committed Jan 15, 2020
1 parent 551276c commit 74aab64
Show file tree
Hide file tree
Showing 17 changed files with 173 additions and 166 deletions.
3 changes: 2 additions & 1 deletion src/AuthBill.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function __construct($authorization_code = null, $customer_email = null,

public function charge()
{
return $this->paymentProvider->chargeAuth($this->attributes);
$this->paymentReference = $this->paymentProvider->chargeAuth($this->attributes);
return $this->paymentReference;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class FailedTransactionException extends BadResponseException
class FailedPaymentException extends BadResponseException
{
/**
* @param RequestInterface $httpRequest
Expand Down
26 changes: 13 additions & 13 deletions src/PaymentProviders/Base/AbstractPaymentProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
use Kofi\NgPayments\Exceptions\FailedTransactionException;
use Kofi\NgPayments\Exceptions\FailedPaymentException;
use Kofi\NgPayments\Exceptions\InvalidRequestBodyException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -52,7 +52,7 @@ abstract class AbstractPaymentProvider
/**
* @var bool
*/
protected $transactionExceptions = true;
protected $paymentExceptions = true;

public function __construct($public_key, $secret_key, $app_env)
{
Expand Down Expand Up @@ -80,7 +80,7 @@ abstract public function initializePayment($request_body);
* @param string $reference
* @param float $expected_naira_amount
* @return boolean
* @throws FailedTransactionException if transactionExceptions are turned on
* @throws FailedPaymentException if paymentExceptions are enabled
*/
abstract public function isPaymentValid($reference, $expected_naira_amount);

Expand All @@ -95,7 +95,7 @@ abstract public function isPaymentValid($reference, $expected_naira_amount);
* @param array $request_body
* @return string|null transaction reference or null if the request failed
* @throws InvalidRequestBodyException
* @throws FailedTransactionException if transactionExceptions are enabled
* @throws FailedPaymentException if paymentExceptions are enabled
*/
abstract public function chargeAuth($request_body);

Expand Down Expand Up @@ -154,28 +154,28 @@ abstract public function deletePlan($plan_id);
* @throws InvalidRequestBodyException
* @throws BadResponseException if httpExceptions are enabled
*/
abstract public function saveSubAccount($request_body);
abstract public function saveSubaccount($request_body);

/**
* @param array $query_params
* @return array|null an array of subaccount assoc arrays or null if the request failed
* @throws BadResponseException if httpExceptions are enabled
*/
abstract public function fetchAllSubAccounts($query_params = []);
abstract public function fetchAllSubaccounts($query_params = []);

/**
* @param $subaccount_id
* @return array|null an assoc array containing the subaccount's data or null if the request failed
* @throws BadResponseException if httpExceptions are enabled
*/
abstract public function fetchSubAccount($subaccount_id);
abstract public function fetchSubaccount($subaccount_id);

/**
* @param $subaccount_id
* @return string|null a success message or null if the request failed
* @throws BadResponseException if httpExceptions are enabled
*/
abstract public function deleteSubAccount($subaccount_id);
abstract public function deleteSubaccount($subaccount_id);

/**
* @param array $query_params
Expand Down Expand Up @@ -257,27 +257,27 @@ public function setHttpClient(Client $http_client)
public function setErrorConfig($error_config)
{
$this->httpExceptions = @$error_config["http_exceptions"] ?? false;
$this->transactionExceptions = @$error_config["transaction_exceptions"] ?? true;
$this->paymentExceptions = @$error_config["transaction_exceptions"] ?? true;
}

public function enableHttpExceptions()
{
$this->httpExceptions = true;
}

public function enableTransactionExceptions()
public function enablePaymentExceptions()
{
$this->transactionExceptions = true;
$this->paymentExceptions = true;
}

public function disableHttpExceptions()
{
$this->httpExceptions = false;
}

public function disableTransactionExceptions()
public function disablePaymentExceptions()
{
$this->transactionExceptions = false;
$this->paymentExceptions = false;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/PaymentProviders/PaymentProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ public static function disableHttpExceptions()
self::$httpExceptions = false;
}

public static function enableTransactionExceptions()
public static function enablePaymentExceptions()
{
self::$transactionExceptions = true;
}

public static function disableTransactionExceptions()
public static function disablePaymentExceptions()
{
self::$transactionExceptions = false;
}
Expand Down Expand Up @@ -136,7 +136,7 @@ protected static function getValidConfig($config_array = [])
?? self::$config['app_env']
?? self::getConstant("APP_ENV")
?? self::getEnv("APP_ENV")
?? 'production';
?? 'testing';

$provider_public = strtoupper($config['provider']) . "_PUBLIC_KEY";
$config['public_key'] = $config_array['public_key']
Expand Down
34 changes: 17 additions & 17 deletions src/PaymentProviders/Paystack.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use Kofi\NgPayments\Exceptions\FailedTransactionException;
use Kofi\NgPayments\Exceptions\FailedPaymentException;
use Kofi\NgPayments\Exceptions\FeatureNotSupportedException;
use Kofi\NgPayments\Exceptions\InvalidRequestBodyException;
use Kofi\NgPayments\PaymentProviders\Base\AbstractPaymentProvider;
Expand Down Expand Up @@ -46,7 +46,7 @@ public function initializePayment($request_body)
* @param string $reference
* @param float $expected_naira_amount
* @return boolean
* @throws FailedTransactionException if transactionExceptions are turned on
* @throws FailedPaymentException if paymentExceptions are turned on
*/
public function isPaymentValid($reference, $expected_naira_amount)
{
Expand All @@ -59,12 +59,12 @@ public function isPaymentValid($reference, $expected_naira_amount)
$expected_amount = $expected_naira_amount * 100;
$amount_paid = @$response_body['data']['amount'];

if ($this->transactionExceptions == true && $status != 'success') {
throw new FailedTransactionException($request, $this->httpResponse);
if ($this->paymentExceptions == true && $status != 'success') {
throw new FailedPaymentException($request, $this->httpResponse);
}

if ($this->transactionExceptions == true && $amount_paid != $expected_amount) {
throw new FailedTransactionException($request, $this->httpResponse);
if ($this->paymentExceptions == true && $amount_paid != $expected_amount) {
throw new FailedPaymentException($request, $this->httpResponse);
}

if ($status == 'success' && $amount_paid == $expected_amount) {
Expand All @@ -81,7 +81,7 @@ public function isPaymentValid($reference, $expected_naira_amount)
* @param array $request_body
* @return string|null transaction reference or null if the request failed
* @throws InvalidRequestBodyException
* @throws FailedTransactionException if transactionExceptions are enabled
* @throws FailedPaymentException if paymentExceptions are enabled
*/
public function chargeAuth($request_body)
{
Expand All @@ -95,8 +95,8 @@ public function chargeAuth($request_body)
$this->sendRequest($request);

$status = @$this->getResponseBody()['data']['status'];
if ($this->transactionExceptions == true && $status != 'success') {
throw new FailedTransactionException($request, $this->httpResponse);
if ($this->paymentExceptions == true && $status != 'success') {
throw new FailedPaymentException($request, $this->httpResponse);
}

if ($status == 'success') {
Expand Down Expand Up @@ -195,16 +195,16 @@ public function deletePlan($plan_id)
* @throws InvalidRequestBodyException
* @throws BadResponseException if httpExceptions are enabled
*/
public function saveSubAccount($request_body)
public function saveSubaccount($request_body)
{
$relative_url = "/subaccount";
$request_body = $this->adaptBodyParamsToPaystackAPI($request_body);

$subaccount_id = @$request_body['id'] ?? @$request_body['subaccount_code'];
if ($subaccount_id == null) {
return $this->createSubAccount($request_body, $relative_url);
return $this->createSubaccount($request_body, $relative_url);
} else {
return $this->updateSubAccount($request_body, $subaccount_id, $relative_url);
return $this->updateSubaccount($request_body, $subaccount_id, $relative_url);
}
}

Expand All @@ -213,7 +213,7 @@ public function saveSubAccount($request_body)
* @return array|null an array of subaccount assoc arrays or null if the request failed
* @throws BadResponseException if httpExceptions are enabled
*/
public function fetchAllSubAccounts($query_params = [])
public function fetchAllSubaccounts($query_params = [])
{
$relative_url = "/subaccount";
$query_params = $this->adaptBodyParamsToPaystackAPI($query_params);
Expand All @@ -227,7 +227,7 @@ public function fetchAllSubAccounts($query_params = [])
* @return array|null an assoc array containing the subaccount's data or null if the request failed
* @throws BadResponseException if httpExceptions are enabled
*/
public function fetchSubAccount($subaccount_id)
public function fetchSubaccount($subaccount_id)
{
$relative_url = "/subaccount" . "/" . $subaccount_id;
$request = $this->createRequestForPaystack($relative_url, [], 'GET', true);
Expand All @@ -239,7 +239,7 @@ public function fetchSubAccount($subaccount_id)
* @param $subaccount_id
* @throws FeatureNotSupportedException if httpExceptions are enabled
*/
public function deleteSubAccount($subaccount_id)
public function deleteSubaccount($subaccount_id)
{
throw new FeatureNotSupportedException("Paystack does not provide an endpoint for deleting subaccounts");
}
Expand Down Expand Up @@ -302,7 +302,7 @@ private function updatePlan($request_body, $plan_id, string $relative_url)
* @throws InvalidRequestBodyException
* @throws BadResponseException if httpExceptions are enabled
*/
private function createSubAccount(array $request_body, string $relative_url)
private function createSubaccount(array $request_body, string $relative_url)
{
$request = $this->createRequestForPaystack($relative_url, $request_body);
$this->validateRequestBodyHasRequiredParams(
Expand All @@ -321,7 +321,7 @@ private function createSubAccount(array $request_body, string $relative_url)
* @return mixed|null
* @throws BadResponseException if httpExceptions are enabled
*/
private function updateSubAccount($request_body, $subaccount_id, string $relative_url)
private function updateSubaccount($request_body, $subaccount_id, string $relative_url)
{
$relative_url .= "/" . $subaccount_id;
$request = $this->createRequestForPaystack($relative_url, $request_body, 'PUT');
Expand Down
36 changes: 18 additions & 18 deletions src/PaymentProviders/Rave.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use Kofi\NgPayments\Exceptions\FailedTransactionException;
use Kofi\NgPayments\Exceptions\FailedPaymentException;
use Kofi\NgPayments\Exceptions\InvalidRequestBodyException;
use Kofi\NgPayments\PaymentProviders\Base\AbstractPaymentProvider;

Expand Down Expand Up @@ -44,7 +44,7 @@ public function initializePayment($request_body)
* @param string $reference
* @param float $expected_naira_amount
* @return bool
* @throws FailedTransactionException if transactionExceptions are enabled
* @throws FailedPaymentException if paymentExceptions are enabled
*/
public function isPaymentValid($reference, $expected_naira_amount)
{
Expand All @@ -55,12 +55,12 @@ public function isPaymentValid($reference, $expected_naira_amount)
$status = @$this->getResponseBody()["data"]["status"];
$amount_paid = @$this->getResponseBody()["data"]["chargedamount"];

if ($this->transactionExceptions == true && $status != "successful") {
throw new FailedTransactionException($request, $this->httpResponse);
if ($this->paymentExceptions == true && $status != "successful") {
throw new FailedPaymentException($request, $this->httpResponse);
}

if ($this->transactionExceptions == true && $amount_paid != $expected_naira_amount) {
throw new FailedTransactionException($request, $this->httpResponse);
if ($this->paymentExceptions == true && $amount_paid != $expected_naira_amount) {
throw new FailedPaymentException($request, $this->httpResponse);
}

if ($status == "successful" && $amount_paid == $expected_naira_amount) {
Expand All @@ -77,7 +77,7 @@ public function isPaymentValid($reference, $expected_naira_amount)
* @param array $request_body
* @return string|null transaction reference or null if the request failed
* @throws InvalidRequestBodyException
* @throws FailedTransactionException if transactionExceptions are enabled
* @throws FailedPaymentException if paymentExceptions are enabled
*/
public function chargeAuth($request_body)
{
Expand All @@ -100,8 +100,8 @@ public function chargeAuth($request_body)
$this->sendRequest($request);
$status = @$this->getResponseBody()["data"]["status"];

if ($this->transactionExceptions == true && $status != "successful") {
throw new FailedTransactionException($request, $this->httpResponse);
if ($this->paymentExceptions == true && $status != "successful") {
throw new FailedPaymentException($request, $this->httpResponse);
}

if ($status == "successful") {
Expand Down Expand Up @@ -209,17 +209,17 @@ public function deletePlan($plan_id)
* @throws InvalidRequestBodyException
* @throws BadResponseException if httpExceptions are enbabled
*/
public function saveSubAccount($request_body)
public function saveSubaccount($request_body)
{
$request_body = $this->adaptBodyParamsToRaveAPI(
$request_body,
["subaccount_code" => "id", "percentage_charge" => "split_value"]
);
$subaccount_id = @$request_body['id'] ?? @$request_body['subaccount_id'];
if ($subaccount_id == null) {
return $this->createSubAccount($request_body);
return $this->createSubaccount($request_body);
} else {
return $this->updateSubAccount($request_body, $subaccount_id);
return $this->updateSubaccount($request_body, $subaccount_id);
}
}

Expand All @@ -228,7 +228,7 @@ public function saveSubAccount($request_body)
* @return array|null an array of subaccount assoc arrays or null if the request failed
* @throws BadResponseException if httpExceptions are enabled
*/
public function fetchAllSubAccounts($query_params = [])
public function fetchAllSubaccounts($query_params = [])
{
$relative_url = "/v2/gpx/subaccounts";
$request = $this->createRequestForRave($relative_url, ['seckey' => $this->secretKey], 'GET', true);
Expand All @@ -241,7 +241,7 @@ public function fetchAllSubAccounts($query_params = [])
* @return array|null an assoc array containing the subaccount's data or null if the request failed
* @throws BadResponseException if httpExceptions are enabled
*/
public function fetchSubAccount($subaccount_id)
public function fetchSubaccount($subaccount_id)
{
$relative_url = "/v2/gpx/subaccounts/get/" . $subaccount_id;
$request = $this->createRequestForRave($relative_url, ['seckey' => $this->secretKey], 'GET', true);
Expand All @@ -254,7 +254,7 @@ public function fetchSubAccount($subaccount_id)
* @return string|null a success message or null if the request failed
* @throws BadResponseException if httpExceptions are enabled
*/
public function deleteSubAccount($subaccount_id)
public function deleteSubaccount($subaccount_id)
{
$relative_url = "/v2/gpx/subaccounts/delete";
$request = $this->createRequestForRave($relative_url, ['seckey' => $this->secretKey, 'id' => $subaccount_id]);
Expand Down Expand Up @@ -320,7 +320,7 @@ private function translateSubAccountId($subaccount_id)
if (is_int($subaccount_id)) {
return $subaccount_id;
}
$subaccount = $this->fetchSubAccount($subaccount_id);
$subaccount = $this->fetchSubaccount($subaccount_id);
return $subaccount['id'];
}

Expand All @@ -330,7 +330,7 @@ private function translateSubAccountId($subaccount_id)
* @throws InvalidRequestBodyException
* @throws BadResponseException if httpExceptions are enabled
*/
private function createSubAccount($request_body)
private function createSubaccount($request_body)
{
$relative_url = "/v2/gpx/subaccounts/create";
$request_body = $this->adaptBodyParamsToRaveAPI($request_body, $this->getRaveCreateSubAccountParams());
Expand All @@ -351,7 +351,7 @@ private function createSubAccount($request_body)
* @return mixed|null
* @throws BadResponseException if httpExceptions are enabled
*/
private function updateSubAccount($request_body, $subaccount_id)
private function updateSubaccount($request_body, $subaccount_id)
{
$relative_url = "/v2/gpx/subaccounts/edit";
$subaccount_id = $this->translateSubAccountId($subaccount_id);
Expand Down
Loading

0 comments on commit 74aab64

Please sign in to comment.