Skip to content

Commit

Permalink
Merge pull request #5 from gathuku/standardize
Browse files Browse the repository at this point in the history
Mpesa express tests
  • Loading branch information
gathuku authored May 10, 2020
2 parents 3028e55 + 98f3302 commit b105a73
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":997:{a:2:{s:7:"defects";a:5:{s:42:"Gathuku\Mpesa\Tests\BaseTest::true_is_true";i:4;s:55:"Gathuku\Mpesa\Tests\Unit\InstallMpesaTest::true_is_true";i:4;s:57:"Gathuku\Mpesa\Tests\Unit\MpesaAuthTokenTest::true_is_true";i:4;s:47:"Gathuku\Mpesa\Tests\Unit\MpesaB2CTest::test_b2c";i:3;s:61:"Gathuku\Mpesa\Tests\Unit\MpesaExpressTest::test_mpesa_express";i:3;}s:5:"times";a:9:{s:42:"Gathuku\Mpesa\Tests\BaseTest::true_is_true";d:0.279;s:55:"Gathuku\Mpesa\Tests\Unit\InstallMpesaTest::true_is_true";d:0.244;s:57:"Gathuku\Mpesa\Tests\Unit\MpesaAuthTokenTest::true_is_true";d:0.153;s:60:"Gathuku\Mpesa\Tests\Unit\MpesaRegisterUrlsTest::true_is_true";d:0.144;s:47:"Gathuku\Mpesa\Tests\Unit\MpesaB2CTest::test_b2c";d:0.16;s:51:"Gathuku\Mpesa\Tests\Unit\MpesaB2CTest::true_is_true";d:0.149;s:61:"Gathuku\Mpesa\Tests\Unit\MpesaExpressTest::test_mpesa_express";d:0.157;s:55:"Gathuku\Mpesa\Tests\Unit\MpesaExpressTest::true_is_true";d:0.147;s:60:"Gathuku\Mpesa\Tests\Unit\MpesaSimeulateC2BTest::true_is_true";d:0.144;}}}
C:37:"PHPUnit\Runner\DefaultTestResultCache":997:{a:2:{s:7:"defects";a:5:{s:42:"Gathuku\Mpesa\Tests\BaseTest::true_is_true";i:4;s:55:"Gathuku\Mpesa\Tests\Unit\InstallMpesaTest::true_is_true";i:4;s:57:"Gathuku\Mpesa\Tests\Unit\MpesaAuthTokenTest::true_is_true";i:4;s:47:"Gathuku\Mpesa\Tests\Unit\MpesaB2CTest::test_b2c";i:3;s:61:"Gathuku\Mpesa\Tests\Unit\MpesaExpressTest::test_mpesa_express";i:3;}s:5:"times";a:9:{s:42:"Gathuku\Mpesa\Tests\BaseTest::true_is_true";d:1.556;s:55:"Gathuku\Mpesa\Tests\Unit\InstallMpesaTest::true_is_true";d:0.14;s:57:"Gathuku\Mpesa\Tests\Unit\MpesaAuthTokenTest::true_is_true";d:0.14;s:60:"Gathuku\Mpesa\Tests\Unit\MpesaRegisterUrlsTest::true_is_true";d:0.152;s:47:"Gathuku\Mpesa\Tests\Unit\MpesaB2CTest::test_b2c";d:3.461;s:51:"Gathuku\Mpesa\Tests\Unit\MpesaB2CTest::true_is_true";d:0.146;s:61:"Gathuku\Mpesa\Tests\Unit\MpesaExpressTest::test_mpesa_express";d:12.852;s:55:"Gathuku\Mpesa\Tests\Unit\MpesaExpressTest::true_is_true";d:0.148;s:60:"Gathuku\Mpesa\Tests\Unit\MpesaSimeulateC2BTest::true_is_true";d:0.142;}}}
19 changes: 12 additions & 7 deletions src/Mpesa.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ public function b2c($amount,$phone, $command_id, $remarks){
$url = $this->base_url.'b2c/v1/paymentrequest';
$response = $this->submit_request($url, $data);
return $response;
// \Log::info($response);
}

