我有一个 Asp.Net WEB API 2 项目,我想实现一个即时付款通知 (IPN) 侦听器控制器。
我找不到任何示例和 nuget 包。我所需要的只是确认用户使用 Paypal 上的标准 html 按钮付款。这很简单。
所有 nuget 包都是用于创建发票或自定义按钮。这不是我需要的
paypal 上的示例适用于经典 asp.net,不适用于 MVC 或 WEB API MVC
我确信已经有人这样做了,当我开始编码时,我有一种感觉,我正在重新发明轮子。
有IPN监听控制器的例子吗?
至少有一个 PaypalIPNBindingModel 来绑定 Paypal 查询。
[Route("IPN")]
[HttpPost]
public IHttpActionResult IPN(PaypalIPNBindingModel model)
{
if (!ModelState.IsValid)
{
return BadRequest();
}
return Ok();
}
EDIT
到目前为止我有以下代码
[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/";
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.PostAsJsonAsync("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;
}
但对于第 3 步,我尝试将我的模型发布为 json,但 paypal 返回 HTML 页面而不是“VALIDATED”或“INVALID”。我发现我必须使用application/x-www-form-urlencoded
并且参数的顺序相同。
如何获取请求的URL?
我将使用查询 Url 并添加&cmd=_notify-validate
to it