We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26958
    • 9 Posts
    He have a client that we recently converted to modX and everything seems to be working great w/ one exception... the Pay Pal Pro integration. We copied over all the files and seem to have narrowed the problem down to the curl. But when I put the revised the call to this ( $response = curl_exec($ch) OR die (’Curl did not work!’); ) in my CallService.php file, I get the die message.

    Is there some kind of module or something that I need to install to enable the curl from our site through modX?
      • 26903
      • 1,336 Posts
      Well, from the PHP docs :-
      In order to use PHP’s CURL functions you need to install the libcurl package. PHP requires that you use libcurl 7.0.2-beta or higher. In PHP 4.2.3, you will need libcurl version 7.9.0 or higher. From PHP 4.3.0, you will need a libcurl version that’s 7.9.8 or higher. PHP 5.0.0 requires a libcurl version 7.10.5 or greater.

      Assuming you have this, check your error log for any other errors, also ensure the $ch is a curl resource. Again, from the PHP docs :-
      This function should be called after you initialize a CURL session and all the options for the session are set. Its purpose is simply to execute the predefined CURL session (given by the ch).

      MODx hasn’t anything to do with this.
        Use MODx, or the cat gets it!
        • 26958
        • 9 Posts
        Sorry... I should have explained that part too. This was working just fine before we converted the content on the site to modX and I if I change my form to pull from the PayPal files we still have out there on the server in an archive /v1/ folder the curl works there too. But I copied over the entire /paypal/ directory and all it’s contents before we published the new site w/ modX and I just continue to get that error. It appears curl is still working from the server, but not when it’s called from the index.php file on our modX site.
          • 10449
          • 956 Posts
          It appears curl is still working from the server, but not when it’s called from the index.php file on our modX site.

          That sounds a bit strange. Do you mean you hacked the core index.php file to include your curl/paypal code? Or did you write a snippet? You should post more concrete details. curl - like any other php function - works fine inside modx if it’s enabled in the server-setting.
            • 26958
            • 9 Posts
            Sorry... hopefully I will give enough details here to present my problem.

            We have a code snippet that is running when we execute the form which looks like this:

            ...
            	if (empty($_POST['approve_waiver'])) {
            		$save_errors++;
            		$_SESSION['err_msg'][0] = 'Sorry - You must approve the waiver before we can accept your registration.';
            		$_SESSION['show_confirmation'] = TRUE;
            	} else {
            		
            		// trying to charge the credit card through PayPal Pro
            		require_once $_SERVER['DOCUMENT_ROOT'] . '/paypal/process_direct_payment.php';
            		if($ack!='SUCCESS') {
            			$err_msg[0] = 'Sorry - there was a problem w/ your credit card. Please try again.';
            		} else {
            			$_SESSION['show_thank_you'] = TRUE;
            ...
            


            I’m using all the same source files that we used before (which worked) and I tried echoing several things in the files that are called from the /paypal/ directory on the server before I narrowed it down to the curl() failure. That call which I referenced in my earlier post is on line 66 of the paypal/CallerService.php files, which is required by the above snippet of code.

            Does that help? There are several files in the /paypal/ directory but I’m only using a few of them for this particular site.
            • I created a single snippet a while ago well copied out the paypal example and modified it slightly.

              Its a single snippet and the page you use just call it [[snippetname]]

              Paypal also posts back to that same page name.

              I never did get round to spliting it so that it is easily editable for different products but our requirements at the time was for just a single product.

              Hope it helps.

              Aaron
                http://www.onesmarthost.co.uk
                UK MODX Hosting with love.
                • 26958
                • 9 Posts
                Thank you for your code snippet Aaron, but I’d really like to try and get what I had working rather than starting over again. I figured maybe I just couldn’t execute the curl from an file that I’m requiring like that, so I took all three files I’m using for the PayPal processing and combined them into one. Then, I put it into a code snippet which I call from the site and I’m still hitting the ’OR die’ on the curl_exec you’ll see below. Here is my entire code snippet:

                <?php
                #########################################
                ##  BEGIN CONTENTS FROM constants.php  ##
                #########################################
                /****************************************************
                constants.php
                
                This is the configuration file for the samples.This file
                defines the parameters needed to make an API call.
                
                PayPal includes the following API Signature for making API
                calls to the PayPal sandbox:
                
                API Username 	sdk-three_api1.sdk.com
                API Password 	QFZCWN5HZM8VBG7Q
                API Signature 	A-IzJhZZjhg29XQ2qnhapuwxIDzyAZQ92FRP5dqBzVesOkzbdUONzmOU
                
                Called by CallerService.php.
                ****************************************************/
                
                /**
                # API user: The user that is identified as making the call. you can
                # also use your own API username that you created on PayPalís sandbox
                # or the PayPal live site
                */
                
                define('API_USERNAME', '* PRIVATE *');
                
                /**
                # API_password: The password associated with the API user
                # If you are using your own API username, enter the API password that
                # was generated by PayPal below
                # IMPORTANT - HAVING YOUR API PASSWORD INCLUDED IN THE MANNER IS NOT
                # SECURE, AND ITS ONLY BEING SHOWN THIS WAY FOR TESTING PURPOSES
                */
                
                define('API_PASSWORD', '* PRIVATE *');
                
                /**
                # API_Signature:The Signature associated with the API user. which is generated by paypal.
                */
                
                define('API_SIGNATURE', '* PRIVATE *');
                
                /**
                # Endpoint: this is the server URL which you have to connect for submitting your API request.
                */
                
                define('API_ENDPOINT', 'https://api-3t.paypal.com/nvp');
                /**
                USE_PROXY: Set this variable to TRUE to route all the API requests through proxy.
                like define('USE_PROXY',TRUE);
                */
                define('USE_PROXY',FALSE);
                /**
                PROXY_HOST: Set the host name or the IP address of proxy server.
                PROXY_PORT: Set proxy port.
                
                PROXY_HOST and PROXY_PORT will be read only if USE_PROXY is set to TRUE
                */
                define('PROXY_HOST', '127.0.0.1');
                define('PROXY_PORT', '808');
                
                /* Define the PayPal URL. This is the URL that the buyer is
                   first sent to to authorize payment with their paypal account
                   change the URL depending if you are testing on the sandbox
                   or going to the live PayPal site
                   For the sandbox, the URL is
                   https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=
                   For the live site, the URL is
                   https://www.paypal.com/webscr&cmd=_express-checkout&token=
                   */
                define('PAYPAL_URL', 'https://www.paypal.com/webscr&cmd=_express-checkout&token=');
                
                /**
                # Version: this is the API version in the request.
                # It is a mandatory parameter for each API request.
                # The only supported value at this time is 2.3
                */
                
                define('VERSION', '53.0');
                #######################################
                ##  END CONTENTS FROM constants.php  ##
                #######################################
                
                
                #############################################
                ##  BEGIN CONTENTS FROM CallerService.php  ##
                #############################################
                /****************************************************
                CallerService.php
                
                This file uses the constants.php to get parameters needed 
                to make an API call and calls the server.if you want use your
                own credentials, you have to change the constants.php
                
                Called by TransactionDetails.php, ReviewOrder.php, 
                DoDirectPaymentReceipt.php and DoExpressCheckoutPayment.php.
                
                ****************************************************/
                
                $API_UserName=API_USERNAME;
                
                $API_Password=API_PASSWORD;
                
                
                $API_Signature=API_SIGNATURE;
                
                
                $API_Endpoint =API_ENDPOINT;
                
                
                $version=VERSION;
                
                //session_start();
                
                /**
                  * 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)
                {
                	//declaring of global variables
                	global $API_Endpoint,$version,$API_UserName,$API_Password,$API_Signature,$nvp_Header;
                
                	//setting the curl parameters.
                	$ch = curl_init();
                	curl_setopt($ch, CURLOPT_URL,$API_Endpoint);
                	curl_setopt($ch, CURLOPT_VERBOSE, 1);
                
                	//turning off the server and peer verification(TrustManager Concept).
                	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);
                    //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); 
                
                	//NVPRequest for submitting to server
                	$nvpreq="METHOD=".urlencode($methodName)."&VERSION=".urlencode($version)."&PWD=".urlencode($API_Password)."&USER=".urlencode($API_UserName)."&SIGNATURE=".urlencode($API_Signature).$nvpStr;
                
                	//setting the nvpreq as POST FIELD to curl
                	curl_setopt($ch,CURLOPT_POSTFIELDS,$nvpreq);
                
                	//getting response from server
                	$response = curl_exec($ch) OR die ('Curl did not work... even from the snippet!');
                
                	//converting 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 = "/paypal/APIError.php";
                		  header("Location: $location");
                	 } else {
                		 //closing the curl
                			curl_close($ch);
                	  }
                
                return $nvpResArray;
                }
                
                /** This function will take NVPString and convert it to an Associative Array and it will decode the response.
                  * It is usefull to search for a particular key and displaying arrays.
                  * @nvpstr is NVPString.
                  * @nvpArray is Associative Array.
                  */
                
                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);
                
                		/*getting the Key and Value values and storing in a Associative Array*/
                		$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;
                }
                ###########################################
                ##  END CONTENTS FROM CallerService.php  ##
                ###########################################
                
                /**
                 * Get required parameters from the web form for the request
                 */
                $paymentType = 'Sale';
                $firstName =urlencode( $_SESSION['reg_first_name']);
                $lastName =urlencode( $_SESSION['reg_last_name']);
                $creditCardType =urlencode(decrypt($_SESSION['reg_cc_type'], $_SESSION['encryption_key']));
                $creditCardNumber = urlencode(decrypt($_SESSION['reg_cc_number'], $_SESSION['encryption_key']));
                $expDateMonth =urlencode(decrypt($_SESSION['reg_cc_exp_m'], $_SESSION['encryption_key']));
                
                // Month must be padded with leading zero
                $padDateMonth = str_pad($expDateMonth, 2, '0', STR_PAD_LEFT);
                
                $expDateYear =urlencode(decrypt($_SESSION['reg_cc_exp_y'], $_SESSION['encryption_key']));
                $cvv2Number = urlencode(decrypt($_SESSION['reg_cc_cvv2'], $_SESSION['encryption_key']));
                $address1 = urlencode($_SESSION['reg_address']);
                $address2 = urlencode($_SESSION['reg_address_2']);
                $city = urlencode($_SESSION['reg_city']);
                $state =urlencode( $_SESSION['reg_state']);
                $zip = urlencode($_SESSION['reg_zip']);
                $email = urlencode($_SESSION['reg_email']);
                $phone = urlencode($_SESSION['reg_phone']);
                $amount = urlencode($_SESSION['reg_total_price']);
                //$currencyCode=urlencode($_SESSION['currency']);
                $currencyCode="USD";
                
                /* Construct the request string that will be sent to PayPal.
                   The variable $nvpstr contains all the variables and is a
                   name value pair string with & as a delimiter */
                $nvpstr="&PAYMENTACTION=$paymentType&AMT=$amount&CREDITCARDTYPE=$creditCardType&ACCT=$creditCardNumber&EXPDATE=".         $padDateMonth.$expDateYear."&CVV2=$cvv2Number&FIRSTNAME=$firstName&LASTNAME=$lastName&STREET=$address1&STREET2=$address2&CITY=$city&STATE=$state".
                "&ZIP=$zip&COUNTRYCODE=US&CURRENCYCODE=$currencyCode&EMAIL=$email&PHONENUM=$phone";
                
                /* Make the API call to PayPal, using API signature.
                   The API response is stored in an associative array called $resArray */
                $resArray=hash_call("doDirectPayment",$nvpstr);
                
                /* Display the API response back to the browser.
                   If the response from PayPal was a success, display the response parameters'
                   If the response was an error, display the errors received using APIError.php.
                   */
                $ack = strtoupper($resArray["ACK"]);
                ?>
                


                Any ideas what I’m doing wrong??
                  • 26903
                  • 1,336 Posts
                  It may be a scoping problem, replace this line :-
                  //declaring of global variables
                  global $API_Endpoint,$version,$API_UserName,$API_Password,$API_Signature,$nvp_Header;
                  


                  with the actual variables defined earlier, ie move the variable defs into the scope of the hash_call function rather than referencing them globally, or pass them explicitly and try that.
                    Use MODx, or the cat gets it!
                    • 10449
                    • 956 Posts
                    Also, try to see / display the actual curl errors, i.e. rewrite this block:

                    
                    //getting response from server
                    	$response = curl_exec($ch) OR die ('Curl did not work... even from the snippet!'); // this doesn't tell us WHY it didn't work
                    
                    ...
                    
                    	if (curl_errno($ch)) { // this might show you hints about what exactly went wrong... empty variables, wrong urls, paths...
                    		// moving to display page to display curl errors
                    		  $_SESSION['curl_error_no']=curl_errno($ch) ;
                    		  $_SESSION['curl_error_msg']=curl_error($ch);
                    		  $location = "/paypal/APIError.php";
                    		  header("Location: $location");
                    	 } else {
                    
                    


                    maybe something like


                    
                    //getting response from server
                    	$response = curl_exec($ch);
                    
                    	if (curl_errno($ch)) { // this might show you hints about what exactly went wrong... empty variables, wrong urls, paths...
                    		die( "curl error number = " . curl_errno($ch) ); // see error codes here: http://curl.haxx.se/libcurl/c/libcurl-errors.html
                    	 }