我正在解释我实施的方式。它可能因人而异。
- 首先,您必须在开发人员 paypal api上创建一个帐户。帐户。
- 然后创建两个测试帐户,一个用于客户,另一个用于业务(商家)。
在web.config文件中声明这些值。
<appSettings>
<!--these keys are for Paypal-->
<add key="paypalURL" value="https://www.sandbox.paypal.com" />
<add key="paypalAccount" value="arshad_Mer_biz@gmail.com" />
<add key="websiteUrl" value="http://www.yourstie.com" />
</appSettings>
现在您必须相应地设置 paypal html 变量,有关详细信息Paypal 变量
在button_click事件中编写以下代码
string redirectUrl = ConfigurationManager.AppSettings["paypalURL"]+"/cgi-bin/webscr?cmd=_xclick";
string sellersEmail = "&business=";
string buyersEmail = "&email=";
string productName = "&item_name=";
string amount = "&amount=";
string shippingOption = "&no_shipping=";
string noteOpton = "&no_note=";
string returnUrl = "&return=";
string cancelUrl = "&cancel_return=";
string rmOption = "&rm=";
string notifyUrl = "¬ify_url=";
string custom = "&custom=";
// Merchant account
sellersEmail += ConfigurationManager.AppSettings["paypalAccount"];
//calling a method that will return current user email id.
buyersEmail += GeneralClass.GetUserEmail();
//optional value if you want to carry
custom += GeneralClass.GetUseriD();
productName += lblProdeutName.Text;
amount +=lblAmount.Text ;
shippingOption += "1"; //1 means no shipping option;
noteOpton += "1"; //1 means no note option;
rmOption += "1";
returnUrl +=ConfigurationManager.AppSettings["websiteUrl"]+"/PaypalThankYou.aspx";
cancelUrl +=ConfigurationManager.AppSettings["websiteUrl"] + "/PaypalCancel.aspx";
notifyUrl += ConfigurationManager.AppSettings["websiteUrl"] + "/PaypalNotifyUrl.aspx";
redirectUrl += sellersEmail + buyersEmail + productName + amount + shippingOption + noteOpton + returnUrl + cancelUrl + notifyUrl + rmOption + custom;
Response.Redirect(redirectUrl);
取消页面:- 如果用户从 paypal 取消交易。它也适用于 localhost。
感谢页面:付款后paypal会跳转到该页面。
通知网址:有时称为 IPN(即时付款通知)。这是您将从贝宝获得价值的地方。它仅适用于托管页面。它不适用于本地主机
Notifyurl页面的代码
添加这些namespaces
:
using System.Net;
using System.IO;
using System.Text;
using System.Collections.Specialized;
在page_load
:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Post back to either sandbox or live
string strURL =ConfigurationManager.AppSettings["paypalURL"]+ "/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strURL);
//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 pay_stat = these_argies["payment_status"];
//.
//. more args as needed look at the list from paypal IPN doc
//.
if (pay_stat.Equals("Completed"))
{
//inserting the database
int intUserID;
int.TryParse(these_argies["custom"],out intUserID);
objUserEntity.UserID=intUserID;
objPapalPayment.strTransactionID = these_argies["txn_id"];
objPapalPayment.dblPaymentAmount = Convert.ToDouble(these_argies["payment_gross"]);
objPapalPayment.strBuyerMail = these_argies["payer_email"];
objPapalPayment.dtmDateAppliedOn = DateTime.Now;
objPapalPayment.blnIsGlobalAdvertisement = true;
objSubscription.blnIsPaid = false;
objSubscription.blnSubscriptionWithHeld = true;
objUserFunction.AddPaypalPayment(objUserEntity, objPapalPayment, objSubscription, enmSubscriptionType.Global);
}
// 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
}
}
}
那么你将如何在 localhost 上进行调试呢?使用价值rm
=2;并将相同的代码粘贴到感谢页面的 page_load 事件中。它会起作用的。
更多详情:
http: //www.codeproject.com/Articles/42894/Introduction-to-PayPal-for-C-ASP-NET-developers
Paypal 变量及其用法
希望对您有所帮助。