PHP code example of sonicpesa / sonicpesa-php

1. Go to this page and download the library: Download sonicpesa/sonicpesa-php 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/ */

    

sonicpesa / sonicpesa-php example snippets



// 1. Initialize SonicPesa
c = new SonicPesa("sonicpesa_api_key", "sonicpesa_secrete_key"); // Replace with your SonicPesa API key and secrete key
$payment = $sonic->payment();

// 2. Create an order
// You can pass an array with all details:

$orderData = [
    "buyer_email" => "[email protected]",
    "buyer_name" => "SAM OCHU",
    "buyer_phone" => "255657779003",
    "amount" => 5000,
    "currency" => "TZS"
];

$order = $payment->create_order($orderData);

print_r($order);


// 3. Check order status
$orderId = $order['order_id'] ?? '';

if ($orderId) {
    $status = $payment->order_status($orderId);
    print_r($status);
} else {
    echo "Order ID not found!";
}

// Example Response
{
  "status": "success",
  "order_id": "sp_xxxxxxxx",
  "status": "pending",
  "amount": 5000,
  "currency": "TZS",
  "buyer_name": "SAM OCHU",
  "buyer_email": "[email protected]",
  "buyer_phone": "255657779003"
}
bash
composer