paypal html5 支付,H5网站接入Paypal支付接口

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值