Answers.Body: <p>You have access to the order from the transaction like this:</p>
  
  <pre><code>craft()-&gt;on('commerce_payments.onBeforeGatewayRequestSend', function($event) {
  
      $transaction = $event-&gt;params['transaction']; 
      $order = $transaction-&gt;order;
  
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-26T08:02:41.797Z
Answers.Text: Ha - even easier.  Would be great if the transaction model was documented (and we could search the Commerce docs)!
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-26T10:54:42.167Z
Answers.Text: @JeremyDaalder just added https://craftcommerce.com/docs/transaction-model
Answers.UserDisplayName: Luke Holder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-26T05:51:00.997Z
Answers.LastActivityDate: 2016-10-26T08:01:34.247Z
Answers.LastEditDate: 2016-10-26T08:01:34.247Z
Answers.UserDisplayName: Luke Holder
Answers.Body: <p>You can see what is available for each event in the docs here: </p>
  
  <p><a href="https://craftcommerce.com/docs/events-reference#commerce_payments.onbeforegatewayrequestsend" rel="nofollow">https://craftcommerce.com/docs/events-reference#commerce_payments.onbeforegatewayrequestsend</a></p>
  
  <p>...and indeed order is not one of the parameters.  (I would say it should be really, and you might want to request @lukeholder add this!)</p>
  
  <p>However it's very simple to get the current cart (it's a cart at this point, technically, but that's just an <code>OrderModel</code> that is not set as completed yet). Use this code:</p>
  
  <pre><code>$cart = craft()-&gt;commerce_cart-&gt;getCart();
  </code></pre>
  
  <p>One other note - you don't need to <code>return $event;</code> as the event is passed in by reference anyway (hence you can set <code>$event-&gt;performAction = false;</code> and that sets it on the original event passed in by reference, not a local copy of the event).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-26T05:11:31.423Z
Answers.Text: I feel sad that I never knew you could grab the cart like that, all this time! That's going to be helpful in a lot of other ways. Thanks for that and the return tip!
Answers.UserDisplayName: taylor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-26T05:51:51.723Z
Answers.Text: don't use `getCart()`, get the transaction's order as per my answer below.
Answers.UserDisplayName: Luke Holder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-26T08:02:39.720Z
Answers.Text: Don't listen to that Luke guy, what does he know?! ;)
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-26T04:57:23.260Z
Answers.LastActivityDate: 2016-10-26T04:57:23.260Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>I am trying to add some logic that can stop an order transaction from happening depending on values stored in the <strong>Order Model</strong>. Due to how my client wants the purchase page setup, I need to be able to do last-minute validating through the <code>commerce/payments/pay</code> action, which I can intervene before it happens using the <code>onBeforeGatewayRequestSend</code> event.</p>
  
  <p>Below is a simplified version of what I'm trying to do. The issue I am having is accessing the <strong>Order Model</strong>, It looks like it isn't available in the <code>$event</code> parameter - which is why the below doesn't work.</p>
  
  <pre><code>craft()-&gt;on('commerce_payments.onBeforeGatewayRequestSend', function($event) {
  
      // This doesn't work. No 'order' param available.
      $order = $event-&gt;params['order']; 
  
      /* logic using $order, 
      -- set $is_valid (boolean) */;
  
      if(!$is_valid) {
  
         $transaction = $event-&gt;params['transaction'];
         $transaction-&gt;message = "This order is not yet complete.";
         $event-&gt;performAction = false;
         return $event;
  
      }
  
  });
  </code></pre>
  
  <p>Is it at all possible to get the order model inside of this hook? Or is this just not the right place to be doing this?</p>
  
Tags: <plugin-development><plugin-craftcommerce><php>
Title: Commerce Order Model in Gateway Event Plugin Hook
ThreadText: # Commerce Order Model in Gateway Event Plugin Hook
  
  (Asked by: username_0 on 2016-10-26)
  
  I am trying to add some logic that can stop an order transaction from happening depending on values stored in the **Order Model**. Due to how my client wants the purchase page setup, I need to be able to do last-minute validating through the `commerce/payments/pay` action, which I can intervene before it happens using the `onBeforeGatewayRequestSend` event.
  
  Below is a simplified version of what I'm trying to do. The issue I am having is accessing the **Order Model**, It looks like it isn't available in the `$event` parameter - which is why the below doesn't work.
  
  ```
  craft()->on('commerce_payments.onBeforeGatewayRequestSend', function($event) {
  
      // This doesn't work. No 'order' param available.
      $order = $event->params['order']; 
  
      /* logic using $order, 
      -- set $is_valid (boolean) */;
  
      if(!$is_valid) {
  
         $transaction = $event->params['transaction'];
         $transaction->message = "This order is not yet complete.";
         $event->performAction = false;
         return $event;
  
      }
  
  });
  
  ```
  
  Is it at all possible to get the order model inside of this hook? Or is this just not the right place to be doing this?
  
  **Tags:** plugin-development, plugin-craftcommerce, php
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-10-26. Score: 3 | Accepted answer
  
  You have access to the order from the transaction like this:
  
  ```
  craft()->on('commerce_payments.onBeforeGatewayRequestSend', function($event) {
  
      $transaction = $event->params['transaction']; 
      $order = $transaction->order;
  
  }
  
  ```
  
  > **Comments:**
  > **username_2**: Ha - even easier.  Would be great if the transaction model was documented (and we could search the Commerce docs)!  
  > **username_1**: @username_2 just added https://craftcommerce.com/docs/transaction-model  
  
  ---
  
  ### Answer by username_2 on 2016-10-26. Score: 1
  
  You can see what is available for each event in the docs here:
  
  <https://craftcommerce.com/docs/events-reference#commerce_payments.onbeforegatewayrequestsend>
  
  ...and indeed order is not one of the parameters. (I would say it should be really, and you might want to request @username_1 add this!)
  
  However it's very simple to get the current cart (it's a cart at this point, technically, but that's just an `OrderModel` that is not set as completed yet). Use this code:
  
  ```
  $cart = craft()->commerce_cart->getCart();
  
  ```
  
  One other note - you don't need to `return $event;` as the event is passed in by reference anyway (hence you can set `$event->performAction = false;` and that sets it on the original event passed in by reference, not a local copy of the event).
  
  > **Comments:**
  > **username_0**: I feel sad that I never knew you could grab the cart like that, all this time! That's going to be helpful in a lot of other ways. Thanks for that and the return tip!  
  > **username_1**: don't use `getCart()`, get the transaction's order as per my answer below.  
  > **username_2**: Don't listen to that username_1 guy, what does he know?! ;)  
  
  ---
  
  