I have an invoice that is sent to stripe to be paid. When the thanks.html is shown, I would like the invoices section's invoicePaid dropdown field to change from "No" to "Yes" using the charge.onCharge hook. I tried the following( and variations ) without success.

    craft()->on('charge.onCharge', function(Event $event) {
        craft()->on('entries.onSaveEntry', function(Event $event ){

            //Is the entry in the section 'invoices'?
            if ($event->params['entry']->section == 'invoices') {

            //Replace values
            $event->params['entry']->getContent()->invoicePaid = 'Yes';


Where am I going wrong, or is there a better way to go about this?


In your current example, you are not actually saving an entry. You are just setting up an onSaveEntry event listener. To save the entry you will need to actually save the entry using the [saveEntry][1] method.

UPDATE: I'm not exactly sure what your setup is — but some variation on the following should work for you.

  1. In the CMS, add a field called 'invoiceID', which you will use to store the invoice entryId to the charge (or 'invoiceEntryId' or whatever you want to call it).

  2. Add the 'invoiceId' field to the charge field layout in charge->settings->fields.

  3. In the charge form, add the 'invoiceId' field to your form (this assumes that you are making the charge from the invoice entry page, otherwise you will need to use a reference to the invoice entry id through whatever relationship you have set up):

    <input type="hidden" name="fields[invoiceId]" value="{{ entry.id }}"/>
  4. In your plugin, add the onCharge event to grab the 'invoiceId' from the charge event, then retrieve, update, and save the entry.

    craft()->on('charge.onCharge', function(Event $event) {
        // get the charge model
        $charge = $event->params['charge'];
        // retrieve the invoiceId (aka entryId) saved on the charge model
        $entryId = $charge->invoiceId;
        // uncomment to test, if needed
        //LogicPlugin::log('Invoice ID: '.$charge->invoiceId, LogLevel::Info);
        // get the invoice entry from the invoiceId
        $entry = craft()->entries->getEntryById($entryId);
        // update the invoicePaid field (in this case I'm using a switch)
        $entry->getContent()->invoicePaid = '1';
        // save the entry
        $success = craft()->entries->saveEntry($entry);
        // log errors
        if (!$success) {
            LogicPlugin::log('Couldn’t save the entry "'.$entry->title.'"', LogLevel::Error);
  • Unfortunately it comes back with Undefined index: entry error message. Been trying to get this to show anything, but so far no luck
    – herbo
    Feb 12 '18 at 5:38
  • I don't have charge installed, so can't investigate directly, but from the docs it looks like charge provides it's own element type (i.e. charge) and models (i.e. charge, payments, customer, subscription, account). You are probably therefore not working with an 'entry' model. You may need to dig into the charge plugin and see what you are working with, and what methods it uses for saving elements (i.e. craft()->charge()->saveCharge() or similar) — or try just using the 'element' methods, which should apply to all elements in craft. Feb 13 '18 at 17:45
  • More specifically, it looks like the onCharge event returns an attribute called charge which contains a reference to the charge model (i.e. $charge = $event->params['charge'];. How you modify and save that is another question, but you might try craft()->charge_charge->saveCharge($charge) which I found by searching the plugin source files. Feb 13 '18 at 17:55
  • Using the $charge = $event->params['charge']; I'm trying to get the entry Id, which is posted as meta[id]. I can get it using {{ dump(charge['meta']['id']) }}, but unable to call it in the plugin, so it could be used $id = charge()->meta()->id(); $entry = craft()->entries()->getEntryById($id);
    – herbo
    Feb 15 '18 at 12:53
  • I installed the plugin on a test install, and was able to get a solution set up. I updated the answer. See if that works. (FYI... I'm using my LogicPlugin for logging. Change to Craft::Log if you want it to go the craft log.) Feb 15 '18 at 20:40

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.