Answers.Body: <p>So, in order to achieve this I had to make a couple of small hacks to the module core, namely in <code>Charge_ActionsService.php</code></p>
  
  <p>First, I added <code>default</code> case to <code>triggerActionByType</code> function:</p>
  
  <pre><code>default : {
      $processor = explode("_", $type);
      $class = $processor[0];
      $function = $processor[1];
      craft()-&gt;$class-&gt;$function($details, $chargeModel);
      break;
  }
  </code></pre>
  
  <p>This allows me to call actions from external files. <code>$processor</code> will be class of service file in my plugin, <code>$function</code> would be name of function to be called. There are separated by <code>_</code> in config (I'll explain this later on)</p>
  
  <p>Next, I had to comment out </p>
  
  <pre><code>//if (!in_array($type, $this-&gt;baseActions)) continue;
  </code></pre>
  
  <p>to allow any actions, not just base ones.</p>
  
  <p>Then in my templates, I based off example provided in <code>charge/actions</code> example template. </p>
  
  <p>Options used:</p>
  
  <pre><code>{% set options = {
      'planAmount' : cart.totalPrice,
      'planInterval' : 'month',
      'planIntervalCount' : '1',
      'actions' : {
          'onSuccess': {
              'chargeCommerce_updateOrder' : { 'orderId': cart.id, 'status': 'subscriptionActive', 'complete': true }
          },
          'onFailure': {
              'chargeCommerce_updateOrder' : { 'orderId': cart.id, 'status': 'subscriptionCancelled' },
              'email' : { 0: 'payment-failed-customer', 1: 'payment-failed-admin'  }
          },
      }} %}
  </code></pre>
  
  <p><code>chargeCommerce_updateOrder</code> is the reference to function <code>updateOrder</code> in <code>ChargeCommerceService.php</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-14T13:59:36.790Z
Answers.LastActivityDate: 2017-04-14T13:59:36.790Z
Answers.UserDisplayName: Yuri Salimovskiy
Answers.Body: <p>I don't see events being triggered in the Actions Service (in my version of Charge anyway), so you won't be able to listen for those. You'll most likely have to modify the core, if you do, document that and when you update charge, you can easily put your modifications back into place or do a pull request to charge in hopes that event gets added to the core.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-13T15:38:42.190Z
Answers.LastActivityDate: 2017-04-13T15:38:42.190Z
Answers.UserDisplayName: Brad
Body: <p>I'm trying to integrate Charge with Craft Commerce.</p>
  
  <p>Basically I need to </p>
  
  <p>a) use Stripe to set up subscription for the amount based on what's in the cart (this seems to be possible out-of-the-box)</p>
  
  <p>b) on successful charge - mark order as complete (I think I can use <code>'charge.onCharge'</code> event here)</p>
  
  <p>c) listen to webhooks - when the next recurring payment succeeds or fails, make change to order.</p>
  
  <p>Is there a way to write custom action for (c) without hacking into Charge core? </p>
  
  <p>I see there is <code>Charge_Actions</code> service fireing some events, but how do I reference/listen to those actions in a custom plugin that I create?</p>
  
Tags: <plugin-development><plugin-craftcommerce><plugin-charge>
Title: Charge plugin: custom actions on webhooks
ThreadText: # Charge plugin: custom actions on webhooks
  
  (Asked by: username_0 on 2017-04-07)
  
  I'm trying to integrate Charge with Craft Commerce.
  
  Basically I need to
  
  a) use Stripe to set up subscription for the amount based on what's in the cart (this seems to be possible out-of-the-box)
  
  b) on successful charge - mark order as complete (I think I can use `'charge.onCharge'` event here)
  
  c) listen to webhooks - when the next recurring payment succeeds or fails, make change to order.
  
  Is there a way to write custom action for (c) without hacking into Charge core?
  
  I see there is `Charge_Actions` service fireing some events, but how do I reference/listen to those actions in a custom plugin that I create?
  
  **Tags:** plugin-development, plugin-craftcommerce, plugin-charge
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2017-04-14. Score: 1 | Accepted answer
  
  So, in order to achieve this I had to make a couple of small hacks to the module core, namely in `Charge_ActionsService.php`
  
  First, I added `default` case to `triggerActionByType` function:
  
  ```
  default : {
      $processor = explode("_", $type);
      $class = $processor[0];
      $function = $processor[1];
      craft()->$class->$function($details, $chargeModel);
      break;
  }
  
  ```
  
  This allows me to call actions from external files. `$processor` will be class of service file in my plugin, `$function` would be name of function to be called. There are separated by `_` in config (I'll explain this later on)
  
  Next, I had to comment out
  
  ```
  //if (!in_array($type, $this->baseActions)) continue;
  
  ```
  
  to allow any actions, not just base ones.
  
  Then in my templates, I based off example provided in `charge/actions` example template.
  
  Options used:
  
  ```
  {% set options = {
      'planAmount' : cart.totalPrice,
      'planInterval' : 'month',
      'planIntervalCount' : '1',
      'actions' : {
          'onSuccess': {
              'chargeCommerce_updateOrder' : { 'orderId': cart.id, 'status': 'subscriptionActive', 'complete': true }
          },
          'onFailure': {
              'chargeCommerce_updateOrder' : { 'orderId': cart.id, 'status': 'subscriptionCancelled' },
              'email' : { 0: 'payment-failed-customer', 1: 'payment-failed-admin'  }
          },
      }} %}
  
  ```
  
  `chargeCommerce_updateOrder` is the reference to function `updateOrder` in `ChargeCommerceService.php`
  
  
  ---
  
  ### Answer by username_1 on 2017-04-13. Score: 1
  
  I don't see events being triggered in the Actions Service (in my version of Charge anyway), so you won't be able to listen for those. You'll most likely have to modify the core, if you do, document that and when you update charge, you can easily put your modifications back into place or do a pull request to charge in hopes that event gets added to the core.
  
  
  ---
  
  