There are times you will want to accept donations or payments on your MODX-powered sites. In my case I wanted to offer the possibility for users to donate towards one of my open source projects. So I whipped up a snippet to help me do that.

My requirements were simple.

  1. Customizable form to match layout
  2. Simple 2 (3) step procedure: type in amount, make donation, thank you.
  3. Ability to display a configurable "thank you" message
  4. Some kind of "posthooks" allowing you to (programmatically) do other stuff based on a donation. For example when people donate for HandyMan, I want to be able of creating a user account for them so they can access the HandyMan member only area (in the somewhat near future, that is). I don't need that for others, so it needs to configurable :)

After spending some time figuring out the PayPal API calls to use for these requirements it was finished pretty quickly, and the open source landing pages on this site now have one of those forms each.

How it's done (slightly technical)

The package (which I called mhPayPal as I needed a name to get started and didn't try to come up with something more fancy, open to suggestions in the comments!) consists of 2 snippets. One snippet, makeDonate, is used to display the form. It basically wraps a chunk which contains a FormIt call and the html for the form.

The second snippet, makeDonateHook, is registered as a posthook on FormIt which is triggered when the form is submitted. It takes some properties (via the FormIt call) and of course the amount and currency. It then uses a slightly modified version of this PHP PayPal class by Drew Johnston (which is the same one I used for SubscribeMe) to send a SetExpressCheckout API call (which prepares the token for the checkout) with the filled in and calculated data.

After redirecting to PayPal, and returning to the same page you were on when filling in the form, the makeDonate snippet will intercept the request and once again use the phpPayPal class to confirm the payment (DoExpressCheckoutPayment API), and to show the success message. Under the hood, there is data stored in the MODX cache to be able of matching data against the right token.

Does it work? Can I use it too?

Yes, and not yet!

You can see it in action on one of my open source pages (be aware that the forms are live though - I appreciate your donation :P). As I'm not entirely done with the package (need to implement requirement 4 still) I haven't yet wrapped it up for the general public, however it will probably pop up on my Github in the near future is available from Github and possibly in the MODX Extras Repository in the near future.

It's insanely easy to use (simply call the [[!makeDonate]] snippet in your template or content and, if you configured your PayPal credentials, you're done) and I think will be an extra value to those people and institutions that rely on donations and need a real easy solution for both visitors and developer.

Let me know in the comments if you have a project coming up this could be useful for. Perhaps you can also come up with a better name for the package while you're at it? :)

Would be nice to see comments about collecting donor data like name, phone, address, email, and sending them to someone via Formit whenever time permits.
Thank you, Tom

Hey Tom,

I've been rewriting it again for more flexibility and for it to be formit-like. It no longer uses FormIt as base, but it will support prehooks, post submit hooks and post payment hooks.

This could for example allow you to preset data (eg from a user profile), update the user profile with the data and to add the user to a custom donors list when the payment was completed.

All I need is some time to finish it and I'll be posting a follow-up to this topic with more information. :)

People may appreciate the ability to have the donor info emailed as well as added to list plus some way to save list as CSV...and/or suggestions/docs how that could be added in...

Have a WordPress site now where forms are a major pain (but NOT permissions/ACLs!! smile!!) and may try converting it to MODx...might lose ability to display site-related tweets though?? Must review Extras to see what's there for FB and Twitter...I don't have full site ownership though, must wait/see.

I think many people will appreciate this addon because simply by changing one line (I think) of PayPal code the button becomes 'Buy Now' rather than 'Donate' -- or something along these lines. The docs appear well-written but they do pre-suppose good programming skills.

Thank you, Tom

Emailing or adding somewhere for CSV would definitely be an option. I'll most likely include an "email" and "redirect" hook for starters (just like FormIt) and see from there. If it's not built into mhPayPal, it would be a matter of using a snippet (though it wont be FormIt compatible, unfortunately).

I'm using getFeed for the tweets on my site, see http://rtfm.modx.com/display/ADDON/getFeed.Adding+a+Twitter+Feed for instructions on that.

Yes, you could definitely make mhPayPal into a buy now instead of donate button too - there'll be an &amount snippet property which will basically force the amount instead of allowing the user to fill something in. No code editing needed, just a snippet property.

Final response: suppose multiple donation/payment amounts are wanted to be chosen from??

Say people want to pay enough $$$ for you to be willing to fly out to their house at a moment's notice, that would be 10000 euros, some only want you to wake up and fix their code no matter what time it is, that would be 9000 euros, some people will be happy with an email within 24 hours, that will be 100 euros.

I don't know yet how your solution can accommodate multiple choices of donation amounts, myself would use radio buttons, never a dropdown, but this is something you can even write a few sentences about to point the way which will satisfy many people.

The goal of this comment is to encourage flexibility and to help make it possible for others to adapt your addon to specific use cases since you yourself may not be able to imagine all the addon's possible usages.

Thank you, Tom

I'm a big fan of flexible snippets :) And yes, once it's ready and published there'll be plenty of docs on how to use it. I can already promise you that you can use a dropdown/radio button for the donation amount. If you want to handle payments/donations differently depending on the amount (ie send a different kind of email, or redirect to a different page), you will probably end up writing a snippet and using that as hook though.

Any estimate/goal/plan/idea/etc. when this will be available??
As soon as it is available I can migrate a site to MODx...also willing to test though not yet fully knowledgeable about PP sandbox etc.
Thank you, Tom

Just spent quite some time writing up docs at http://rtfm.modx.com/display/ADDON/mhPayPal - and while still incomplete that should be enough to get started imo.

Latest package is available here: https://github.com/Mark-H/mhPayPal/tree/master/_packages

It's not yet officially released, and I don't recommend using it on production yet. I will be deploying it on this site soon for donations.. if that all works out, it'll be released.

Benjamin Morrison Benjamin Morrison
May 12, 2012 at 06:59 PM

You mentioned the ability to have a user account created after payment has been confirmed. That would be a HUGE help but I am unsure of how to implement that... especially since formit hooks are not compatible with this snippet. Can I ask, why did you not use formit as a basis for the snippet? I'm just curious.

Because FormIt doesn't do the post-payment bits, and adding that to FormIt would make it more messy. Doing it separately allows a cleaner implementation and makes it a ton easier to maintain and setup as well.

Hey Mark,

great work — any news, about when the package will be available through the ModX Extras.

Naming suggestion: PayPalPayment (simple, good to remember, descriptive)

Cheers from Germany

Hi Mark,
do you have any plans when to finish the package for release on ModX Extras?

I do, but as I'm really busy it may be a while.

Clayton Kinney Clayton Kinney
Nov 20, 2012 at 08:55 PM

I've got an old Evo site this would be useful for, any way to make it work in Evo? I assume this is a Revo package (I confess, I haven't looked at it thoroughly)

Comments are closed :(

While I would prefer to keep comments open indefinitely, the amount of spam that old articles attract is becoming a strain to keep up with and I can't always answer questions about ancient blog postings. If you have valuable feedback or important questions, please feel free to get in touch.