问题描述
我有一个Asp.Net WEB API 2项目,我想实现一个即时付款通知(IPN)侦听器控制器.
I have an Asp.Net WEB API 2 project and I would like to implement an Instant Payment Notification (IPN) listener controller.
我找不到任何示例和nuget包.我需要做的就是确认用户使用Paypal上的标准html按钮付款.这很简单.
I can't find any example and nuget package. All I need is to acknowledge that the user paid with the standard html button on Paypal. It's quite simple.
所有的nuget软件包都是用来创建发票或自定义按钮的.这不是我所需要的
All the nuget packages are to create invoice or custom button. It's not what I need
paypal上的示例适用于经典的asp.net,而不适用于MVC或WEB API MVC
The samples on paypal are for classic asp.net and not for MVC or WEB API MVC
我确定有人已经这样做了,当我开始编码时,我有种感觉,我正在重新发明轮子.
I'm sure somebody did that already and when I started coding I had a feeling that I was reinventing the wheel.
是否有任何IPN侦听器控制器示例?
Is there any IPN listener controller example?
至少有一个PaypalIPNBindingModel绑定Paypal查询.
At least a PaypalIPNBindingModel to bind the Paypal query.
[Route("IPN")]
[HttpPost]
public IHttpActionResult IPN(PaypalIPNBindingModel model)
{
if (!ModelState.IsValid)
{
return BadRequest();
}
return Ok();
}
编辑
到目前为止,我有以下代码
So far I have the following code
[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
,并且参数必须使用相同的顺序.
but for the step 3 I tried to post my model as json but paypal returns a HTML page instead of VALIDATED or INVALID. I figured out that I have to use application/x-www-form-urlencoded
and it the parameters as to be in the same order.
如何获取请求网址?
我将使用查询网址并向其中添加&cmd=_notify-validate
I would use the query Url and add &cmd=_notify-validate
to it
推荐答案
这是我的代码
随时查看是错的
[Route("IPN")]
[HttpPost]
public IHttpActionResult IPN()
{
// if you want to use the PayPal sandbox change this from false to true
string response = GetPayPalResponse(true);
if (response == "VERIFIED")
{
//Database stuff
}
else
{
return BadRequest();
}
return Ok();
}
string GetPayPalResponse(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
string rawRequest = response.Content.ReadAsStringAsync().Result;
rawRequest += "&cmd=_notify-validate";
HttpContent content = new StringContent(rawRequest);
response = client.PostAsync("cgi-bin/webscr", content).Result;
if(response.IsSuccessStatusCode)
{
responseState = response.Content.ReadAsStringAsync().Result;
}
}
}
return responseState;
}
这篇关于PayPal IPN是否有任何样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!