Banner

Banner
Ramast
Linux Developer

Saturday, August 31, 2013

Calling Paypal API

I have had this terrible experience with paypal trying doing something simple like doing an API call.

In my opinion Paypal has the worst nightmare for any developer unlike for example stripe which is a peace of cake compared to it.

Anyway after search many websites excluding developers.paypal.com I found this great page
http://coding.smashingmagazine.com/2011/09/05/getting-started-with-the-paypal-api/

In a nutshell the page present a very simple code for calling any paypal api method.

class Paypal {
   /**
    * Last error message(s)
    * @var array
    */
   protected $_errors = array();

   /**
    * API Credentials
    * Use the correct credentials for the environment in use (Live / Sandbox)
    * @var array
    */
   protected $_credentials = array(
      'USER' => 'seller_1297608781_biz_api1.lionite.com',
      'PWD' => '1297608792',
      'SIGNATURE' => 'A3g66.FS3NAf4mkHn3BDQdpo6JD.ACcPc4wMrInvUEqO3Uapovity47p',
   );

   /**
    * API endpoint
    * Live - https://api-3t.paypal.com/nvp
    * Sandbox - https://api-3t.sandbox.paypal.com/nvp
    * @var string
    */
   protected $_endPoint = 'https://api-3t.sandbox.paypal.com/nvp';

   /**
    * API Version
    * @var string
    */
   protected $_version = '74.0';

   /**
    * Make API request
    *
    * @param string $method string API method to request
    * @param array $params Additional request parameters
    * @return array / boolean Response array / boolean false on failure
    */
   public function request($method,$params = array()) {
      $this -> _errors = array();
      if( empty($method) ) { //Check if API method is not empty
         $this -> _errors = array('API method is missing');
         return false;
      }

      //Our request parameters
      $requestParams = array(
         'METHOD' => $method,
         'VERSION' => $this -> _version
      ) + $this -> _credentials;

      //Building our NVP string
      $request = http_build_query($requestParams + $params);

      //cURL settings
      $curlOptions = array (
         CURLOPT_URL => $this -> _endPoint,
         CURLOPT_VERBOSE => 1,
         //I (ramast) commented the following 3 lines for the sake of simplicity
         //CURLOPT_SSL_VERIFYPEER => true, 
         //CURLOPT_SSL_VERIFYHOST => 2,
         //CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem', //CA cert file
         CURLOPT_RETURNTRANSFER => 1,
         CURLOPT_POST => 1,
         CURLOPT_POSTFIELDS => $request
      );

      $ch = curl_init();
      curl_setopt_array($ch,$curlOptions);

      //Sending our request - $response will hold the API response
      $response = curl_exec($ch);

      //Checking for cURL errors
      if (curl_errno($ch)) {
         $this -> _errors = curl_error($ch);
         curl_close($ch);
         return false;
         //Handle errors
      } else  {
         curl_close($ch);
         $responseArray = array();
         parse_str($response,$responseArray); // Break the NVP string to an array
         return $responseArray;
      }
   }
}

After that you can go to https://developer.paypal.com/webapps/developer/docs/classic/api/ find the call you want (make sure you click on NVP not SOAP) Then you can make a call like this
$paypal = new PayPal();
$response = $paypal->request("DoCapture", Array(
        "AUTHORIZATIONID" => "61P02217YA6177331",
        "AMT" => "10.92",
        "COMPLETETYPE" => "Complete",
        "NOTE" => "Thank you for buying from us"));
echo json_encode($response);

No comments:

Post a Comment