PHP code example of akika / laravel-mpesa-multivendor

1. Go to this page and download the library: Download akika/laravel-mpesa-multivendor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

akika / laravel-mpesa-multivendor example snippets


Mpesa::using($credentials)->stk();
Mpesa::using($credentials)->c2b();
Mpesa::using($credentials)->b2c();
Mpesa::using($credentials)->b2b();
Mpesa::using($credentials)->accountBalance();
Mpesa::using($credentials)->dynamicQr();
Mpesa::using($credentials)->billManager();
Mpesa::using($credentials)->taxRemittance();
Mpesa::using($credentials)->ratiba();
Mpesa::using($credentials)->transactionHistory();
Mpesa::using($credentials)->transactionStatus();
Mpesa::using($credentials)->pochi();
Mpesa::using($credentials)->org();
Mpesa::using($credentials)->imsi();
Mpesa::using($credentials)->reversal();

$credentials = [
    'shortcode' => '600000',
    'consumer_key' => 'xxx',
    'consumer_secret' => 'xxx',
    'api_username' => 'testapi',
    'api_password' => 'xxx',
    'passkey' => 'xxx',
];

return [
    // Define mpesa environment
    'env' => env('MPESA_ENV', 'sandbox'),
    'debug' => env('MPESA_DEBUG_MODE', true),
    'sandbox' => [
        'url' => 'https://sandbox.safaricom.co.ke',
    ],
    'production' => [
        'url' => 'https://api.safaricom.co.ke',
    ],
];

use Akika\LaravelMpesaMultivendor\Mpesa;

$mpesa = Mpesa::default();

use Akika\Mpesa\Facades\Mpesa;

$mpesa = Mpesa::using($credentials);

$response = Mpesa::using($credentials)
    ->accountBalance()
    ->query(
        resultUrl: $resultUrl,
        queueTimeoutUrl: $timeoutUrl
    );

$response = Mpesa::using($credentials)
    ->c2b()
    ->registerUrls(
        confirmationUrl: $confirmationUrl, // The URL to receive payment confirmation notifications.
        validationUrl: $validationUrl, // The URL to receive payment validation requests.
        ResponseType: $ResponseType, // nullable | Either Cancelled or Completed, default is Completed
    );

$response = Mpesa::using($credentials)
    ->c2b()
    ->simulate(
        phoneNumber: $phoneNumber,
        amount: $amount,
        billRefNumber: $billRefNumber // Account reference for Customer paybills and null for customer buy goods
    );

$response = Mpesa::using($credentials)
    ->stk()
    ->push(
        phoneNumber: $phoneNumber,
        amount: $amount,
        callbackUrl: $callbackUrl,
        accountReference: $accountReference
        receivingShortCode: $receivingShortCode // shortcode to receive funds, if null, the shortcode in credentials is used
    );

$response = Mpesa::using($credentials)
    ->stk()
    ->query($checkoutRequestId);

$response = Mpesa::using($credentials)
    ->reversal()
    ->reverse(
        transactionId: $transactionId,
        amount: $amount,
        resultUrl: $resultUrl,
        queueTimeoutUrl: $timeoutUrl
    );

$response = Mpesa::using($credentials)
    ->b2c()
    ->send(
        phoneNumber: $phoneNumber,
        amount: $amount,
        resultUrl: $resultUrl,
        queueTimeoutUrl: $timeoutUrl,
        remarks: $remarks, // Comments that are sent along with the transaction.
        occasion: $occasion, // A reference for the transaction, such as an invoice number or account number. (Max 100 characters)
        commandId: $commandId, // The command ID for the transaction (default: 'BusinessPayment')
    );

$response = Mpesa::using($credentials)
    ->b2c()
    ->topUp(
        receiverShortCode: $receiverShortCode,
        amount: $amount,
        resultUrl: $resultUrl,
        timeoutUrl: $timeoutUrl,
        remarks: $remarks,
        accountReference: $accountReference,
        requester: $requester
    );

$response = Mpesa::using($credentials)
    ->b2b()
    ->buyGoods(
        receiverShortCode: $shortcode,
        amount: $amount,
        resultUrl: $resultUrl,
        queueTimeoutUrl: $timeoutUrl,
        remarks: $remarks,
        requester: $requester
    );

$response = Mpesa::using($credentials)
    ->b2b()
    ->paybill(
        receiverShortCode: $shortcode,
        amount: $amount,
        accountReference: $accountReference
        resultUrl: $resultUrl,
        queueTimeoutUrl: $timeoutUrl,
        remarks: $remarks,
        requester: $requester
    );

