Home > Software engineering >  How to send OK 200 response to TinkoffPayments
How to send OK 200 response to TinkoffPayments

Time:11-17

I have this POST method which recieves notifications from TinkoffPayments API and now I'm returning Task<HttpResponseMessage> but Tinkoff does not recive status OK 200 response. Here is how they advice to make this response in their documentation

@POST
@Path("/ok")
public Response NotifyResponse() {
    return Response.status(200).entity("OK").build();
}

But I'm not like a pro programmer, I don't know how to make proper analogy in asp.net. I've tried to make returned value of my method IActionResult and returning Ok("OK"). I've tried to asigning values to HttpContext.Response but nothing works. Tinkoff always shows this error in tests: "Notification Error: we don't receive answer "OK" for next notification requests: CONFIRMED. Check and fix notification management: CONFIRMED. Then try test case again" What can be the problem?

[HttpPost]
        public async Task<HttpResponseMessage> New()
        {
            string t1 = "";
            try
            {
                StreamReader sr = new StreamReader(Request.Body);
                t1 = await sr.ReadToEndAsync();
            }
            catch (Exception e){}

         BotContext _dbContext = new BotContext(Environment.GetEnvironmentVariable("ConnectionString"));

        Notification notification = new Notification();
        try
        {
            TinkoffNotification tn = JsonConvert.DeserializeObject<TinkoffNotification>(t1);

            notification.Amount = tn.Amount;
            notification.Status = tn.Status;
            notification.OrderId = tn.OrderId;
            notification.PaymentId = tn.PaymentId;
            notification.Pan = tn.Pan;
            notification.RebillId = tn.RebillId;
            notification.Amount = tn.Amount;
            notification.CardId = tn.CardId;
        }
        catch{}
        try
        {
            _dbContext.Notifications.Add(not);
            _dbContext.SaveChanges();
            _dbContext.Database.Connection.Close();
        }
        catch { }

        HttpResponseMessage ht = new HttpResponseMessage();
        ht.StatusCode = HttpStatusCode.OK;
        ht.Content = JsonContent.Create("OK");
        ht.ReasonPhrase = "OK";
        return ht;
    }

CodePudding user response:

You shouldn't be returning an HttpResponseMessage, as that is a lower level construct. Change the return type to Task<IActionResult> and return an OK object.

public async Task<IActionResult> New()
{
    ...
    return Ok();
}

CodePudding user response:

Neil's advice to use fiddler realy helped.Tinkoff actually wanted to Respond.Body to be "OK", not just standart respond 200 OK. And when I wrote "OK" earlier to the Body they did not accepted it because I hadn't set ContentLength and my actual Request.Body was something like that:

2
OK
0

Using fiddler helped to understand this. So, this code works

[HttpPost]
        public async Task<IActionResult> New()
...
StreamWriter sw = new StreamWriter(Response.Body);
            Response.ContentType = "application/json";
            Response.ContentLength = 2;
            Response.StatusCode = 200;
            await sw.WriteAsync("OK");   
            await sw.FlushAsync();
return Ok();
}
  • Related