1

我正在尝试在我的 asp.net 项目中找到一种实现 paypal api 的方法。

基本上,我想要实现的是创建一个用户汇款的页面和一个检查付款的页面。

我找不到检查付款的方法。

这是我项目中的一个重要步骤,因为我需要检查是否付款以便将一些数据插入 SQL 数据库,并且我不希望使用付款/检查方法来利用数据库。

有人可以指导我吗?

提前致谢,

4

1 回答 1

3

我相信您正在寻找PayPal IPN(即时付款通知)。它正是为此目的而设计的。

本质上,PayPal 将调用您网站上的特殊页面/处理程序,并断言交易已成功完成,并且可以安全地向您的用户发布产品。由于握手发生的方式,您可以信任此调用。

示例 IPN 处理程序

这段代码直接来自PayPal 文档,但我自己实现了它的一个变体,并且可以验证它是否“像宣传的那样”工作。

using System;
using System.IO;
using System.Text;
using System.Net;
using System.Web;

public partial class csIPNexample : System.Web.UI.Page
{
    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);
        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
        }
        else if (strResponse == "INVALID")
        {
            //log for manual investigation
        }
        else
        {
            //log response/ipn data for manual investigation
        }
    }
}
于 2013-02-26T17:42:39.840 回答