Coder Social home page Coder Social logo

laravel-pesa-demo's Introduction

Laravel Pesa Example

Tested in php 8.x and Laravel Version 9

Requirements

  • PHP 8.x+
  • Laravel Version 9
  • Composer

Steps

  1. Create a Laravel application or use existing one There are several ways to create a Laravel application. For this example we will use the composer command to create a new Laravel application
composer create-project --prefer-dist laravel/laravel laravel-example

Then change directory to the newly created application

cd laravel-example
  1. Install laravel-pesa package
composer require openpesa/laravel-pesa
  1. Publish config files
php artisan vendor:publish --tag=pesa-config --force
  1. Add/Update .env variables as per your environment

You will get the public key and secret key from the M-Pesa Dashboard

You can find the .env file in the config directory of your Laravel application

The .env file should contain the following variables:

The public keys will be in the documentation while the secret keys will be in the Application Settings on the M-Pesa Postal

PESA_PUBLIC_KEY=you will get this from the M-Pesa Portal Documentation
PESA_API_KEY=
PESA_ENV=sandbox
  1. Adding the logic

For simplicity, we have added a simple example API route in the file routes/api.php

This package comes with a Pesa facade. You can use it to make transactions.

// add the following line to the `routes/api.php` file
use Openpesa\Pesa\Facades\Pesa;
// This route will be mapped to  =>  `your-URL/api/charge`
Route::get('/charge', function () {
    $response =  Pesa::c2b([
        'input_Amount' => 5000, // Amount to be charged
        'input_Country' => 'TZN',
        'input_Currency' => 'TZS',
        'input_CustomerMSISDN' => '000000000001', // replace with your phone number
        'input_ServiceProviderCode' => '000000', // replace with your service provider code given by M-Pesa
        'input_ThirdPartyConversationID' => 'rasderekf', // unique
        'input_TransactionReference' => 'asdodfdferre', // unique
        'input_PurchasedItemsDesc' => 'Test  Item'
    ]);


    return $response;
});
  1. Start the development server
php artisan serve
  1. Access and test the API route
curl -X GET http://localhost:8000/api/charge

response:

{
    "output_ResponseCode": "INS-0",
    "output_ResponseDesc": "Request processed successfully",
    "output_TransactionID": "rQTfMMrPDRdj",
    "output_ConversationID": "4e77dadd00b94b1985ee73839f4a23b4",
    "output_ThirdPartyConversationID": "rerekf"
}

laravel-pesa-demo's People

Contributors

alphaolomi avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

nickyraph

laravel-pesa-demo's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.