1

I am a new to ASP.NET MVC. I want to call an API on a payment gateway. The API only tries to resolve Users Identity. I have written the CURL in C# but I seem to be stuck on how to proceed to get the API called and also return a JSON using AJAX.

Below is the Curl converted to C#.

[HttpPost]
public JsonResult ResolveBVN()
{
        //string str = BVN;

        var secretKey = "secretkey";

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {secretKey}");
            var response = client.PostAsync("https://api.paystack.co/bvn/match", new StringContent("{ bvn: \"22146592120\",\n      account_number: \"0689688306\",\n      bank_code: \"044\",\n      first_name: \"uthman\",\n      last_name: \"jinadu\"\n  }")).Result;

            PaystackAPI paystackAPI = new PaystackAPI()
            {
                statuscode = response.IsSuccessStatusCode,
                message = response.StatusCode
            };

            return Json(paystackAPI);
        }
}

The AJAX call is below:

$("#btnGetBVN").click(function () {
    if ($('#BVN').val() == '' || $('#BVN').val() == undefined) {
        alert('Please Enter Customer BVN');
        return false;
    }

    $('.spinner').css('display', 'block');  //if clicked ok spinner shown

    $.ajax({
                    type: "POST",
                     url: "@Url.Action("ResolveBVN", "Transactions")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert(response.status);
                        $('#Firstname').val(response.data.first_name);
                        $('#Surname').val(response.data.last_name);
                      //  $('#Phone_Number').val(response.data.mobile);
                    
                        $('.spinner').css('display', 'none');
                    },
                    failure: function (response) {
                        
                        alert('BVN Does Not Exist Or Error Processing Request');
                        $('.spinner').css('display', 'none');
                    },
                    error: function (response) {
                       
                        alert('BVN Does Not Exist Or Error Processing Request');
                        $('.spinner').css('display', 'none');
                    }
    });
});

The alert message response is UNDEFINED

EDIT

I have added the Class to return the JSon to the AJAX call. I can only use the statuscode of the response.

How can I access the other part of the response? The response sample is below:

 {
  "status": true,
  "message": "BVN lookup successful",
  "data": {
   "bvn": "000000000000",
"is_blacklisted": false,
"account_number": true,
"first_name": true,
"last_name": true
  },
  "meta": {
"calls_this_month": 1,
"free_calls_left": 9
  }
}

How do I access the other parts in the class like account_Number, message and the likes.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
uthumvc
  • 155
  • 1
  • 11

1 Answers1

0

Please use below :-

var secretKey = string.Empty;
            using (var client = new HttpClient())
            {

                client.DefaultRequestHeaders.Add("Authorization", $"Bearer {secretKey}");
                var response = httpClient.PostAsync("https://api.paystack.co/bvn/match", new StringContent("{ bvn: \"12345678912\",\n      account_number: \"0000000000\",\n      bank_code: \"087\",\n      first_name: \"bojack\",\n      last_name: \"horseman\"\n  }")).Result;
            }

Please make sure to set the correct secret key. you should be writing this code in the the method which is being called by the ajax.Response variable will contain the response returned by the paystack api

Aditya Singh
  • 137
  • 6
  • curl https://api.paystack.co/bvn/match -H "Authorization: Bearer YOUR_SECRET_KEY" -H "Content-Type: application/json" -d '{ bvn: "12345678912", account_number: "0000000000", bank_code: "087", first_name: "bojack", last_name: "horseman" }' -X POST – uthumvc Aug 12 '20 at 15:28
  • Above is the Curl for the API – uthumvc Aug 12 '20 at 15:29
  • Please can you explain where to implement the above? – uthumvc Aug 12 '20 at 15:30
  • 1
    @uthumvc - i have updated my answer please take a look – Aditya Singh Aug 12 '20 at 15:43
  • Thank you boss. I will try it out and revert – uthumvc Aug 12 '20 at 16:08
  • I have edited the question to reflect what i have now as regards your answer but the response is Undefined. Am I missing anything? – uthumvc Aug 12 '20 at 22:05
  • @uthumvc - i would strongly urge you please remove the secret key from your sample code someone could abuse it. – Aditya Singh Aug 13 '20 at 04:56
  • @uthumvc- can you please check what is the response status you are getting from the api call ? if the status is 200 ok then you would have to deserialize the response into a suitable format and send it to the ajax call – Aditya Singh Aug 13 '20 at 05:03
  • The secret key here is a test key. So not to be used for production. How do I deserialse it? The URL it is calling is the localhost and the status is 200. – uthumvc Aug 13 '20 at 06:42
  • you can take a look at - https://stackoverflow.com/questions/10928528/receiving-json-data-back-from-http-request/10928807 – Aditya Singh Aug 13 '20 at 07:01
  • if you want to deserialize your response to a class you can use json.net nuget package – Aditya Singh Aug 13 '20 at 07:02
  • I have edited the post. I added the class, how do i access other parts of the response sample given above? – uthumvc Aug 13 '20 at 09:50