private string myEmail = "953478297@qq.com";///
///接收Paypal的IPN消息///
///
[HttpPost]publicHttpStatusCodeResult Receive()
{//Store the IPN received from PayPal
LogRequest(Request);//Fire and forget verification task
Task.Run(() =>VerifyTask(Request));//Reply back a 200 code
return newHttpStatusCodeResult(HttpStatusCode.OK);
}///
///验证是否是paypal发过来的消息///
///
private voidVerifyTask(HttpRequest ipnRequest)
{var verificationResponse = string.Empty;try{
System.Net.ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12;var verificationRequest = (HttpWebRequest)WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr");//Set values for the verification request
verificationRequest.Method = "POST";
verificationRequest.ContentType= "application/x-www-form-urlencoded";var param =Request.BinaryRead(ipnRequest.ContentLength);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
var streamOut = newStreamWriter(verificationRequest.GetRequestStream(), Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();//Send the request to PayPal and get the response
var streamIn = newStreamReader(verificationRequest.GetResponse().GetResponseStream());
verificationResponse=streamIn.ReadToEnd();
streamIn.Close();ProcessVerificationResponse(verificationResponse, ipnRequest);
}catch(Exception exception)
{//插入错误日志
ErrorLogPO errorLog = newErrorLogPO();
errorLog.KID=Guid.NewGuid();
errorLog.ErrorMessage=exception.Message;
errorLog.ErrorState=Interface.Offer.Eum.EumErrorLogType.Paypal;
errorLog.OperaTime=DateTime.Now;
errorLog.Del= false;
OfferServer.ErrorLog.AddErrorLog(errorLog);}
}///
///记录txn_id和状态验证重复///
///
private voidLogRequest(HttpRequest request)
{//交易状态
string payment_status = request.Form["payment_status"].ToString();//交易号
string txn_id = request.Form["txn_id"].ToString();//Persist the request values into a database or temporary data store
PaypalTradeWayPayServer pay = newPaypalTradeWayPayServer();
pay.OneCreate(payment_status, txn_id);
}///
///执行业务操作///
///
///
private void ProcessVerificationResponse(stringverificationResponse, HttpRequest request)
{try{if (verificationResponse.Equals("VERIFIED"))
{//check that Payment_status=Completed//check that Txn_id has not been previously processed//check that Receiver_email is your Primary PayPal email//check that Payment_amount/Payment_currency are correct//process payment//NameValueCollection coll;
Load Form variables into NameValueCollection variable.
//coll = Request.Form;
Get names of all forms into a string array.
//String[] requestItem = coll.AllKeys;//for (var i = 0; i < requestItem.Length; i++)//{//WriteError(requestItem[i] + ":" + Request.Form[requestItem[i]]);//}//卖方邮箱
string receiver_email = request.Form["receiver_email"].ToString();//交易状态
string payment_status = request.Form["payment_status"].ToString();//交易号
string txn_id = request.Form["txn_id"].ToString();string mc_gross = request.Form["mc_gross"].ToString();//金额
string mc_currency = request.Form["mc_currency"].ToString();//货币//DateTime Payment_date = Convert.ToDateTime(request.Form["payment_date"].ToString());//交易时间
DateTime Payment_date =DateTime.Now;//订单号
string custom = request.Form["custom"].ToString();if (payment_status == "Completed")
{PaypalWayPayPO model= PayWayServer.PaypalWayPay.Find(x => x.Txn_id ==txn_id.Trim());var order = OrderServer.Order.Find(x => x.OrderNo ==custom.Trim());
PaypalTradeWayPayServer pay = newPaypalTradeWayPayServer();bool verity = model.Payment_status == payment_status && receiver_email == myEmail && (order.TotalAmount.Value + order.TotalFreight.Value).ToString() == mc_gross && pay.ChangeCurrency(order.CurrencyCode.Trim()) ==mc_currency;if(verity)
{
pay.UpdatePaypalData(Payment_date, custom, txn_id, payment_status);
}}
}else if (verificationResponse.Equals("INVALID"))
{string custom1 = request.Form["custom"].ToString();
ErrorLogPO model= newErrorLogPO();
model.KID=Guid.NewGuid();
model.ErrorState=EumErrorLogType.Paypal;
model.ErrorMessage= "Paypal支付订单号为:" + custom1 + "状态无效";
model.OperaTime=DateTime.Now;
model.Del= false;
OfferServer.ErrorLog.AddErrorLog(model);
}else{
ErrorLogPO model= newErrorLogPO();
model.KID=Guid.NewGuid();
model.ErrorState=EumErrorLogType.Paypal;
model.ErrorMessage= "Palpal请求失败";
model.OperaTime=DateTime.Now;
model.Del= false;
OfferServer.ErrorLog.AddErrorLog(model);
}
}catch(Exception ex)
{
ErrorLogPO model= newErrorLogPO();
model.KID=Guid.NewGuid();
model.ErrorState=EumErrorLogType.Paypal;
model.ErrorMessage=ex.Message;
model.OperaTime=DateTime.Now;
model.Del= false;
OfferServer.ErrorLog.AddErrorLog(model);
}
}