SBP Blog

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;
      // 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_AMT=100";

      // 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;
          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()))

      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 


No comments yet.

Your Comment: