Working IPN Handler with Parallel Payments – PayPal Adaptive Payments

Hey everyone,

Just a dummy implementation of a Parallel Payment and an accompanying IPN Handler using the C#/.NET SDK. Feel free to use it however you like.

  1. //Handles PayPal IPN
  2.         public String IPN()
  3.         {
  4.             //Post back to either sandbox or live
  5.             string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
  6.             //string strLive = "https://www.paypal.com/cgi-bin/webscr";
  7.             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
  8.  
  9.             ////Set values for the request back
  10.             req.Method = "POST";
  11.             req.ContentType = "application/x-www-form-urlencoded";
  12.             byte[] param = Request.BinaryRead(Request.ContentLength);
  13.             string strRequest = Encoding.ASCII.GetString(param);
  14.             strRequest += "&cmd=_notify-validate";
  15.             req.ContentLength = strRequest.Length;
  16.  
  17.             //Send the request to PayPal and get the response
  18.             StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
  19.             streamOut.Write(strRequest);
  20.             streamOut.Close();
  21.             StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
  22.             string strResponse = streamIn.ReadToEnd();
  23.             streamIn.Close();
  24.  
  25.             if (strResponse == "VERIFIED")
  26.             {
  27.                 //check the payment_status is Completed
  28.                 //check that txn_id has not been previously processed
  29.                 //check that receiver_email is your Primary PayPal email
  30.                 //check that payment_amount/payment_currency are correct
  31.                 //process payment
  32.             }
  33.             else if (strResponse == "INVALID")
  34.             {
  35.                 //log for manual investigation
  36.             }
  37.             else
  38.             {
  39.                 //log response/ipn data for manual investigation
  40.             }
  41.  
  42.  
  43.             return "";
  44.         }
  45.  
  46.         //Pay for an order
  47.         public void Pay(int OrderId)
  48.         {
  49.             RequestEnvelope envelopeRequest = new RequestEnvelope();
  50.             envelopeRequest.errorLanguage = "en_US";
  51.             PaySample paySample = new PaySample();
  52.  
  53.             List<Receiver> listReceiver = new List<Receiver>();
  54.             // Amount to be credited to the receiver's account
  55.             Receiver receiverA = new Receiver(Convert.ToDecimal("4.00"));
  56.  
  57.             // A receiver's email address
  58.             receiverA.email = "test_buyer1@learnerlessons.com.au";
  59.             listReceiver.Add(receiverA);
  60.  
  61.             // Amount to be credited to the receiver's account
  62.             Receiver receiverB = new Receiver(Convert.ToDecimal("2.00"));
  63.  
  64.             // A receiver's email address
  65.             receiverB.email = "test_buyer2@learnerlessons.com.au";
  66.             listReceiver.Add(receiverB);
  67.  
  68.             ReceiverList receiverList = new ReceiverList(listReceiver);
  69.  
  70.             PayRequest requestPay = new PayRequest(envelopeRequest, "PAY", "http://localhost:53034/orders/cancel", "AUD", receiverList, "http://localhost:53034/orders/return");
  71.             requestPay.reverseAllParallelPaymentsOnError = true;
  72.             requestPay.ipnNotificationUrl = "http://123.123.123.123/Orders/IPN";
  73.  
  74.             //Send request to paypal, retrieve payKey
  75.             PayResponse payResponse = paySample.PayAPIOperations(requestPay);
  76.  
  77.             Response.Redirect("https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-payment&paykey=" + payResponse.payKey);
  78.         }

2 thoughts on “Working IPN Handler with Parallel Payments – PayPal Adaptive Payments

  1. Max

    Hi there, I am implementing this into my ASP.NET I am wondering how do I do this.. the Docs are badly explained in paypal site and its hard to understand it. there is a PaySample(); and im getting errors how do I fix that? as well what changes need to be done on the web.config file.

    Thanks, this is for a school project.

    Reply
  2. Chris Owens Post author

    Hey Max,

    You’re definitely right about the docs. This example is for parallel payments, do you definitely need that? If not I’d probably recommend using the JSON API – it looks a lot nicer from what I’ve seen.

    Cheers,
    Chris

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.