Saturday, March 29, 2008

Paypal NVP API example for VB.Net (asp.net)

This API is a very easy way to accept credit cards through your web app. Although, I don't think there are any simple examples available for ASP.Net 2.0 (VB.net).
This beginner example will at least allow you to send a request to paypal and then get the response back from the API and display the error to a label.

Prereqs:
- Paypal business account with paypal websites pro enabled
- Paypal sandbox account (you must login and accept the agreement for websites pro from this account)
- get an API username, password, & signature from your sandbox account

Steps:
1- create an ASPX form with the following fields:
cctypeDdl, ccnumberTextbox, expdateDropDown, yearDropDown, CVVcodeTextBox, amountTextBox, firstnameTextbox, lastnameTextbox, addressTextbox, cityTextbox, regionTextbox, countryDropDown, postalTextbox
and these labels: successLabel, errLabel, errcodeLabel

2- Create a button control and button_click event handler

3- copy below code into your event handler


'API Credentials (3-token)
Dim strUsername As String = "brad_apiX.w3XXXXX.com"
Dim strPassword As String = "XXXCCGUJP2EXXXX"

Dim strSignature As String = "XXXXXXZLVYanEw944w0oPBsJXXXXXXX"
Dim strCredentials As String = "USER=" + strUsername + "&PWD=" + strPassword + "&SIGNATURE=" + strSignature

Dim strNVPSandboxServer As String = "https://api-3t.sandbox.paypal.com/nvp"
Dim strAPIVersion As String = "2.3"

Dim strNVP As String = strCredentials + "&METHOD=DoDirectPayment" + "&CREDITCARDTYPE=" + cctypeDdl.Text + "&ACCT=" + ccnumberTextbox.Text + "&EXPDATE=" + expdateDropDown.Text + yearDropDown.Text + "&CVV2=" + CVVcodeTextBox.Text + "&AMT=" + amountTextBox.Text + "&FIRSTNAME=" + firstnameTextbox.Text + "&LASTNAME=" + lastnameTextbox.Text + "&IPADDRESS=255.55.167.002" + "&STREET=" + addressTextbox.Text + "&CITY=" + cityTextbox.Text + "&STATE=" + regionTextbox.Text + "&COUNTRY=" + countryDropDown.Text + "&ZIP=95110" + postalTextbox.Text + "&COUNTRYCODE=US" + "&PAYMENTACTION=Sale" + "&VERSION=" + strAPIVersion
Try
'Create web request and web response objects, make sure you using the correct server (sandbox/live)
Dim wrWebRequest As HttpWebRequest = DirectCast(WebRequest.Create(strNVPSandboxServer), HttpWebRequest)
wrWebRequest.Method = "POST"
Dim requestWriter As New StreamWriter(wrWebRequest.GetRequestStream())
requestWriter.Write(strNVP)
requestWriter.Close()

' Get the response.
Dim hwrWebResponse As HttpWebResponse = DirectCast(wrWebRequest.GetResponse(), HttpWebResponse)
Dim responseReader As New StreamReader(wrWebRequest.GetResponse().GetResponseStream())

'and read the response
Dim responseData As String = responseReader.ReadToEnd()
responseReader.Close()

Dim result As String = Server.UrlDecode(responseData)

Dim arrResult As String() = result.Split("&"c)
Dim htResponse As New Hashtable()
Dim responseItemArray As String()
For Each responseItem As String In arrResult
responseItemArray = responseItem.Split("="c)
htResponse.Add(responseItemArray(0), responseItemArray(1))
Next

Dim strAck As String = htResponse("ACK").ToString()

If strAck = "Success" OrElse strAck = "SuccessWithWarning" Then
Dim strAmt As String = htResponse("AMT").ToString()
Dim strCcy As String = htResponse("CURRENCYCODE").ToString()
Dim strTransactionID As String = htResponse("TRANSACTIONID").ToString()
'ordersDataSource.InsertParameters("TransactionID").DefaultValue = strTransactionID

