A Simpleguide to integrate Chapa gateway to your Laravel Application.

Are you working on Laravel project and want to integrate Chapa to process your payments?

Installation

First thing First we have to install and this will guide you in installing the package

Prerequisite

PHP 7.3+, Laravel and Composer are required.

To get the latest version of Chapa Laravel Package, simply use composer

composer require chapa/chapa-laravel

Configuration

Publish the configuration file using this command:

php artisan vendor:publish --provider="Chapa\Chapa\ChapaServiceProvider"

A configuration-file named chapa.php will be placed in your config directory

Usage

Open your .env file and add your public key, secret key, environment variable and logo url like so:

Get your keys form here

CHAPA_SECRET_KEY = YOUR_API_KEY
CHAPA_PUBLIC_KEY = YOUR_API_KEY
CHAPA_WEBHOOK_SECRET = YOUR_API_KEY

Assuming you have done the installation

Now time to accept payment.

Payment Checkout Implementation

Initiating Chapa Payment Modal is simple using this package:

1. Setup Routes

<?php
 
use Illuminate\Support\Facades\Route;
 
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
 
// The page that displays the payment form
Route::get('/', function () {
    return view('welcome');
});
// The route that the button calls to initialize payment
 
Route::post('pay', 'App\Http\Controllers\ChapaController@initialize')->name('pay');
 
// The callback url after a payment
Route::get('callback/{reference}', 'App\Http\Controllers\ChapaController@callback')->name('callback');

2. Setup the Payment Page

A sample payment button will look like so:

welcome.blade.php

<h3>Buy Borsa for 100.00 ETB</h3>
<form method="POST" action="{{ route('pay') }}" id="paymentForm">
    {{ csrf_field() }}
 
 
    <input type="submit" value="Buy" />
</form>

3. Setup your Controller

Setup your controller to handle the routes. for this guide we created ChapaController. Use the Chapa as Chapa facade.

Example

<?php
 
namespace App\Http\Controllers;
 
use Chapa\Chapa\Facades\Chapa as Chapa;
 
class ChapaController extends Controller
{
    /**
     * Initialize Rave payment process
     * @return void
     */
    protected $reference;
 
    public function __construct(){
        $this->reference = Chapa::generateReference();
 
    }
    public function initialize()
    {
        //This generates a payment reference
        $reference = $this->reference;
        
 
        // Enter the details of the payment
        $data = [
            
            'amount' => 100,
            'email' => 'hi@negade.com',
            'tx_ref' => $reference,
            'currency' => "ETB",
            'callback_url' => route('callback',[$reference]),
            'first_name' => "Israel",
            'last_name' => "Goytom",
            "customization" => [
                "title" => 'Chapa Laravel Test',
                "description" => "I amma testing this"
            ]
        ];
        
 
        $payment = Chapa::initializePayment($data);
 
 
        if ($payment['status'] !== 'success') {
            // notify something went wrong
            return;
        }
 
        return redirect($payment['data']['checkout_url']);
    }
 
    /**
     * Obtain Rave callback information
     * @return void
     */
    public function callback($reference)
    {
        
        $data = Chapa::verifyTransaction($reference);
        dd($data);
 
        //if payment is successful
        if ($data['status'] ==  'success') {
        
 
        dd($data);
        }
 
        else{
            //oopsie something ain't right.
        }
 
 
    }
}

Once the initialize is called, you get redirected to a chapa payment page.

After a successful payment, the user is redirected to the callback url with status

You can find the source code for this example here