我正在尝试在我的asp.net项目中找到一种实现paypal api的方法。
基本上,我想要实现的是创建一个用户发送资金的页面和一个检查付款的页面。
我无法找到检查付款方式。
这是我项目中的一个重要步骤,因为我需要检查付款是否是为了将一些数据插入SQL数据库而我不希望使用付款/检查方法来利用数据库。
有人可以指导我吗?
提前致谢,
答案 0 :(得分:3)
我相信你正在寻找PayPal IPN (Instant Payment Notification)。它的目的正是为了这个目的。
基本上,PayPal会在您的网站上调用特殊页面/处理程序,并断言交易已成功完成,并且可以安全地向您的用户发布产品。由于握手的发生方式,您可以信任此呼叫。
此代码直接来自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
}
}
}