Reputation: 3630
Here is my set up, I will try to keep the code short. This is an ASP.NET MVC4 application
I am setting the information here:
@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">
I set the variables in the controller:
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 = "[email protected]";
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();
Then I have this IPN Controller action:
//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);
}
The thing that is throwing me off is I get routed to the paypal sandbox fine, I log in with a test user, confirm the payment amount, and then click return to the site. Returning hits my IPN action, but I get the response that it is "INVALID". Am I missing a variable here or something? I am relatively new to the api.
Upvotes: 1
Views: 1737
Reputation: 902
Another one, cmd=_notify-validate should be the first param value that needs to be sent.
As per the official docs
"Verify that your response contains exactly the same IPN variables and values in the same order, preceded with cmd=_notify-validate."
Upvotes: 5
Reputation: 2617
you're trying to test this locally, well, paypal doesnt' know about localhost. you have to upload the code and test it using the live url
Upvotes: 3