Dipu Dey 17th Oct 2022

Nagad Payment is a Bangladeshi Payment Gateway System. In this blog, I explain how you can easily use Nagad Payment Gateway API by using laravel.

Requirements

  • PHP >=7.2
  • Laravel >= 6

Installation

<code class="language-php">
composer require codeboxr/nagad
</code>

vendor publish (config)

<code class="language-php">
php artisan vendor:publish --provider="Codeboxr\Nagad\NagadServiceProvider"
</code>

After publish config file setup your credential. you can see this in your config directory nagad.php file

<code class="language-php">
return [
    "sandbox"         => env("NAGAD_SANDBOX", true), // if true it will redirect to sandbox url
    "merchant_id"     => env("NAGAD_MERCHANT_ID", ""), 
    "merchant_number" => env("NAGAD_MERCHANT_NUMBER", ""),
    "public_key"      => env("NAGAD_PUBLIC_KEY", ""),
    "private_key"     => env("NAGAD_PRIVATE_KEY", ""),
    'timezone'        => 'Asia/Dhaka', // By default 
    "callback_url"    => env("NAGAD_CALLBACK_URL", "http://127.0.0.1:8000/nagad/callback"), // By default you can change it in your callback url
    "response_type"   => "json" // By default json you can change response type json,html 
];
</code>

Usage

1. Create Payment
<code class="language-php">
use Codeboxr\Nagad\Payment\Payment;

(new Payment)->create(2, "dwAbcd343223wDweg")  // 1st parameter is amount and 2nd is unique invoice number 
</code>

or

<code class="language-php">
use Codeboxr\Nagad\Facade\NagadPayment;

NagadPayment::create(2, 'abc1DefS34');
</code>
2. Verify Payment
<code class="language-php">
use Codeboxr\Nagad\Payment\Payment;

(new Payment)->verify($paymentRefId) // $paymentRefId which you will find callback URL request parameter
</code>

or

<code class="language-php">
use Codeboxr\Nagad\Facade\NagadPayment;

NagadPayment::verify($paymentRefId);
</code>
3. Refund Payment
<code class="language-php">
use Codeboxr\Nagad\Payment\Refund;

(new Refund)->refund($paymentRefId,$refundAmount); // $paymentRefId which you will find callback URL request parameter,
</code>

or

<code class="language-php">
use Codeboxr\Nagad\Facade\NagadRefund;

NagadRefund::refund($paymentRefId,$refundAmount);
</code>

Contributing

Contributions to the Nagad package are welcome. Please note the following guidelines before submitting your pull request.

  • Follow PSR-4 coding standards.
  • Read Nagad API documentations first

License

Nagad is licensed under the MIT License.

Copyright 2022 Codeboxr