Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any sample for PayPal IPN

I have an Asp.Net WEB API 2 project and I would like to implement an Instant Payment Notification (IPN) listener controller.

I can't find any example and nuget package. All I need is to acknowledge that the user paid with the standard html button on Paypal. It's quite simple.

All the nuget packages are to create invoice or custom button. It's not what I need

The samples on paypal are for classic asp.net and not for MVC or WEB API MVC

I'm sure somebody did that already and when I started coding I had a feeling that I was reinventing the wheel.

Is there any IPN listener controller example?

At least a PaypalIPNBindingModel to bind the Paypal query.

    [Route("IPN")]
    [HttpPost]
    public IHttpActionResult IPN(PaypalIPNBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest();
        }

        return Ok();
    }

EDIT

So far I have the following code

        [Route("IPN")]
        [HttpPost]
        public void IPN(PaypalIPNBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                // if you want to use the PayPal sandbox change this from false to true
                string response = GetPayPalResponse(model, true);

                if (response == "VERIFIED")
                {

                }
            }
        }

        string GetPayPalResponse(PaypalIPNBindingModel model, bool useSandbox)
        {
            string responseState = "INVALID";

            // Parse the variables
            // Choose whether to use sandbox or live environment
            string paypalUrl = useSandbox ? "https://www.sandbox.paypal.com/"
            : "https://www.paypal.com/";

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(paypalUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

                //STEP 2 in the paypal protocol
                //Send HTTP CODE 200
                HttpResponseMessage response = client.PostAsJsonAsync("cgi-bin/webscr", "").Result;

                if (response.IsSuccessStatusCode)
                {
                    //STEP 3
                    //Send the paypal request back with _notify-validate
                    model.cmd = "_notify-validate";
                    response = client.PostAsync("cgi-bin/webscr", THE RAW PAYPAL REQUEST in THE SAME ORDER ).Result;

                    if(response.IsSuccessStatusCode)
                    {
                        responseState = response.Content.ReadAsStringAsync().Result;
                    }
                }
            }

            return responseState;
        }

but for the step 3 I tried to post my model as json but paypal returns a HTML page instead of VALIDATED or INVALID. I figured out that I have to use application/x-www-form-urlencoded and it the parameters as to be in the same order.

How can I get the request URL?

I would use the query Url and add &cmd=_notify-validate to it

like image 985
Marc Avatar asked Oct 29 '14 19:10

Marc


People also ask

How do I get a PayPal IPN?

Click the settings icon at the top of your PayPal account page and then click Account Settings. On the Notifications page, click the Update link for the Instant payment notifications item. Click Choose IPN Settings to specify your listener's URL and activate the listener.

What are PayPal IPN messages?

Instant Payment Notification (IPN) is a message service that automatically notifies merchants of events related to PayPal transactions. Merchants can use it to automate back-office and administrative functions, including automatically fulfilling orders and providing customers with order status.

Can you have more than one IPN on PayPal?

PayPal only permits one IPN per account, so if you are already using your PayPal IPN (as part of your own checkout system for example) you may need to set up a relay to forward the IPN notification on to ChartMogul. A webhook relay will allow you to use your PayPal IPN in multiple locations.


1 Answers

There is an official c# example here: https://github.com/paypal/ipn-code-samples in path \c#\paypal_ipn_mvc.cs

The C# example shows an ASP.NET MVC controller with an action that responds to the IPN.

like image 93
Hakakou Avatar answered Oct 25 '22 18:10

Hakakou