PHP code example of shubhamc4 / cgrate-laravel

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

    

shubhamc4 / cgrate-laravel example snippets


use CGrate\Laravel\Facades\CGrate;

// Get account balance
try {
    $response = CGrate::getAccountBalance();

    if ($response->isSuccessful()) {
        echo 'Account Balance: ' . $response->balance;
    } else {
        echo 'Error: ' . $response->responseMessage;
    }
} catch (\CGrate\Php\Exceptions\CGrateException $e) {
    echo 'API Error: ' . $e->getMessage();
}

use CGrate\Laravel\Facades\CGrate;

try {
    $response = CGrate::getAvailableCashDepositIssuers();
    print_r($response);
} catch (\CGrate\Php\Exceptions\CGrateException $e) {
    echo "Exception: " . $e->getMessage();
}

use CGrate\Php\DTOs\PaymentRequestDTO;
use CGrate\Laravel\Facades\CGrate;

// Create a payment request
$payment = new PaymentRequestDTO(
    transactionAmount: 10.50,
    customerMobile: '260970000000', // Zambian mobile number format (without the + sign)
    paymentReference: 'INVOICE-123'
);

// Or use the factory method for convenience
$payment = PaymentRequestDTO::create(
    transactionAmount: 10.50,
    customerMobile: '260970000000',
    paymentReference: 'INVOICE-123'
);

// Process the payment
try {
    $response = CGrate::processCustomerPayment($payment);

    if ($response->isSuccessful()) {
        echo 'Payment successful! Payment ID: ' . $response->paymentID;
    } else {
        echo 'Payment failed: ' . $response->responseMessage;
    }
} catch (\CGrate\Php\Exceptions\ValidationException $e) {
    echo 'Validation Error: ' . $e->getMessage();
    foreach ($e->errors() as $field => $errors) {
        echo "\n- {$field}: " . implode(', ', $errors);
    }
} catch (\CGrate\Php\Exceptions\CGrateException $e) {
    echo 'API Error: ' . $e->getMessage();
}

use CGrate\Laravel\Facades\CGrate;

// Query transaction status
try {
    $response = CGrate::queryCustomerPayment('INVOICE-123');

    if ($response->isSuccessful()) {
        echo 'Transaction reference: ' . $response->transactionReference;
    } else {
        echo 'Status query failed: ' . $response->responseMessage;
    }
} catch (\CGrate\Php\Exceptions\CGrateException $e) {
    echo 'API Error: ' . $e->getMessage();
}

use CGrate\Php\DTOs\CashDepositRequestDTO;
use CGrate\Laravel\Facades\CGrate;

$customerAccount = '260970000000';  // Customer account number

// Create a cash deposit request
$cashDeposit = new CashDepositRequestDTO(
    transactionAmount: 10.50,
    customerAccount: $customerAccount,
    issuerName: CGrate::getCustomerIssuerName($customerAccount),
    depositorReference: 'INVOICE-123'
);

// Or use the factory method for convenience
$cashDeposit = CashDepositRequestDTO::create(
    transactionAmount: 10.50,
    customerAccount: $customerAccount,
    issuerName: CGrate::getCustomerIssuerName($customerAccount),
    depositorReference: 'INVOICE-123'
);

try {
    $response = CGrate::processCashDeposit($cashDeposit);

    if ($response->isSuccessful()) {
        echo "Depositor reference: " . $response->depositorReference;
    } else {
        echo "Cash deposit failed: " . $response->responseMessage;
    }
} catch (\CGrate\Php\Exceptions\CGrateException $e) {
    echo "Exception: " . $e->getMessage();
}
bash
php artisan vendor:publish --provider="CGrate\Laravel\CGrateServiceProvider"
bash
php artisan cgrate:balance