Giter Site home page Giter Site logo

laravel-nagad-api's Introduction

Nagad (Bangladesh) payment gateway for Laravel 6.x+, 7.x+, 8.x+

Nagad is one of the Financial Services in Bangladesh. This package is built for Nagad Payment Gateway for Laravel 6.x, 7.x and 8.x+

Contents

Installation

You can install the package via composer:

composer require anovob/laravel-nagad-api

Setting up your configuration

Extract the nagad config files:

php artisan vendor:publish --tag=nagad-config
  • This will publish and config file in config_path() of your application. Eg. config/nagad.php

  • Configure the configurations for the nagad merchant account. Use sandbox = true for development stage.

  • Be sure to set the timezone of you application to Asia/Dhaka in order to work with Nagad PGW. To do this: go to config/app.php and set 'timezone' => 'Asia/Dhaka'

Usage

NagadPGW uses three stages of payment process, and two of theme are simultaneous. To get started, first you have to setup a callback route (GET) for the Nagad Callback and name the route in the nagad config file.

    // in routes/web.php
    Route::get('/nagad/callback', 'NagadController@callback')->name('nagad.callback');

    //in config/nagad.php
    'callback' => 'nagad.callback' // or use env variable to store

env setup

NAGAD_METHOD=sandbox
NAGAD_MERHCANT_ID=683002007104225
NAGAD_MERHCANT_PHONE=01817535192
NAGAD_KEY_PUBLIC=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjBH1pFNSSRKPuMcNxmU5jZ1x8K9LPFM4XSu11m7uCfLUSE4SEjL30w3ockFvwAcuJffCUwtSpbjr34cSTD7EFG1Jqk9Gg0fQCKvPaU54jjMJoP2toR9fGmQV7y9fz31UVxSk97AqWZZLJBT2lmv76AgpVV0k0xtb/0VIv8pd/j6TIz9SFfsTQOugHkhyRzzhvZisiKzOAAWNX8RMpG+iqQi4p9W9VrmmiCfFDmLFnMrwhncnMsvlXB8QSJCq2irrx3HG0SJJCbS5+atz+E1iqO8QaPJ05snxv82Mf4NlZ4gZK0Pq/VvJ20lSkR+0nk+s/v3BgIyle78wjZP1vWLU4wIDAQAB
NAGAD_KEY_PRIVATE=MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCJakyLqojWTD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WtSMPNHXsx+crKY/+YvhnujWKwhphcbtqkfj5L0dWPDNpqOXJKV1wHt+vUexhKwus2mGF0flnKIPG2lLN5UU6rs0tuYDgyLhAyds5ub6zzfdUBG9Gh0ZrfDXETRUyoJjcGChC71AoGAfmSciL0SWQFU1qjUcXRvCzCK1h25WrYS7E6pppm/xia1ZOrtaLmKEEBbzvZjXqv7PhLoh3OQYJO0NM69QMCQi9JfAxnZKWx+m2tDHozyUIjQBDehve8UBRBRcCnDDwU015lQN9YNb23Fz+3VDB/LaF1D1kmBlUys3//r2OV0Q4ECgYBnpo6ZFmrHvV9IMIGjP7XIlVa1uiMCt41FVyINB9SJnamGGauW/pyENvEVh+ueuthSg37e/l0Xu0nm/XGqyKCqkAfBbL2Uj/j5FyDFrpF27PkANDo99CdqL5A4NQzZ69QRlCQ4wnNCq6GsYy2WEJyU2D+K8EBSQcwLsrI7QL7fvQ==
NAGAD_CALLBACK_URL=nagad.callback

To Start payment, in your NagadController:

    use NagadAPI\Nagad;    
    use Illuminate\Http\Request;
    
    public function createPayment() 
    {
        /**
         * Method 1: Quickest
         * This will automatically redirect you to the Nagad PG Page
         * */

        return Nagad::setOrderID('ORDERID123')
            ->setAmount('540')
            ->checkout()
            ->redirect();
        
        /**
         * Method 2: Manual Redirection
         * This will return only the redirect URL and manually redirect to the url
         * */

        $url = Nagad::setOrderID('ORDERID123')
            ->setAmount('540')
            ->checkout()
            ->getRedirectUrl();

        return ['url' => $url];


        /**
         * Method 3: Advanced 
         * You set additional params which will be return at the callback
         * */

        return Nagad::setOrderID('ORDERID123')
            ->setAmount('540')
            ->setAddionalInfo(['pid' => 9, 'myName' => 'DG'])
            ->checkout()
            ->redirect();


        /**
         * Method 4: Advanced Custom Callabck
         * You can set/override callback url while creating payment
         * */

        return Nagad::setOrderID('ORDERID123')
            ->setAmount('540')
            ->setAddionalInfo(['pid' => 9, 'myName' => 'DG'])
            ->setCallbackUrl("https://manual-callback.url/callback")
            ->checkout()
            ->redirect();
    }
    

	//To receive the callback response use this method: 

    /**
     * This is the routed callback method
     * which receives a GET request.
     * 
     * */

    public function callback(Request $request)
    {
        $verified = Nagad::callback($request)->verify();
        if($verified->success()) {

            // Get Additional Data
            dd($verified->getAdditionalData());
            
            // Get Full Response
            dd($verified->getVerifiedResponse());
        } else {
            dd($verified->getErrors());
        }
    }

To receive error response use this in App/Exceptions/Handler.php:

public function render($request, Exception $exception)
{
    if($exception instanceof NagadException) {
    //return custom error page when custom exception is thrown
    return response()->view('errors.nagad', compact('exception'));
    }

    return parent::render($request, $exception);
}

Available Methods

For Checking-out

  • setOrderID(string $orderID) : $orderID to be any unique AlphaNumeric String
  • setAmount(string $amount) : $amount to be any valid currency numeric String
  • setAddionalInfo(array $array) : $array to be any array to be returned at callback
  • setCallbackUrl(string $url) : $url to be any url string to be overidden the defualt callback url set in config
  • checkout() : to initiate checkout process.
  • redirect() : to direct redirect to the NagadPG Web Page.
  • getRedirectUrl() : instead of redirecting, getting the redirect url manually.

For Callback

  • callback($request) : $request to be Illuminate\Http\Request instance
  • verify() : to verify the response.
  • success() : to check if transaction is succeed.
  • getErrors() : to get the error and errorCode if fails transactions | returns array[]
  • getVerifiedResponse() : to get the full verified response | returns array[]
  • getAdditionalData(bool $object) : to get the additional info passed during checkout. $object is to set return object or array.

License

The MIT License (MIT). Please see License File for more information.

laravel-nagad-api's People

Contributors

anovob avatar

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.