Chris Cooper
Chris Cooper

Reputation: 389

C# paypal IPN communication

I've set up the IPN to communicate with my website within Paypal, but for some reason paypal does not seem to call it. Below is the method that is listening, but its does not seem to get anything.

Could someone please point me in the write direction?

Thanks

[HttpPost]
    public void paypalipn(string receiver_email, string receiver_id, string test_ipn, string txn_id, string payer_id, string payer_status, string payment_date, string payment_status, string payment_type)
    {
        string subject = "Audit - Paypal IPN ";
        string buildemail = "receiver_email: " + receiver_email;
        buildemail += "receiver_id: "+receiver_id;
        buildemail += "<br>test_ipn: "+test_ipn;
        buildemail += "<br>txn_id: " + txn_id;
        buildemail += "<br>payer_id: " + payer_id;
        buildemail += "<br>payer_status: " + payer_status;
        buildemail += "<br>payment_date: " + payment_date;
        buildemail += "<br>payment_status: " + payment_status;
        buildemail += "<br>payment_type: " + payment_type;

        Libraries.Email.Product.SendAudit(subject, buildemail);



    }

Upvotes: 0

Views: 417

Answers (3)

Henke
Henke

Reputation: 302

I did something like this:

public class IPNData
{
    public string Response { get; set; }
    public NameValueCollection Args { get; set; }
}

Then a method for collecting data from the request, and sends the data to Paypal for verification.

IPNData GetIPNData()
{
    var ipnData = new IPNData();
    var param = Request.BinaryRead(HttpContext.Request.ContentLength);
    var ipnStr = Encoding.ASCII.GetString(param);
    ipnData.Args = HttpUtility.ParseQueryString(ipnStr);

    // Return the ipn string to paypal for validation
    ipnStr += "&cmd=_notify-validate";

    var request = (HttpWebRequest)WebRequest.Create("https://www.paypal.com/cgi-bin/webscr");
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = strRequest.Length;

    using (var sw = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
    {
        sw.Write(ipnStr);
    }

    using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))
    {
        // Response should be "VERIFIED"
        ipnData.Response = sr.ReadToEnd();
    }

    return ipnData;
}

PaymentController.cs example for a subscriptionpayment, or if a user cancel a subscription.

public ActionResult PaypalIPNExample()
{
    var ipn = GetIPNData();
    if (ipn.Response != "VERIFIED")
    {
        // Do some logging
        return null;
    }

    var type = ipn.Args["txn_type"];
    var email = ipn.Args["payer_email"];
    var transactionId = ipn.Args["txn_id"];

    switch (type)
    {
        case "subscr_cancel":
            UserService.CancelSubscription(email);
            break;
        case "subscr_payment":
            if (ipn.Args["payment_status"].Equals("Completed"))
            {
                LogPayment(email, transactionId);
                UserService.ExtendMembership(email);
            }
            break;
        default:
            // Do some logging?
            break;                    
    }

    return Json("ok");
}

Upvotes: 1

Big Gunz
Big Gunz

Reputation: 31

When I was debugging PayPal IPN responses, I did something like this:

string keysReceived = "";
foreach (string key in Request.Params.AllKeys)
   keysReceived += "\r\nKey: " + key + ", Val: " + Request.Params[key];
string debugLogPath = HttpContext.Server.MapPath("/") + "debug_PayPalNotify.log";
System.IO.File.AppendAllText(debugLogPath, keysReceived);

Note the use of the global object Request. You can do a lot with that for debugging.

Upvotes: 0

PP_MTS_Chad
PP_MTS_Chad

Reputation: 7319

If you are setting your IPN in your PayPal account, make sure that it is enabled and that the URL is correct. Also, if you are setting it in your account you can check your IPN history to see if the IPN POSTs are being sent out. They will either be marked as sent, retrying, or failed. If they are in a failed or retrying status you can click the message and if your server is sending back any error code it should be listed here. Also, check your error logs on your server to make sure the script is not erroring out.

Upvotes: 1

Related Questions