使用 C# 设置 PayPal 即时付款通知 (IPN)
用于执行 IPN 的工作 C# 示例代码
引言
我想从我的网站上销售一些 PDA 应用程序并使用 PayPal 立即购买 按钮,这很容易,在 PayPal 完成交易后,需要向买家发送一封额外的电子邮件,其中包含下载链接。当然,我需要一些特定的 MIME 类型,尽管有一些处理器声称可以收取费用。我无法从他们那里获得关于 MIME 类型和文件更新策略的良好答案。
因此,我决定研究 PayPal 开发人员网站,他们有一些关于如何设置此即时付款通知 (IPN) 的示例。它并不难,并且我可以完全控制我的网站。这是使用 C# 的我的工作代码的摘要,使用不同的后端类似且同样简单。
概述
为了完成此操作,绝对需要 PayPal 沙盒,以便可以调试代码,并且它来自 PayPal 开发人员链接。创建一个买家和一个卖家帐户并进行调试。
本质上,当点击 立即购买 按钮时,PayPal 会进行处理,最后,它会向双方发送一封电子邮件。对于完全自动销售电子媒体的情况,需要向买家发送一封带有特定下载链接的电子邮件,在成功交易之后。使用 IPN,这可以在您的网站上实现,并且非常简单,他们所需要的只是一个“侦听器”。当您列出的商品的交易发生时,他们会向该侦听器发送一则包含交易信息的、带有所有基本信息(如买家/卖家/商品信息)的消息,这些信息是一组 URL 查询变量 (例如,payer_email=jdoe@mailcom&item_name=killer_App ...),然后侦听器回复这些传递的参数以及一个额外的令牌,最后 PayPal 回复一个 VERIFIED 消息(如果一切顺利)或一个失败消息。
这是链接,其中包含来自 PayPal 的 URL 查询变量列表。
C# 中的侦听器
如果您的网站托管支持 .NET,则在 C# 中实现此侦听器非常简单。下面的代码是对 PayPal 示例的修改,添加了一些用于执行电子邮件的基本功能,当然还有在成功交换后如何发送它。为了使其更简单,它被更改为脚本模式,因此您只需要将页面放在您的网站上即可,无需编译项目等等,而且脚本模式更快。在您的 PayPal 配置文件中设置您的侦听器的 URL,如下一节所述。您可能需要在验证整个过程后进行一些额外的处理,例如检查您的本地数据库中的某些参数,但这些只是小细节。
假设我们称此文件为 ipn_pal.aspx
<!--
OK enough of this VB balloney, let's get real although C# is not real it is close
let's use it here to handle it
-->
<%@ Page Language="C#" %>
<%@ Import Namespace = "System"%>
<%@ Import Namespace = "System.IO"%>
<%@ Import Namespace = "System.Text" %>
<%@ Import Namespace = "System.Net" %>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System.Net.Mail" %>
<script Language="JavaScript">
//Some JavaScript you may need goes here
</script>
<script Language="C#" Option="Explicit" runat="server">
void Send_download_link (string from, string to, string subject, string body)
{
try
{ // Construct a new e-mail message
SmtpClient client = new SmtpClient (smtpClient);
client.Send (from, to, subject, body);
}
catch (SmtpException ex)
{
debuggy.Text = "Send_download_link: " + ex.Message;
}
} // --- end of Send_download_link --
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 strResponse_copy = strRequest; //Save a copy of the initial info sent by PayPal
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
//for proxy
//WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
//req.Proxy = proxy;
//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();
if (strResponse == "VERIFIED")
{
//check the payment_status is 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
// pull the values passed on the initial message from PayPal
NameValueCollection these_argies = HttpUtility.ParseQueryString(strResponse_copy);
string user_email = these_argies["payer_email"];
string pay_stat = these_argies["payment_status"];
//.
//. more args as needed look at the list from paypal IPN doc
//.
if(pay_stat.Equals("Completed") )
{
Send_download_link ("yours_truly@mycompany.com",
user_email, "Your order",
"Thanks for your order this the download link ... blah blah blah" );
}
// more checks needed here specially your account number and related stuff
}
else if (strResponse == "INVALID")
{
//log for manual investigation
}
else
{
//log response/ipn data for manual investigation
}
} // --- end of page load --
</script>
<html>
<head runat="server" />
<title>IPN PayPal</title>
<asp:label id="debuggy" runat="server"/>
<h2> my test page</h2>
Load this first to check the syntax of your page
</body>
</html>
这里的 sendmail 最简单。您的网站设置可能允许也可能不允许,但这不会阻止您。
在您的 PayPal 上启用 IPN 并指向侦听器
在您的 Paypal 帐户中,转到 主页->个人资料。您将在“销售偏好”列中看到即时付款通知的链接。启用它并将通知 URL(侦听器)设置为您网站上的路径,例如 http://mycoolwebsite.com/ ipn_pal.aspx。就是这样。
最终想法
如您所见,这很简单。当然,应该使用沙盒卖家/买家帐户进行一系列测试,以确保流程正确。此外,需要进行更多基本检查,尤其是检查您的帐号,并且可能需要进行一些额外的处理,所有这些都可以连接到“侦听器”页面。例如,我需要将买家添加到邮件列表中以向他们发送通知。
历史
- 2010 年 5 月 29 日:初始版本