/**
Expand Down Expand Up @@ -418,16 +417,22 @@ public function express($amount, $phone, $ref = "Payment",$desc="Payment"){
$data = json_encode($data);
$url = $this->base_url.'stkpush/v1/processrequest';
$response = $this->submit_request($url, $data);
$result = json_decode($response);
if(isset($result) && array_key_exists("CheckoutRequestID",$result)){
$c_id = $result->CheckoutRequestID;
return $this->lnmo_query($c_id);

if(isset($response)){
return $response;
}else{
return FALSE;
return false;
}
// $result = json_decode($response);
// if(isset($result) && isset($result->CheckoutRequestID)){
// $c_id = $result->CheckoutRequestID;
// return $this->lnmo_query($c_id);
// }else{
// return FALSE;
// }
}

private function lnmo_query($checkoutRequestID = null){
private function lnmoQuery($checkoutRequestID = null){
$timestamp = date('YmdHis');
$passwd = base64_encode($this->lipa_na_mpesa.$this->lipa_na_mpesa_key.$timestamp);

Expand Down
20 changes: 16 additions & 4 deletions tests/Unit/MpesaB2CTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,21 @@ class MpesaB2CTest extends BaseTest
public function test_b2c(){
$response = Mpesa::b2c(100,'254708374149','PromotionPayment','testing');
$data = json_decode($response,true);
$this->assertTrue($response);
$this->assertArrayHasKey('ConversationID',$data);
$this->assertArrayHasKey('OriginatorCoversationID',$data);
$this->assertArrayHasKey('ResponseDescription',$data);
//$this->assertTrue($response);
$this->assertArrayHasKey(
'ConversationID',
$data,
"response don't have ConversationID"
);
$this->assertArrayHasKey(
'OriginatorConversationID'
,$data,
"response dont have OriginatorConversationID"
);
$this->assertArrayHasKey(
'ResponseDescription',
$data,
"response dont have ResponseDescription"
);
}
}
13 changes: 8 additions & 5 deletions tests/Unit/MpesaExpressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ class MpesaExpressTest extends BaseTest
{
public function test_mpesa_express()
{
$response = Mpesa::express(100,'2547112855','24242524','Testing Payment');
$response = Mpesa::express(100,'254705112855','24242524','Testing Payment');
$data = json_decode($response,true);
$this->assertTrue($response);
$this->assertArrayHasKey('MerchantRequestID',$data);
$this->assertArrayHasKey('CheckoutRequestID',$data);
$this->assertArrayHasKey('ResponseDescription',$data);

if(isset($data['CheckoutRequestID'])){
$this->assertArrayHasKey('MerchantRequestID',$data,"response don't have MerchantRequestID");
$this->assertArrayHasKey('CheckoutRequestID',$data, "response don't have CheckoutRequestID");
$this->assertArrayHasKey('ResponseDescription',$data, "response don't have ResponseDescription");
}

}
}
9 changes: 4 additions & 5 deletions tests/Unit/MpesaRegisterUrlsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ class MpesaRegisterUrlsTest extends BaseTest
public function it_can_register_urls()
{
$response = Mpesa::registerUrls();
$data = json_decode($response, true);
$this->assertTrue($response);
$this->assertEqual(200,$response->getStatusCode());
$this->assertTrue($response->hasHeader('content-type'));
$data = json_decode($response,true);
//$this->assertTrue($response);

$this->assertArrayHasKey('ConversationID',$data);
$this->assertArrayHasKey('OriginatorCoversationID',$data);
$this->assertArrayHasKey('OriginatorConversationID',$data);
$this->assertArrayHasKey('ResponseDescription',$data);
}
}
4 changes: 2 additions & 2 deletions tests/Unit/MpesaSimulateC2BTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public function it_can_simulate()
{
$response = Mpesa::simulateC2B(100, "254708374149", "Testing");
$data = json_decode($response,true);
$this->assertTrue($response);
//$this->assertTrue($response);
$this->assertArrayHasKey('ConversationID',$data);
$this->assertArrayHasKey('OriginatorCoversationID',$data);
$this->assertArrayHasKey('OriginatorConversationID',$data);
$this->assertArrayHasKey('ResponseDescription',$data);
}
}

0 comments on commit b105a73

Please sign in to comment.