Link to home
Start Free TrialLog in
Avatar of Nika Gudge
Nika GudgeFlag for United States of America

asked on

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
Avatar of Michael Vasilevsky
Michael Vasilevsky
Flag of United States of America image

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

Avatar of Nika Gudge

ASKER

the solution didn’t work
ASKER CERTIFIED SOLUTION
Avatar of Michael Vasilevsky
Michael Vasilevsky
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.