在ApiController中获取原始发布请求

本文关键字:请求 布请求 ApiController 获取 原始 | 更新日期: 2023-09-27 18:28:33

我正在尝试实现Paypal即时支付通知(IPN)

协议是

  1. PayPal HTTP向您的侦听器发布一条IPN消息,通知您发生了一个事件
  2. 您的侦听器向PayPal返回一个空的HTTP 200响应
  3. 侦听器HTTP将完整的、未更改的消息发布回PayPal;消息必须包含相同的字段(按相同顺序)作为原始消息,并以与原始消息
  4. PayPal发回一个单词-验证(如果消息与原件匹配)或INVALID(如果消息与原件)

到目前为止,我有

        [Route("IPN")]
        [HttpPost]
        public void IPN(PaypalIPNBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                // if you want to use the PayPal sandbox change this from false to true
                string response = GetPayPalResponse(model, true);
                if (response == "VERIFIED")
                {
                }
            }
        }
        string GetPayPalResponse(PaypalIPNBindingModel model, bool useSandbox)
        {
            string responseState = "INVALID";
            // Parse the variables
            // Choose whether to use sandbox or live environment
            string paypalUrl = useSandbox ? "https://www.sandbox.paypal.com/"
            : "https://www.paypal.com/cgi-bin/webscr";
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(paypalUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
                //STEP 2 in the paypal protocol
                //Send HTTP CODE 200
                HttpResponseMessage response = client.PostAsync("cgi-bin/webscr", "").Result;
                if (response.IsSuccessStatusCode)
                {
                    //STEP 3
                    //Send the paypal request back with _notify-validate
                    model.cmd = "_notify-validate";
                    response = client.PostAsync("cgi-bin/webscr", THE RAW PAYPAL REQUEST in THE SAME ORDER ).Result;
                    if(response.IsSuccessStatusCode)
                    {
                        responseState = response.Content.ReadAsStringAsync().Result;
                    }
                }
            }
            return responseState;
        }

我的问题是,我不知道如何用相同的顺序将原始请求发送到Paypal。我可以用我的PaypalIPNBindingModel建造一个HttpContent,但我不能保证订单。

我有什么办法做到这一点吗?

感谢

在ApiController中获取原始发布请求

我认为您不应该使用参数绑定,而应该自己读取原始请求。随后,您可以自己反序列化到模型中。或者,如果您想利用Web API的模型绑定,同时访问原始请求主体,这里是我可以想到的一种方法。

当Web API将请求主体绑定到参数中时,请求主体流将被清空。随后,您将无法再次阅读。

[HttpPost]
public async Task IPN(PaypalIPNBindingModel model)
{
    var body = await Request.Content.ReadAsStringAsync(); // body will be "".
}

因此,在Web API管道中运行模型绑定之前,您必须阅读主体。如果您创建了一个消息处理程序,您可以在那里准备好正文,并将其存储在请求对象的属性字典中。

public class MyHandler : DelegatingHandler
{
    protected async override Task<HttpResponseMessage> SendAsync(
                                           HttpRequestMessage request, 
                                             CancellationToken cancellationToken)
    {
        if (request.Content != null)
        {
            string body = await request.Content.ReadAsStringAsync();
            request.Properties["body"] = body;
        }
        return await base.SendAsync(request, cancellationToken);
    }
}

然后,您可以从控制器中检索正文字符串,如下所示。此时,您已经拥有了原始请求主体以及参数绑定模型。

[HttpPost]
public void IPN(PaypalIPNBindingModel model)
{
    var body = (string)(Request.Properties["body"]);
}