PHPackages                             astrasoftwares/astrapay - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Payment Processing](/categories/payments)
4. /
5. astrasoftwares/astrapay

ActiveLibrary[Payment Processing](/categories/payments)

astrasoftwares/astrapay
=======================

A complete PHP SDK for Safaricom M-Pesa Integration (STK Push, C2B, and B2C).

v1.1.1(4mo ago)09MITPHPPHP &gt;=7.4

Since Aug 1Pushed 4mo agoCompare

[ Source](https://github.com/PackagesLibraries/astrapay-php)[ Packagist](https://packagist.org/packages/astrasoftwares/astrapay)[ RSS](/packages/astrasoftwares-astrapay/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (0)

---

AstraPay PHP SDK
================

[](#astrapay-php-sdk)

AstraPay is a lightweight PHP SDK that simplifies integrating **Safaricom M-Pesa STK Push, C2B (Customer to Business), and B2C (Business to Customer)** APIs into your web applications. Built and maintained by [Astra Softwares](https://astrasoft.online), this SDK allows you to initiate secure mobile payments and disbursements with ease.

---

📦 Installation
--------------

[](#-installation)

Install via Composer:

```
composer require astrasoftwares/astrapay
```

> Requires PHP &gt;= 7.4

---

⚙️ Configuration
----------------

[](#️-configuration)

Initialize the client with your credentials. Depending on the service you are using (STK, C2B, or B2C), different keys are required.

```
require 'vendor/autoload.php';

use Astrapay\AstraMpesa;

$config = [
    'consumerKey'    => 'YOUR_CONSUMER_KEY',
    'consumerSecret' => 'YOUR_CONSUMER_SECRET',
    'shortcode'      => '174379', // Paybill or Till Number
    'passkey'        => 'YOUR_PASSKEY', // Required for STK Push
    'callbackUrl'    => 'https://yourdomain.com/callback', // Global callback for STK
    'env'            => 'sandbox', // 'sandbox' or 'live'

    // Required only for B2C
    'initiatorName'      => 'YOUR_INITIATOR_NAME',
    'securityCredential' => 'YOUR_ENCRYPTED_CREDENTIAL'
];

$client = new AstraMpesa($config);
```

---

🚀 Usage Examples
----------------

[](#-usage-examples)

### 1. STK Push (M-Pesa Express)

[](#1-stk-push-m-pesa-express)

Initiate a payment prompt on the customer's phone.

```
// Simple usage
$response = $client->pay('254712345678', 100);

// Advanced usage with custom reference and description
$response = $client->pay(
    '254712345678',
    100,
    'Invoice #102', // Account Reference
    'School Fees'   // Transaction Description
);

print_r($response);
```

### 2. C2B (Customer to Business)

[](#2-c2b-customer-to-business)

Handle payments sent directly to your Paybill/Till via the SIM toolkit.

**Step A: Register URLs (Run Once)**You must tell Safaricom where to send validation and confirmation data.

```
$client->registerC2BUrls(
    'https://yourdomain.com/mpesa/validation',
    'https://yourdomain.com/mpesa/confirmation'
);
```

**Step B: Simulate C2B (Sandbox Only)**Since you cannot use real money in Sandbox, use this to test your confirmation URLs.

```
$client->simulateC2B(
    '254708374149',
    1000,
    'INV/001' // BillRefNumber
);
```

### 3. B2C (Business to Customer)

[](#3-b2c-business-to-customer)

Send money from your business account to a user (e.g., Salaries, Refunds).

```
$response = $client->b2cPayment(
    '254712345678',
    500,
    'BusinessPayment', // Options: SalaryPayment, BusinessPayment, PromotionPayment
    'Refund for Order #20', // Remarks
    'https://yourdomain.com/b2c/timeout', // Queue Timeout URL
    'https://yourdomain.com/b2c/result'   // Result URL
);

print_r($response);
```

---

🔐 How to Get M-Pesa API Credentials
-----------------------------------

[](#-how-to-get-m-pesa-api-credentials)

To integrate with M-Pesa, you’ll need to create a **Safaricom Daraja** developer account:

1. **Register on Daraja Portal**➔
2. **Create an App**

- Log in and click “My Apps” → “Add a New App”
- Ensure you check the boxes for:
- **Lipa na M-Pesa Sandbox** (for STK)
- **M-Pesa Sandbox** (for C2B/B2C)

3. **Get your Credentials:**

- **Consumer Key &amp; Secret:** Found in your App dashboard.
- **Passkey:** Generated via the "Simulate" tab in Daraja or sent via email in production.
- **Security Credential (B2C Only):** This is an encrypted password. In Sandbox, Safaricom provides a test credential. In Production, you must generate it using the M-Pesa Public Certificate.

---

🧲 Sample Test Numbers (Sandbox)
-------------------------------

[](#-sample-test-numbers-sandbox)

Phone NumberPINOTP2547083741491111123456Use the sandbox environment for development/testing. When moving to production (`env => 'live'`), ensure your Shortcode is active and approved by Safaricom.

---

📖 Documentation
---------------

[](#-documentation)

- [Python SDK](https://github.com/astrasoftwares/astrapay/blob/main/Astrapay-python.md)
- [JavaScript SDK](https://github.com/astrasoftwares/astrapay/blob/main/astrapay-js.md)

---

🤝 Contribute
------------

[](#-contribute)

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

---

🧑‍💻 Author
----------

[](#‍-author)

Built with ❤️ by [Ishmael Bett](https://astrasoft.tech)📧

---

📄 License
---------

[](#-license)

This project is licensed under the [MIT License](https://www.google.com/search?q=LICENSE).

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance82

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~78 days

Total

3

Last Release

124d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/fb35fd40b86a49f57633e6454e57c9ce79c454208c078b67d5d4ac123395c222?d=identicon)[astrasoftwares](/maintainers/astrasoftwares)

---

Top Contributors

[![astrasoftwares](https://avatars.githubusercontent.com/u/139498700?v=4)](https://github.com/astrasoftwares "astrasoftwares (4 commits)")

---

Tags

paymentphp-sdkmpesaB2Csafaricomdarajac2bstk-push

### Embed Badge

![Health badge](/badges/astrasoftwares-astrapay/health.svg)

```
[![Health](https://phpackages.com/badges/astrasoftwares-astrapay/health.svg)](https://phpackages.com/packages/astrasoftwares-astrapay)
```

###  Alternatives

[itsmurumba/laravel-mpesa

Laravel Package for Mpesa Daraja API

191.6k](/packages/itsmurumba-laravel-mpesa)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
