<p>You mean the errors of your form? You can access those via <code>paymentForm</code></p>

<pre class="lang-Twig prettyprint-override"><code>{% for attribute, error in paymentForm %}
    {{ attribute }}: {{ dump(error) }}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2019-03-02T15:59:21.610Z
no, I mean from my gateway, like an invalid credit card number or an invalid CVV number
Flying Car 0013
CC BY-SA 4.0
2019-03-02T16:02:41.470Z
Your gateway does not have a property credit card number, only your payment form has those. And the Payment Form is provided by your gateway. Your gateway can't contain errors, it isn't even validated
Robin Schambach
CC BY-SA 4.0
2019-03-02T16:06:24.277Z
No no, what I mean is like coupons. if i want to get any errors on the coupon, i use this: cart.getFirstError('couponCode')
isn't there something like this: cart.getFirstError('payment')
Flying Car 0013
CC BY-SA 4.0
2019-03-02T16:52:33.880Z
No that's what the `paymentForm` is for. The errors of your payment have nothing to do with your order
Robin Schambach
CC BY-SA 4.0
2019-03-02T15:46:08.157Z
2019-03-02T15:46:08.157Z
Robin Schambach
<p>When submitting a payment form to the <code>commerce/payments/pay</code> controller action there are three potential sources of error messages.</p>

<p>If you are submitting via a normal form submission (not ajax) you will be able to get access to the errors in the following manner from your template.</p>

<p><strong>General Errors</strong></p>

<p><code>{% set flashError = craft.app.session.getFlash('error') %}</code> For general errors e.g. <code>requireBillingAddressAtCheckout</code> is set to true and there is no billing address on the order.</p>

<hr>

<p><strong>Order Save Errors</strong></p>

<p><code>{% set orderErrors = order.getErrors() %}</code> The order is recalculated and saved during the pay process to make sure something has not changed elsewhere. The order is validated and therefore errors will be returned if there are validation issues.</p>

<hr>

<p><strong>Payment Form Errors</strong></p>

<p><code>{% set paymentFormErrors = paymentForm.getErrors() %}</code> The payment form errors will contain any errors related to the payment form and, dependant on gateway, may also return payment gateway errors.</p>

CC BY-SA 4.0
2020-05-22T23:13:15.570Z
This should be the correct answer I think.
iambriansreed
CC BY-SA 4.0
2020-05-22T23:23:08.280Z
Also don't forget to dump those errors too.
iambriansreed
CC BY-SA 4.0
2019-10-20T17:35:15.913Z
2020-05-24T05:50:06.280Z
2020-05-24T05:50:06.280Z
nfourtythree
<p>I was able to use the "flash message" that's included in their demo templates (shop/_layout/main)</p>

<pre><code>{% set flashNotice = craft.app.session.getFlash('notice') %}

{% if flashNotice %}
    &lt;div class="bg-blue text-white"&gt;
        &lt;div class="container mx-auto px-6 py-4"&gt;
            {{ flashNotice }}
        &lt;/div&gt;
    &lt;/div&gt;
{% endif %}

{% set flashError = craft.app.session.getFlash('error') %}

{% if flashError %}
    &lt;div class="bg-red text-white"&gt;
        &lt;div class="container mx-auto px-6 py-4"&gt;
            {{ flashError }}
        &lt;/div&gt;
    &lt;/div&gt;
{% endif %}

{% if cart.errors|length %}
    &lt;div class="bg-red text-white"&gt;
        &lt;div class="container mx-auto px-6 py-4"&gt;
            {% for attribute, errors in cart.errors %}
                {% if loop.first %}&lt;ul&gt;{% endif %}
                {% for key, error in errors %}
                    &lt;li&gt;{{ attribute }}: {{ error }}&lt;/li&gt;
                {% endfor %}
                {% if loop.last %}&lt;/ul&gt;{% endif %}
            {% endfor %}
        &lt;/div&gt;
    &lt;/div&gt;
{% endif %}
</code></pre>

