Answers.Body: <p>I'm not sure whether you're referring to displaying this in the Control Panel somewhere or on the front-end of a site in a user account area.</p>
  
  <p>Assuming it's on the front-end of a site and you've already got a reference to the order, it'd be something like:</p>
  
  <pre><code>{# Grab an order #}
  {% set order = craft.commerce.orders.id(15).first() %}
  
  {% if order %}
      {% Grab all of it's transactions %}
      {% set transactions = order.getTransactions() %}
  
      {# Loop through all of them looking for the one(s) you're interested in on this order.  Here, we just grab the first. #}
      {% set transaction = transactions[0] %}
  
      {# Output the reference #}
      {{ transaction.reference }}
  {% endif %}
  </code></pre>
  
  <p>Keep in mind that the reference value is a response that comes back directly from the particular payment gateway, so it may or may not even be JSON, much less include the last 4 digits of a credit card number.  If it is JSON, XML, etc. then you'll need to parse that value looking for where they save the last 4 digits.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-17T01:52:40.290Z
Answers.Text: Thanks. I was looking to do so in a front-end template so good example. Kinda messy for something that seems to be so common in online e-commerce experiences.
Answers.UserDisplayName: Ben Parizek
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-17T01:58:58.210Z
Answers.Text: This does seem like a common enough requirement that it could be helpful to have a method on the Transaction Model `{{ transaction.last4digits() }}` that does the gateway-specific logic and returns null if a gateway doesn't provide those digits.
Answers.UserDisplayName: Ben Parizek
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-17T19:03:21.480Z
Answers.Text: Is this still the only way to achieve this even with Commerce 3?
Answers.UserDisplayName: foamcow
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-16T23:20:16.797Z
Answers.LastActivityDate: 2017-10-16T23:25:46.060Z
Answers.LastEditDate: 2017-10-16T23:25:46.060Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>An alternate approach to Brad's would be to save the four digits to a field on the order at the time of the transaction...would make for easier retrieval later.  We do this with a fraud score our one of our gateways gives us... </p>
  
  <pre><code>    // Save the Beagle Anti-Fraud Score to the order
      craft()-&gt;on('commerce_transactions.onSaveTransaction', function ($event)
      {
  
              $transaction = $event-&gt;params['transaction'];
              $order = $transaction-&gt;order;
  
              // Transactions are saved before responses too....
              if($transaction-&gt;response){
                  BusinessLogicPlugin::log($transaction-&gt;response);
  
                  if (array_key_exists("BeagleScore",$transaction-&gt;response)){
                      $score = (string) $transaction-&gt;response['BeagleScore'];
                      BusinessLogicPlugin::log("Beagle score found: $score");
                      $order-&gt;setContentFromPost(["beagleScore"=&gt;$score]);
                      craft()-&gt;commerce_orders-&gt;saveOrder($order);
                  }
              }
       });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-17T01:54:38.163Z
Answers.Text: Thanks for the suggestion Jeremy. It does seem like a gateway-specific plugin or macro may be handy to make this feel less tedious.
Answers.UserDisplayName: Ben Parizek
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-17T00:57:54.633Z
Answers.LastActivityDate: 2017-10-17T00:57:54.633Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>I'd like to display the last 4 digits of the credit card used for a purchase in a front-end template. It appears the data is in the db in the <code>craft_commerce_transactions</code> table <code>reference</code> column in JSON format.</p>
  
  <pre><code>{
      "approvalCode":"PNRABL",
      "transId":"60007205645",
      "card": {
          "number":"1234",
          "expiry":"112318"
      }
  }
  </code></pre>
  
  <p>Is there a method supported by Craft Commerce to display this data in a users account area?</p>
  
Tags: <plugin-craftcommerce>
Title: How to display the last 4 digits of the credit card used for a Craft Commerce purchase
ThreadText: # How to display the last 4 digits of the credit card used for a Craft Commerce purchase
  
  (Asked by: username_0 on 2017-10-16)
  
  I'd like to display the last 4 digits of the credit card used for a purchase in a front-end template. It appears the data is in the db in the `craft_commerce_transactions` table `reference` column in JSON format.
  
  ```
  {
      "approvalCode":"PNRABL",
      "transId":"60007205645",
      "card": {
          "number":"1234",
          "expiry":"112318"
      }
  }
  
  ```
  
  Is there a method supported by Craft Commerce to display this data in a users account area?
  
  **Tags:** plugin-craftcommerce
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-10-16. Score: 1 | Accepted answer
  
  I'm not sure whether you're referring to displaying this in the Control Panel somewhere or on the front-end of a site in a user account area.
  
  Assuming it's on the front-end of a site and you've already got a reference to the order, it'd be something like:
  
  ```
  {# Grab an order #}
  {% set order = craft.commerce.orders.id(15).first() %}
  
  {% if order %}
      {% Grab all of it's transactions %}
      {% set transactions = order.getTransactions() %}
  
      {# Loop through all of them looking for the one(s) you're interested in on this order.  Here, we just grab the first. #}
      {% set transaction = transactions[0] %}
  
      {# Output the reference #}
      {{ transaction.reference }}
  {% endif %}
  
  ```
  
  Keep in mind that the reference value is a response that comes back directly from the particular payment gateway, so it may or may not even be JSON, much less include the last 4 digits of a credit card number. If it is JSON, XML, etc. then you'll need to parse that value looking for where they save the last 4 digits.
  
  > **Comments:**
  > **username_0**: Thanks. I was looking to do so in a front-end template so good example. Kinda messy for something that seems to be so common in online e-commerce experiences.  
  > **username_0**: This does seem like a common enough requirement that it could be helpful to have a method on the Transaction Model `{{ transaction.last4digits() }}` that does the gateway-specific logic and returns null if a gateway doesn't provide those digits.  
  > **username_2**: Is this still the only way to achieve this even with Commerce 3?  
  
  ---
  
  ### Answer by username_3 on 2017-10-17. Score: 1
  
  An alternate approach to username_1's would be to save the four digits to a field on the order at the time of the transaction...would make for easier retrieval later. We do this with a fraud score our one of our gateways gives us...
  
  ```
      // Save the Beagle Anti-Fraud Score to the order
      craft()->on('commerce_transactions.onSaveTransaction', function ($event)
      {
  
              $transaction = $event->params['transaction'];
              $order = $transaction->order;
  
              // Transactions are saved before responses too....
              if($transaction->response){
                  BusinessLogicPlugin::log($transaction->response);
  
                  if (array_key_exists("BeagleScore",$transaction->response)){
                      $score = (string) $transaction->response['BeagleScore'];
                      BusinessLogicPlugin::log("Beagle score found: $score");
                      $order->setContentFromPost(["beagleScore"=>$score]);
                      craft()->commerce_orders->saveOrder($order);
                  }
              }
       });
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for the suggestion username_3. It does seem like a gateway-specific plugin or macro may be handy to make this feel less tedious.  
  
  ---
  
  