Skip to content

Commit

Permalink
Get access token use base url depending on mpesa_env (#19)
Browse files Browse the repository at this point in the history
* Get access token use base url depending on  mpesa_env

* Fix reference

* fix paths
  • Loading branch information
gathuku authored Dec 22, 2020
1 parent af68b8f commit cc61d34
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.3, 7.4]
php: [7.4]
laravel: [6.*, 7.*, 8.*]
dependency-version: [prefer-lowest, prefer-stable]
dependency-version: [prefer-stable]
include:
- laravel: 8.*
testbench: 6.*
Expand Down
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: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;}}}
C:37:"PHPUnit\Runner\DefaultTestResultCache":998:{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:5;}s:5:"times";a:9:{s:42:"Gathuku\Mpesa\Tests\BaseTest::true_is_true";d:0.164;s:55:"Gathuku\Mpesa\Tests\Unit\InstallMpesaTest::true_is_true";d:0.003;s:57:"Gathuku\Mpesa\Tests\Unit\MpesaAuthTokenTest::true_is_true";d:0.003;s:60:"Gathuku\Mpesa\Tests\Unit\MpesaRegisterUrlsTest::true_is_true";d:0.004;s:47:"Gathuku\Mpesa\Tests\Unit\MpesaB2CTest::test_b2c";d:0.846;s:51:"Gathuku\Mpesa\Tests\Unit\MpesaB2CTest::true_is_true";d:0.004;s:61:"Gathuku\Mpesa\Tests\Unit\MpesaExpressTest::test_mpesa_express";d:2.148;s:55:"Gathuku\Mpesa\Tests\Unit\MpesaExpressTest::true_is_true";d:0.004;s:60:"Gathuku\Mpesa\Tests\Unit\MpesaSimeulateC2BTest::true_is_true";d:0.003;}}}
24 changes: 12 additions & 12 deletions src/Mpesa.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ class Mpesa
public function __construct()
{
if (config('mpesa.mpesa_env')=='sandbox') {
$this->base_url = 'https://sandbox.safaricom.co.ke/mpesa/';
$this->base_url = 'https://sandbox.safaricom.co.ke';
} else {
$this->base_url = 'https://api.safaricom.co.ke/mpesa/';
$this->base_url = 'https://api.safaricom.co.ke';
}
//Base URL for the API endpoints. This is basically the 'common' part of the API endpoints
$this->consumer_key = config('mpesa.consumer_key'); //App Key. Get it at https://developer.safaricom.co.ke
Expand Down Expand Up @@ -150,7 +150,7 @@ public function getAccessToken()
{
$credentials = base64_encode($this->consumer_key.':'.$this->consumer_secret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials');
curl_setopt($ch, CURLOPT_URL, $this->base_url.'/oauth/v1/generate?grant_type=client_credentials');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$credentials, 'Content-Type: application/json'));
$response = curl_exec($ch);
Expand Down Expand Up @@ -221,7 +221,7 @@ public function b2c($amount, $phone, $command_id, $remarks)
'Occasion' => '' //Optional
);
$data = json_encode($request_data);
$url = $this->base_url.'b2c/v1/paymentrequest';
$url = $this->base_url.'/mpesa/b2c/v1/paymentrequest';
$response = $this->submit_request($url, $data);
return $response;
}
Expand Down Expand Up @@ -253,7 +253,7 @@ public function b2b($amount, $shortcode)
'ResultURL' => $this->bbresult,
);
$data = json_encode($request_data);
$url = $this->base_url.'b2b/v1/paymentrequest';
$url = $this->base_url.'/mpesa/b2b/v1/paymentrequest';
$response = $this->submit_request($url, $data);
return $response;
}
Expand All @@ -279,7 +279,7 @@ public function c2bRegisterUrls()
$data = json_encode($request_data);
//header('Content-Type: application/json');

$url = $this->base_url.'c2b/v1/registerurl';
$url = $this->base_url.'/mpesa/c2b/v1/registerurl';
$response = $this->submit_request($url, $data);
//\Log::info($response);
return $response;
Expand All @@ -306,7 +306,7 @@ public function simulateC2B($amount, $msisdn, $ref)
'BillRefNumber' => $ref
);
$data = json_encode($data);
$url = $this->base_url.'c2b/v1/simulate';
$url = $this->base_url.'/c2b/v1/simulate';
$response = $this->submit_request($url, $data);
return $response;
}
Expand All @@ -331,7 +331,7 @@ public function check_balance()
'ResultURL' => $this->balresult
);
$data = json_encode($data);
$url = $this->base_url.'accountbalance/v1/query';
$url = $this->base_url.'/mpesa/accountbalance/v1/query';
$response = $this->submit_request($url, $data);
return $response;
}
Expand Down Expand Up @@ -360,7 +360,7 @@ public function status_request($transaction = 'LH7819VXPE')
'Occassion' => 'Test'
);
$data = json_encode($data);
$url = $this->base_url.'transactionstatus/v1/query';
$url = $this->base_url.'/mpesa/transactionstatus/v1/query';
$response = $this->submit_request($url, $data);
return $response;
}
Expand Down Expand Up @@ -391,7 +391,7 @@ public function reverse_transaction($receiver, $trx_id, $amount)
'TransactionID' => 'LIE81C8EFI'
);
$data = json_encode($data);
$url = $this->base_url.'reversal/v1/request';
$url = $this->base_url.'/mpesa/reversal/v1/request';
$response = $this->submit_request($url, $data);
return $response;
}
Expand Down Expand Up @@ -424,7 +424,7 @@ public function express($amount, $phone, $ref = "Payment", $desc="Payment")
'TransactionDesc' => $desc,
);
$data = json_encode($data);
$url = $this->base_url.'stkpush/v1/processrequest';
$url = $this->base_url.'/mpesa/stkpush/v1/processrequest';
$response = $this->submit_request($url, $data);

if (isset($response)) {
Expand Down Expand Up @@ -458,7 +458,7 @@ private function lnmoQuery($checkoutRequestID = null)
'CheckoutRequestID' => $checkoutRequestID
);
$data = json_encode($data);
$url = $this->base_url.'stkpushquery/v1/query';
$url = $this->base_url.'/mpesa/stkpushquery/v1/query';
$response = $this->submit_request($url, $data);
return $response;
}
Expand Down

0 comments on commit cc61d34

Please sign in to comment.