We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43432
    • 19 Posts
    I'm currently working on integrating paypal with my MODx installation, and at this point in the code, I've just received the data from paypal which contains some variables, most notably being "$username" which is the MODx username of the user (which I sent to paypal at the very beginning, and am now receiving it back.)

    Since the file I'm running this through is in a subdirectory of MODx, and stand-alone from MODx, I have to import the MODx system, and the result is as shown below:

    Main Class
    purchase.php
    <?php
    // This initialises all MODx Commands and Permissions! :D
    require_once '../config.core.php';
    require_once MODX_CORE_PATH.'model/modx/modx.class.php';
    $modx = new modX();
    $modx->initialize('mgr');
    $modx->getService('error','error.modError', '', '');
    function userPurchasedItem($item_id, $payment_amount, $payer_email, $username) {
    // Setup the user, using the $username passed to the function
    	$myUser = $modx->getObject('modUser', array('username'=>$username));
    	 if ($myUser instanceof modUser) {
            $modx->user = $myUser;
    		 echo 'user Success!';
        } else {
            die('No User');
        }
    }
    ?>


    I then made a test.php class which included and called on the file & function, and I've run into an error which I'm not quite sure how to fix.

    Fatal error: Call to a member function getObject() on a non-object in path/to/upgrade/purchase.php on line 10

    Can someone please let me know what I am doing wrong?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      It's in a function. You have to declare the $modx variable global.
      function userPurchasedItem($item_id, $payment_amount, $payer_email, $username) {
          global $modx;
      


      BobRay explains here https://forums.modx.com/thread/15948/when-use-global-modx#dis-post-86192 [ed. note: sottwell last edited this post 10 years, 3 months ago.]
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 43432
        • 19 Posts
        Quote from: sottwell at Jun 18, 2014, 04:16 AM
        It's in a function. You have to declare the $modx variable global.
        function userPurchasedItem($item_id, $payment_amount, $payer_email, $username) {
            global $modx;
        


        BobRay explains here https://forums.modx.com/thread/15948/when-use-global-modx#dis-post-86192

        Awesome! Thank you... however, that still leaves one thing to be desired... it's echoing success, yet the user is not being moved to the user group, and I'm not really sure why (no errors or anything.)

        Can someone please expand on this? I also tried it with
        $modx->initialize('web');
        instead of mgr, but that didn't work.

        Update:

        I'm thinking this has something to do with the fact that I haven't actually looked at any way of authenticating/confirming that my script is not some random hackers' script, trying to attack the webserver. Any help

        Update:

        The current code is:
        <?php
        // Copyright Pixel Crescent 2014 All Rights Reserved
        // This initialises all MODx Commands and Permissions! :D
        require_once '../config.core.php';
        require_once MODX_CORE_PATH.'model/modx/modx.class.php';
        $modx = new modX();
        $modx->initialize('web');
        $modx->getService('error','error.modError', '', '');
        function userPurchasedItem($item_id, $payment_amount, $payer_email, $username) {
        	global $modx;
        	$myUser = $modx->getObject('modUser', array('username'=>$username));
        	 if ($myUser instanceof modUser) {
        		$myUser->joinGroup('4');
        		echo 'user Success!';
            } else {
                die('No User');
            }
        }
        ?>

        I noticed the code in my original post did not have the "joinGroup" php line, and I've added that in. (No success however) [ed. note: muffinjello last edited this post 10 years, 3 months ago.]
          • 3749
          • 24,544 Posts
          You have a function that adds a user to a User Group, but you're never calling that function in your code (at least not in the code you've posted).

          This line at the end would add me to group 4 (assuming that I was in your database):

          userPurchasedItem(12, 24.95, [email protected], 'BobRay');


          FYI, you can send the ID of a role as the second argument to joinGroup() if you want to assign the user a role in the group.

            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 43432
            • 19 Posts
            Quote from: BobRay at Jun 18, 2014, 05:17 AM
            You have a function that adds a user to a User Group, but you're never calling that function in your code (at least not in the code you've posted).

            This line at the end would add me to group 4 (assuming that I was in your database):

            userPurchasedItem(12, 24.95, [email protected], 'BobRay');


            FYI, you can send the ID of a role as the second argument to joinGroup() if you want to assign the user a role in the group.


            My apologies, I left out the test.php code that I was navigating to, and calling my function through.
            <?php
            include('userPurchase.php');
            userPurchasedItem('001', '25', '[email protected]', 'muffinjello');
            ?>
              • 3749
              • 24,544 Posts
              Try it without the quotes around the user group ID. I think joinGroup() may assume that it's the *name* of the user group if it gets a string. If that doesn't work, try sending the name of the user group in quotes (make sure the name isn't numeric).

              You may also have to clear the cache before the user shows up in the group.

              You do have a potential security issue if you don't use the authentification system PayPal provides to make sure the request isn't coming from somewhere else, though all a hacker could do is add users to the user group (assuming that they know or can guess the username of a real user). You could also check the email against the user's email address (by getting the User Profile), though you'd have to be sure the user used the same email address at PayPal that they did on your system.

                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 43432
                • 19 Posts
                Special thank yous to sottwell and BobRay, without their assistance, my shiny new MODx setup wouldn't be fully working!!! Thank you so much to these two!!!

                And yeah, BobRay, removing the '' around the user group ID and clearing the cache did the trick. I was just wondering - will my members have to have the cache cleared before they show up as having the required permission on the front end (let's say I made an if statement which checked their usergroup) to view restricted information to that group? If not, it'll be unfortunate that clients won't be able to download their files directly after purchase, but rather wait for me to manually clear the cache - how do I circumvent this problem?

                Thanks again for all of your help!!!
                  • 3749
                  • 24,544 Posts
                  They might, though I think they would have it the next time they log in. You can clear the cache in the code with:

                  $cm = $modx->getCacheManager();
                  $cm->refresh();


                  That clears the entire site cache, though. It's possible to clear just the relevant parts of the cache, but it's fairly complex and difficult to set up.



                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 43432
                    • 19 Posts
                    Quote from: BobRay at Jun 18, 2014, 05:44 PM
                    They might, though I think they would have it the next time they log in. You can clear the cache in the code with:

                    $cm = $modx->getCacheManager();
                    $cm->refresh();


                    That clears the entire site cache, though. It's possible to clear just the relevant parts of the cache, but it's fairly complex and difficult to set up.

                    Everything works! Woohooo!!! I've successfully integrated paypal with my system!!!!!!!!! laugh
                    Yes, I'm just clearing the entire cache at this point.
                      • 3749
                      • 24,544 Posts
                      I'm glad you got it sorted. Thanks for reporting back. smiley
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting