Skip to content

Commit

Permalink
fix bug in error response and updates
Browse files Browse the repository at this point in the history
  • Loading branch information
karson committed Jun 30, 2022
1 parent 4b90642 commit 49011fd
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 45 deletions.
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">= 7.2",
"guzzlehttp/guzzle": "7.*"
"php": ">=7.2",
"guzzlehttp/guzzle": "7.*",
"ext-curl": "*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "6.*",
"vlucas/phpdotenv": "^4.0"
"phpunit/phpunit": "^9.5",
"vlucas/phpdotenv": "^5.4"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 4 additions & 2 deletions src/Paymentsds/MPesa/ErrorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Paymentsds\MPesa;

class ErrorType {
class ErrorType
{
const SUCCESS = 'SUCCESS';
const INTERNAL_ERROR = 'INTERNAL_ERROR';
const TRANSACTION_CANCELLED_BY_CUSTOMER = 'TRANSACTION_CANCELLED_BY_CUSTOMER';
Expand Down Expand Up @@ -37,4 +38,5 @@ class ErrorType {

const INVALID_INPUT = 'INVALID_INPUT';
const MISSING_INPUT = 'MISSING_INPUT';
}
const UNAUTHORIZED_API_OR_SESSION = 'UNAUTHORIZED_API_OR_SESSION';
}
11 changes: 7 additions & 4 deletions src/Paymentsds/MPesa/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Paymentsds\MPesa;

class Response {
class Response
{
const PARAMS = [
'success',
'error',
Expand All @@ -13,17 +14,19 @@ class Response {
private $error;
private $data;

public function __construct($success, $error, $data) {
public function __construct($success, $error, $data = null)
{
$this->success = $success;
$this->error= $error;
$this->data = $data;
}

public function __get($property) {
public function __get($property)
{
if (in_array($property, self::PARAMS)) {
return $this->{$property};
}

return null;
}
}
}
44 changes: 24 additions & 20 deletions src/Paymentsds/MPesa/Service.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php
namespace Paymentsds\MPesa;

use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Client;
use Paymentsds\MPesa\Response;

use Paymentsds\MPesa\ErrorType;
use Paymentsds\MPesa\Configuration;
use Paymentsds\MPesa\Exception\AuthenticationException;
use Paymentsds\MPesa\Exception\InvalidHostException;
use Paymentsds\MPesa\Exception\MissingPropertiesException;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use Paymentsds\MPesa\Exception\TimeoutException;
use Paymentsds\MPesa\Exception\ValidationException;
use Paymentsds\MPesa\Exception\InvalidHostException;
use Paymentsds\MPesa\Exception\AuthenticationException;
use Paymentsds\MPesa\Exception\InvalidReceiverException;
use Paymentsds\MPesa\ErrorType;
use Paymentsds\MPesa\Response;
use Paymentsds\MPesa\Exception\MissingPropertiesException;

class Service
{
Expand All @@ -25,11 +26,11 @@ public function __construct($args)
}

public function handleSend($intent)
{
{
try {
$opcode = $this->detectOperation($intent);

return $this->handleRequest($opcode, $intent);
return $this->handleRequest($opcode, $intent);
} catch (InvalidReceiverException $e) {
return new Response(false, ErrorType::INVALID_RECEIVER);
}
Expand Down Expand Up @@ -199,29 +200,32 @@ private function performRequest($opcode, $intent)
$data['json'] = $body;
}

$httpClient = new \GuzzleHttp\Client([
$httpClient = new Client([
'base_uri' => $baseURL,
'timeout' => $this->config->timeout,
]);


try {
$response = $httpClient->request(strtoupper($operation['method']), $operation['path'], $data);
$body = json_decode($response->getBody()->getContents(), true);


return new Response(true, null, $this->buildResponse($body));
} catch (ConnectionException $e) {

} catch (ClientException | ServerException $e) {
} catch (ClientException | ServerException $e) {
$response = $e->getResponse();
$body = json_decode($response->getBody()->getContents(), true);

$errorType = $this->detectErrorType($body['output_ResponseCode']);
if (isset($body['output_error'])) {
$errorType = ErrorType::UNAUTHORIZED_API_OR_SESSION;
} elseif (isset($body['output_ResponseCode'])) {
$errorType = $this->detectErrorType($body['output_ResponseCode']);
} else {
$errorType = ErrorType::UNKNOWN;
}

return new Response(false, $errorType, $this->buildResponse($body));
} catch (Exception $e) {
} catch (\Exception $e) {
return new Response(false, null, null);
}
}
} else {
throw new AuthenticationException('No auth data');
}
Expand All @@ -237,7 +241,6 @@ private function generateAccessToken()

private function buildResponse($body)
{

$mapping = [
'output_ConversationID' => 'conversation',
'output_ResponseCode' => 'code',
Expand All @@ -257,7 +260,8 @@ private function buildResponse($body)
return $output;
}

private function detectErrorType($code) {
private function detectErrorType($code)
{
$mapping = [
'INS-0' => ErrorType::SUCCESS,
'INS-1' => ErrorType::INTERNAL_ERROR,
Expand Down
30 changes: 15 additions & 15 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use PHPUnit\Framework\TestCase;
use Dotenv\Dotenv;

class ClientTest extends TestCase{

public function setUp()
class ClientTest extends TestCase
{
public function setup():void
{
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
Expand All @@ -18,51 +18,51 @@ public function setUp()
'serviceProviderCode' => $_ENV['SERVICE_PROVIDER_CODE'] // input_ServiceProviderCode
]);

$this->clientRevert = new Client([
$this->clientRevert = new Client([
'apiKey' => $_ENV['API_KEY'], // API Key
'publicKey' => $_ENV['PUBLIC_KEY'], // Public Key
'serviceProviderCode' => $_ENV['SERVICE_PROVIDER_CODE'], // input_ServiceProviderCode
'initiatorIdentifier' => $_ENV['INITIATOR_IDENTIFIER'], // input_InitiatorIdentifier,
'securityCredential' => $_ENV['SECURITY_CREDENTIAL'] // input_SecurityCredential
]);

$this->paymentDataSend = [
$this->paymentDataSend = [
'to' => $_ENV['PHONE_NUMBER'], // input_CustomerMSISDN
'reference' => '11115' . rand(1, 99), // input_ThirdPartyReference
'transaction' => 'T12344CC', // input_TransactionReference
'amount' => '10' // input_Amount
];

$this->paymentDataReceive = [
$this->paymentDataReceive = [
'from' => $_ENV['PHONE_NUMBER'], // input_CustomerMSISDN
'reference' => '11114' . rand(1, 99), // input_ThirdPartyReference
'transaction' => 'T12344CC', // input_TransactionReference
'amount' => '10' // input_Amount
];

$this->paymentDataBusiness = [
$this->paymentDataBusiness = [
'to' => '979797', // input_ReceiverPartyCode
'reference' => '11114' . rand(1, 99), // input_ThirdPartyReference
'transaction' => 'T12344CC', // input_TransactionReference
'amount' => '10' // input_Amount
];

$this->paymentDataRevert = [
$this->paymentDataRevert = [
'reference' => '11114' . rand(1, 99), // input_ThirdPartyReference
'transaction' => 'T12344CC', // input_TransactionReference
'amount' => '10' // input_Amount
];
}
public function testSend()
{
$result = $this->client->send($this->paymentDataSend);
$this->assertTrue($result->success);
{
$result = $this->client->send($this->paymentDataSend);
$this->assertTrue($result->success);
}

public function testSendBusiness()
{
$result = $this->client->send($this->paymentDataBusiness);
$this->assertTrue($result->success);
{
$result = $this->client->send($this->paymentDataBusiness);
$this->assertTrue($result->success);
}

public function testReceive()
Expand All @@ -76,4 +76,4 @@ public function testRevert()
$result = $this->clientRevert->revert($this->paymentDataRevert);
$this->assertTrue($result->success);
}
}
}

0 comments on commit 49011fd

Please sign in to comment.