Answers.Body: <p>You want something like this:</p>
  
  <pre><code>craft()-&gt;on('charge.onCharge', function($event)
  {
      $charge = $event-&gt;params['charge'];
  
      $pounds = $charge-&gt;amount;
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-30T17:14:42.737Z
Answers.LastActivityDate: 2016-11-30T17:14:42.737Z
Answers.UserDisplayName: Jamie Pittock
Body: <p>I'm trying to use a custom action with Charge. When a successful charge has been made I want to edit a user field. Charge appears to have Actions but little documentation on how they should be used ( <a href="http://demos.squarebit.co.uk/charge/actions" rel="nofollow noreferrer">http://demos.squarebit.co.uk/charge/actions</a> )</p>
  
  <p>So, I had opened charge/services/Charge_ActionsService.php and created a new one in there, which worked...</p>
  
  <pre><code>private function triggerActionCredits($details, ChargeModel $chargeModel)
  {
      craft()-&gt;charge_log-&gt;action('Credits action triggered successfully');
  
      if (!is_array($details)) {
          craft()-&gt;charge_log-&gt;action("Details passed were: $details");
      } else {
          $detailsAsString = print_r($details);
          craft()-&gt;charge_log-&gt;action("Details passed were: $detailsAsString");
      }
  
      $userID = $chargeModel-&gt;userId;
      $theUser = craft()-&gt;users-&gt;getUserById($userId);
      $currentCredits = $theUser-&gt;creditsAvailable;
  
      craft()-&gt;charge_log-&gt;action("Got the user: $theUser");
      craft()-&gt;charge_log-&gt;action("Current credits: $currentCredits");
      craft()-&gt;charge_log-&gt;action("Details passed were: $detailsAsString");
  
      ... do stuff ...
  
      return true;
  }
  </code></pre>
  
  <p>However, this should really be a plugin - so I've tried to port that code out of the Actions file and into its own plugin as follows:</p>
  
  <pre><code>public function init()
  {
      craft()-&gt;on('charge.onCharge', function($charge){
          craft()-&gt;charge_log-&gt;action('Credits action triggered successfully');
  
          $pounds = $charge-&gt;amount;
          ...
  }
  </code></pre>
  
  <p>The problem is that the line <code>$pounds = $charge-&gt;amount;</code> is throwing an error:</p>
  
  <pre><code>Property "Craft\Event.amount" is not defined
  </code></pre>
  
  <p>But; the event should be getting a Charge model? What am I doing wrong? <a href="http://squarebit.co.uk/software/craft/charge/usage/developer/hooks-and-events" rel="nofollow noreferrer">http://squarebit.co.uk/software/craft/charge/usage/developer/hooks-and-events</a> &amp; <a href="http://squarebit.co.uk/software/craft/charge/usage/models/charge-model" rel="nofollow noreferrer">http://squarebit.co.uk/software/craft/charge/usage/models/charge-model</a></p>
  
Tags: <plugins><plugin-charge>
Title: Custom action with Charge
ThreadText: # Custom action with Charge
  
  (Asked by: username_0 on 2016-11-30)
  
  I'm trying to use a custom action with Charge. When a successful charge has been made I want to edit a user field. Charge appears to have Actions but little documentation on how they should be used ( <http://demos.squarebit.co.uk/charge/actions> )
  
  So, I had opened charge/services/Charge\_ActionsService.php and created a new one in there, which worked...
  
  ```
  private function triggerActionCredits($details, ChargeModel $chargeModel)
  {
      craft()->charge_log->action('Credits action triggered successfully');
  
      if (!is_array($details)) {
          craft()->charge_log->action("Details passed were: $details");
      } else {
          $detailsAsString = print_r($details);
          craft()->charge_log->action("Details passed were: $detailsAsString");
      }
  
      $userID = $chargeModel->userId;
      $theUser = craft()->users->getUserById($userId);
      $currentCredits = $theUser->creditsAvailable;
  
      craft()->charge_log->action("Got the user: $theUser");
      craft()->charge_log->action("Current credits: $currentCredits");
      craft()->charge_log->action("Details passed were: $detailsAsString");
  
      ... do stuff ...
  
      return true;
  }
  
  ```
  
  However, this should really be a plugin - so I've tried to port that code out of the Actions file and into its own plugin as follows:
  
  ```
  public function init()
  {
      craft()->on('charge.onCharge', function($charge){
          craft()->charge_log->action('Credits action triggered successfully');
  
          $pounds = $charge->amount;
          ...
  }
  
  ```
  
  The problem is that the line `$pounds = $charge->amount;` is throwing an error:
  
  ```
  Property "Craft\Event.amount" is not defined
  
  ```
  
  But; the event should be getting a Charge model? What am I doing wrong? <http://squarebit.co.uk/software/craft/charge/usage/developer/hooks-and-events> & <http://squarebit.co.uk/software/craft/charge/usage/models/charge-model>
  
  **Tags:** plugins, plugin-charge
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-11-30. Score: 2 | Accepted answer
  
  You want something like this:
  
  ```
  craft()->on('charge.onCharge', function($event)
  {
      $charge = $event->params['charge'];
  
      $pounds = $charge->amount;
  }
  
  ```
  
  
  ---
  
  