定制 astrasoftwares/astrapay 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

astrasoftwares/astrapay

Composer 安装命令:

composer require astrasoftwares/astrapay

包简介

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

README 文档

README

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, this SDK allows you to initiate secure mobile payments and disbursements with ease.

📦 Installation

Install via Composer:

composer require astrasoftwares/astrapay

Requires PHP >= 7.4

⚙️ 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

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)

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)

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

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

  1. Register on Daraja Portalhttps://developer.safaricom.co.ke
  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)
  1. Get your Credentials:
  • Consumer Key & 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)

Phone Number PIN OTP
254708374149 1111 123456

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

📖 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.

astrasoftwares/astrapay 适用场景与选型建议

astrasoftwares/astrapay 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「payment」 「php sdk」 「mpesa」 「B2C」 「safaricom」 「c2b」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 astrasoftwares/astrapay 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 astrasoftwares/astrapay 我们能提供哪些服务?
定制开发 / 二次开发

基于 astrasoftwares/astrapay 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 12
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-01