Ayo
Ayo

Reputation: 43

Get Data from an Object in the Browser

How can I get an object from my browser / get the data stored in the object from my browser?

  1. The user presses submit button after filling out a form with a bad credit card number (a scenario I am testing), which triggers a jQuery function SubmitAPI() (code below).
  2. Google Chrome console displays 400 Bad Request error (because the credit card number is bad) as well as an API Response holding the object with the declined credit card data (response below)
  3. I specifically need to grab "response_type":"D","response_code":"U20","response_desc":"INVALID CREDIT CARD NUMBER" because I want to display this error message to the user. How can I do this in jQuery?
  4. I have tried for hours to figure this out. I use response.response.response_type to get the Response Type when the transaction is successful (approved credit card). However with a bad credit card number, this same attempt results in "undefined". So, instead, I just want to grab the data from my Google Chrome browser that managed to get the Response Code response.

PART 1: jQuery Code (Directly from the API's documentation - except I changed the credit card to be a bad number)

  function SubmitAPI() {
    var settings = {
        "url": 
    "https://sandbox.forte.net/api/v3/organizations/org_ID/locations/loc_ID/transactions",
        "method": "POST",
        "headers": {
            "X-Forte-Auth-Organization-Id": "org_ID",
            "Authorization": "ID",
            "Content-Type": "application/json"
        },
        "data": JSON.stringify({ "action": "sale", "authorization_amount": 102.45, "subtotal_amount": 99.95, "billing_address": { "first_name": "Jennifer", "last_name": "McFly" }, "card": { "card_type": "visa", "name_on_card": "Jennifer McFly", "account_number": "41111sdf11111111", "expire_month": "12", "expire_year": "2017", "card_verification_value": "123" } }),
    };

    $.ajax(settings).always(function (response) {
        console.log(response);
    });
}

PART 2: Console Response:

400 (Bad Request)

PART 3: Response Object in Browser:

{"location_id":"loc_241789","action":"sale","authorization_amount":102.45,"entered_by":"59ae172b3bd78bed493ecd5892975764","billing_address":{"first_name":"Jennifer","last_name":"McFly"},"card":{"name_on_card":"Jennifer McFly","last_4_account_number":"1111","masked_account_number":"****1111","expire_month":12,"expire_year":2017,"card_type":"visa"},"response":{"environment":"sandbox","response_type":"D","response_code":"U20","response_desc":"INVALID CREDIT CARD NUMBER"}}

Upvotes: 0

Views: 68

Answers (1)

epascarello
epascarello

Reputation: 207501

Using error handler for the Ajax call and was able to get the error message with no issues.

var settings = {
  "url": "https://sandbox.forte.net/api/v3/organizations/org_381529/locations/loc_241789/transactions",
  "method": "POST",
  "headers": {
    "X-Forte-Auth-Organization-Id": "org_381529",
    "Authorization": "Basic NTlhZTE3MmIzYmQ3OGJlZDQ5M2VjZDU4OTI5NzU3NjQ6ZWUwZTZiZDA4ZThlMWNhNWQ3MzUyNGU0ZWU5ZDFjNTg=",
    "Content-Type": "application/json"
  },
  "data": JSON.stringify({
    "action": "sale",
    "authorization_amount": 102.45,
    "subtotal_amount": 99.95,
    "billing_address": {
      "first_name": "Jennifer",
      "last_name": "McFly"
    },
    "card": {
      "card_type": "visa",
      "name_on_card": "Jennifer McFly",
      "account_number": "41111sdf11111111",
      "expire_month": "12",
      "expire_year": "2017",
      "card_verification_value": "123"
    }
  }),
};

$.ajax(settings).error(function(xhr) {
  console.log("Error", xhr.responseJSON.response.response_desc);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

Upvotes: 3

Related Questions