PayPal Digital Goods Express Checkout C# Library

On May 27, 2014, in HTML5, Make Money Online, by James Liu

PayPal express checkout is a great way to integrate payment system in .net website. It enables us to sell our digital products much easier. Though PayPal provide a SDK for .net platform, it is too complicate to use. Hence, I decide to write a simple PayPal digital goods express checkout library in C#. Before I start to introduce my PayPal express checkout library for C#, you’d better to know how PayPal express checkout works. If you are not familiar with PayPal express checkout, you can check this:
Integrate PayPal Digital Goods Express Checkout Into PHP Website

I will create this PayPal express checkout c# example in Visual Studio 2010. In the example, I will create a simple sale page and put a buy button on this page. When you click the buy button, it will popup a window to let you make payment. After purchase successfully, it will show an download link in the popup window. When you integrate this payment system in your .net website, it will make your digital products sales simple and fast. Your customers can get what they purchased immediately once they make the payment. Here are some screenshot.

Sale Page with Buy Now Button

Sale Page with Buy Now Button



PayPal Payment Login Page

PayPal Payment Login Page


PayPal Payment Info Page

PayPal Payment Info Page


Payment Successfully  and Download Page

Payment Successfully and Download Page

PayPal Express Checkout C# MVC Example Source Code

In this example source code, there are four main pages:

  • Sale Page: a simple page with a “Buy Now” button
  • Correspondingly, there are four main ActionResult functions in controller. For the sale page, the function is very simple:

            public ActionResult Index()
            {
                return View();
            }
    

    The page html source code look like:

    <h2>Purchase Testing</h2>
    <p>
        This is a testing sale page. You can use your PayPal sandbox account to finish the purchase.
    </p>
    
    <form action='/home/checkout' METHOD='POST'>
    	<input type='image' name='paypal_submit' id='paypal_submit'  src='https://www.paypal.com/en_US/i/btn/btn_dg_pay_w_paypal.gif' border='0' align='top' alt='Pay with PayPal'/>
    </form>
    
    <!-- Add Digital goods in-context experience. Ensure that this script is added before the closing of html body tag -->
    
    <script src='https://www.paypalobjects.com/js/external/dg.js' type='text/javascript'></script>
    <script>
        var dg = new PAYPAL.apps.DGFlow(
    	{
    	    trigger: 'paypal_submit',
    	    expType: 'instant'
    	    //PayPal will decide the experience type for the buyer based on his/her 'Remember me on your computer' option.
    	});
    </script>
    
  • Checkout Page: gather sale information and show payment page
  • When you click the “pay with paypal” button, it will call to checkout page. The Checkout() function in controller will set up PayPal Express Checkout and show the payment page from PayPal.

            public ActionResult Checkout()
            {
                PayPalExpressCheckout pp = new PayPalExpressCheckout(API_UserName, API_Password, API_Signature, isTestingMode);
    
                List<PayPalExpressCheckout.PayPalItem> items = new List<PayPalExpressCheckout.PayPalItem>();
                PayPalExpressCheckout.PayPalItem item = new PayPalExpressCheckout.PayPalItem();
                item.name = "My Digital Product 1";
                item.amt = "0.99";
                item.qty = "1";
                items.Add(item);
    
                //add more for shopping cart
                item = new PayPalExpressCheckout.PayPalItem();
                item.name = "Product 2";
                item.amt = "1.99";
                item.qty = "1";
                items.Add(item);
    
                float totalAmount = 0;
                foreach (PayPalExpressCheckout.PayPalItem i in items)
                {
                    totalAmount += float.Parse(i.amt);
                }
    
                string paymentAmount = totalAmount.ToString();
                string customField = "trackcode=1&id=2"; //for your self tracking
    
                NameValueCollection nvpResArray = pp.SetExpressCheckoutDG(paymentAmount, currencyCodeType, paymentType, returnURL, cancelURL, items, customField, true, itemCategory);
                string ack = nvpResArray.GetValues("ACK").First().ToUpper();
                if (ack == "SUCCESS" || ack == "SUCCESSWITHWARNING")
                {
                    string token = nvpResArray.GetValues("TOKEN").First();
                    //redirect to paypal
                    if(itemCategory == "Digital")
                    {
                        Response.Redirect(pp.PAYPAL_DG_URL + token);
                    }
                    else
                    {
                        Response.Redirect(pp.PAYPAL_URL + token);
                    }
                }
                else
                {
                    //failed
                    ViewBag.result = nvpResArray;
                    ViewBag.status = "paypal failed";
                }
    
                return View();
            }
    
  • Return Page: payment result page
  • When you make the payment, you will be redirect to the result page. If your payment is successfully, a download link will be shown for you to download digital goods which you paid for. Otherwise, it will show error code. Here are the example source code:

            public ActionResult ReturnPage()
            {
                string token = Request["token"];
                string payerID = Request["PayerID"];
    
                PayPalExpressCheckout pp = new PayPalExpressCheckout(API_UserName, API_Password, API_Signature, isTestingMode);
                //get payment info
                NameValueCollection res = pp.GetExpressCheckoutDetails(token);
                string ack = res.GetValues("ACK").First().ToUpper();
                if (ack != "SUCCESS" && ack != "SUCCESSWITHWARNING")
                {
                    ViewBag.status = "paypal failed";
                    ViewBag.result = res;
                    return View("ReturnPage");
                }
    
                string finalPaymentAmount = res.GetValues("PAYMENTREQUEST_0_AMT").First();
                string paymentType = "Sale";
                string currencyCodeType = res.GetValues("CURRENCYCODE").First();
                string customField = res.GetValues("PAYMENTREQUEST_0_CUSTOM").First();
                string payerEmail = res.GetValues("EMAIL").First();
                string payerFirstName = res.GetValues("FIRSTNAME").First();
                string payerLastName = res.GetValues("LASTNAME").First();
                string country = res.GetValues("COUNTRYCODE").First();
    
                List<PayPalExpressCheckout.PayPalItem> items = new List<PayPalExpressCheckout.PayPalItem>();
                List<string> itemsNameList = new List<string>();
                int itemNumber = 0;
    
                while (res.GetValues("L_PAYMENTREQUEST_0_NAME" + itemNumber.ToString()) != null)
                {
                    PayPalExpressCheckout.PayPalItem item = new PayPalExpressCheckout.PayPalItem();
                    item.name = res.GetValues("L_PAYMENTREQUEST_0_NAME" + itemNumber.ToString()).First();
                    item.amt = res.GetValues("L_PAYMENTREQUEST_0_AMT" + itemNumber.ToString()).First();
                    item.qty = res.GetValues("L_PAYMENTREQUEST_0_QTY" + itemNumber.ToString()).First();
                    items.Add(item);
                    itemsNameList.Add(item.name);
                    itemNumber++;
                }
    
                NameValueCollection result = pp.ConfirmPayment(token, paymentType, currencyCodeType, payerID, finalPaymentAmount, items, customField, Request.ServerVariables["SERVER_NAME"], itemCategory);
                ack = result.GetValues("ACK").First().ToUpper();
                if (ack == "SUCCESS" || ack == "SUCCESSWITHWARNING")
                {
                    ViewBag.status = "successfully";
                    string transactionId = result.GetValues("PAYMENTINFO_0_TRANSACTIONID").First();
                    string currency = result.GetValues("PAYMENTINFO_0_CURRENCYCODE").First();
                    string orderTime = result.GetValues("PAYMENTINFO_0_ORDERTIME").First();
                    string amt = result.GetValues("PAYMENTINFO_0_AMT").First();
                    string feeAmt = result.GetValues("PAYMENTINFO_0_FEEAMT").First();
                    string taxAmt = result.GetValues("PAYMENTINFO_0_TAXAMT").First();
                    string paymentStatus = result.GetValues("PAYMENTINFO_0_PAYMENTSTATUS").First();
                    string pendingReason = result.GetValues("PAYMENTINFO_0_PENDINGREASON").First();
    
                    ViewBag.transactionId = transactionId;
                    ViewBag.amt = amt;
                    ViewBag.currency = currency;
                }
    
                return View();
            }
    
  • Cancel Page: payment cancelled page
  • In the middle of payment, if your customers cancel the payment, they will be redirected to this page. It is a normal html page.

            public ActionResult CancelPage()
            {
                return View();
            }
    

Get Full Source Code under $5.99


To save your time and effort, you can get the full PayPal Express Checkout C# library source code and example source code under $5.99. You can use it in your project without any limitation. With this source code, you can:

  • Build your own PayPal digital goods payment system
  • Build your own PayPal express checkout payment system
  • Build your own PayPal shopping cart
 

2 Responses to “PayPal Digital Goods Express Checkout C# Library”

  1. Chye Ling says:

    I have already purchase. Why cannot download the code?

    • James Liu says:

      Hello Chye,

      Basically, a download link will show when you finish the payment. But anyway, I sent your the full source code to your email already.

      Best Regards
      James

Leave a Reply

WordPress Themes

Free WordPress Theme