Skip to content

Commit 44d52ab

Browse files
committed
small fix
1 parent 88a5a67 commit 44d52ab

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,17 @@ return [
6565
'passkey' => env('SAFARICOM_PASSKEY', 'bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919'),
6666

6767
/*-----------------------------------------
68-
|The Lipa na Mpesa Online ShortCode (Paybill or Till Number)
68+
|The Lipa na Mpesa Online ShortCode (Paybill Number)
6969
|------------------------------------------
7070
*/
7171
'shortcode' => env('MPESA_BUSINESS_SHORTCODE', '174379'),
7272

73+
/*--------------------------------------------------------------
74+
|The Lipa na Mpesa Online ShortCode (Till Number)
75+
|-----------------------------------------------------------
76+
*/
77+
'till_number' => env('MPESA_BUY_GOODS_TILL', '174379'),
78+
7379
/*-----------------------------------------
7480
|The Mpesa Initator Name
7581
|------------------------------------------

USAGE.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@ return $result;
8080

8181
### Till Number Example
8282

83-
To initiate a `Till Number (Buy Goods)` STK Push, call `stkpush()` with `$transactionType = Mpesa::TILL`.
83+
To initiate a `Till Number (Buy Goods)` STK Push, call `stkpush()` with `$transactionType = Mpesa::TILL` and ensure you set a till number in `config/mpesa.php`.
8484
Note these points:
8585

8686
- Use a Till Number shortcode (not a PayBill shortcode).
87-
8887
- Do not supply an accountNumber for Till flows
8988

9089
```php

config/mpesa.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@
2323
'passkey' => env('SAFARICOM_PASSKEY', 'bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919'),
2424

2525
/*--------------------------------------------------------------
26-
|The Lipa na Mpesa Online ShortCode (Paybill or Till Number)
26+
|The Lipa na Mpesa Online ShortCode (Paybill Number)
2727
|-----------------------------------------------------------
2828
*/
2929
'shortcode' => env('MPESA_BUSINESS_SHORTCODE', '174379'),
3030

31+
/*--------------------------------------------------------------
32+
|The Lipa na Mpesa Online ShortCode (Till Number)
33+
|-----------------------------------------------------------
34+
*/
35+
'till_number' => env('MPESA_BUY_GOODS_TILL', '174379'),
36+
3137
/*-----------------------------------------
3238
|The Mpesa Initator Name
3339
|------------------------------------------

src/Mpesa.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class Mpesa
2626
*/
2727
public $shortcode;
2828

29+
/**
30+
* The Lipa Na MPesa till number
31+
*
32+
* @var int
33+
*/
34+
public $till_number;
35+
2936
/**
3037
* The Mpesa B2C shortcode
3138
*
@@ -57,6 +64,7 @@ public function __construct()
5764

5865
$this->security_credential = $this->generate_security_credential();
5966
$this->shortcode = $this->getConfig('shortcode');
67+
$this->till_number = $this->getConfig('till_number');
6068
$this->initiator_name = $this->getConfig('initiator_name');
6169
$this->b2c_shortcode = $this->getConfig('b2c_shortcode');
6270
}
@@ -76,10 +84,6 @@ public function __construct()
7684
public function stkpush($phonenumber, $amount, $account_number = null, $callbackurl = null, $transactionType = self::PAYBILL)
7785
{
7886

79-
if ($transactionType === self::PAYBILL && empty($account_number)) {
80-
throw new \InvalidArgumentException('Account number is required for Pay Bill transactions.');
81-
}
82-
8387
$validTypes = [self::PAYBILL, self::TILL];
8488
if (! in_array($transactionType, $validTypes, true)) {
8589
throw new \InvalidArgumentException(
@@ -88,14 +92,22 @@ public function stkpush($phonenumber, $amount, $account_number = null, $callback
8892
);
8993
}
9094

95+
if ($transactionType === self::PAYBILL && empty($account_number)) {
96+
throw new \InvalidArgumentException('Account number is required for Pay Bill transactions.');
97+
}
98+
99+
if ($transactionType === self::TILL && (empty($this->till_number) || is_null($this->till_number))) {
100+
throw new \InvalidArgumentException('Till number is required for Buy Goods transactions.');
101+
}
102+
91103
$url = $this->url . '/mpesa/stkpush/v1/processrequest';
92104
$data = [
93105
'BusinessShortCode' => $this->shortcode, //Can be a paybill or till number
94106
'Password' => $this->LipaNaMpesaPassword(),
95107
'Timestamp' => $this->getFormattedTimeStamp(),
96108
'Amount' => (int) $amount,
97109
'PartyA' => $this->phoneValidator($phonenumber), // replace this with your phone number
98-
'PartyB' => $this->shortcode,
110+
'PartyB' => $transactionType == self::PAYBILL ? $this->shortcode : $this->till_number, //Can be a paybill or till number
99111
'TransactionType' => $transactionType, //Can be CustomerPayBillOnline or CustomerBuyGoodsOnline
100112
'PhoneNumber' => $this->phoneValidator($phonenumber), // replace this with your phone number
101113
'TransactionDesc' => 'Payment', //Maximum of 13 Characters.
@@ -279,7 +291,7 @@ public function c2bregisterURLS($shortcode, $confirmurl = null, $validateurl = n
279291
*/
280292
public function c2bsimulate($phonenumber, $amount, $shortcode, $command_id, $account_number = null)
281293
{
282-
if ($command_id == 'CustomerPayBillOnline') {
294+
if ($command_id == self::PAYBILL) {
283295
//Paybill Request Body
284296
$data = [
285297
'Msisdn' => $this->phoneValidator($phonenumber),

0 commit comments

Comments
 (0)