簡體   English   中英

如何向 TinkoffPayments 發送 OK 200 響應

[英]How to send OK 200 response to TinkoffPayments

我有這個 POST 方法,它接收來自 TinkoffPayments API 的通知,現在我正在返回Task<HttpResponseMessage>但 Tinkoff 沒有接收狀態 OK 200 響應。 以下是他們建議如何在他們的文檔中做出回應

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

但我不像專業程序員,我不知道如何在asp.net中進行適當的類比。 我試圖使我的方法IActionResult返回值並返回 Ok("OK")。 我試圖將值分配給HttpContext.Response但沒有任何效果。 Tinkoff 總是在測試中顯示此錯誤: "Notification Error: we don't receive answer "OK" for next notification requests: CONFIRMED. Check and fix notification management: CONFIRMED. Then try test case again"可能是什么問題?

[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;
    }

您不應該返回HttpResponseMessage ,因為這是一個較低級別的構造。 將返回類型更改為Task<IActionResult>並返回一個OK對象。

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

尼爾關於使用 fiddler 的建議真的很有幫助。Tinkoff 實際上想要 Respond.Body 是“OK”,而不僅僅是標准響應 200 OK。 當我早些時候給 Body 寫“OK”時,他們沒有接受它,因為我沒有設置 ContentLength 而我的實際 Request.Body 是這樣的:

2
OK
0

使用 fiddler 有助於理解這一點。 所以,這段代碼有效

[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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM