[英]Trouble connecting DB using Paypal's IPN, C#
public partial class ipn : System.Web.UI.Page
{
string connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString();
String fname, lname, mc_gross, Pmtcurrency, Payer_email;
string receiver_email, payment_date, payment_status;
string txn_type, amount;
string payer_id, txn_id;
string Verify_sign;
string result;
protected void Page_Load(object sender, EventArgs e)
{
//Post back to either sandbox or live
string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
string strLive = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
string ipnPost = strRequest;
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
//Send the request to PayPal and get the response
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
// logging ipn messages... be sure that you give write
// permission to process executing this code
string logPathDir = ResolveUrl("Messages");
string logPath = string.Format("{0}\\{1}.txt",
Server.MapPath(logPathDir), DateTime.Now.Ticks);
File.WriteAllText(logPath, ipnPost);
if (strResponse == "VERIFIED")
{
result = "VERIFIED";
fname = Convert.ToString(HttpContext.Current.Request.Form["first_name"]);
lname = Convert.ToString(HttpContext.Current.Request.Form["last_name"]);
mc_gross = Convert.ToString(HttpContext.Current.Request.Form["mc_gross"]);
Pmtcurrency = Convert.ToString(HttpContext.Current.Request.Form["mc_currency"]);
Payer_email = Convert.ToString(HttpContext.Current.Request.Form["payer_email"]);
Payer_email = Payer_email.Replace("%40", "@");
txn_id = Convert.ToString(HttpContext.Current.Request.Form["txn_id"]);
payer_id = Convert.ToString(HttpContext.Current.Request.Form["payer_id"]);
amount = Convert.ToString(HttpContext.Current.Request.Form["payment_gross"]); ;
payment_date = Convert.ToString(HttpContext.Current.Request.Form["payment_date"]); ;
payment_date = payment_date.Replace("%3A", ":");
payment_date = payment_date.Replace("+", " ");
payment_date = payment_date.Replace("%2C", " ");
txn_type = Convert.ToString(HttpContext.Current.Request.Form["txn_type"]); // 'cart'
Verify_sign = "ipn";
payment_status = Convert.ToString(HttpContext.Current.Request.Form["payment_status"]); ;
receiver_email = Convert.ToString(HttpContext.Current.Request.Form["receiver_email"]);
receiver_email = receiver_email.Replace("%40", "@");
if (payment_status.Equals("Completed"))
{
/*
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "UPDATE table SET memPhone = @memPhone WHERE uName='testName'";
command.Parameters.AddWithValue("@memPhone", payment_status);
connection.Open();
command.ExecuteNonQuery();
connection.Dispose();
}
*/
}
else if (payment_status.Equals("Denied") || payment_status.Equals("Failed") || payment_status.Equals("Refunded") || payment_status.Equals("Reversed") || payment_status.Equals("Voided"))
{
}
else if (payment_status.Equals("In-Progress") || payment_status.Equals("Pending") || payment_status.Equals("Processed"))
{
}
else if (payment_status.Equals("Canceled_Reversal"))
{
}
else
{
}
}
else if (strResponse == "INVALID")
{
//log for manual investigation
}
else
{
//log response/ipn data for manual investigation
}
}
}
當Payment_status.Equals(“ Completed”)分支中存在IPN處理程序(或任何其他IPN實現)時,該IP處理程序不起作用:
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "UPDATE table SET memPhone = @memPhone WHERE uName='testname'";
command.Parameters.AddWithValue("@memPhone", payment_status);
connection.Open();
command.ExecuteNonQuery();
connection.Dispose();
}
當此sql命令代碼不存在時,IPN處理程序和實現即可工作,但我顯然需要它來更新數據庫。
我已將此代碼放置在所有其他if分支中(“拒絕”,“進行中”,“ Canceled_Reversal”),並且沒有任何效果,因此我知道IPN正在正確的“ Completed”分支中查找,而不在其他分支中查找。
有人知道發生這種情況的特定Paypal原因,還是在這里看到代碼有問題?
順便說一句,我知道我正在嘗試將“ completed”傳遞給memPhone(這是一個varchar),這沒有任何意義。 我只是用它來測試SQL是否正常工作,因為兩者都是字符串。
不幸的是,我無法在Visual Studio中調試它
是的你可以。 您可以根據需要模擬簡單或完整的請求,以便在內部逐步執行代碼。 您可以將原始有效負載“保存”到某個地方(甚至只是通過電子郵件發送給自己),然后對其進行模擬。 還有其他工具-例如
使用PayPal的IPN模擬器 -盡管這不如處理您收到的實際數據...
作為上述過程的替代方案,您可能可以使用Requestb.in或類似的服務,以便您可以獲取要使用的原始數據的快照並模擬請求。
獲得原始數據后,您可以選擇以任何方式模擬POST(例如, curl
到在調試模式下運行應用程序的本地框)。
樣本IPN模擬器POST到Requestb.in 。 如果示例鏈接已過期(截至本文發布48小時后),請參見以下快照:
一般來說,僅基於以上發布/評論,您的問題就集中在您的SQL流程上。 根據建議,您應該try/catch
因為對建議的答案似乎只是您的假設。 您的catch
可能只是通過電子郵件將異常消息發送給您,然后您獲得了更多詳細信息(也許不必進行上述所有嘲弄)。
心連心...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.