Yes, I can, Mr. Eaton!
TestCondition.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
public static class TestCondition
{
public static void IsTrue(bool condition, string failMessage)
{
if (!condition)
{
AssertFailed(failMessage);
}
}
public static void IsNotNull(object o, string failMessage)
{
if (o == null)
{
AssertFailed(failMessage);
}
}
public static void IsNotEmptyString(string s, string failMessage)
{
if (s == string.Empty)
{
AssertFailed(failMessage);
}
}
public static void IsNotNullOrEmptyString(string s, string failMessage)
{
if (!String.IsNullOrEmpty(s))
{
AssertFailed(failMessage);
}
}
public static void IsGreaterThanZero(int i, string failMessage)
{
if (i <= 0)
{
AssertFailed(failMessage);
}
}
public static void IsGreaterThanZero(decimal i, string failMessage)
{
if (i <= 0)
{
AssertFailed(failMessage);
}
}
// Function to test for Positive Integers.
public static void IsNaturalNumber(String strNumber, string failMessage)
{
Regex regNotNaturalPattern = new Regex("[^0-9]");
Regex regNaturalPattern = new Regex("0*[1-9][0-9]*");
if (!regNotNaturalPattern.IsMatch(strNumber) &&
regNaturalPattern.IsMatch(strNumber))
{
AssertFailed(failMessage);
}
}
// Function to test for Positive Integers with zero inclusive
public static void IsWholeNumber(string strNumber, string failMessage)
{
Regex regNotWholePattern = new Regex("[^0-9]");
if (regNotWholePattern.IsMatch(strNumber))
{
AssertFailed(failMessage);
}
}
// Function to Test for Integers both Positive & Negative
public static void IsInteger(string strNumber, string failMessage)
{
Regex regNotIntPattern = new Regex("[^0-9-]");
Regex regIntPattern = new Regex("^-[0-9]+$|^[0-9]+$");
if (regNotIntPattern.IsMatch(strNumber) &&
regIntPattern.IsMatch(strNumber))
{
AssertFailed(failMessage);
}
}
// Function to test whether the string is valid number or not
public static void IsNumber(string strNumber, string failMessage)
{
Regex regNotNumberPattern = new Regex("[^0-9.-]");
Regex regTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex regTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
Regex regNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
if (regNotNumberPattern.IsMatch(strNumber) &&
!regTwoDotPattern.IsMatch(strNumber) &&
!regTwoMinusPattern.IsMatch(strNumber) &&
regNumberPattern.IsMatch(strNumber))
{
AssertFailed(failMessage);
}
}
// Function To test for Alphabets.
public static void IsAlpha(string strToCheck, string failMessage)
{
Regex regAlphaPattern = new Regex("[^a-zA-Z]");
if (regAlphaPattern.IsMatch(strToCheck))
{
AssertFailed(failMessage);
}
}
public static void IsValidEmail(string email, string failMessage)
{
string emailPattern = @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
Regex regEmailPattern = new Regex(emailPattern);
if (regEmailPattern.IsMatch(email))
{
AssertFailed(failMessage);
}
}
// Function to Check for AlphaNumeric.
public static void IsAlphaNumeric(string strToCheck, string failMessage)
{
Regex regAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
if (regAlphaNumericPattern.IsMatch(strToCheck))
{
AssertFailed(failMessage);
}
}
static void AssertFailed(string message)
{
throw new Exception(message);
}
}
PDTHandler.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using Commerce.Common;
public partial class PDTHandler : System.Web.UI.Page
{
void Page_Load(object sender, EventArgs e)
{
//###############################################################################
// Page Validators
//###############################################################################
//your transaction ID can be null/empty if your account is not validated, verified, if
//your business email is wrong, your PDT id is wrong, or PayPal's just having a bad day
TestCondition.IsNotNull(Request.QueryString["tx"], "No TransactionID - Invalid");
TestCondition.IsNotNull(Request.QueryString["cm"], "No TransactionID - Invalid");
//##############################################################################
string ppTX = Request.QueryString["tx"].ToString();
string sOrderID = Request.QueryString["cm"].ToString();
string pdtResponse = GetPDT(ppTX);
//all we need at this point is the SUCCESS flag
if (pdtResponse.StartsWith("SUCCESS"))
{
string sAmount = GetPDTValue(pdtResponse, "mc_gross");
//make sure the totals add up
try
{
//make sure to unencode it
sOrderID = Server.UrlDecode(sOrderID);
Order order = OrderController.GetOrder(sOrderID);
if (order != null)
{
//commit the order
OrderController.CommitStandardOrder(order, ppTX, decimal.Parse(sAmount));
//send off to the receipt page
Response.Redirect("../receipt.aspx?t=" + sOrderID, true);
}
else
{
Response.Write("Can't find the order");
}
}
catch (Exception x)
{
Response.Write("Invalid Order: " + x.Message);
}
}
else
{
Response.Write("PDT Failure: " + pdtResponse);
}
}
string GetPDTValue(string pdt, string key)
{
string[] keys = pdt.Split('\n');
string thisVal = "";
string thisKey = "";
foreach (string s in keys)
{
string[] bits = s.Split('=');
if (bits.Length > 1)
{
thisVal = bits[1];
thisKey = bits[0];
if (thisKey.ToLower().Equals(key))
break;
}
}
return thisVal;
}
string GetPDT(string transactionID)
{
string sOut = "";
string PDTID = "";
PDTID = SiteConfig.PayPalPDTID;
string sCmd = "_notify-synch";
string serverURL = "";
if (SiteConfig.UsePPStandardSandbox)
{
serverURL = "https://www.sandbox.paypal.com/cgi-bin/webscr";
}
else
{
serverURL = "https://www.paypal.com/cgi-bin/webscr"; ;
}
try
{
string strFormValues = Request.Form.ToString();
string strNewValue;
string strResponse;
// Create the request back
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(serverURL);
// Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
strNewValue = strFormValues + "&cmd=_notify-synch&at=" + PDTID + "&tx=" + transactionID;
req.ContentLength = strNewValue.Length;
// Write the request back IPN strings
StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(strNewValue);
stOut.Close();
// Do the request to PayPal and get the response
StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();
sOut = Server.UrlDecode(strResponse);
}
catch (Exception x)
{
}
return sOut;
}
}
Hope that helps you figure out why things aren't working! Thanks!
~ Bruce