Simple way to integrate PayPal into your C# web app - Part 3
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_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.
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();
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