CC BY-SA 4.0
2019-10-19T15:50:22.530Z
2019-10-19T15:50:22.530Z
EDub
<p>I am trying to display the error message coming from the payment gateway ONLY.
I know I can use</p>

<pre><code>{% set message = craft.app.session.getFlash('error') %}
{{ message|length ? message }}
</code></pre>

<p>but commerce returns to me a vague error like this </p>

<blockquote>
  <p>Unable to update cart.</p>
</blockquote>

<p>If anything happens to the cart, even if it is not the payment. I don't know what the error is, nor does the customer. So is there a way to catch payment errors of the cart only?</p>

<plugin-craftcommerce2><error-handling>
How to display payment gateway error message only
# How to display payment gateway error message only

(Asked by: username_0 on 2019-03-02)

I am trying to display the error message coming from the payment gateway ONLY.
I know I can use
{% set message = craft.app.session.getFlash('error') %}
{{ message|length ? message }}

but commerce returns to me a vague error like this 

Unable to update cart.

If anything happens to the cart, even if it is not the payment. I don't know what the error is, nor does the customer. So is there a way to catch payment errors of the cart only?

**Tags:** plugin-craftcommerce2, error-handling


---

## 3 answers

### Answer by username_1 on 2019-03-02. Score: 2 | Accepted answer

You mean the errors of your form? You can access those via paymentForm
{% for attribute, error in paymentForm %}
    {{ attribute }}: {{ dump(error) }}
{% endfor %}

> **Comments:**
> **username_0**: no, I mean from my gateway, like an invalid credit card number or an invalid CVV number  
> **username_1**: Your gateway does not have a property credit card number, only your payment form has those. And the Payment Form is provided by your gateway. Your gateway can't contain errors, it isn't even validated  
> **username_0**: No no, what I mean is like coupons. if i want to get any errors on the coupon, i use this: cart.getFirstError('couponCode') isn't there something like this: cart.getFirstError('payment')  
> **username_1**: No that's what the `paymentForm` is for. The errors of your payment have nothing to do with your order  

---

### Answer by username_2 on 2019-10-20. Score: 5

When submitting a payment form to the `commerce/payments/pay` controller action there are three potential sources of error messages.

If you are submitting via a normal form submission (not ajax) you will be able to get access to the errors in the following manner from your template.

**General Errors**

`{% set flashError = craft.app.session.getFlash('error') %}` For general errors e.g. `requireBillingAddressAtCheckout` is set to true and there is no billing address on the order.

---

**Order Save Errors**

`{% set orderErrors = order.getErrors() %}` The order is recalculated and saved during the pay process to make sure something has not changed elsewhere. The order is validated and therefore errors will be returned if there are validation issues.

---

**Payment Form Errors**

`{% set paymentFormErrors = paymentForm.getErrors() %}` The payment form errors will contain any errors related to the payment form and, dependant on gateway, may also return payment gateway errors.

> **Comments:**
> **username_3**: This should be the correct answer I think.  
> **username_3**: Also don't forget to dump those errors too.  

---

### Answer by username_4 on 2019-10-19. Score: 1

I was able to use the "flash message" that's included in their demo templates (shop/_layout/main)
{% set flashNotice = craft.app.session.getFlash('notice') %}

{% if flashNotice %}
    
        
            {{ flashNotice }}
        
    
{% endif %}

{% set flashError = craft.app.session.getFlash('error') %}

{% if flashError %}
    
        
            {{ flashError }}
        
    
{% endif %}

{% if cart.errors|length %}
    
        
            {% for attribute, errors in cart.errors %}
                {% if loop.first %}{% endif %}
                {% for key, error in errors %}
                    {{ attribute }}: {{ error }}
                {% endfor %}
                {% if loop.last %}{% endif %}
            {% endfor %}
        
    
{% endif %}


---

