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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

12 Comments

curl 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
Above is the Curl for the API
Please can you explain where to implement the above?
@uthumvc - i have updated my answer please take a look
Thank you boss. I will try it out and revert
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.