Skip to content

Returns error in case api response contains 'errors' key#179

Merged
FranceBe merged 1 commit intomasterfrom
feature/mxp-3219-sessile-bagde
Jul 31, 2025
Merged

Returns error in case api response contains 'errors' key#179
FranceBe merged 1 commit intomasterfrom
feature/mxp-3219-sessile-bagde

Conversation

@FranceBe
Copy link
Copy Markdown
Contributor

Widget is based on POST v2/payments/eligibility response to define its behavior.
Most of the time, eligibility response in case of error is constructed with a property error_code.
In the widget code, we explicitly covered this case with

  if ('error_code' in res) {
       setStatus(statusResponse.FAILED)
  } else {
  ...

But there is a specific scenario in which the API respond with a error that does not contains error_code, so
it was not correctly catched in the widgets's code, and so the widget was blocked in "PENDING" state
Capture d’écran 2025-07-31 à 10 54 11

This behavior can be reproduced locally by configuring the widget this way in basic.html (from the master branch) :

function renderPaymentPlans() {
  var purchaseAmount = 0
  widgets.add(Alma.Widgets.PaymentPlans, {
    container: '#alma-widget-payment-plans',
    locale: 'fr',
    purchaseAmount,
    plans: [{
      installmentsCount: 1,
      deferredDays: 0,
      deferredMonths: 0
    }, {
      installmentsCount: 2,
      deferredDays: 0,
      deferredMonths: 0
    }]
  })
}

Which corresponds to calling POST v2/payments/eligibility with the payload :

{
    "purchase_amount": 0,
    "queries": [
        {
            "installments_count": 1,
            "deferred_days": 0,
            "deferred_months": 0
        },
        {
            "installments_count": 2,
            "deferred_days": 0,
            "deferred_months": 0
        }
    ],
    "billing_address": null,
    "shipping_address": null
}

Then, you'll notice the API response is :

{
    "errors": {
        "running": {
            "message": "'purchase_amount'"
        }
    }
}

Now, this PR fixes this behavior by adding a condition to the previous one :

if ('error_code' in res || 'errors' in res) {
  setStatus(statusResponse.FAILED)
   } else {
   ....

And now, the widget is correctly hidden because of the eligibility error
Capture d’écran 2025-07-31 à 10 59 02

@sonarqubecloud
Copy link
Copy Markdown

@FranceBe FranceBe merged commit 0aa5a42 into master Jul 31, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants