Is there any sample for PayPal IPN

后端 未结 5 1133
半阙折子戏
半阙折子戏 2021-02-05 20:47

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

5条回答
  •  难免孤独
    2021-02-05 21:26

    Extending Michal Hosala's answer, there are two things needed to get a successful handshake with PayPal

    First, setting the security protocol before making request to PayPal

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    

    Second, avoiding the dictionary because for verification, PayPal requires the data to be posted back in the same order and preceded by the cmd variable. I ended up doing this

    Request.InputStream.Seek(0, SeekOrigin.Begin);
    string rawRequestBody = new StreamReader(Request.InputStream).ReadToEnd();
    var ipnVarsWithCmd = rawRequestBody.Split('&').Select(x => new KeyValuePair(x.Split('=')[0], x.Split('=')[1])).ToList();
    ipnVarsWithCmd.Insert(0, new KeyValuePair("cmd", "_notify-validate"));
    

提交回复
热议问题