/**
  * hash_call: Function to perform the API call to PayPal using API signature
  * @methodName is name of API  method.
  * @nvpStr is nvp string.
  * returns an associtive array containing the response from the server.
*/
function hash_call($methodName, $nvpStr, $sandboxEmailAddress = '')
{
    //declaring of global variables
    $URL = API_ENDPOINT . $methodName;
    //setting the curl parameters.
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    //turning off the server and peer verification(TrustManager Concept).
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    //if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
    //Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php
    if (USE_PROXY) {
        curl_setopt($ch, CURLOPT_PROXY, PROXY_HOST . ":" . PROXY_PORT);
    }
    $headers_array = setupHeaders();
    if (!empty($sandboxEmailAddress)) {
        $headers_array[] = "X-PAYPAL-SANDBOX-EMAIL-ADDRESS: " . $sandboxEmailAddress;
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_array);
    curl_setopt($ch, CURLOPT_HEADER, false);
    //setting the nvpreq as POST FIELD to curl
    curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpStr);
    //getting response from server
    $response = curl_exec($ch);
    //convrting NVPResponse to an Associative Array
    $nvpResArray = deformatNVP($response);
    //$nvpReqArray=deformatNVP($nvpreq);
    //$_SESSION['nvpReqArray']=$nvpReqArray;
    if (curl_errno($ch)) {
        // moving to display page to display curl errors
        $_SESSION['curl_error_no'] = curl_errno($ch);
        $_SESSION['curl_error_msg'] = curl_error($ch);
        $location = "APIError.php";
        header("Location: {$location}");
    } else {
        //closing the curl
        curl_close($ch);
    }
    return $nvpResArray;
}
Ejemplo n.º 2
0
/**
  * call: Function to perform the API call to PayPal using API signature
  * @methodName is name of API  method.
  * @a is  String
  * $serviceName is String
  * returns an associtive array containing the response from the server.
*/
function call($MsgStr, $endpoint, $sandboxEmailAddress = '')
{
    //setting the curl parameters.
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $endpoint);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    //turning off the server and peer verification(TrustManager Concept)
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, !TRUST_ALL_CONNECTION);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, !TRUST_ALL_CONNECTION);
    if (!TRUST_ALL_CONNECTION) {
        curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/Certs/api_cert_chain.crt');
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    $headers_array = setupHeaders(API_AUTHENTICATION_MODE);
    if (!empty($sandboxEmailAddress)) {
        $headers_array[] = "X-PAYPAL-SANDBOX-EMAIL-ADDRESS: " . $sandboxEmailAddress;
    }
    if (API_AUTHENTICATION_MODE == 'ssl') {
        curl_setopt($ch, CURLOPT_SSLCERT, realpath(SSL_CERTIFICATE_PATH));
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_array);
    curl_setopt($ch, CURLOPT_HEADER, false);
    //if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
    //Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php
    if (USE_PROXY) {
        curl_setopt($ch, CURLOPT_PROXY, PROXY_HOST . ":" . PROXY_PORT);
    }
    //setting the MsgStr as POST FIELD to curl
    $conf = array('mode' => 0600, 'timeFormat' => '%X %x');
    $logger =& Log::singleton('file', 'logs/' . date('Ymd') . '_invest.log', 'caller', $conf);
    $logger->log('##### PAYPAL call ' . date('d/m/Y') . ' User:'******'user']->id . '#####');
    if (TRUST_ALL_CONNECTION == true) {
        $log_data = 'TRUST_ALL_CONNECTION is set to true, Server and peer certificate verification are turned off';
        $logger->warning($log_data);
    }
    $logger->log("request: {$MsgStr}");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $MsgStr);
    if (isset($_SESSION['curl_error_no'])) {
        unset($_SESSION['curl_error_no']);
    }
    if (isset($_SESSION['curl_error_msg'])) {
        unset($_SESSION['curl_error_msg']);
    }
    //getting response from server
    $response = curl_exec($ch);
    $logger->log("response: {$response}");
    $logger->log('##### END PAYPAL call ' . date('d/m/Y') . ' User:'******'user']->id . '#####');
    $logger->close();
    if (curl_errno($ch)) {
        // moving to display page to display curl errors
        die('curl_error: ' . curl_errno($ch) . '<br />' . curl_error($ch));
    } else {
        //closing the curl
        curl_close($ch);
    }
    return $response;
}
/**
  * call: Function to perform the API call to PayPal using API signature
  * @methodName is name of API  method.
  * @a is  String
  * $serviceName is String
  * returns an associtive array containing the response from the server.
*/
function call($MsgStr, $endpoint, $sandboxEmailAddress = '')
{
    //setting the curl parameters.
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $endpoint);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    //turning off the server and peer verification(TrustManager Concept)
    if (strtoupper(TRUST_ALL_CONNECTION) == 'FALSE' | TRUST_ALL_CONNECTION == 0) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    } else {
        if (strtoupper(TRUST_ALL_CONNECTION) == 'TRUE' | TRUST_ALL_CONNECTION == 1) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        }
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    $headers_array = setupHeaders(API_AUTHENTICATION_MODE);
    if (!empty($sandboxEmailAddress)) {
        $headers_array[] = "X-PAYPAL-SANDBOX-EMAIL-ADDRESS: " . $sandboxEmailAddress;
    }
    if (API_AUTHENTICATION_MODE == 'ssl') {
        curl_setopt($ch, CURLOPT_SSLCERT, realpath(SSL_CERTIFICATE_PATH));
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_array);
    curl_setopt($ch, CURLOPT_HEADER, false);
    //if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
    //Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php
    if (USE_PROXY) {
        curl_setopt($ch, CURLOPT_PROXY, PROXY_HOST . ":" . PROXY_PORT);
    }
    //setting the MsgStr as POST FIELD to curl
    $conf = array('mode' => 0600, 'timeFormat' => '%X %x');
    $logger =& Log::singleton('file', LOGFILENAME, 'caller', $conf);
    if (X_PAYPAL_REQUEST_DATA_FORMAT == 'JSON') {
        $log_data = '#####JSON#####';
        $logger->log($log_data);
    }
    if (X_PAYPAL_REQUEST_DATA_FORMAT == 'SOAP11') {
        $log_data = '#####SOAP#####';
        $logger->log($log_data);
    }
    if (X_PAYPAL_REQUEST_DATA_FORMAT == 'XML') {
        $log_data = '#####XML#####';
        $logger->log($log_data);
    }
    if (TRUST_ALL_CONNECTION == true) {
        $log_data = 'TRUST_ALL_CONNECTION is set to true, Server and peer certificate verification are turned off';
        $logger->warning($log_data);
    }
    $logger->log("request: {$MsgStr}");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $MsgStr);
    if (isset($_SESSION['curl_error_no'])) {
        unset($_SESSION['curl_error_no']);
    }
    if (isset($_SESSION['curl_error_msg'])) {
        unset($_SESSION['curl_error_msg']);
    }
    //getting response from server
    $response = curl_exec($ch);
    if (curl_errno($ch) == 60) {
        $logger->log("Invalid or no certificate authority found,using bundled information");
        curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
        $response = curl_exec($ch);
    }
    $logger->log("response: {$response}");
    $logger->close();
    if (curl_errno($ch)) {
        // moving to display page to display curl errors
        $_SESSION['curl_error_no'] = curl_errno($ch);
        $_SESSION['curl_error_msg'] = curl_error($ch);
        $location = "APIError.php";
        header("Location: {$location}");
    } else {
        //closing the curl
        curl_close($ch);
    }
    return $response;
}