gpt4 book ai didi

c# - 有没有PayPal IPN的样本

转载 作者:可可西里 更新时间:2023-11-01 08:37:05 26 4
gpt4 key购买 nike

我有一个 Asp.Net WEB API 2 项目,我想实现一个即时支付通知 (IPN) 监听器 Controller 。

我找不到任何示例和 nuget 包。我只需要确认用户使用 Paypal 上的标准 html 按钮付款。这很简单。

所有 nuget 包都用于创建发票或自定义按钮。这不是我需要的

paypal 上的示例适用于经典的 asp.net,不适用于 MVC 或 WEB API MVC

我敢肯定有人已经这样做了,当我开始编码时,我感觉自己正在重新发明轮子。

有没有IPN listener controller的例子?

至少一个 PaypalIPNBindingModel 来绑定(bind) Paypal 查询。

    [Route("IPN")]
[HttpPost]
public IHttpActionResult IPN(PaypalIPNBindingModel model)
{
if (!ModelState.IsValid)
{
return BadRequest();
}

return Ok();
}

编辑

到目前为止我有以下代码

        [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

最佳答案

基于已接受的答案,我想出了以下代码,为 ASP.NET MVC 实现 IPN 监听器。该解决方案已经部署并且似乎可以正常工作。

[HttpPost]
public async Task<ActionResult> Ipn()
{
var ipn = Request.Form.AllKeys.ToDictionary(k => k, k => Request[k]);
ipn.Add("cmd", "_notify-validate");

var isIpnValid = await ValidateIpnAsync(ipn);
if (isIpnValid)
{
// process the IPN
}

return new EmptyResult();
}

private static async Task<bool> ValidateIpnAsync(IEnumerable<KeyValuePair<string, string>> ipn)
{
using (var client = new HttpClient())
{
const string PayPalUrl = "https://www.paypal.com/cgi-bin/webscr";

// This is necessary in order for PayPal to not resend the IPN.
await client.PostAsync(PayPalUrl, new StringContent(string.Empty));

var response = await client.PostAsync(PayPalUrl, new FormUrlEncodedContent(ipn));

var responseString = await response.Content.ReadAsStringAsync();
return (responseString == "VERIFIED");
}
}

编辑:

让我分享一下我的经验 - 上面的代码到目前为止工作得很好,但是突然它在处理一个 IPN 时失败了,即 responseString == "INVALID"

问题原来是我的帐户设置为使用 charset == windows-1252,这是 PayPal 默认值。但是,FormUrlEncodedContent 使用 UTF-8 进行编码,因此验证失败,因为国家字符如“ř”。解决方案是将 charset 设置为 UTF-8,这可以在配置文件 > 我的销售工具 > PayPal 按钮语言编码 > 更多选项中完成,参见 this SO thread .

关于c# - 有没有PayPal IPN的样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26638857/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com