Need javascript for request validation

For the json request ,
1. Match the key with a merchantUniqueIdentifier (key) in the request,
2. If the key matches then match the value extracted from a custom attribute.

Please find the attachment for the same. It has the request payload and the mid which needs to be verified.
Request.pdf
Newton21205Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael VasilevskySolutions ArchitectCommented:
You code as written:
for(var key in request){

Open in new window

is only going to find top-level keys. It won't find "merchantUniqueId" because that is a key in the "merchant" object. You can hard code it like this:

JSFiddle

let myDiv = document.getElementById("myResult");
let authId = 'WALLET';
let myJSON = {
  "merchantRef": "ffd03152",
  "transactionType": "score_only",
  "originalTransactionType": "transaction/purchase",
  "originalTransactionId": "1231231",
  "amount": "1100",
  "currencyCode": "USD",
  "customer": {
    "id": "125Xasdf57D",
    "startDate": "2017-01-04",
    "firstName": "John",
    "lastName": "Smith",
    "middleName": "First",
    "email": "accept@xyz.com",
    "sessionId": "session-1",
    "username": "username",
    "gender": "male",
    "dateOfBirth": "2017",
    "address": {
      "street": "123 Main Street",
      "street2": "Apartment 123",
      "stateProvince": "NY",
      "city": "New York",
      "country": "US",
      "phone": {
        "type": "cell",
        "number": "212-515-1212"
      },
      "zipPostalCode": "11375"
    }
  },
  "billingAddress": {
    "firstName": "John",
    "lastName": "Smith",
    "middleName": "First",
    "street": "123Mainstreet",
    "stateProvince": "NY",
    "city": "NewYork",
    "country": "CAN",
    "phone": {
      "type": "cell",
      "number": "212-515-1212"
    },
    "zipPostalCode": "11375"
  },
  "device": {
    "deviceType": "device/mobile",
    "deviceId": "BDE000:008945:58AC03:F02569",
    "networks": [
      {
        "networkType": "network/wifi",
        "ip": "10.201.0.244",
        "mac": "02:00:00:00:00:00",
        "ssid": "Boston-5G-1",
        "bssid": "e8:fc:af:fb:4b:8c"
      }
    ],
    "latitude": 90,
    "longitude": 90,
    "imei": "49-015420-323751",
    "model": "iphone",
    "manufacturer": "apple",
    "timezoneOffset": "+02:00",
    "rooted": false,
    "malwareDetected": false,
    "userDefined": {
      "battery": "5h 10m",
      "uptime": "0.95"
    }
  },
  "loyalty": {
    "id": "LY342XYS",
    "program": "GOLD",
    "balance": 200
  },
  "payment": {
    "paymentType": "payment/card",
    "method": {
      "methodType": "method/card",
      "methodId": "123",
      "methodAlias": "card_method",
      "card": {
        "cardholderName": "John Smith",
        "cardNumber": "444444444444",
        "expDate": "122018",
        "cvvPresent": false,
        "issuer": "Bank of America",
        "cardReissuedNumber": "3"
      },
      "provider": "apple"
    },
    "pinPresent": false,
    "cardBrand": "VISA",
    "entryMethod": "remote",
    "issuerResponse": {
      "code": "000",
      "status": "approved",
      "scheme": "visa",
      "issuerApprovedAmount": "8000",
      "cardBalance": "9000"
    },
    "verificationAvs": {
      "code": "1",
      "status": "2",
      "scheme": "3"
    },
    "verification_3ds": {
      "code": "4",
      "status": "approved",
      "scheme": "6"
    },
    "verificationCvv": {
      "code": "7",
      "status": "approved",
      "scheme": "9"
    },
    "userDefined": {
      "failedPinAttempts": "20"
    }
  },
  "merchant": {
    "merchantUniqueId": "WALLET",
    "location": {
      "locationId": "WALLET",
      "merchantAddress": {
        "street": "123 Main street",
        "street2": "St",
        "stateProvince": "NY",
        "city": "New York",
        "country": "US",
        "zipPostalCode": "11375"
      },
      "timeZoneOffset": "-05:00",
      "hierarchy": "abc"
    },
    "mcc": "7311"
  },
  "order": {
    "shipToAddress": {
      "phone": "123-123-1234",
      "address_1": "123 Second Ave.",
      "address_2": "222",
      "city": "Atlanta",
      "state": "GA",
      "zip": "30024",
      "country": "UK"
    },
    "items": [
      {
        "id": "1234",
        "name": "mouse",
        "quantity": "6",
        "unit": "carret",
        "unitPrice": "1299",
        "categories": [
          [
            "Books",
            "Computer",
            "Programming"
          ],
          [
            "Books",
            "Text Books",
            "Computer Science"
          ]
        ],
        "detailsUrl": "https://mystore.domain/product/978-0321751041",
        "userDefined": {
          "weight": "5021.23",
          "vat": "0.06"
        }
      }
    ],
    "detailsUrl": "https://mystore.domain/product/978-0321751041",
    "userDefined": {
      "delivery": "express",
      "carrier": "ups"
    }
  },
  "userDefined": {
    "inauthTransId": "1234"
  }
}

let request = myJSON.merchant

for (var key in request) {
  if (key == 'merchantUniqueId') {
    console.log("values match");
    if (request[key] == authId) {
      myDiv.innerHTML = "MID verification passed: " + request[key] + "=" +
        authId;
    } else {
      myDiv.innerHTML = "MID does not match the authId";
    }
  }
}

Open in new window

0
Newton21205Author Commented:
the solution didn’t work
0
Michael VasilevskySolutions ArchitectCommented:
Could you kindly provide more detail? Did you check the JSFiddle?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Newton21205Author Commented:
Yes I did check js fiddle. The request is the Json string. When I execute the JavaScript I don’t see the print statements being executed within.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.