Skip to content

Commit

Permalink
Change classes name and add append method to the mobile money status
Browse files Browse the repository at this point in the history
  • Loading branch information
prinx committed Apr 26, 2021
1 parent 16abc29 commit c95c1ff
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/Payswitch/Contracts/MobileMoneyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ interface MobileMoneyInterface
* @param string|int|null $voucherCode
* @param array $curlOptions Additional Curl options to pass to the request
*
* @return MobileMoneyResponseInterface
* @return MobileMoneyStatusInterface
*/
public function pay(
$amount,
$msisdn,
$network,
$voucherCode = null,
$curlOptions = []
): MobileMoneyResponseInterface;
): MobileMoneyStatusInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Prinx\Payswitch\Contracts;

interface MobileMoneyResponseInterface
interface MobileMoneyStatusInterface
{
/**
* Has user successfully payed?
Expand Down Expand Up @@ -96,4 +96,11 @@ public function getReason();
* @return int|float
*/
public function getAmount();

/**
* Data appended to the response by the developer (typically used in the callbacks).
*
* @return mixed
*/
public function getAppended();
}
8 changes: 4 additions & 4 deletions src/Payswitch/MobileMoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Prinx\Payswitch;

use Prinx\Payswitch\Contracts\MobileMoneyInterface;
use Prinx\Payswitch\Contracts\MobileMoneyResponseInterface;
use Prinx\Payswitch\Contracts\MobileMoneyStatusInterface;
use function Prinx\Dotenv\env;

