Skip to content

Commit

Permalink
Readd STK class
Browse files Browse the repository at this point in the history
  • Loading branch information
maukoese committed Dec 17, 2020
1 parent 5d767c1 commit 06e60b7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/C2B.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function register($callback = null, $response_type = "Completed")
: "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl";

$curl_post_data = array(
"ShortCode" => parent::$config->shortcode,
"ShortCode" => parent::$config->headoffice,
"ResponseType" => $response_type,
"ConfirmationURL" => parent::$config->confirmation_url,
"ValidationURL" => parent::$config->validation_url,
Expand Down
53 changes: 53 additions & 0 deletions src/STK.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Osen\Mpesa;

use Osen\Mpesa\Service;

class STK extends Service
{
/**
* @param $phone The MSISDN sending the funds.
* @param $amount The amount to be transacted.
* @param $reference Used with M-Pesa PayBills.
* @param $description A description of the transaction.
* @param $remark Remarks
*
* @return array Response
*/
public static function send($phone, $amount, $reference = "ACCOUNT", $description = "Transaction Description", $remark = "Remark", $callback = null)
{
$phone = (substr($phone, 0, 1) == "+") ? str_replace("+", "", $phone) : $phone;
$phone = (substr($phone, 0, 1) == "0") ? preg_replace("/^0/", "254", $phone) : $phone;
$phone = (substr($phone, 0, 1) == "7") ? "254{$phone}" : $phone;

$timestamp = date("YmdHis");
$password = base64_encode(parent::$config->shortcode . parent::$config->passkey . $timestamp);

$endpoint = (parent::$config->env == "live")
? "https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
: "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest";

$curl_post_data = array(
"BusinessShortCode" => parent::$config->headoffice,
"Password" => $password,
"Timestamp" => $timestamp,
"TransactionType" => (parent::$config->type == 4) ? "CustomerPayBillOnline" : "CustomerBuyGoodsOnline",
"Amount" => round($amount),
"PartyA" => $phone,
"PartyB" => parent::$config->shortcode,
"PhoneNumber" => $phone,
"CallBackURL" => parent::$config->callback_url,
"AccountReference" => $reference,
"TransactionDesc" => $description,
"Remark" => $remark,
);

$response = parent::remote_post($endpoint, $curl_post_data);
$result = json_decode($response, true);

return is_null($callback)
? $result
: \call_user_func_array($callback, array($result));
}
}

0 comments on commit 06e60b7

Please sign in to comment.