Click here to Skip to main content
15,868,141 members
Articles / Web Development / ASP.NET

Implementing PayPal Credit Card Processing in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
9 Feb 2015CPOL 12K   16   2
How to implement PayPal credit card processing in ASP.NET MVC

In this post, we will learn how to implement a PayPal credit card processing in your website.

You can create a developer account here.

Prerequisites

  1. PayPal API Credentials
  2. API Signature

paypal sandbox credentials

Paypal sandbox credentials

Implementation

C#
public ActionResult Index()
        {
            if (ModelState.IsValid)
            {
                //API Credentials (3-token)
                string strUsername = ConfigurationManager.AppSettings["InStoreAPIUsername"].ToString();
                string strPassword = ConfigurationManager.AppSettings["InStoreAPIPassword"].ToString();
                string strSignature = ConfigurationManager.AppSettings["InStoreAPISignature"].ToString();
                string strCredentials = "USER=" + strUsername + 
                "&PWD=" + strPassword + "&SIGNATURE=" + strSignature;

                //SandBox server Url - https://api-3t.paypal.com/nv
                //Live server Url - https://api.paypal.com/nvp

                //strNVPSandboxServer  represents server url
                string strNVPSandboxServer = 
                ConfigurationManager.AppSettings["NVPSandboxServer"].ToString();

                string strAPIVersion = "60.0";

                //Your IP Address
                string ipAddress = "xx.xxx.xxx.xxx";

                //sId represents the stateId
                //NY - NewYork
                var stateName = "NY";

                //grandTotal represents the shopping cart total 
                string grandTotal = "10.00";
                var expirationMonth = "12";
                var expirationYear = "31";
                var billingFirstName = "John";
                var billingLastName = "Doe";
                var billingAddress1 = "123 Main Street";

                string strNVP = "METHOD=DoDirectPayment" +
                            "&VERSION=" + strAPIVersion +
                            "&PWD=" + strPassword +
                            "&USER=" + strUsername +
                            "&SIGNATURE=" + strSignature +
                            "&PAYMENTACTION=Sale" +
                            "&IPADDRESS=82.69.92.153" +
                            "&RETURNFMFDETAILS=0" +
                            "&CREDITCARDTYPE=" + "Visa" +
                            "&ACCT=" + "4111111111111111" +
                            "&EXPDATE=" + expirationMonth + expirationYear +
                            "&CVV2=" + 111 +
                            "&STARTDATE=" +
                            "&ISSUENUMBER=" +
                            "&EMAIL=youremail@gmail.com" +
                            //the following  represents the billing details
                            "&FIRSTNAME=" + billingFirstName +
                            "&LASTNAME=" + billingLastName +
                            "&STREET=" + billingAddress1 +
                            "&STREET2=" + "" +
                            "&CITY=" + "Memphsis" +
                            "&STATE=" + "TN" +
                            "&COUNTRYCODE=US" +
                            "&ZIP=" + "38134" +
                            "&AMT=" + "100.00" 
                            +//orderdetails.GrandTotal.ToString("0.0")+
                            "&CURRENCYCODE=USD" +
                            "&DESC=Test Sale Tickets" +
                            "&INVNUM=" + "";

                try
                {
                    //Create web request and web response objects, 
                    //make sure you using the correct server (sandbox/live)
                    HttpWebRequest wrWebRequest = (HttpWebRequest)WebRequest.Create(strNVPSandboxServer);
                    wrWebRequest.Method = "POST";
                    StreamWriter requestWriter = new StreamWriter(wrWebRequest.GetRequestStream());
                    requestWriter.Write(strNVP);
                    requestWriter.Close();

                    // Get the response.
                    HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();
                    StreamReader responseReader = 
                    	new StreamReader(wrWebRequest.GetResponse().GetResponseStream());

                    //and read the response
                    string responseData = responseReader.ReadToEnd();
                    responseReader.Close();

                    string result = Server.UrlDecode(responseData);

                    string[] arrResult = result.Split('&');
                    Hashtable htResponse = new Hashtable();
                    string[] responseItemArray;
                    foreach (string responseItem in arrResult)
                    {
                        responseItemArray = responseItem.Split('=');
                        htResponse.Add(responseItemArray[0], responseItemArray[1]);
                    }

                    string strAck = htResponse["ACK"].ToString();

                    //strAck = "Success";

                    if (strAck == "Success" || strAck == "SuccessWithWarning")
                    {
                        string strAmt = htResponse["AMT"].ToString();
                        string strCcy = htResponse["CURRENCYCODE"].ToString();
                        string strTransactionID = htResponse["TRANSACTIONID"].ToString();

                        //SaveOrder();
                        //Send Email

                        string strSuccess = "Thank you, your order for: $" + 
                        	strAmt + " " + strCcy + " has been processed.";

                        ViewBag.Amount = strAmt;
                        ViewBag.Currency = strCcy;

                    }
                    else
                    {
                        ViewBag.Error = "Error: " + 
                        	htResponse["L_LONGMESSAGE0"].ToString();
                        ViewBag.ErrorCode = "Error code: " + 
                        	htResponse["L_ERRORCODE0"].ToString();

                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }
            else
            {
                ViewBag.Failure = "failure";
                return View();
            }
            return View();
        }

You can find the source code in GitHub.

The post Implementing PayPal Credit Card Processing in ASP.NET MVC appeared first on Venkat Baggu Blog.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) eBiz Solutions http://venkatbaggu.com/
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhile executing the code error is coming. Pin
Member 150595176-Feb-21 22:37
Member 150595176-Feb-21 22:37 
Questionquestion regarding PayPal Credit Card Processing Pin
Member 139391965-Aug-18 23:32
Member 139391965-Aug-18 23:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.