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.
- Customizable form to match layout
- Simple 2 (3) step procedure: type in amount, make donation, thank you.
- Ability to display a configurable "thank you" message
- 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? :)