We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37801
    • 11 Posts
    I am using MODX Revolution 2.1.3-pl (traditional) and am rather new to developing within modx. I previously created my own php outside of modx and when I tried to put it into a snippet, none of my functions worked properly. So, because of the tight deadline i am on, and because the functions were rather simple, I just re-coded and avoided the use of functions. NOW, I am trying to implement paypal and have downloaded the api code to use. It is working great outside of modx, but once again, I put it in a snippet and it is not working because it has a php function in it.

    I know it has something to do with how modx accesses variables and it is not able to access my variables inside of the functions. Adding global $modx; inside my function as many searches suggested did nothing. Here is the [shortened] code I need to execute inside of my snippet - any help would be appreciated.

    // Set request-specific fields. WORKS FINE
    $paymentType = urlencode('Authorization');
    $currencyID = urlencode('USD');
    
    // DoDirectPayment NVP  WORKS FINE
    $environment = 'sandbox';	// or 'beta-sandbox' or 'live'
    
    //Send HTTP POST Request - HERE IS THE FUNCTION - I am not able to get these values to pass to paypal
    
    function PPHttpPost($methodName_, $nvpStr_) {
           global $environment;
    
      // Set up your API credentials, PayPal end point, and API version.
    	$API_UserName = urlencode('username');
    	$API_Password = urlencode('password');
    	$API_Signature = urlencode('signature');
    	$API_Endpoint = "paypal endpoint";
    
    // Curl stuff here (not needed for this question)
    
    // Set the API operation, version, and API signature in the request.
            $nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_";
    
    // Get response from the server.
    	$httpResponse = curl_exec($ch);
    
    	if(!$httpResponse) {
    		exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
    	}
    
    	// Extract the response details.
    	$httpResponseAr = explode("&", $httpResponse);
    
    	$httpParsedResponseAr = array();
    	foreach ($httpResponseAr as $i => $value) {
    		$tmpAr = explode("=", $value);
    		if(sizeof($tmpAr) > 1) {
    			$httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
    		}
    	}
    
    	if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {
    		exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");
    	}
    
    	return $httpParsedResponseAr;
    }
    
    // Add request-specific fields to the request string.
    $nvpStr =	"&PAYMENTACTION=$paymentType&AMT=$amount";
    
    // Execute the API operation; see the PPHttpPost function above.
    $httpParsedResponseAr = PPHttpPost('DoDirectPayment', $nvpStr);
    
    if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) {
    print_r($httpParsedResponseAr, true));
    } else  {
    	exit('Payment Processing failed: ' . print_r($httpParsedResponseAr, true));
    }
      • 22303 MODX Staff
      • 10,725 Posts
      Wrap functions with:

      if (!function_exists('function_name')) {
          function function_name() {
              //code of function here
          }
      }

      or, even better, use a method on a class which you can instantiate and reuse across multiple snippets.