📄 ipnhandler.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
public partial class IPNHandler : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ppTX = Request.Form["txn_id"].ToString();
string sOrderID = Request.Form["custom"].ToString();
string sAmount = Request.Form["mc_gross"].ToString();
string status = Request.Form["payment_status"].ToString();
//log the incoming IPN
Log.Write("IPN", "消息", "订单: " + sOrderID, sOrderID, "");
//all we need at this point is the SUCCESS flag
if (VerifyIPN()) {
//get the amount
if (sAmount != string.Empty) {
double dPaidFor = double.Parse(sAmount);
//make sure the totals add up
int orderID = Convert.ToInt16(sOrderID);
Order order = OrdersManager.GetOrderByID(orderID);
double dTotal = order.OrderSubTotal + order.Tax + order.Shipping;
if (dTotal >= dPaidFor) {
OrdersManager.CommitOrder(orderID, ppTX);
Log.Write("IPN", "消息", "订单 " + sOrderID, sOrderID, order.UserName);
//if your PayPal account is setup to accept eChecks,
//the payment will come back with a status of "Pending"
//if it's pending, mark the order as such
//the IPN will fire the order when the check clears
if (status.ToLower().Equals("pending")) {
OrdersManager.UpdateStatus(orderID, OrdersManager.WAITING_ON_PAYPAL_PAYMENT_ID);
}
} else {
//set as invalid
OrdersManager.UpdateStatus(orderID, OrdersManager.INVALID_TOTALS_DONT_MATCH);
Log.Write("IPN", "警告", "订单无效 " + sOrderID, sOrderID, order.UserName);
//TODO: Send an email to notify customer as well as store owner of an Invalid Payment
}
}
}
}
bool VerifyIPN() {
string strFormValues = Request.Form.ToString();
string strNewValue;
string strResponse;
string serverURL = "";
if (SiteConfiguration.UseSandbox) {
serverURL = "https://www.sandbox.paypal.com/cgi-bin/webscr";
} else {
serverURL = "https://www.paypal.com/cgi-bin/webscr"; ;
//serverURL="http://www.eliteweaver.co.uk/cgi-bin/webscr ";
}
// Create the request back
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(serverURL);
// Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
strNewValue = strFormValues + "&cmd=_notify-validate";
req.ContentLength = strNewValue.Length;
// Write the request back IPN strings
StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(strNewValue);
stOut.Close();
// Do the request to PayPal and get the response
StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();
// Confirm whether the IPN was VERIFIED or INVALID. If INVALID, just ignore the IPN
return strResponse == "VERIFIED";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -