/**
  * Post to PayPal
  * 
  * Makes an API call using an NVP String and an Endpoint. Based on code available here: https://www.x.com/blogs/Nate/2011/01/07/digital-goods-with-express-checkout-in-php
  * 
  * @param action, string, required. The API operation to be performed, eg. GetExpressCheckoutDetails. The action is abstracted from you (the developer) by the appropriate helper function eg. GetExpressCheckoutDetails via get_checkout_details()
  */
 protected function call_paypal($action, $profile_id = '', $status = '', $args = array())
 {
     // Use the one function for all PayPal API operations
     $api_parameters = $this->get_payment_details_url($action, $profile_id, $status, $args);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, PayPal_Digital_Goods_Configuration::endpoint());
     curl_setopt($ch, CURLOPT_VERBOSE, 1);
     // Make sure we use TLS as PayPal no longer supports SSLv3: https://ppmts.custhelp.com/app/answers/detail/a_id/1191/session/L2F2LzEvdGltZS8xNDE2MzUyMTgwL3NpZC8tVzFLaU03bQ%3D%3D
     curl_setopt($ch, CURLOPT_SSLVERSION, 1);
     // Turn off server and peer verification
     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);
     // Set the API parameters for this transaction
     curl_setopt($ch, CURLOPT_POSTFIELDS, $api_parameters);
     // Request response from PayPal
     $response = curl_exec($ch);
     // If no response was received from PayPal there is no point parsing the response
     if (!$response) {
         throw new Exception($action . ' failed: ' . curl_error($ch) . '(' . curl_errno($ch) . ')');
     }
     curl_close($ch);
     // An associative array is more usable than a parameter string
     parse_str($response, $parsed_response);
     if (0 == sizeof($parsed_response) || !array_key_exists('ACK', $parsed_response)) {
         throw new Exception("Invalid HTTP Response for POST request({$api_parameters}) to " . PayPal_Digital_Goods_Configuration::endpoint());
     }
     if ($parsed_response['ACK'] == 'Failure') {
         throw new Exception("Calling PayPal with action {$action} has Failed: " . $parsed_response['L_LONGMESSAGE0'], $parsed_response['L_ERRORCODE0']);
     }
     return $parsed_response;
 }
 /**
  * Post to PayPal
  * 
  * Makes an API call using an NVP String and an Endpoint. Based on code available here: https://www.x.com/blogs/Nate/2011/01/07/digital-goods-with-express-checkout-in-php
  * 
  * @param action, string, required. The API operation to be performed, eg. GetExpressCheckoutDetails. The action is abstracted from you (the developer) by the appropriate helper function eg. GetExpressCheckoutDetails via get_checkout_details()
  */
 protected function call_paypal($action, $profile_id = '', $status = '', $args = array())
 {
     // Use the one function for all PayPal API operations
     $api_parameters = $this->get_payment_details_url($action, $profile_id, $status, $args);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, PayPal_Digital_Goods_Configuration::endpoint());
     curl_setopt($ch, CURLOPT_VERBOSE, 1);
     // Turn off server and peer verification
     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);
     // Set the API parameters for this transaction
     curl_setopt($ch, CURLOPT_POSTFIELDS, $api_parameters);
     // Request response from PayPal
     $response = curl_exec($ch);
     // If no response was received from PayPal there is no point parsing the response
     if (!$response) {
         exit($action . ' failed: ' . curl_error($ch) . '(' . curl_errno($ch) . ')');
     }
     curl_close($ch);
     // An associative array is more usable than a parameter string
     parse_str($response, $parsed_response);
     if (0 == sizeof($parsed_response) || !array_key_exists('ACK', $parsed_response)) {
         exit("Invalid HTTP Response for POST request({$api_parameters}) to " . PayPal_Digital_Goods_Configuration::endpoint());
     }
     if ($parsed_response['ACK'] == 'Failure') {
         exit("Calling PayPal with action {$action} has Failed: " . $parsed_response['L_LONGMESSAGE0']);
     }
     return $parsed_response;
 }