一种检查是否进行了贝宝交易的方法

本文关键字:交易 方法 一种 是否 检查 | 更新日期: 2023-09-27 18:01:09

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

基本上,我试图实现的是创建一个用户发送资金的页面和一个检查付款的页面。

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

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

有人能给我指路吗?

提前感谢,

一种检查是否进行了贝宝交易的方法

我相信您正在寻找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
        }
    }
}