Dim strSuccess As String = "Thank you, your order for: $" + strAmt + " " + strCcy + " has been processed."
successLabel.Text = strSuccess
Else
Dim strErr As String = "Error: " + htResponse("L_LONGMESSAGE0").ToString()
Dim strErrcode As String = "Error code: " + htResponse("L_ERRORCODE0").ToString()
errLabel.Text = strErr
errcodeLabel.Text = strErrcode
Return
End If
Catch ex As Exception
' do something to catch the error, like write to a log file.
Response.Write("error processing")
End Try

Keep in mind that you'll need to serve this page via SSL for production use.
Otherwise, someone could sniff all your customers credit card numbers. For a inexpensive SSL certs, try godaddy.com

Wednesday, March 12, 2008

Card Shuffle routine in C#

This code example simply shows you how to take a deck of cards and return them to a console "shuffled". If you have any ideas that would make it more realistic, please post a comment. Thx

Here's the code:
//----------------------------------------

class Program
{
public static void Main()
{
int[] array = new int[52];
for( int i = 0; i < 52; i++ )
{
array[i] = i;
}
int[] newArray = new int[52];
bool[] used = new bool[52];

for( int j = 0; j < 52; j++ )
{
used[j] = false;
}
Random rnd = new Random();
int iCount = 0;
int iNum;
while( iCount < 52 )
{
iNum = rnd.Next( 0, 52 ); // between 0 and 51
if( used[iNum] == false )
{
newArray[iCount] = iNum;
used[iNum] = true;
iCount++;
}
}
// End of Shuffle Routine
// Load original array with shuffled array
array = newArray;
for( int n = 0; n < 52; n++ )
{
string cardvalue ="";
switch (array[n])
{
case 0: cardvalue = "AC"; break; case 1: cardvalue = "AH"; break; case 2: cardvalue = "AS"; break; case 3: cardvalue = "AD"; break;
case 4: cardvalue = "KC"; break; case 5: cardvalue = "KH"; break; case 6: cardvalue = "KS"; break; case 7: cardvalue = "KD"; break;
case 8: cardvalue = "QC"; break; case 9: cardvalue = "QH"; break; case 10: cardvalue = "QS"; break; case 11: cardvalue = "QD"; break;
case 12: cardvalue = "JC"; break; case 13: cardvalue = "JH"; break; case 14: cardvalue = "JS"; break; case 15: cardvalue = "JD"; break;
case 16: cardvalue = "10C"; break; case 17: cardvalue = "10H"; break; case 18: cardvalue = "10S"; break; case 19: cardvalue = "10D"; break;
case 20: cardvalue = "9C"; break; case 21: cardvalue = "9H"; break; case 22: cardvalue = "9S"; break; case 23: cardvalue = "9D"; break;
case 24: cardvalue = "8C"; break; case 25: cardvalue = "8H"; break; case 26: cardvalue = "8S"; break; case 27: cardvalue = "8D"; break;
case 28: cardvalue = "7C"; break; case 29: cardvalue = "7H"; break; case 30: cardvalue = "7S"; break; case 31: cardvalue = "7D"; break;
case 32: cardvalue = "6C"; break; case 33: cardvalue = "6H"; break; case 34: cardvalue = "6S"; break; case 35: cardvalue = "6D"; break;
case 36: cardvalue = "5C"; break; case 37: cardvalue = "5H"; break; case 38: cardvalue = "5S"; break; case 39: cardvalue = "5D"; break;
case 40: cardvalue = "4C"; break; case 41: cardvalue = "4H"; break; case 42: cardvalue = "4S"; break; case 43: cardvalue = "4D"; break;
case 44: cardvalue = "3C"; break; case 45: cardvalue = "3H"; break; case 46: cardvalue = "3S"; break; case 47: cardvalue = "3D"; break;
case 48: cardvalue = "2C"; break; case 49: cardvalue = "2H"; break; case 50: cardvalue = "2S"; break; case 51: cardvalue = "2D"; break;

default: cardvalue = "XX"; break;
}
Console.Write( "{0} ", cardvalue );
}
Console.ReadLine();
}