PayPal Web Payments in vb.net

BluJag

Perch
If anyone is interested, here is some code to accept PayPal IPNs from a PayNow type of web payments button, where the person is just paying for a single item or product. The "return" hidden form field on your payment button page will point to this page http://www.yoursite.com/premium_upgrade_test.aspx

///////////////////////////////////////////////////////////////////
Imports System.Net
Imports System.IO
Public Class premium_upgrade_test
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents lblDate As System.Web.UI.WebControls.Label
System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

'// Step 1a: Modify the POST string.
Dim formPostData As String = "cmd=_notify-validate"
Dim postValue As String
Dim postKey As String

Dim postByteArray As Byte()
Dim responseArray As Byte()

Dim Respons As String 'this is not a spelling mistake!

Dim MemID As String = Request.Form("item_number")

For Each postKey In Request.Form
postValue = Encode(Request.Form(postKey))
formPostData += String.Format("&{0}={1}", postKey, postValue)
Next

'// Step 1b: POST the data back to PayPal.
Dim client As New System.Net.WebClient
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
postByteArray = System.Text.Encoding.ASCII.GetBytes(formPostData)
responseArray = client.UploadData("https://www.paypal.com/cgi-bin/webscr", "POST", postByteArray)

Respons = System.Text.Encoding.ASCII.GetString(responseArray)

Dim DB As New PropChains.Helpers 'one of my classes
Select Case Respons

Case "VERIFIED"
DB.UpgradeAccount(MemID) 'this public sub gets the returned member id and updates the SQL Server database.

Response.Redirect("mychain.aspx", True) 'send the user back to the website
Case Else
'// Possible fraud. Log for investigation.
Response.Redirect("paymentfail.aspx?memid=" & MemID, True) 'page which explains there has been a problem.

End Select

End Sub


Private Function Encode(ByVal OldValue as String) As String
' This helper method encodes a string correctly for an HTTP POST
Dim newValue As String
newValue = OldValue.Replace("\", "")
newValue = System.Web.HttpUtility.UrlEncode(newValue)
newValue = newValue.Replace("%2f", "/")
Return newValue
End Function

End Class
/////////////////////////////////////////
The associated aspx page has just this code in it
<%@ Page CodeBehind="premium_upgrade_test.aspx.vb" Language="vb" AutoEventWireup="false" Inherits="pchain.premium_upgrade_test" %>
/////////////////////////////////////////
There are probably a few enhancements you can make, but it all works fine.

Hope someone finds this useful since there is no vb.net stuff on the PayPal developer network.

Rob
 
Back
Top