PHP code example of prevailexcel / laravel-marasoftpay

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

    

prevailexcel / laravel-marasoftpay example snippets


'providers' => [
    ...
    PrevailExcel\MarasoftPay\MarasoftPayServiceProvider::class,
    ...
]

'aliases' => [
    ...
    'MarasoftPay' => PrevailExcel\MarasoftPay\Facades\MarasoftPay::class,
    ...
]



return [

    /**
     * Public Key From MARASOFTPAY Dashboard
     *
     */
    'publicKey' => getenv('MARASOFTPAY_PUBLIC_KEY'),

    /**
     * Encryption Key From MARASOFTPAY Dashboard
     *
     */
    'encryptionKey' => getenv('MARASOFTPAY_ENCRYPTION_KEY'),

    /**
     * You enviroment can either be live or stage.
     * Make sure to add the appropriate API key after changing the enviroment in .env
     *
     */
    'env' => env('MARASOFTPAY_ENV', 'test'), // OR "LIVE"

    /**
     * Your secret hash is a unique key which is part of the data sent with your webhooks
     * It serves as a form of verification to prove that the webhook is coming from Marasoft Pay.
     *
     */
    'hash' => env('MARASOFTPAY_HASH', 'MarasoftPay'), // OR "LIVE"    
    
    /**
     * Should user bear charge?
     *
     */
    'user_bear_charge' => "yes", // or "no"

    /**
     * MARASOFTPAY Base URL
     *
     */
    'baseUrl' => env('MARASOFTPAY_LIVE_URL', "https://api.marasoftpay.live"),
];

// Laravel 5.1.17 and above
Route::post('/pay', 'PaymentController@createPayment')->name('pay');
Route::callback(PaymentController::class, 'handleGatewayCallback');
Route::webhook(WebhookController::class, 'handleWebhook');

// Laravel 8 & 9
Route::post('/pay', [PaymentController::class, 'createPayment'])->name('pay');
Route::callback(PaymentController::class, 'handleGatewayCallback');
Route::webhook(WebhookController::class, 'handleWebhook');



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
use PrevailExcel\MarasoftPay\Facades\MarasoftPay;

class PaymentController extends Controller
{

    /**
     * Redirect the User to Marasoft Pay Payment Page
     * @return Url
     */
    public function redirectToGateway()
    {
        try{
            return MarasoftPay::getLink()->redirectNow();
        }catch(\Exception $e) {
            return Redirect::back()->withMessage(['msg'=> $e->getMessage(), 'type'=>'error']);
        }        
    }

    /**
     * Obtain Marasoft Pay payment information
     * @return void
     */
    public function handleGatewayCallback()
    {
        $paymentDetails = marasoftpay()->getPaymentData();

        dd($paymentDetails);
        // Now you have the payment details,
        // you can store the reference ID in your db.
        // you can then redirect or do whatever you want
    }
}

/**
 *  In the case where you need to pass the data from your
 *  controller or via your client or app instead of a form
 *  
 */
 $data = [       
        'name' => "Prevail Ambrose",
        'email_address' => "[email protected]",
        'phone_number' => "08100000000",
        'amount' => "9000",
        'description' => "Gold Color"
    ];

    // if monolithic, do
    return MarasoftPay::getLink($data)->redirectNow();

    // if API, do
    return MarasoftPay::getLink($data, true);




namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PrevailExcel\MarasoftPay\Facades\MarasoftPay;

class PaymentController extends Controller
{  
    /**
     * You collect data from your blade form
     * and this returns the Account details for payment
     */
    public function createPayment()
    {
        try {
            // You can use the global helper marasoftpay()->method() or the Facade MarasoftPay::method().
            
            // Dynamic Account Payment
            return MarasoftPay::payWithBankTransfer("100000");

            // Reserved Account Payment
            $data = [        
                'first_name' => "Prevail",
                'last_name' => "Ambrose",
                'phone_number' => "08052344545",
                'tag' => "whatever",
                'bvn' => "2522222222"
            ];
            return MarasoftPay::getReservedAccount($data);
            
        } catch (\Exception $e) {
            return redirect()->back()->withMessage(['msg' => $e->getMessage(), 'type' => 'error']);
        }
    }
}

    public function handleWebhook()
    {
        // verify webhook and get data
        marasoftpay()->getWebhookData()->proccessData(function ($data) {
            // Do something with $data
            logger($data);
            $decodedData = json_decode($data, true);
            if (decodedData['event'] == 'CHECKOUT') {
            // Do Something
            } else if (decodedData['event'] == 'RESERVED') {
            // Do Another Thing
            } else {
                // Do Any other thing
            }
            
            // If you have heavy operations, dispatch your queued jobs for them here
            // OrderJob::dispatch($data);
        });
        
        // Acknowledge you received the response
        return http_response_code(200);
    }

protected $except = [
    'marasoftpay/webhook',
];

/**
 * Make payment with MPESA mobile money
 * @returns array
 */
MarasoftPay::payWithMobileMoney(?array $data);
// Or
marasoftpay()->payWithMobileMoney(?array $data);

/**
 * Make payment via USSD
 * @returns array
 */
MarasoftPay::ussd();

/**
 * Check your balance for the different currencies available
 * @returns array
 */
MarasoftPay::checkBalance();

/**
 * Generate account statements with custom date ranges
 * @returns array
 */
MarasoftPay::accountStatement(?string $start_date = null, ?string $end_date = null);

/**
 * Generate transfer history statements with custom date ranges
 * @returns array
 */
MarasoftPay::transferHistory(?string $start_date = null, ?string $end_date = null);

/**
 * Generate payments history statements with custom date ranges
 * @returns array
 */
MarasoftPay::paymentsHistory(?string $start_date = null, ?string $end_date = null);

/**
 * Generate reserved account history statements with custom date ranges
 * @returns array
 */
MarasoftPay::reservedAccountHistory(?string $start_date = null, ?string $end_date = null);


/**
 * Move funds from your Marasoft Pay balance to a bank account.
 * @returns array
 */
MarasoftPay::transfer($data = null);

/**
 * Get all the bank codes for all existing banks in our operating countries.
 * @returns array
 */
MarasoftPay::getBanks();

/**
 * Verify the status of a transaction carried out on your Marasoft Pay account
 * @returns array
 */
MarasoftPay::verifyTransaction(?string $ref = null);
// Or
request()->ref = "reference";
marasoftpay()->verifyTransaction();

/**
 * Verify the status of a transfer carried out from your Marasoft Pay account 
 * @returns array
 */
MarasoftPay::verifyTransfer(?string $ref = null);

/**
 * Verify the owner of a bank account using the bank code and the account uumber 
 * @returns array
 */
MarasoftPay::confirmAccount(?string $bank_code = null, ?string $account_number = null);

bash
php artisan vendor:publish --provider="PrevailExcel\MarasoftPay\MarasoftPayServiceProvider"