Simple way to integrate PayPal into your C# web app - Part 3

Oct 06, 2015 by BogdanD

Last month we took a look at how to integrate PayPal's Express Checkout control into our C# application. At that point we were only able to send data to PayPal'a API to generate transaction tokens for future use, so now it's time to finalize the transaction and request payment from the PayPal API.

Do you remember the accounts that you've setup up at the beginning of this series? Well, after this article, you'll want to log into your master account and check the balance, because this final call to the PayPal API is where the good stuff happens. Keeping in theme with our previous C# sample, the data that we'll send is hardcoded into the app, in order to keep things simple.

All you have to do is to follow the comments and the C# code, and leave a reply if you feel that something needs to be more detailed :)

protected void btnConfirmaPayPalPurchase_Click(object sender, EventArgs e)
  {
      // First thing's first. We'll need to get the token and payer ID returned from the previous call:
      if (string.IsNullOrEmpty(Request.QueryString["token"]) || string.IsNullOrEmpty(Request.QueryString["PayerID"]))
      {
          lblError.Visible = true;
          return;
      }
          
      // Than we add the tokens to string type variables as we'll need to rebuild the NVP string
      string token = Request.QueryString["token"].ToString();
      string payerId = Request.QueryString["PayerID"].ToString();

      // Rebuilding the NVP string for the request; I've hardcoded the payment values again as this sample app does not have a database behind it.
      string NVP = string.Empty;

      NVP += "METHOD=DoExpressCheckoutPayment";
      NVP += "&VERSION=123";

      NVP += "&USER=" + _sellerUsername;
      NVP += "&PWD=" + _sellerPassword;
      NVP += "&SIGNATURE=" + _sellerSignature;

      NVP += "&TOKEN=" + token;
      NVP += "&PAYERID=" + payerId;

      NVP += "&PAYMENTREQUEST_0_PAYMENTACTION=Sale";
      NVP += "&PAYMENTREQUEST_0_AMT=100";
      NVP += "&PAYMENTREQUEST_0_ITEMAMT=80";
      NVP += "&PAYMENTREQUEST_0_SHIPPINGAMT=10";
      NVP += "&PAYMENTREQUEST_0_HANDLINGAMT=5";
      NVP += "&PAYMENTREQUEST_0_TAXAMT=5";

      // Making the API call
      string response = APICall(NVP);

      // Interpreting the response from PayPal; As a simple UI for checking the transaction, I'm displaying the transaction ID in the page on success so to make things easier when I'm checking the transaction log in PayPal's web UI.
      if (response.Contains("Success"))
      {
          string transactionId = response.Substring(response.IndexOf("PAYMENTINFO_0_TRANSACTIONID"), response.IndexOf("&", response.IndexOf("PAYMENTINFO_0_TRANSACTIONID")) - response.IndexOf("PAYMENTINFO_0_TRANSACTIONID"));
          lblPayPalPaymentSuccess.Text += transactionId;
          lblPayPalPaymentSuccess.Visible = true;
      }
      else
          lblError.Visible = true;

      btnConfirmPayPalPurchase.Visible = false;
      lblPayPalCheckoutSuccess.Visible = false;
  }

  private string APICall(string NVP)
  {
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_endpointUrl);
      request.Method = "POST";
      request.ContentLength = NVP.Length;

      string sResponse = string.Empty;
      using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
      {
          sw.Write(NVP);
      }

      HttpWebResponse response = (HttpWebResponse)request.GetResponse();
      using (StreamReader sr = new StreamReader(response.GetResponseStream()))
      {
          sResponse = sr.ReadToEnd();
      }

      return sResponse;
  }
 }
}

There you have it! PayPal is a payment system easy to implement and which can be integrated into your C# applications in no time. For the remaining time, you can get your online business rolling, working on refining your services and adding more value to your products.

While there are many payment systems out there, PayPal Express Checkout offers a great starting point for a startup business, thanks to the easy implementation and the well-known PayPal system, which is used all over the world.

This series of articles comes to an end, but if you've encountered a similar situation where you needed a quick and solid solution to a similar problem, feel free to share your experience with us.

 

Other posts in this series:

Simple way to integrate PayPal into your C# web app - Part 1
Simple way to integrate PayPal into your C# web app - Part 2


Tags: How To  PayPal 


Comments


asad commented on 8/11/2016 5:31:37 PM

This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

Love from <a href="http://www.pprogramming.com/"> Pprogramming </a>
     

Amine commented on 10/26/2016 2:03:11 PM

Hello,

This is a great and simple job :)
I want to know how to get seller username, password and signature ?

thanks
     

Your Comment:







Blog Home   SBP Home
RSS Feed       Contact








 Blog Archives  |  Terms of Use  |  Privacy Policy
© 2017 SBP Romania. All rights reserved.