Saturday, March 29, 2008

Paypal NVP API example for VB.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 (
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.

- 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

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 = ""
Dim strPassword As String = "XXXCCGUJP2EXXXX"

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

Dim strNVPSandboxServer As String = ""
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=" + "&STREET=" + addressTextbox.Text + "&CITY=" + cityTextbox.Text + "&STATE=" + regionTextbox.Text + "&COUNTRY=" + countryDropDown.Text + "&ZIP=95110" + postalTextbox.Text + "&COUNTRYCODE=US" + "&PAYMENTACTION=Sale" + "&VERSION=" + strAPIVersion
'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())

' 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()

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))

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
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
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


Unknown said...

you have not defined URLDecode, dude.

Unknown said...

sorry, should be Server.URLDecode

r4 ds card said...

Thanx for the information. I was looking for something like this. This will be a great help for my project.

Anonymous said...

im lost, what do i do for "server.URLDecode(" ?


.Net Development said...

I like because this is the free technology that i can create modern website for my personal use or for business use.

Help said...

plz give me the code With c#