We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20135
    • 188 Posts
    I'm trying to set up a feature whereby a user can effectively "purchase" access to resources secured by certain usergroups. To do this, I imagined that it would be possible to utilise the Register snippet combined with mhPayPal snippet - ideally, the user would complete a form requesting all the information required to add the user (including selecting the set of resources they wanted to access, which contained both pricing and usergroup info), but before the user was added, PayPal would confirm payment and allow Register to add the user.

    mhPayPal is working - I'm receiving the token and PayerID back from PayPal. I included Register as a postPaymentHook, with the basic properties it required to function. However, when mhPayPal runs the hook, the Register snippet is returning false.

    I've also tried flipping the process - using mhPayPal as a preHook for Register, but again to no avail.

    I'm looking for some ideas here - how can I go about creating a PayPal-based payment process that will then allow a user to be added to requested usergroups? And if these 2 snippets should do it, how can I get them to integrate properly?
      • 4172
      • 5,888 Posts
      Can't you let them register in a first step as usual.
      Then, when they are logged-in, let them pay and just use

      in the postPaymentHook


        you can buy me a beer, if you like MIGX


        • 20135
        • 188 Posts
        Hmm, that looks easy, but I need to make sure that the process will work in terms of ease of use for the end user. I'll spend some more time on it next week once I've discussed this with the client further. Thanks for your prompt response Bruno smiley
          • 20135
          • 188 Posts
          Hoping someone can help me please.

          I've finally sorted out the process for registering a new member to the site, and then sending them to the payment page where they make payment to Paypal. The payment goes through easily enough, and so I tried adding Bruno17's suggestion of a postPaymentHook to add the new user to the "premium" usergroup. But I realised that there's no reference to who the user actually is at this point.

          The Register snippet sends the username as a URL parameter, which I have been able to grab and add as a hidden field in the mhPayPal form. This will then use that username as a placeholder in the successTpl chunk without issue. But that doesn't help me when I'm trying to grab the username from mhPP for my postPaymentHook.

          Does mhPP allow the postPaymentHook access to its properties? If so, how would I be able to get the username I've embedded in there from my postPaymentHook?
            • 20135
            • 188 Posts
            Bump... Can anyone please give me an answer for this?
              • 46886
              • 1,154 Posts
              I find your question a little unclear. You say you have gotten the user name as a hidden field. Bruno said put in a posthook to send the user into the new group.

              But now you seem to say you don't have the username...but earlier you said you did get it. So what's the story here? If you have the confirmed username then completing the shift to the new group isn't hard, whether with a posthook or another way.

              I would also suggest at least considering how you could break up this process somewhat, it might be a better way. If you have the username then onsuccess send that username to some file and then use a simple tool to upgrade all users in the file.

              Anyway could you explain your problem again. I guess it is, I have the username but can't get it to the posthook in time. So I suggest simply sending the confirmed username to another place where it can be upgraded sort of independently of this tool you are dealing with now.

              Someone probably has a better solution than me though ;-)
                • 20135
                • 188 Posts
                Thanks Nuan88, I'll see if I can restate this, maybe by using steps:

                1. User completes form generated by Register snippet, where they input their chosen username, amongst other things. They are automatically added as a MODx user, assigned to a basic usergroup, but not to the premium usergroup that they are about to pay for access.
                2. On submission of the registration form, the user is redirected to another page containing the mhPayPal snippet, not as a Register snippet postHook, but using the &submittedResourceId parameter to completely add the user to MODx, assigning them to the basic usergroup.
                3. At this point, the only way to connect the user who has just completed the registration form with the mhPayPal snippet in any way, is to get the username which the Register snippet has added to the URL, and implement it in the mhPayPal snippet, otherwise the payment would not achieve its purpose of adding the user to the premium usergroup. This is done by adding a hidden input to the mhPayPal form named "username" and the value taken from the URL parameters.
                4. With the username added as a hidden field, it can be retrieved on completion of payment - the &successTpl is able to display the username. So at some point, the values submitted with the form are saved and accessible following completion of payment. It's therefore assumable that those values are available to any &postPaymentHook snippets. Keeping in mind that, though a user has registered, the user is not yet logged in, and so using the $modx->user->joinGroup() function is not possible unless the user has been defined. To define the user, I need the username that was passed in the form; but I don't know how to retrieve the username from the mhPayPal snippet to use it with the &postPaymentHook.
                Does that make better sense? All I need is some instruction on how to access the mhPayPay form values from the postPaymentHook snippet.
                  • 51042
                  • 1 Posts
                  thanks for post.
                    • 46886
                    • 1,154 Posts
                    Ok cool it is clearer now.

                    I agree with you that the username data probably should be available when the tool fires, but you might have to parse the form to get it and then define it as the current user, then upgrade.

                    I agree with you that, with the user not logged in then Bruno's code can't work. We don't need the user to be logged in to upgrade them but that code assumes the current user is defined.

                    My lower-tech suggestion is pass that username to a different tool to complete the upgrade. You got the confirmed username and so upgrading should be a breeze.

                    Let's see if someone comes around with a more elegant way of handling this.
                    • Keep in mind you're using an unreleased and undocumented extra there - it is 100% at your own risk.

                      It's been ages since I've so much as looked at that, but try checking $mhpp->values. As you've found out it stores data in the cache temporarily and I think that's where it should be made available again.
                        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.