这是我的设置,我会尽量保持代码简短。这是一个ASP.NET MVC4应用程序
我在这里设置信息:
@Html.Hidden("cmd", Model.PayPal.Cmd)
@Html.Hidden("business", Model.PayPal.Business)
@Html.Hidden("return", Model.PayPal.Return)
@Html.Hidden("cancel_return", Model.PayPal.CancelUrl)
@Html.Hidden("notify_url", Model.PayPal.NotifyUrl)
@Html.Hidden("currency_code", Model.PayPal.CurrencyCode)
@Html.Hidden("item_name", Model.PayPal.PlanName)
@Html.Hidden("item_number", Model.Id)
@Html.Hidden("src", Model.PayPal.AutoRecurring)
@Html.Hidden("a3", Model.PayPal.Price)
@Html.Hidden("p3", Model.PayPal.Interval)
@Html.Hidden("t3", Model.PayPal.IntervalType)
@Html.Hidden("txn_type", "subscr_signup")
<input type="image" name="submit"
src="https://www.paypalobjects.com/en_US/i/btn/btn_subscribe_LG.gif"
alt="PayPal - The safer, easier way to pay online">
<img alt="" border="0" width="1" height="1"
src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif">
我在控制器中设置变量:
PayPal paypal = new PayPal();
bool useSanbox = true;
if (useSanbox)// for test
paypal.ActionUrl = "https://www.sandbox.paypal.com/cgi-bin/webscr";
else//real
paypal.ActionUrl = "https://www.paypal.com/cgi-bin/webscr";
paypal.Cmd = "_xclick-subscriptions";
paypal.Business = "david_1355300634_biz@domain.com";
paypal.CancelUrl = "http://localhost:25914/home/";
paypal.Return = "http://localhost:25914/home/ipn";
paypal.NotifyUrl ="http://localhost:25914/home/ipn";
paypal.AutoRecurring = "1";
paypal.Price = ctx.SubscriptionPlans.First(x => x.Name == signInModel.SubscritionPlan).Price.ToString();
paypal.Interval = "1";
paypal.IntervalType = "M";
paypal.CurrencyCode = "USD";
paypal.PlanName = signInModel.SubscritionPlan;
paypal.Amount = ctx.SubscriptionPlans.First(x => x.Name == signInModel.SubscritionPlan).Price.ToString();
然后我有这个IPN控制器动作:
//Answer from PayPal
public ActionResult IPN()
{
var signInModel = Session["SignUp"] as SignUp;
//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(this.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
//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")
{
//any param from form
var text = Request.Form["custom"];
ctx.SignIns.Add(signInModel);
ctx.SaveChanges();
//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
return View("RegistrationConfirmation", signInModel);
}
else if (strResponse == "INVALID")
{
//log for manual investigation
return View("SignUp", signInModel);
}
else
{
//log response/ipn data for manual investigation
}
//change view
return View("SignUp", signInModel);
}
让我失望的是我被罚款到paypal沙盒,我与测试用户登录,确认付款金额,然后点击返回该网站。返回命中我的IPN操作,但我得到它是“无效”的响应。我在这里错过了一个变量吗?我对api比较陌生。
答案 0 :(得分:5)
另一个,cmd = _notify-validate应该是第一个需要发送的参数值。
根据官方文档
“验证您的响应是否包含完全相同的IPN变量和值 订单,以cmd = _notify-validate开头。“
答案 1 :(得分:3)
你试图在本地测试这个,好吧,paypal并不知道localhost。你必须上传代码并使用实时网址
进行测试