📄 pdthandler.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 PDTHandler : System.Web.UI.Page
{
void Page_Load(object sender, EventArgs e)
{
string ppTX = Request.QueryString["tx"].ToString();
string sOrderID = Request.QueryString["cm"].ToString();
string sAmount = Request.QueryString["amt"].ToString();
string status = Request.QueryString["st"].ToString();
//log this
Log.Write("PDT", "消息", "订单: " + sOrderID, sOrderID, "");
string pdtResponse = GetPDT(ppTX);
//all we need at this point is the SUCCESS flag
if(pdtResponse.StartsWith("SUCCESS")){
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 (dPaidFor>=dTotal) {
OrdersManager.CommitOrder(orderID, ppTX);
//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_ECHECK);
}
Log.Write("PDT", "消息", "订单 " + sOrderID, sOrderID, order.UserName);
//send them to the receipt page
Response.Redirect(Utility.GetSiteRoot() + "/receipt.aspx?t=" + ppTX);
} else {
//set as invalid\
//commit the order, then invalidate it
OrdersManager.CommitOrder(orderID, ppTX);
OrdersManager.UpdateStatus(orderID, OrdersManager.INVALID_TOTALS_DONT_MATCH);
Log.Write("PDT", "警告", "订单无效 (须付款:"+dPaidFor.ToString("c")+"; 剩余款: "+dTotal.ToString("c"), sOrderID, order.UserName);
//TODO: Send an email to notify customer as well as store owner of an Invalid Payment
}
}
}
string GetPDTValue(string pdt, string key) {
string[] keys = pdt.Split('\n');
string thisVal = "";
string thisKey="";
foreach (string s in keys) {
string[] bits = s.Split('=');
if (bits.Length > 1) {
thisVal = bits[1];
thisKey = bits[0];
if (thisKey.ToLower().Equals(key))
break;
}
}
return thisVal;
}
string GetPDT(string transactionID)
{
string sOut = "";
string PDTID = "";
try {
PDTID = System.Configuration.ConfigurationSettings.AppSettings["PayPalPDTID"].ToString();
} catch {
}
string sCmd = "_notify-synch";
string serverURL = "";
if (SiteConfiguration.UseSandbox)
{
serverURL = "https://www.sandbox.paypal.com/cgi-bin/webscr";
}
else
{
serverURL = "https://www.paypal.com/cgi-bin/webscr"; ;
}
try
{
string strFormValues = Request.Form.ToString();
string strNewValue;
string strResponse;
// 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-synch&at=" + PDTID + "&tx=" + transactionID;
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();
sOut = Server.UrlDecode(strResponse);
}
catch (Exception x)
{
}
return sOut;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -