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
- PayPal API Credentials
- API Signature
Paypal sandbox credentials
Implementation
public ActionResult Index()
{
if (ModelState.IsValid)
{
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;
string strNVPSandboxServer =
ConfigurationManager.AppSettings["NVPSandboxServer"].ToString();
string strAPIVersion = "60.0";
string ipAddress = "xx.xxx.xxx.xxx";
var stateName = "NY";
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" +
"&FIRSTNAME=" + billingFirstName +
"&LASTNAME=" + billingLastName +
"&STREET=" + billingAddress1 +
"&STREET2=" + "" +
"&CITY=" + "Memphsis" +
"&STATE=" + "TN" +
"&COUNTRYCODE=US" +
"&ZIP=" + "38134" +
"&AMT=" + "100.00"
+ "&CURRENCYCODE=USD" +
"&DESC=Test Sale Tickets" +
"&INVNUM=" + "";
try
{
HttpWebRequest wrWebRequest = (HttpWebRequest)WebRequest.Create(strNVPSandboxServer);
wrWebRequest.Method = "POST";
StreamWriter requestWriter = new StreamWriter(wrWebRequest.GetRequestStream());
requestWriter.Write(strNVP);
requestWriter.Close();
HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();
StreamReader responseReader =
new StreamReader(wrWebRequest.GetResponse().GetResponseStream());
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();
if (strAck == "Success" || strAck == "SuccessWithWarning")
{
string strAmt = htResponse["AMT"].ToString();
string strCcy = htResponse["CURRENCYCODE"].ToString();
string strTransactionID = htResponse["TRANSACTIONID"].ToString();
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.