starnerz /
laravel-daraja
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Starnerz\LaravelDaraja\Requests; |
||
| 4 | |||
| 5 | use Starnerz\LaravelDaraja\MpesaApiClient; |
||
| 6 | |||
| 7 | class STK extends MpesaApiClient |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Safaricom Lipa Na Mpesa Online API end point. |
||
| 11 | * |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | protected $stkEndpoint = 'mpesa/stkpush/v1/processrequest'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Safaricom Lipa Na Mpesa Online API transaction status |
||
| 18 | * end point. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $statusEndPoint = 'mpesa/stkpushquery/v1/query'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Business short code that will receive money. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $shortCode; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The passkey associated with Lipa Na Mpesa Online Transactions |
||
| 33 | * for the short code. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $passKey; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The url that will handle the result of the transaction from |
||
| 41 | * the Saaricom Lipa Na Mpesa Online API. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $callbackURL; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * STK constructor. |
||
| 49 | */ |
||
| 50 | public function __construct() |
||
| 51 | { |
||
| 52 | parent::__construct(); |
||
| 53 | $this->shortCode = config('laravel-daraja.stk_push.short_code'); |
||
| 54 | $this->passKey = config('laravel-daraja.stk_push.pass_key'); |
||
| 55 | $this->callbackURL = $this->setUrl(config('laravel-daraja.stk_push.callback_url')); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Set the business short code that will be used for the transaction. |
||
| 60 | * |
||
| 61 | * @param string $code |
||
| 62 | */ |
||
| 63 | public function setShortCode($code) |
||
| 64 | { |
||
| 65 | $this->shortCode = $code; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Set the pass key associated with the business short code set. |
||
| 70 | * |
||
| 71 | * @param string $key |
||
| 72 | */ |
||
| 73 | public function setPassKey($key) |
||
| 74 | { |
||
| 75 | $this->passKey = $key; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Set the URL which will hadle the result from the MPESA API. |
||
| 80 | * |
||
| 81 | * @param string $url |
||
| 82 | */ |
||
| 83 | public function setCallbackURL($url) |
||
| 84 | { |
||
| 85 | $this->callbackURL = $url; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Generate the password to be used for the transaction. |
||
| 90 | * |
||
| 91 | * @param string $shortCode |
||
| 92 | * @param string $passKey |
||
| 93 | * @param string $timestamp |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | protected function generatePassword($shortCode, $passKey, $timestamp) |
||
| 97 | { |
||
| 98 | return base64_encode($shortCode.$passKey.$timestamp); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Initiate an STK push to a Safaricom mobile number. |
||
| 103 | * |
||
| 104 | * @param string $mobileNo |
||
| 105 | * @param string $amount |
||
| 106 | * @param string $description |
||
| 107 | * @param string $accountReference |
||
| 108 | * @param null|string $shortCode short code receiving the money |
||
| 109 | * @return mixed |
||
| 110 | */ |
||
| 111 | public function push($mobileNo, $amount, $description, $accountReference, $shortCode = null, $transactionType) |
||
| 112 | { |
||
| 113 | $timestamp = date('YmdHis'); |
||
| 114 | |||
| 115 | $parameters = [ |
||
| 116 | 'BusinessShortCode' => $this->shortCode, |
||
| 117 | 'Password' => $this->generatePassword($this->shortCode, $this->passKey, $timestamp), |
||
| 118 | 'Timestamp' => $timestamp, |
||
| 119 | 'TransactionType' => $transactionType, |
||
| 120 | 'Amount' => $amount, |
||
| 121 | 'PartyA' => $mobileNo, |
||
| 122 | 'PartyB' => is_null($shortCode) ? $this->shortCode : $shortCode, |
||
| 123 | 'PhoneNumber' => $mobileNo, |
||
| 124 | 'CallBackURL' => $this->callbackURL, |
||
| 125 | 'AccountReference' => $accountReference, |
||
| 126 | 'TransactionDesc' => str_limit($description, 20, ''), |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 127 | ]; |
||
| 128 | |||
| 129 | return $this->call($this->stkEndpoint, ['json' => $parameters]); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Check the status of a Lipa Na Mpesa Online Transaction. |
||
| 134 | * |
||
| 135 | * @param string $checkoutRequestId |
||
| 136 | * @param null|string $shortCode |
||
| 137 | * @return mixed |
||
| 138 | */ |
||
| 139 | public function transactionStatus($checkoutRequestId, $shortCode = null) |
||
| 140 | { |
||
| 141 | $timestamp = date('YmdHis'); |
||
| 142 | |||
| 143 | $parameters = [ |
||
| 144 | 'BusinessShortCode' => is_null($shortCode) ? $this->shortCode : $shortCode, |
||
| 145 | 'Password' => $this->generatePassword($this->shortCode, $this->passKey, $timestamp), |
||
| 146 | 'Timestamp' => $timestamp, |
||
| 147 | 'CheckoutRequestID' => $checkoutRequestId, |
||
| 148 | ]; |
||
| 149 | |||
| 150 | return $this->call($this->statusEndPoint, ['json' => $parameters]); |
||
| 151 | } |
||
| 152 | } |
||
| 153 |