class MobileMoney implements MobileMoneyInterface
Expand Down Expand Up @@ -59,15 +59,15 @@ class MobileMoney implements MobileMoneyInterface
* @param string|int|null $voucherCode
* @param array $curlOptions Additional Curl options to pass to the request
*
* @return MobileMoneyResponseInterface
* @return MobileMoneyStatusInterface
*/
public function pay(
$amount,
$msisdn,
$network,
$voucherCode = null,
$curlOptions = []
): MobileMoneyResponseInterface {
): MobileMoneyStatusInterface {
if (isset(self::$networks[$network])) {
$network = self::$networks[$network];
$data = $this->prepareData($msisdn, $network, $amount, $voucherCode);
Expand All @@ -83,7 +83,7 @@ public function pay(
$err = 'Unsupported network "'.$network.'"';
}

return new MobileMoneyResponse($response ?? null, $err, $data['transaction_id'] ?? null);
return new MobileMoneyStatus($response ?? null, $err, $data['transaction_id'] ?? null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Prinx\Payswitch;

use Prinx\Payswitch\Contracts\MobileMoneyResponseInterface;
use Prinx\Payswitch\Traits\GetResponseAttributes;
use Prinx\Payswitch\Contracts\MobileMoneyStatusInterface;
use Prinx\Payswitch\Traits\GetStatusAttributes;

class MobileMoneyResponse implements MobileMoneyResponseInterface
class MobileMoneyStatus implements MobileMoneyStatusInterface
{
use GetResponseAttributes;
use GetStatusAttributes;

protected $rawResponse = null;
protected $response = null;
Expand All @@ -16,12 +16,14 @@ class MobileMoneyResponse implements MobileMoneyResponseInterface
protected $isSuccessful = false;
protected $isBeingProcessed = false;
protected $status = 0;
protected $appended = null;

public function __construct($response, $error, $transactionId)
public function __construct($response, $error, $transactionId, $appended = null)
{
$this->rawResponse = $response;
$this->response = $response;
$this->transactionId = $transactionId;
$this->appended = $appended;

if ($error) {
$this->error = $error;
Expand Down Expand Up @@ -158,4 +160,9 @@ public function getRawResponse()
{
return $this->rawResponse;
}

public function getAppended()
{
return $this->appended;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Exception;

class MobileMoneyResponseCheck
class MobileMoneyStatusChecker
{
const MOMO_RESPONSE_CHECK_ENDPOINT = 'https://[env].theteller.net/v1.1/users/transactions/[transaction_id]/status';

Expand Down Expand Up @@ -83,7 +83,7 @@ public function check($ids)
if (!$error) {
$response = curl_multi_getcontent($curlHandle);
$error = curl_error($curlHandle);
$responses['data'][$transactionId] = new MobileMoneyResponse(
$responses['data'][$transactionId] = new MobileMoneyStatus(
$response,
$error,
$transactionId
Expand All @@ -95,7 +95,7 @@ public function check($ids)

curl_multi_close($multiHandle);

return new MobileMoneyResponseCheckCallbackHandler($responses);
return new MobileMoneyStatusCheckerResponse($responses);
}

public function formatUrl($transactionId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Prinx\Payswitch\Exceptions\InvalidCurrentResponseKeyException;
use Txtpay\Support\SlackLog;

class MobileMoneyResponseCheckCallbackHandler
class MobileMoneyStatusCheckerResponse
{
protected $customConditions = [
'success',
Expand Down Expand Up @@ -369,6 +369,44 @@ public function getResponses()
return $this->responses;
}

/**
* Append extra data to the responses.
*
* $toAppend must be in form:
* [
* '012345678' => 'data_to_append_for_this_transaction_id',
* '112345678' => 'data_to_append_for_this_transaction_id',
* '212345678' => 'data_to_append_for_this_transaction_id',
* ]
*
* The indexes are the transaction ids.
*
* You can append extra data for only the transaction id that you need.
*
* @return $this
*/
public function append(array $toAppend)
{
$this->appended = array_merge($this->appended, $toAppend);

return $this;
}

public function setAppended(array $toAppend)
{
$this->appended = $toAppend;

return $this;
}

/**
* @return array
*/
public function getAppended()
{
return $this->appended;
}

public function getLogger()
{
return $this->logger ?? $this->logger = new Log();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Prinx\Payswitch\Traits;

trait GetResponseAttributes
trait GetStatusAttributes
{
public function getCode()
{
Expand Down
32 changes: 0 additions & 32 deletions tests/Unit/MobileMoneyResponseCheckTest.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace Tests\Unit;

use Prinx\Payswitch\MobileMoneyResponse;
use Prinx\Payswitch\MobileMoneyResponseCheckCallbackHandler;
use Prinx\Payswitch\MobileMoneyStatus;
use Prinx\Payswitch\MobileMoneyStatusCheckCallbackHandler;
use Prinx\Payswitch\MobileMoneyStatusCheckerResponse;
use Tests\TestCase;

class MobileMoneyResponseCheckCallbackHandlerTest extends TestCase
class MobileMoneyStatusCheckerResponseTest extends TestCase
{
public function testCallbackCalledWhenSuccess()
{
$responses = [
'success' => true,
'data' => [
new MobileMoneyResponse(json_encode([
new MobileMoneyStatus(json_encode([
'code' => '000',
'status' => 'approved',
'reason' => 'Transaction Successful',
Expand All @@ -33,7 +34,7 @@ public function testCallbackCalledOnSuccessCode()
$responses = [
'success' => true,
'data' => [
new MobileMoneyResponse(json_encode([
new MobileMoneyStatus(json_encode([
'code' => '000',
'status' => 'approved',
'reason' => 'Transaction Successful',
Expand All @@ -53,7 +54,7 @@ public function testCallbackCalledWhenFailure()
$responses = [
'success' => true,
'data' => [
new MobileMoneyResponse(json_encode([
new MobileMoneyStatus(json_encode([
'code' => '105',
'status' => '',
'reason' => 'Transaction',
Expand All @@ -70,13 +71,13 @@ public function testCallbackCalledWhenFailure()

public function testCallbackCalledOnFailureCodes()
{
$failureCodes = (new MobileMoneyResponseCheckCallbackHandler([]))->getFailureValues();
$failureCodes = (new MobileMoneyStatusCheckerResponse([]))->getFailureValues();

foreach ($failureCodes as $code) {
$responses = [
'success' => true,
'data' => [
new MobileMoneyResponse(json_encode([
new MobileMoneyStatus(json_encode([
'code' => $code,
'status' => '',
'reason' => 'Transaction',
Expand All @@ -97,7 +98,7 @@ public function testCallbackCalledAlways()
$responses = [
'success' => true,
'data' => [
new MobileMoneyResponse(json_encode([
new MobileMoneyStatus(json_encode([
'code' => '000',
'status' => 'approved',
'reason' => 'Transaction Successful',
Expand All @@ -106,7 +107,7 @@ public function testCallbackCalledAlways()
'subscriber_number' => '************1999',
'amount' => 1,
]), '', 0),
new MobileMoneyResponse(json_encode([
new MobileMoneyStatus(json_encode([
'code' => '114',
'status' => '',
'reason' => 'Transaction',
Expand All @@ -115,7 +116,7 @@ public function testCallbackCalledAlways()
'subscriber_number' => '************1999',
'amount' => 1,
]), '', 1),
new MobileMoneyResponse(json_encode([
new MobileMoneyStatus(json_encode([
'code' => '104',
'status' => '',
'reason' => 'Transaction',
Expand All @@ -142,7 +143,10 @@ public function testCallbackCalledWhenCurlError()

public function callbackCalledOn($condition, $responses, $isCustomCondition = false, $expectedCalls = 1)
{
$callbackHandler = $this->getMockBuilder(MobileMoneyResponseCheckCallbackHandler::class)
/**
* @var \PHPUnit\Framework\MockObject\MockObject|MobileMoneyStatusCheckerResponse
*/
$callbackHandler = $this->getMockBuilder(MobileMoneyStatusCheckCallbackHandler::class)
->setConstructorArgs([$responses])
->onlyMethods(['runClosure'])
->getMock();
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/MobileMoneyStatusCheckerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Tests\Unit;

use Prinx\Payswitch\MobileMoneyStatusChecker;
use Prinx\Payswitch\MobileMoneyStatusCheckerResponse;
use Tests\TestCase;
use function Prinx\Dotenv\addEnv;
use function Prinx\Dotenv\env;

class MobileMoneyStatusCheckerTest extends TestCase
{
public function testRequest()
{
$check = (new MobileMoneyStatusChecker())->check([env('TEST_TRANSACTION_ID')]);

$this->assertInstanceOf(MobileMoneyStatusCheckerResponse::class, $check);
}

public function testFormatUrl()
{
$expected = 'https://test.theteller.net/v1.1/users/transactions/123456/status';

addEnv('PAYSWITCH_MOMO_API_ENV', 'test');
$transactionId = 123456;
$actual = (new MobileMoneyStatusChecker())->formatUrl($transactionId);

$this->assertEquals($expected, $actual);
}
}

0 comments on commit c95c1ff

Please sign in to comment.