astrasoftwares/astrapay

AstraPay PHP SDK for M-Pesa STK Push

v1.0.0 2025-08-01 22:50 UTC

This package is not auto-updated.

Last update: 2025-08-02 11:41:22 UTC


README

AstraPay is a lightweight PHP SDK that simplifies integrating Safaricom M-Pesa STK Push into your web applications. Built and maintained by Astra Softwares, this SDK allows you to initiate secure mobile payments with ease.

📦 Installation

Install via Composer:

composer require astrasoftwares/astrapay

Requires PHP >= 7.4

⚙️ Getting Started

require 'vendor/autoload.php';

use Astrapay\AstraMpesa;

$client = new AstraMpesa([
  'consumerKey' => 'YOUR_CONSUMER_KEY',
  'consumerSecret' => 'YOUR_CONSUMER_SECRET',
  'shortcode' => '174379',
  'passkey' => 'YOUR_PASSKEY',
  'callbackUrl' => 'https://yourdomain.com/callback'
]);

$response = $client->pay('254712345678', 100); // phone, amount
print_r($response);

🔐 How to Get M-Pesa API Credentials

To integrate with M-Pesa, you’ll need to create a Safaricom Daraja developer account and set up your app:

  1. Register on Daraja Portalhttps://developer.safaricom.co.ke

  2. Create an App

    • Log in and click “My Apps” → “Add a New App”
    • Choose M-Pesa Express (STK Push) product
    • Give it a name and save
  3. Get the following from your App dashboard:

    • Consumer Key
    • Consumer Secret
  4. Get your Shortcode and Passkey:

    • Shortcodes are provided by Safaricom (test or production)
    • You can generate your passkey via the Daraja portal (or request from Safaricom if in production)
  5. Set your Callback URL:

    • Must be publicly accessible (e.g., https://yourdomain.com/callback)
    • This is where M-Pesa will send payment confirmations

🧲 Sample Test Numbers (Sandbox)

Phone Number PIN OTP
254708374149 1111 123456

Use the sandbox environment for development/testing. Production use requires approval.

📖 Documentation

🤝 Contribute

Pull requests are welcome! If you find a bug or want a feature added, open an issue or submit a PR.

🧑‍💻 Author

Built with ❤️ by Ishmael Bett 📧 info.astrasoft@gmail.com

📄 License

This project is licensed under the MIT License.