$response = Mpesa::using($credentials)
    ->b2b()
    ->expressCheckout(
        partnerName: $partnerName, // The name of the organization that receives the transaction
        destShortcode: $destShortcode,
        amount: $amount,
        paymentReference: $paymentReference, // The reference for the payment
        callbackUrl: $callbackUrl
        requestRefID: $requestRefID
    );

$response = Mpesa::using($credentials)
    ->dynamicQr()
    ->generate(
        merchantName: 'Akika Digital',
        reference: 'INV001',
        amount: 100
    );

$response = Mpesa::using($credentials)
    ->billManager()
    ->optin(
        email: $email,
        officialContact: $officialContact,
        sendReminders: $sendReminders, // 1 or 0
        logo: $logo, // Optional logo URL for the bill manager account.
        callbackUrl: $callbackUrl
    );

$response = Mpesa::using($credentials)
    ->billManager()
    ->singleInvoice(
        externalReference: $externalReference, // A unique reference for the invoice.
        billedFullName: $billedFullName, // The full name of the billed customer.
        billedPhoneNumber: $billedPhoneNumber, // The phone number of the billed customer.
        invoiceName: $invoiceName, // A descriptive name for the invoice (e.g., "Water Bill").
        amount: $amount,
        dueDate: $dueDate, // The due date for the invoice (in 'Y-m-d' format).
        accountReference: $accountReference, // A reference for the account being billed.
        billingPeriod: $billingPeriod, // The billing period (e.g., "June 2026").
        items: $items, // Optional additional billable items to 

$response = Mpesa::using($credentials)
    ->billManager()
    ->bulkInvoice(
        invoices: $invoices
    );

$invoices = [
    [
        "externalReference" => "#9932340",
        "billedFullName" => "John Doe",
        "billedPhoneNumber" => "0712345678",
        "billedPeriod" => "August 2021",
        "invoiceName" => "Jentrys",
        "dueDate" => "2021-10-12",
        "accountReference" => "1ASD678H",
        "amount" => "800",
        "invoiceItems" => [
            [
                "itemName" => "Food",
                "amount" => "700",
            ],
            [
                "itemName" => "Water",
                "amount" => "100",
            ],
        ],
    ],

    [
        "externalReference" => "#9932341",
        "billedFullName" => "Jane Smith",
        "billedPhoneNumber" => "0723456789",
        "billedPeriod" => "September 2021",
        "invoiceName" => "BlueWave Supplies",
        "dueDate" => "2021-11-15",
        "accountReference" => "2BSD789J",
        "amount" => "1500",
        "invoiceItems" => [
            [
                "itemName" => "Office Supplies",
                "amount" => "1200",
            ],
            [
                "itemName" => "Delivery Fee",
                "amount" => "300",
            ],
        ],
    ],

    [
        "externalReference" => "#9932342",
        "billedFullName" => "Peter Mwangi",
        "billedPhoneNumber" => "0734567890",
        "billedPeriod" => "October 2021",
        "invoiceName" => "Tech Solutions",
        "dueDate" => "2021-12-01",
        "accountReference" => "3CSD890K",
        "amount" => "2500",
        "invoiceItems" => [
            [
                "itemName" => "Laptop Repair",
                "amount" => "2000",
            ],
            [
                "itemName" => "Spare Parts",
                "amount" => "500",
            ],
        ],
    ],
];

$response = Mpesa::using($credentials)
    ->billManager()
    ->cancelSingleInvoice(
        externalReference: $externalReference // The unique reference of the invoice to be cancelled.
    );

$response = Mpesa::using($credentials)
    ->billManager()
    ->cancelBulkInvoices(
        externalReferences: $externalReferences // An array of unique references for the invoices to be cancelled.
    );

$response = Mpesa::using($credentials)
    ->taxRemmitance()
    ->remit(
        amount: $amount,
        accountReference: $accountReference, // The payment registration number (PRN) issued by KRA.
        resultUrl: $resultUrl,
        queueTimeoutUrl: $queueTimeoutUrl, // Optional remarks for the transaction (default: 'Tax remittance').
        remarks: $remarks,
        commandId: $commandId, // The due date for the invoice (in 'Y-m-d' format).
        partyA: $partyA,
    );

$response = Mpesa::using($credentials)
    ->ratiba()
    ->createStandingOrder(
        name: $name,
        startDate: $startDate,
        endDate: $endDate,
        transactionType: $transactionType,
        amount: $amount,
        phoneNumber: $phoneNumber,
        callbackUrl: $callbackUrl,
        accountReference: $accountReference,
        frequency: $frequency,
        transactionDesc: $transactionDesc
    );

$response = Mpesa::using($credentials)
    ->ratiba()
    ->query(
        standingOrderId: $standingOrderId
    );

$response = Mpesa::using($credentials)
    ->ratiba()
    ->cancel(
        standingOrderId: $standingOrderId, // Standing order to cancel
        callbackUrl: $callbackUrl,
    );

$response = Mpesa::using($credentials)
    ->transactionHistory()
    ->registerCallbackUrl(
        nominatedNumber: $nominatedNumber, // The phone number to receive the transaction history callbacks (in international format, e.g., 2547XXXXXXXX).
        callbackUrl: $callbackUrl,
    );

$response = Mpesa::using($credentials)
    ->transactionHistory()
    ->query(
        startDate: $startDate,
        endDate: $endDate,
        offset: $offset,
    );

$response = Mpesa::using($credentials)
    ->imsi()
    ->query(
        phoneNumber: $phoneNumber
    );

{
    "ResponseRefID": "8bab-42cc-bb85-5056a6c01e6915928124",
    "ResponseCode": "1000",
    "ResponseMessage": "Success",
    "Response": [
        [
            {
                "transactionId": "XXXXXXXXX",
                "trxDate": "2025-01-24T10:56:11+03:00",
                "msisdn": 2547XXXXXXXX,
                "sender": "MPESA",
                "transactiontype": "c2b-buy-goods-debit",
                "billreference": "",
                "amount": "2697.0",
                "organizationname": "VENDOR"
            }
        ]
    ]
}

{
    "OriginatorConversationID": "5118-111210482-1",
    "ConversationID": "AG_20230420_2010759fd5662ef6d054",
    "ResponseCode": "0",
    "ResponseDescription": "Accept the service request successfully."
}

{
 "Result":
 {
   "ResultType": "0",
   "ResultCode":"0",
   "ResultDesc": "The service request is processed successfully",
   "OriginatorConversationID":"626f6ddf-ab37-4650-b882-b1de92ec9aa4",
   "ConversationID":"12345677dfdf89099B3",
   "TransactionID":"QKA81LK5CY",
   "ResultParameters":
     {
       "ResultParameter":
          [{
           "Key":"DebitAccountBalance",
           "Value":"{Amount={CurrencyCode=KES, MinimumAmount=618683, BasicAmount=6186.83}}"
          },
          {
          "Key":"Amount",
           "Value":"190.00"
          },
           {
          "Key":"DebitPartyAffectedAccountBalance",
           "Value":"Working Account|KES|346568.83|6186.83|340382.00|0.00"
          },
           {
          "Key":"TransCompletedTime",
           "Value":"20221110110717"
          },
           {
          "Key":"DebitPartyCharges",
           "Value":""
          },
           {
          "Key":"ReceiverPartyPublicName",
           "Value":000000– Biller Companty
          },
          {
          "Key":"Currency",
           "Value":"KES"
          },
          {
           "Key":"InitiatorAccountCurrentBalance",
           "Value":"{Amount={CurrencyCode=KES, MinimumAmount=618683, BasicAmount=6186.83}}"
          }]
       },
     "ReferenceData":
       {
        "ReferenceItem":[
           {"Key":"BillReferenceNumber", "Value":"19008"},
           {"Key":"QueueTimeoutURL", "Value":"https://mydomain.com/b2b/businessbuygoods/queue/"}
         ]
      }
 }
}

{
 "Result":
 {
   "ResultType":0,
   "ResultCode":2001,
   "ResultDesc":"The initiator information is invalid.",
   "OriginatorConversationID":"12337-23509183-5",
   "ConversationID":"AG_20200120_0000657265d5fa9ae5c0",
   "TransactionID":"OAK0000000",
   "ResultParameters":{
     "ResultParameter":{
        "Key":"BillReferenceNumber",
        "Value":12323333
      }
   },
   "ReferenceData":{
     "ReferenceItem":[
      {
        "Key":"BillReferenceNumber",
        "Value":12323333
      },
      {
        "Key":"QueueTimeoutURL",
        "Value":"https://internalapi.safaricom.co.ke/mpesa/abresults/v1/submit"
      }
      {
        "Key":"Occassion"
      }
     ]
    }
 }
}
bash
php artisan mpesa-multivendor:install
bash
php artisan vendor:publish --tag=mpesa-multivendor-config