PHP code example of astrasoftwares / astrapay

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

    

astrasoftwares / astrapay example snippets



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);


// 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);


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


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


$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);