これが私のセットアップです。コードを短く保つようにします。これは 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 アクションにヒットしますが、「INVALID」という応答が返されます。ここに変数がありませんか?私はAPIに比較的慣れていません。