PayPal API Get Account Balance - Kvcodes
X

Congrats, You are Subscribed to Receive Updates.

PayPal API Get Account Balance


Introduction

Lets get the PayPal API Get Account Balance of your PayPal wallet. This feature may good for the users those who wants to know their balance from their website or web applications. Let’s make the API Functionality to get the balance from your PayPal. First you need to login to your PayPal and get API Keys. So just login your PayPal and open PayPal Developers to get Client Id. Just do the following steps to get it.

Getting API  credentials

Once you logged into the Developer PayPal.you can see a link from left menu, “My Accounts”  And you can see “Create” button with in this page.  Just create follow with your merchant and and details. And follow their steps and once you complete, you will be prompted to see  Profile of newly generated id. with that you can get the API Username,API Password, API Signature.

GetBalance

Lets write an curl function to get balance from PayPal.

function CallGetBalance($API_Endpoint, $version, $user, $pwd, $signature) {// setting the curl parameters.
   $ch = curl_init ();
   curl_setopt ( $ch, CURLOPT_URL, $API_Endpoint );
   curl_setopt ( $ch, CURLOPT_VERBOSE, 1 );
   curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
   curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
   curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
   curl_setopt ( $ch, CURLOPT_POST, 1 );

   // NVPRequest for submitting to server
   $nvpreq = "METHOD=GetBalance" . "&RETURNALLCURRENCIES=1" . "&VERSION=" . $version . "&PWD=" . $pwd . "&USER=" . $user . "&SIGNATURE=" . $signature;
   curl_setopt ( $ch, CURLOPT_POSTFIELDS, $nvpreq );
   $response = curl_exec ( $ch );
   $nvpResArray = deformatNVP ( $response );
   curl_close ( $ch );
   return $nvpResArray;
}

And than we have to write array to variable separator,which helps to extract the balance from given string. Lets write deformatNVP function.

function deformatNVP($nvpstr) {
   $intial = 0;
   $nvpArray = array ();

    while ( strlen ( $nvpstr ) ) {// postion of Key
       $keypos = strpos ( $nvpstr, '=' );// position of value
       $valuepos = strpos ( $nvpstr, '&' ) ? strpos ( $nvpstr, '&' ) : strlen ( $nvpstr );

       $keyval = substr ( $nvpstr, $intial, $keypos );
       $valval = substr ( $nvpstr, $keypos + 1, $valuepos - $keypos - 1 );// decoding the respose
       $nvpArray [urldecode ( $keyval )] = urldecode ( $valval );
       $nvpstr = substr ( $nvpstr, $valuepos + 1, strlen ( $nvpstr ) );
   }
   return $nvpArray;
}

And finally, we will call the function to Get Balance

$version = "124";
$user = "****************************";
$pwd = "*****************";
$signature = "***************************************************";
$API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp";

$resArray = CallGetBalance ( $API_Endpoint, $version, $user, $pwd, $signature );
$ack = strtoupper ( $resArray ["ACK"] );
if ($ack == "SUCCESS") {
    $balance = urldecode ( $resArray ["L_AMT0"] );
    $currency = urldecode ( $resArray ["L_CURRENCYCODE0"] );
    echo "Account Balance: " . $balance . " " . $currency;
}

That’s it to receive balance from your paypal account.Remember,this is sandbox account.  When you make it live. remove the sandbox from $API_Endpoint.

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, OpenCart Theme Development, PHP, HTML, CSS, jQuery, Bootstrap development and content SEO.

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar

Subscribe E-mail Updates

Congrats, You are Subscribed to Receive Updates.

Advertisement

Category Posts