How to use DoDirect/Paypal Pro in asp.net?
- by ptahiliani
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Net;using System.IO;using System.Collections;public partial class Default2 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //API Credentials (3-token) string strUsername = "<<enter your sandbox username here>>"; string strPassword = "<<enter your sandbox password here>>"; string strSignature = "<<enter your signature here>>"; string strCredentials = "USER=" + strUsername + "&PWD=" + strPassword + "&SIGNATURE=" + strSignature; string strNVPSandboxServer = "https://api-3t.sandbox.paypal.com/nvp"; string strAPIVersion = "2.3"; string strNVP = strCredentials + "&METHOD=DoDirectPayment" + "&CREDITCARDTYPE=" + "Visa" + "&ACCT=" + "4710496235600346" + "&EXPDATE=" + "10" + "2017" + "&CVV2=" + "123" + "&AMT=" + "12.34" + "&FIRSTNAME=" + "Demo" + "&LASTNAME=" + "User" + "&IPADDRESS=192.168.2.236" + "&STREET=" + "Lorem-1" + "&CITY=" + "Lipsum-1" + "&STATE=" + "Lorem" + "&COUNTRY=" + "INDIA" + "&ZIP=" + "302004" + "&COUNTRYCODE=IN" + "&PAYMENTACTION=Sale" + "&VERSION=" + strAPIVersion; 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(); if (strAck == "Success" || strAck == "SuccessWithWarning") { string strAmt = htResponse["AMT"].ToString(); string strCcy = htResponse["CURRENCYCODE"].ToString(); string strTransactionID = htResponse["TRANSACTIONID"].ToString(); //ordersDataSource.InsertParameters["TransactionID"].DefaultValue = strTransactionID; string strSuccess = "Thank you, your order for: $" + strAmt + " " + strCcy + " has been processed."; //successLabel.Text = strSuccess; Response.Write(strSuccess.ToString()); } else { string strErr = "Error: " + htResponse["L_LONGMESSAGE0"].ToString(); string strErrcode = "Error code: " + htResponse["L_ERRORCODE0"].ToString(); //errLabel.Text = strErr; //errcodeLabel.Text = strErrcode; Response.Write(strErr.ToString() + "<br/>" + strErrcode.ToString()); return; } } catch (Exception ex) { // do something to catch the error, like write to a log file. Response.Write("error processing"); } }}