简体   繁体   中英

Paypal IPN sample code has no ActionResult. How am i meant to give a url if there is no view?

This is paypals sample code for the ipn. I have been on it for a few days now and am getting no where. I am not sure what i am meant to call. In a previous question i asked some said you dont call the Receive method. So this then lead me on to think how will Paypal know where to go. (ie what method to use.)

public class IPNController : Controller
{
    [HttpPost]
    public HttpStatusCodeResult Receive()
    {
        //Store the IPN received from PayPal
        LogRequest(Request);

        //Fire and forget verification task
        Task.Run(() =>  VerifyTask(Request));

        //Reply back a 200 code
        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }

    private void VerifyTask(HttpRequestBase ipnRequest)
    {
        var verificationResponse = string.Empty;

        try
        {
            var verificationRequest = (HttpWebRequest)WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr");

            //Set values for the verification request
            verificationRequest.Method = "POST";
            verificationRequest.ContentType = "application/x-www-form-urlencoded";
            var param = Request.BinaryRead(ipnRequest.ContentLength);
            var strRequest = Encoding.ASCII.GetString(param);

            //Add cmd=_notify-validate to the payload
            strRequest = "cmd=_notify-validate&" + strRequest;
            verificationRequest.ContentLength = strRequest.Length;

            //Attach payload to the verification request
            var streamOut = new StreamWriter(verificationRequest.GetRequestStream(), Encoding.ASCII);
            streamOut.Write(strRequest);
            streamOut.Close();

            //Send the request to PayPal and get the response
            var streamIn = new StreamReader(verificationRequest.GetResponse().GetResponseStream());
            verificationResponse = streamIn.ReadToEnd();
            streamIn.Close();

        }
        catch ( Exception exception)
        {
            //Capture exception for manual investigation
        }

        ProcessVerificationResponse(verificationResponse);
    }


    private void LogRequest(HttpRequestBase request)
    {
        // Persist the request values into a database or temporary data store
    }

    private void ProcessVerificationResponse(string verificationResponse)
    {
        if (verificationResponse.Equals("VERIFIED"))
        {
            // check that Payment_status=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
        }
        else if (verificationResponse.Equals("INVALID"))
        {
            //Log for manual investigation
        }
        else
        {
            //Log error
        }
    }
}

There is no ActionResult so how am i meant to give a URL. I thought of this but it seems to return invalid

public ActionResult IPN() {
    Receive()


}

I have asked previous questions but still cant seem to get my head around this IPN.

For anyone that sees this, this was an inexperienced me and all i had to do was return Ok() so my action result would look like the below:

public ActionResult IPN() 
{
    Receive();  
    return Ok();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM