【问题标题】:A way of checking if a paypal transaction was made检查是否进行了贝宝交易的一种方法
【发布时间】:2013-02-12 06:57:59
【问题描述】:

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

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

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

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

有人可以指导我吗?

提前致谢,

【问题讨论】:

标签: c# asp.net paypal


【解决方案1】:

我相信您正在寻找PayPal IPN (Instant Payment Notification)。它正是为此目的而设计的。

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

示例 IPN 处理程序

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

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
        }
    }
}

【讨论】:

  • 我知道这个示例,但有些东西我无法理解。这个页面是用户付款后被重定向的页面?页面将从 url 获取所需的数据?
  • 实际上,用户从来没有看到过这个页面。它仅用于 PayPal 通知您,以便您执行付款后操作。我相信如果您将用户重定向到 PayPal 的网站以接收付款信息(即不在您自己的网站上收集),则需要 IPN 处理程序。
  • 所以,他付款后,paypal会打电话给我的页面,并把付款的ipn贴出来,这样我就可以在页面加载中查看付款了?
  • 1) 用户在贝宝的网站上付款。 2) 贝宝向您的网站(IPN 页面)发送一条独立于用户的消息。这通常会在几秒钟内发生,但并不总是立即发生。 3) 一旦你收到那条消息,你就知道交易完成了。
  • 您可以将一些隐藏字段与提交给 PayPal 的初始请求一起传递,然后将通过 IPN 消息将其发送回给您。我查看了我几年前的一些代码,该字段只是称为“自定义”,您可以在其中放入任何您想要的内容。
猜你喜欢
  • 2016-12-02
  • 2016-01-04
  • 2016-09-10
  • 2012-08-31
  • 2014-11-20
  • 2014-01-29
  • 2013-09-15
  • 2015-10-13
  • 1970-01-01
相关资源
最近更新 更多