【发布时间】:2018-07-24 10:11:53
【问题描述】:
我正在尝试设置 PayPal 来处理付款。一切正常,除了当我的 IPN 没有向 PayPal 发送验证消息时付款没有被取消。据我了解,如果 IPN 未验证应取消交易的付款。 代码如下:
[HttpPost]
public IActionResult Receive()
{
//Store the IPN received from PayPal
LogRequest(Request);
Task.Run(() => VerifyTask(Request));
//Reply back a 200 code
return new EmptyResult();
}
private void VerifyTask(HttpRequest ipnRequest)
{
try
{
VerifyIpnParamsWithException(ipnRequest);
var verificationRequest = (HttpWebRequest)WebRequest.Create("https://www.paypal.com/cgi-bin/webscr");
//Set values for the verification request
verificationRequest.Method = "POST";
verificationRequest.ContentType = "application/x-www-form-urlencoded";
var param = ReadRequestBody(ipnRequest.Body);
var strRequest = Encoding.ASCII.GetString(param);
//Add cmd=_notify-validate to the payload
strRequest = "cmd=_notify-validate&" + strRequest;
verificationRequest.ContentLength = strRequest.Length;
//Attach payload to the verification request
using (var streamOut = new StreamWriter(verificationRequest.GetRequestStream(), Encoding.ASCII))
{
streamOut.Write(strRequest);
streamOut.Close();
}
var verificationResponse = string.Empty;
//Send the request to PayPal and get the response
using (var streamIn = new StreamReader(verificationRequest.GetResponse().GetResponseStream()))
{
verificationResponse = streamIn.ReadToEnd();
streamIn.Close();
}
ProcessVerificationResponse(verificationResponse);
}
catch (Exception exception)
{
DAL.LogError(exception, ipnRequest);
}
}
当我测试它时,根据我的日志,在向 PayPal 发送验证消息之前引发了异常。这笔钱已转入 PayPal 账户并完成交易这一事实并没有改变。我误会了吗?为了取消交易,我还需要做些什么吗?
【问题讨论】:
标签: c# asp.net-mvc paypal paypal-ipn