I need a forum that can help me with coding PayPal payment using MVC (C#)

MoneySlob
Contributor
Contributor

Both my Sandbox code and Production code return a querystring to the "thank you " page. This query string has amount paid and transaction number (tx) which is used to send back to PayPal in a POST form to get all the transaction details.

I need a forum where I can ask for the syntax to send the "txt" value and the Account's "Identity Token", which is what PayPal says somewhere in an explanation of PDT.

I have all the correct configuration settings, and I also have the "Identity Token", I just don't know the code syntax that will return my POST with name/value pairs I can parse.

Here is the code that works to get the transaction id ("tx"). I know this is also where I do a POST back to PayPal with tx and Identity token.

public ActionResult Thankyou(string item_number, string item_name, string tx, string amt)
{

var CourseID = Convert.ToInt32(item_number);

var trans = _context.PayPalTransactions.FirstOrDefault(x => x.NewCourseIDTemp == CourseID);

if (trans != null)
{
// update now
trans.AmountPaid = amt;
trans.pp_txn_id = tx;

_context.SaveChanges();
}
else
{
// item does not exist in table. Handle as needed.
}

Login to Me Too
1 ACCEPTED SOLUTION

Accepted Solutions
Solved

MoneySlob
Contributor
Contributor

Ok I did a Google search and found this on GitHub. Just what I want.

// ASP .NET C#
 
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Web;
using System.Collections.Generic;

public partial class csPDTSample : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		// CUSTOMIZE THIS: This is the seller's Payment Data Transfer authorization token.
		// Replace this with the PDT token in "Website Payment Preferences" under your account.
		string authToken = "Dc7P6f0ZadXW-U1X8oxf8_vUK09EHBMD7_53IiTT-CfTpfzkN0nipFKUPYy";
		string txToken = Request.QueryString["tx"];
		string query = "cmd=_notify-synch&tx=" + txToken + "&at=" + authToken;
            
		//Post back to either sandbox or live
		string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
		string strLive = "https://www.paypal.com/cgi-bin/webscr";
		HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
     
		//Set values for the request back
		req.Method = "POST";
		req.ContentType = "application/x-www-form-urlencoded";
		req.ContentLength = query.Length;
     
                
		//Send the request to PayPal and get the response
		StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
		streamOut.Write(query);
		streamOut.Close();
		StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
		string strResponse = streamIn.ReadToEnd();
		streamIn.Close();

		Dictionary<string,string> results = new Dictionary<string,string>();
		if(strResponse != "")
		{
			StringReader reader = new StringReader(strResponse);
			string line=reader.ReadLine();

			if(line == "SUCCESS")
			{
                    
				while ((line = reader.ReadLine()) != null)
				{
					results.Add(line.Split('=')[0], line.Split('=')[1]);
                        
	                	}                    
				Response.Write("<p><h3>Your order has been received.</h3></p>");
				Response.Write("<b>Details</b><br>");
				Response.Write("<li>Name: " + results["first_name"] + " " + results["last_name"] + "</li>");
				Response.Write("<li>Item: " + results["item_name"] + "</li>");
				Response.Write("<li>Amount: " + results["payment_gross"] + "</li>");
				Response.Write("<hr>");
			}
			else if(line == "FAIL")
			{
				// Log for manual investigation
				Response.Write("Unable to retrive transaction detail");
			}
		}
		else
		{
			//unknown error
			Response.Write("ERROR");
		}            
	}
}

View solution in original post

Login to Me Too
2 REPLIES 2
Solved

MoneySlob
Contributor
Contributor

Ok I did a Google search and found this on GitHub. Just what I want.

// ASP .NET C#
 
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Web;
using System.Collections.Generic;

public partial class csPDTSample : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		// CUSTOMIZE THIS: This is the seller's Payment Data Transfer authorization token.
		// Replace this with the PDT token in "Website Payment Preferences" under your account.
		string authToken = "Dc7P6f0ZadXW-U1X8oxf8_vUK09EHBMD7_53IiTT-CfTpfzkN0nipFKUPYy";
		string txToken = Request.QueryString["tx"];
		string query = "cmd=_notify-synch&tx=" + txToken + "&at=" + authToken;
            
		//Post back to either sandbox or live
		string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
		string strLive = "https://www.paypal.com/cgi-bin/webscr";
		HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
     
		//Set values for the request back
		req.Method = "POST";
		req.ContentType = "application/x-www-form-urlencoded";
		req.ContentLength = query.Length;
     
                
		//Send the request to PayPal and get the response
		StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
		streamOut.Write(query);
		streamOut.Close();
		StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
		string strResponse = streamIn.ReadToEnd();
		streamIn.Close();

		Dictionary<string,string> results = new Dictionary<string,string>();
		if(strResponse != "")
		{
			StringReader reader = new StringReader(strResponse);
			string line=reader.ReadLine();

			if(line == "SUCCESS")
			{
                    
				while ((line = reader.ReadLine()) != null)
				{
					results.Add(line.Split('=')[0], line.Split('=')[1]);
                        
	                	}                    
				Response.Write("<p><h3>Your order has been received.</h3></p>");
				Response.Write("<b>Details</b><br>");
				Response.Write("<li>Name: " + results["first_name"] + " " + results["last_name"] + "</li>");
				Response.Write("<li>Item: " + results["item_name"] + "</li>");
				Response.Write("<li>Amount: " + results["payment_gross"] + "</li>");
				Response.Write("<hr>");
			}
			else if(line == "FAIL")
			{
				// Log for manual investigation
				Response.Write("Unable to retrive transaction detail");
			}
		}
		else
		{
			//unknown error
			Response.Write("ERROR");
		}            
	}
}
Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.