Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' This page never posts back If Not Page.IsPostBack Then Dim bOrderValid As Boolean = False Dim sLogError As String = "" ' Validate payment Dim orderId As String Dim processorOrderId As String Dim strPostedVariables As String Dim arrPostedVariables As New System.Collections.Specialized.NameValueCollection Try ' Get the posted variables. Exclude the signature (it must be excluded when we hash and also when we validate) Dim req As System.Collections.Specialized.NameValueCollection = Request.Form Dim key, value As String Dim i As Integer For i = 0 To req.Count - 1 key = req.Keys(i) value = req(i) If key <> "signature" Then strPostedVariables = strPostedVariables & key & "=" & HttpUtility.UrlEncode(value) & "&" arrPostedVariables.Add(key, value) End If Next ' Remove the last & strPostedVariables = strPostedVariables.TrimEnd("&"c) orderId = Request.Form("m_payment_id") processorOrderId = Request.Form("pf_payment_id") ' Are we testing or making live payments Dim query As System.Collections.Specialized.NameValueCollection = Me.Request.QueryString Dim site, merchant_id, paymentMode As String Dim arrStr() As String If System.Configuration.ConfigurationSettings.AppSettings.Get("OpMode") = "Test" Then paymentMode = "Test" site = System.Configuration.ConfigurationSettings.AppSettings.Get("PayFast_Test_Validations") merchant_id = System.Configuration.ConfigurationSettings.AppSettings.Get("PayFast_Test_MerchantId") arrStr = Split(System.Configuration.ConfigurationSettings.AppSettings.Get("PayFast_Test_Servers"), ",") Else paymentMode = "Live" site = System.Configuration.ConfigurationSettings.AppSettings.Get("PayFast_Live_Validations") merchant_id = System.Configuration.ConfigurationSettings.AppSettings.Get("PayFast_Live_MerchantId") arrStr = Split(System.Configuration.ConfigurationSettings.AppSettings.Get("PayFast_Live_Servers"), ",") End If ' Get the posted signature from the form Dim postedSignature As String = Request.Form("signature") If postedSignature = "" Then sLogError = "Warning: " & orderId & " :: " & processorOrderId & " :: " & "Missing Signature." GoTo Bail End If ' Verify that we are the intended merchant If arrPostedVariables("merchant_id") <> merchant_id Then sLogError = "Warning: " & orderId & " :: " & processorOrderId & " :: " & "Invalid merchantId." GoTo Bail End If ' Check if this is a legitimate request from the payment processor Dim sServer As String = CStr(Request.UserHostAddress) Dim bIp As Boolean = False For i = LBound(arrStr) To UBound(arrStr) If sServer = arrStr(i) Then bIp = True Exit For End If Next If Not bIp Then sLogError = "Warning: " & orderId & " :: " & processorOrderId & " :: " & "Invalid notification source." GoTo Bail End If ' Check if order already processed If CheckOrderStatus(orderId) <> "PROVISIONAL" Then sLogError = "Warning: " & orderId & " :: " & processorOrderId & " :: " & "Order already processed." GoTo Bail End If ' The request is legitimate. Post back to payment processor to validate the data received Dim wc As System.Net.WebClient Try wc = New System.Net.WebClient Dim arrResponse As Byte() = wc.UploadValues(site, "POST", arrPostedVariables) Dim result As String = System.Text.Encoding.ASCII.GetString(arrResponse) ' Get the response and replace the line breaks with spaces result = result.Replace(vbCrLf, " ").Replace(vbCr, " ").Replace(vbLf, " ") ' Was the data valid? If Not result.StartsWith("VALID") Then sLogError = "Warning: " & orderId & " :: " & processorOrderId & " :: " & "Validation failed." GoTo Bail End If Catch ex As Exception sLogError = "Error (PayFastNotify_Page_Load): " & ex.Source & " :: " & ex.Message & vbCrLf & ex.StackTrace Finally wc.Dispose() If sLogError <> "" Then GoTo Bail End If End Try Catch ex As Exception sLogError = "Error (PayFastNotify_Page_Load): " & ex.Source & " :: " & ex.Message & vbCrLf & ex.StackTrace GoTo Bail End Try ' Confirm order ' Add some parameters to the arrPostedVariables collection to pass to the ConfirmOrder Sub arrPostedVariables.Add("signature", Request.Form("signature").ToString) arrPostedVariables.Add("pf_server_ip", CStr(Request.UserHostAddress)) ConfirmOrder(orderId, arrPostedVariables) Exit Sub Bail: ' Log any errors or warnings to the database UpdateLog(sErrorLog) End If End Sub