3

A project I'm working on requires that publicly registered users pay a fee upon registration, and when payment is successful an Entry belonging to them should automatically be created.

I have component pieces of this working but am struggling with how to tie them together.

I currently have a basic plugin I wrote which handles the creation of an Entry when a user is Activated. The plugin has no money-taking facility. It works fine; the public user registration form posts to the built-in 'users/saveUser' action, and the plugin listens for the users.activateUser event which is fired when an admin manually activates their account.

We want to take that 'manual' step out by having the system automatically activate the user if a Charge has been successful. This means integrating Charge into the public registration form. Which would seem to involve me having to change the action for the form to 'charge/charge', so Charge can do its thing.

How then do I also get it to create the user?

I see that Charge has an event for successful payment, which I'd like to hook onto and trigger the 'user/saveUser' like what's already working - but how, and how do I pass the username/email fields through from the form to 'user/saveUser' via Charge?

3

Developer of Charge here.

Setup for installs up to (and including) 2.0.x :

The simplest setup would be to use the onBeforeCharge event.

This fires at the very start of the charge request, after validation but before anything has actually happened.

You'd use that to jump in, and create your user, entry, or anything else you might want to do. As part of that same event you can modify the actual ChargeModel which will be used in the payment request too.

The onBeforeCharge event fires before we pull out the user for the request, so as long as you're creating the user at that point, you won't need to do anything else to have all the subsequent steps properly associated to that user either.

Of course be aware that a payment can fail for any number of reasons, so check in that onBeforeCharge event if you actually do need to create the user before blindly going ahead.

Additionally, you can use the onCharge event to later perform any clean up you might need once the payment has actually succeeded.

Setup for installs from 2.1+

2.1 (not yet released as of 25/04/16) is bringing inline user registration to the payment forms, so in that case you'd just pass a new flag on the request to registerUser = true, and let Charge do the rest.

Charge will only attempt to register a new user if required, and will use the customerEmail as the default base, but that can be overridden with other parameters and inputs on the form.

| improve this answer | |
  • Thanks Joel, will be working my way through this ASAP, Charge 2.1 looks perfect for this use case - wish we could wait! – Matt Wilcox Apr 26 '16 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.