Answers.Body: <p>In your current example, you are not actually saving an entry. You are just setting up an <code>onSaveEntry</code> event listener. To save the entry you will need to actually save the entry using the [saveEntry][1] method.</p>
  
  <p>UPDATE: I'm not exactly sure what your setup is — but some variation on the following should work for you.</p>
  
  <ol>
  <li><p>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).</p></li>
  <li><p>Add the 'invoiceId' field to the charge field layout in charge->settings->fields.</p></li>
  <li><p>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):</p>
  
  <pre><code>&lt;input type="hidden" name="fields[invoiceId]" value="{{ entry.id }}"/&gt;
  </code></pre></li>
  <li><p>In your plugin, add the onCharge event to grab the 'invoiceId' from the charge event, then retrieve, update, and save the entry.</p>
  
  <pre><code>craft()-&gt;on('charge.onCharge', function(Event $event) {
  
      // get the charge model
      $charge = $event-&gt;params['charge'];
  
      // retrieve the invoiceId (aka entryId) saved on the charge model
      $entryId = $charge-&gt;invoiceId;
  
      // uncomment to test, if needed
      //LogicPlugin::log('Invoice ID: '.$charge-&gt;invoiceId, LogLevel::Info);
  
      // get the invoice entry from the invoiceId
      $entry = craft()-&gt;entries-&gt;getEntryById($entryId);
  
      // update the invoicePaid field (in this case I'm using a switch)
      $entry-&gt;getContent()-&gt;invoicePaid = '1';
  
      // save the entry
      $success = craft()-&gt;entries-&gt;saveEntry($entry);
  
      // log errors
      if (!$success) {
          LogicPlugin::log('Couldn’t save the entry "'.$entry-&gt;title.'"', LogLevel::Error);
      }
  
  });
  </code></pre></li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-12T05:38:35.620Z
Answers.Text: Unfortunately it comes back with  _Undefined index: entry_  error message. Been trying to get this to show anything, but so far no luck
Answers.UserDisplayName: herbo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-13T17:45:53.380Z
Answers.Text: 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.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-13T17:55:36.883Z
Answers.Text: 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.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-15T12:53:27.613Z
Answers.Text: 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);`
Answers.UserDisplayName: herbo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-15T20:40:30.797Z
Answers.Text: 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.)
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-20T03:31:05.667Z
Answers.Text: Thank you, this worked. It took a while to see it work as I was having a plugin problem that was affecting it, but it works now.
Answers.UserDisplayName: herbo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-10T18:00:26.303Z
Answers.LastActivityDate: 2018-02-16T21:09:37.620Z
Answers.LastEditDate: 2018-02-16T21:09:37.620Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>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.</p>
  
  <pre><code> {
      craft()-&gt;on('charge.onCharge', function(Event $event) {
          craft()-&gt;on('entries.onSaveEntry', function(Event $event ){
  
              //Is the entry in the section 'invoices'?
              if ($event-&gt;params['entry']-&gt;section == 'invoices') {
  
              //Replace values
              $event-&gt;params['entry']-&gt;getContent()-&gt;invoicePaid = 'Yes';
              }
          });
  
  
      });
  }
  </code></pre>
  
  <p>Where am I going wrong, or is there a better way to go about this?</p>
  
Tags: <plugins><plugin-charge><hook>
Title: How do I edit a dropdown field in the charge.onCharge hook?
ThreadText: # How do I edit a dropdown field in the charge.onCharge hook?
  
  (Asked by: username_0 on 2018-02-10)
  
  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?
  
  **Tags:** plugins, plugin-charge, hook
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-02-10. Score: 2 | Accepted answer
  
  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);
         }
  
     });
  
     ```
  
  > **Comments:**
  > **username_0**: Unfortunately it comes back with  _Undefined index: entry_  error message. Been trying to get this to show anything, but so far no luck  
  > **username_1**: 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.  
  > **username_1**: 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.  
  > **username_0**: 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);`  
  > **username_1**: 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.)  
  > **username_0**: Thank you, this worked. It took a while to see it work as I was having a plugin problem that was affecting it, but it works now.  
  
  ---
  
  