DEV Community

Ariful Islam
Ariful Islam

Posted on • Updated on

Use Nagad API PHP SDK to PHP Application for Taking Payment

Nagad is a popular mobile banking service in Bangladesh. A large number of people around the country are using nagad. For paying payment using nagad wallet you need to have nagad account and website/app owner need to have merchant account. Merchant can take payment easily by implementing api provided by nagad. Here is the github link for this package https://github.com/arif98741/nagadApi

PHP is a popular programming language. To use nagad in php based application its needed to implement API. Here I've made a php sdk library called xenon/nagad-api that will reduce your headache regarding nagad api. Here I am guiding you how to use nagad php sdk library in your php application.

Step 1:
composer require xenon/nagad-api

Step 2:

Example Code


<?php

use Xenon\NagadApi\Helper;
use Xenon\NagadApi\Base;

require 'vendor/autoload.php';

//all configuration are used here for demo purpose.
//for use in dev mode use 'development'
//for use in production mode use 'production'
$config = [
    'NAGAD_APP_ENV' => 'development', //development|production
    'NAGAD_APP_LOG' => '1',
    'NAGAD_APP_ACCOUNT' => '016XXXXXXXX', //demo
    'NAGAD_APP_MERCHANTID' => '6800000025', //demo
    'NAGAD_APP_MERCHANT_PRIVATE_KEY' => 'MIIEvFAAxN1qfKiRiCL720FtrI7QL7fvQ==',
    'NAGAD_APP_MERCHANT_PG_PUBLIC_KEY' => 'MIIBIjANBc54jjMJoP2toR9fGmQV7y9fzj6TIz9SFfsTQOugHkhyRzzhvZisiKzOAAWNX8RMpG+iqQi4p9W9VrmmiCfFDmLFnMrwhncnMsvlXB8QSJCq2irrx3HG0SJJCbS5+atz+E1iqO8QaPJ05snxv82Mf4NlZ4gZK0Pq/VvJ20lSkR+0nk+s/v3BgIyle78wjZP1vWLU4wIDAQAB',
    'NAGAD_APP_TIMEZONE' => 'Asia/Dhaka',
];

$nagad = new Base($config, [
    'amount' => 100,
    'invoice' => Helper::generateFakeInvoice(15, true),
    'merchantCallback' => 'https://example.com/payment/success/id=4',
]);
//way 1 - use for website
$status = $nagad->payNow($nagad); //will redirect to payment url of Nagad

//way 2 - useful for rest api or graphQL 
$paymentUrl = $nagad->payNowWithoutRedirection($nagad); //will return payment url like below. You can use that url and do whatever u want to get payment from clients. 
//
`http://sandbox.mynagad.com:10060/check-out/MDYyODAwNTcyNTYxNi42ODMwMDIwMDcxMDQyMjUuOU5PTEFVNkVaWkdUWVRBLmJiZGMyNTE3MTVmZTNiNjIzN2Zk`

//after that use below method for extracting payment response that will return an array
$response = Helper::successResponse('https://example.com/payment/success/id=4/?merchant=683XXXX225&order_id=CKH060JXXXXXFRA2&payment_ref_id=MXXXXXXXXtIMDYwSjFRSlBRMUZSQTIuMTg0NTE2Yzc3ZmEzNmEwZTJlZjk=&status=Success&status_code=00_0000_000&message=Successful%20Transaction&payment_dt=20211123235008&issuer_payment_ref=MTEyMzIzNDg1NzUwOS42ODMwMDIwMDcxMDQyMjUuQ0tIMDYwSjFRSlBRMUZSQTIuMTg0NTE2Yzc3ZmEzNmEwZTJlZjk=');
Array
(
    [merchant] => 683XXXX225
    [order_id] => CKH060JXXXXXFRA2
    [payment_ref_id] => MXXXXXXXXtIMDYwSjFRSlBRMUZSQTIuMTg0NTE2Yzc3ZmEzNmEwZTJlZjk=
    [status] => Success
    [status_code] => 00_0000_000
    [message] => Successful Transaction
    [payment_dt] => 20211123235008
    [issuer_payment_ref] => MTEyMzIzNDg1NzUwOXXXXXtIMDYwSjFRSlBRMUZSQTIuMTg0NTE2Yzc3ZmEzNmEwZTJlZjk=
)


//For payment verification use below method. You will then get below json as response. 
echo $config->verifyPayment($response['payment_ref_id']);
## Payment verification Response
{
    merchantId: "683XXXX225",
    orderId: "CKH060JXXXXXFRA2",
    paymentRefId: "MXXXXXXXXtIMDYwSjFRSlBRMUZSQTIuMTg0NTE2Yzc3ZmEzNmEwZTJlZjk=",
    amount: "16",
    clientMobileNo: "016****5428",
    merchantMobileNo: "01XXXXXXX10",
    orderDateTime: "2021-11-23 23:48:22.0",
    issuerPaymentDateTime: "2021-11-23 23:50:08.0",
    issuerPaymentRefNo: "000XXXW",
    additionalMerchantInfo: null,
    status: "Success",
    statusCode: "000",
    cancelIssuerDateTime: null,
    cancelIssuerRefNo: null
}

Enter fullscreen mode Exit fullscreen mode

Information:

Sandbox

  1. Need sandbox details for sandbox testing. Check your email that you have got from nagad authority
  2. Use sandbox details such as pgpublickey, privatekey, merchant-id for sandbox testing
  3. You need to register a mobile number for sandbox testing. Contact with your account manager for doing this
  4. You should test environment before going to live

Live

  1. Need production details for production final. You will get through email
  2. Your server ip/domain should be whitelisted before running in production

Login to your nagad merchant panel

https://auth.mynagad.com:10900/authentication-service-provider-1.0/login

Step 1:
In the Merchant Portal, Go to Merchant Integration Details under Merchant Management Menu.
You will get the Merchant ID which is your Merchant ID for Integration.

Then, Click on “Key Generate” and
Download the Merchant Private Key and Merchant Public Key.

Step 2:
Go to Merchant Integration under Merchant Management Menu.
Put your Call Back URL and Upload the Merchant Public Key which you have downloaded in Step 1. Add and Submit!

Top comments (3)

Collapse
 
khan922 profile image
Emran922

Hello Ariful
I have questons, may we talk by w/a?

Collapse
 
villain707 profile image
VILLAIN707

HELLO
I AM TALK WITH YOU

Collapse
 
arif98741 profile image
Ariful Islam

what do you want to say?