We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22574
    • 181 Posts
    Hey all,

    I cant find any info on how to get use the paypal ipn to create a webuser account.

    I have set up a button for a subscription and it returns to a php file the code looks like this (I found it in a tute)
    <?php
    /*
    Simple IPN processing script
    based on code from the "PHP Toolkit" provided by PayPal
    */
    
    $url = 'https://www.paypal.com/cgi-bin/webscr';
    $postdata = '';
    foreach($_POST as $i => $v) {
    	$postdata .= $i.'='.urlencode($v).'&';
    }
    $postdata .= 'cmd=_notify-validate';
    
    $web = parse_url($url);
    if ($web['scheme'] == 'https') { 
    	$web['port'] = 443;  
    	$ssl = 'ssl://'; 
    } else { 
    	$web['port'] = 80;
    	$ssl = ''; 
    }
    $fp = @fsockopen($ssl.$web['host'], $web['port'], $errnum, $errstr, 30);
    
    if (!$fp) { 
    	echo $errnum.': '.$errstr;
    } else {
    	fputs($fp, "POST ".$web['path']." HTTP/1.1\r\n");
    	fputs($fp, "Host: ".$web['host']."\r\n");
    	fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
    	fputs($fp, "Content-length: ".strlen($postdata)."\r\n");
    	fputs($fp, "Connection: close\r\n\r\n");
    	fputs($fp, $postdata . "\r\n\r\n");
    
    	while(!feof($fp)) { 
    		$info[] = @fgets($fp, 1024); 
    	}
    	fclose($fp);
    	$info = implode(',', $info);
    	if (eregi('VERIFIED', $info)) { 
    		// yes valid, f.e. change payment status  
    		
    	}
    	} else {
    		// invalid, log error or something
    	}
    }
    ?>


    I am looking to put some code into the "// yes valid, f.e. change payment status" that pulls in the paypal data like payer_email password and username, as well as set the new user to a specific document group.

    Anyone done this before?

    Thanks!
      • 22574
      • 181 Posts
      So for the connect to modx database part I found this:

      http://www.flashgoddess.com/forum/viewtopic.php?t=1419

      But it wont connect?

      I double checked my logins as well.

      Does a modx database need some other type of connect?
      • If you convert this script to a snippet in a page within MODx, you will have access to the MODx db connection and the MODx object.
          • 22574
          • 181 Posts
          Thanks,

          If I do this will paypal still be able to use the page as a IPN?
          • Quote from: pagalo at Jul 18, 2009, 11:58 PM

            Thanks,

            If I do this will paypal still be able to use the page as a IPN?
            Yes, there’s really no difference between this as a standalone script or as a snippet in MODx, other than you should return the output rather than echo()’ing it.
              • 22574
              • 181 Posts
              Thanks Okay I have got it working except I am not sure on how to set this new user to a web user group?

              Here is my code so far (I havent put in the paypal place holders yet, still testing).
              <?php
              $web_users = $modx->getFullTableName('web_users');
              $web_user_attributes = $modx->getFullTableName('web_user_attributes');
              $web_groups = $modx->getFullTableName('web_groups');
              
              $username = "mat1";
              $password = "guest";
              $cachepwd = time();
              
              $newUser = "INSERT INTO ".$web_users." (`username`, `password`, `cachepwd`) VALUES ('".$username."', '".md5($password)."', '".$cachepwd."')";
              $createNewUser = $modx->db->query($newUser);
              
              $key = $modx->db->getInsertId();
              $NewUser['internalKey'] = $key;
              
              $newUserAttr = "INSERT INTO ".$web_user_attributes.
              " (internalKey, fullname, email, phone, mobilephone, dob, gender, country, state, zip, fax, photo, comment) VALUES".
              " ('".$key."', '".$fullname."', '".$email."', '".$phone."', '".$mobilephone."', '".$dob."', '".$gender."', '".$country."', '".$state."', '".$zip."', '".$fax."', '".$photo."', '".$comment."')";
              $insertUserAttr = $modx->db->query($newUserAttr);
              ?>


              Its basically a cut and past from webloginPE.

              So the question is how do I set this new user to a group called ’web users’?

              Maybe something like this?
              $webGroupId = "web users";
              $modx->db->query("REPLACE INTO ".$web_groups." (`webgroup`, `webuser`) VALUES ('".$webGroupId."', '".$key."')");
                • 22574
                • 181 Posts
                Hey I figured out the above.

                But now when I test it through paypal it doesnt look as if it is going to this page with the ipn code?

                  • 8425
                  • 159 Posts
                  Hi,
                  I have a similar issue. I can use Paypal IPN script if it refers to a php script posted on the web server but when I include the same script in a snippet and I use the notify_url to point to a MODx document including this snippet I get a 503 error and paypal tries to resent it.
                  OpenGeek seems to mention that it should work. Has any tried to invoke a modx document using paypal ipn?
                  Thanks in advance for your help.

                  <input type="hidden" name="notify_url" value="http://test.com/notify_ipn">

                    • 22574
                    • 181 Posts
                    Hey Guys,

                    Almost there.

                    I need my ipn to delete a user.

                    Here is what the webloginPE has to delete accounts:
                    function RemoveProfile($internalKey)
                    {
                    global $modx;
                    $deletedUser = $modx->getWebUserInfo($internalKey);
                    $web_users = $modx->getFullTableName(’web_users’);
                    $web_user_attributes = $modx->getFullTableName(’web_user_attributes’);
                    $web_groups = $modx->getFullTableName(’web_groups’);
                    $active_users = $modx->getFullTableName(’active_users’);

                    $deleteUser = $modx->db->query("DELETE FROM ".$web_users." WHERE `id`=’".$internalKey."’");
                    $deleteAttributes = $modx->db->query("DELETE FROM ".$web_user_attributes." WHERE `internalKey`=’".$internalKey."’");
                    $deleteFromGroups = $modx->db->query("DELETE FROM ".$web_groups." WHERE `webuser`=’".$internalKey."’");
                    $deleteFromActiveUsers = $modx->db->query("DELETE FROM ".$active_users." WHERE `internalKey`=’-".$internalKey."’");

                    if (!$deleteUser || !$deleteAttributes || !$deleteFromGroups || !$deleteFromActiveUsers)
                    {
                    return $this->FormatMessage($this->LanguageArray[13]);
                    }
                    $this->OnWebDeleteUser($internalKey, $deleteUser[’username’]);
                    return;
                    }

                    How could I modify this to delete a user based on the paypal email?

                    Thanks
                      • 22574
                      • 181 Posts
                      So I need to get the ’internalKey’ value from the users table, I figure I need to loop through the $web_user_attributes table then when the paypal email matches a stored email then set $internalKey to that users ’internalKey’.

                      This is what I have so far.
                      It works if I put in the ’internalKey’ manually.


                      <?php
                      //delete user

                      $web_users = $modx->getFullTableName(’web_users’);
                      $web_user_attributes = $modx->getFullTableName(’web_user_attributes’);
                      $web_groups = $modx->getFullTableName(’web_groups’);
                      $active_users = $modx->getFullTableName(’active_users’);

                      $email = "[email protected]"; //this will be a paypal email

                      $checkEmail = $modx->db->query("SELECT * FROM ".$web_user_attributes." WHERE `email`=’".$email."’");
                      $limit = $modx->recordCount($checkEmail);
                      if ($limit > 0)
                      {
                      $internalKey = $checkEmail[’internalKey’];
                      }

                      echo "<script>alert(’$internalKey’);</script>";

                      $deleteUser = $modx->db->query("DELETE FROM ".$web_users." WHERE `id`=’".$internalKey."’");
                      $deleteAttributes = $modx->db->query("DELETE FROM ".$web_user_attributes." WHERE `internalKey`=’".$internalKey."’");
                      $deleteFromGroups = $modx->db->query("DELETE FROM ".$web_groups." WHERE `webuser`=’".$internalKey."’");
                      $deleteFromActiveUsers = $modx->db->query("DELETE FROM ".$active_users." WHERE `internalKey`=’-".$internalKey."’");
                      ?>