/home/techb158/ileadtechnology.com/vendor/smodav/mpesa/src/Mpesa/C2B
Edit: /home/techb158/ileadtechnology.com/vendor/smodav/mpesa/src/Mpesa/C2B/STK.php (6995B)
callback = $callback;
return $this;
}
/**
* Set the account to be used.
*
* @param string $account
*
* @return self
*/
public function usingAccount($account)
{
$this->account = $account;
return $this;
}
/**
* Set the request amount to be deducted.
*
* @param int $amount
*
* @return self
*
* @throws InvalidArgumentException
*/
public function request($amount)
{
$this->validateAmount($amount);
$this->amount = $amount;
return $this;
}
/**
* Set the Mobile Subscriber Number to deduct the amount from.
* Must be in format 2547XXXXXXXX.
*
* @param int $number
*
* @return self
*
* @throws InvalidArgumentException
*/
public function from($number)
{
$this->validateNumber($number);
$this->number = $number;
return $this;
}
/**
* Set the product reference number to bill the account.
*
* @param int $reference
* @param string $description
*
* @return self
*/
public function usingReference($reference, $description)
{
$this->reference = $reference;
$this->description = $description;
return $this;
}
/**
* Set the unique command for this transaction type.
*
* @param string $command
*
* @return self
*
* @throws InvalidArgumentException
*/
public function setCommand($command)
{
if (! in_array($command, self::VALID_COMMANDS)) {
throw new InvalidArgumentException('Invalid command sent');
}
$this->command = $command;
return $this;
}
/**
* Set the properties that require validation.
*
* @param string|null $amount
* @param string|null $number
* @param string|null $command
*
* @return void
*/
private function set($amount, $number, $command)
{
$map = [
'amount' => 'request',
'number' => 'from',
'command' => 'setCommand',
];
foreach ($map as $var => $method) {
if ($$var) {
call_user_func([$this, $method], $$var);
}
}
}
/**
* Prepare the STK Push request
*
* @param int|null $amount
* @param int|null $number
* @param string|null $reference
* @param string|null $description
* @param string|null $account
* @param string|null $command
*
* @return mixed
*/
public function push($amount = null, $number = null, $reference = null, $description = null, $account = null, $command = null)
{
$this->set($amount, $number, $command);
$this->core->useAccount($account ?: $this->account);
$time = Carbon::now()->format('YmdHis');
$paybill = $this->core->configRepository()->getAccountKey('lnmo.paybill');
$shortCode = $this->core->configRepository()->getAccountKey('lnmo.shortcode');
$passkey = $this->core->configRepository()->getAccountKey('lnmo.passkey');
$callback = $this->callback ?: $this->core->configRepository()->getAccountKey('lnmo.callback');
$partyB = $this->command == self::CUSTOMER_PAYBILL_ONLINE ? $shortCode : $paybill;
$body = [
'BusinessShortCode' => $shortCode,
'Password' => $this->password($shortCode, $passkey, $time),
'Timestamp' => $time,
'TransactionType' => $this->command,
'Amount' => $this->amount,
'PartyA' => $this->number,
'PartyB' => $partyB,
'PhoneNumber' => $number ?: $this->number,
'CallBackURL' => $callback,
'AccountReference' => $reference ?: $this->reference,
'TransactionDesc' => $description ?: $this->description,
];
try {
$response = $this->clientRequest(
$body,
$this->core->configRepository()->url(Endpoint::MPESA_LNMO)
);
return json_decode($response->getBody());
} catch (RequestException $exception) {
return json_decode($exception->getResponse()->getBody());
}
}
/**
* Validate an initialized transaction.
*
* @param string $checkoutRequestID
*
* @return json
*/
public function validate($checkoutRequestID, $account = null)
{
$this->core->useAccount($account ?: $this->account);
$time = Carbon::now()->format('YmdHis');
$shortCode = $this->core->configRepository()->getAccountKey('lnmo.shortcode');
$passkey = $this->core->configRepository()->getAccountKey('lnmo.passkey');
$body = [
'BusinessShortCode' => $shortCode,
'Password' => $this->password($shortCode, $passkey, $time),
'Timestamp' => $time,
'CheckoutRequestID' => $checkoutRequestID,
];
try {
$response = $this->clientRequest(
$body,
$this->core->configRepository()->url(Endpoint::MPESA_LNMO_VALIDATE)
);
return json_decode($response->getBody());
} catch (RequestException $exception) {
return json_decode($exception->getResponse()->getBody());
}
}
}