|
| 1 | + |
| 2 | +Partial Class nochexapc_vb |
| 3 | + Inherits System.Web.UI.Page |
| 4 | + |
| 5 | + Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load |
| 6 | + |
| 7 | + Dim toEmail As String = "[email protected]" |
| 8 | + |
| 9 | + Try |
| 10 | + |
| 11 | + ' Get all the POST details into a NameValueCollection |
| 12 | + Dim nvc As NameValueCollection = Request.Form |
| 13 | + |
| 14 | + |
| 15 | + 'Uncomment below to force a DECLINED response |
| 16 | + 'nvc = Request.GetType.GetField("_form", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance).GetValue(Request) |
| 17 | + 'Dim readable As Reflection.PropertyInfo = nvc.GetType.GetProperty("IsReadOnly", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance) |
| 18 | + 'readable.SetValue(nvc, False, Nothing) |
| 19 | + 'nvc("order_id") = "1" |
| 20 | + 'readable.SetValue(nvc, True, Nothing) |
| 21 | + |
| 22 | + ' Get all the POST details from the NameValueCollection and convert to String |
| 23 | + Dim postdetails As String = nvc.ToString |
| 24 | + |
| 25 | + ' Create a request to the Nochex server. |
| 26 | + 'Dim webrequest As Net.HttpWebRequest = Net.WebRequest.Create("https://secure.nochex.com/callback/callback.aspx") |
| 27 | + Dim webrequest As Net.HttpWebRequest = Net.WebRequest.Create("https://www.nochex.com/apcnet/apc.aspx") |
| 28 | + ' Set as a POST request. |
| 29 | + webrequest.Method = "POST" |
| 30 | + webrequest.ContentType = "application/x-www-form-urlencoded" |
| 31 | + ' Encode the POST details into bytes. |
| 32 | + Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postdetails) |
| 33 | + webrequest.ContentLength = byteArray.Length |
| 34 | + |
| 35 | + ' Create a stream object to send the POST details. |
| 36 | + Dim dataStream As IO.Stream = webrequest.GetRequestStream |
| 37 | + ' Write data to the stream object. |
| 38 | + dataStream.Write(byteArray, 0, byteArray.Length) |
| 39 | + ' Close the data stream object. |
| 40 | + dataStream.Close() |
| 41 | + |
| 42 | + ' Get the response. |
| 43 | + Dim webresponse As Net.HttpWebResponse = webrequest.GetResponse |
| 44 | + ' Create reader to get response. |
| 45 | + Dim reader As New IO.StreamReader(webresponse.GetResponseStream) |
| 46 | + ' Return the APC response as a String. |
| 47 | + Dim apcresponse As String = reader.ReadToEnd |
| 48 | + ' Close the reader. |
| 49 | + reader.Close() |
| 50 | + |
| 51 | + If apcresponse = "AUTHORISED" Then |
| 52 | + ' If APC repsonse is AUTHORISED do something. |
| 53 | + |
| 54 | + ' Create a mail object. |
| 55 | + Dim mail As New Net.Mail.MailMessage() |
| 56 | + ' Set SMTP details and port if required. |
| 57 | + Dim smtpClient As New Net.Mail.SmtpClient("mail.nochex.com") |
| 58 | + |
| 59 | + ' Add credentials if the SMTP server requires them. |
| 60 | + ' smtpClient.Credentials = Net.CredentialCache.DefaultNetworkCredentials |
| 61 | + |
| 62 | + ' Specify the from and to email address along with the subject and body Strings. |
| 63 | + ' The sender of the email. |
| 64 | + mail.From = New Net.Mail.MailAddress("[email protected]") |
| 65 | + ' The recipient of the email. |
| 66 | + mail.To.Add("" + toEmail + "") |
| 67 | + 'Subject of the email, which should reflect the apc response, this should be Authorised. |
| 68 | + mail.Subject = "APC AUTHORISED" |
| 69 | + ' Contents of the email which will display the response of the APC. The response should be Authorised. |
| 70 | + mail.Body = "-- APC Response: " + apcresponse |
| 71 | + |
| 72 | + Dim formvalues As NameValueCollection = Request.Form |
| 73 | + ' Loop through the POST details and attaches them to the email body after the response of the apc. These variables are collected from the APC Post. |
| 74 | + For Each formkey As String In formvalues.AllKeys |
| 75 | + mail.Body += Environment.NewLine + " -- " + formkey + " -- " + formvalues(formkey) |
| 76 | + Next |
| 77 | + |
| 78 | + ' Sends the complete email. (Headers, Subject, Contents.) |
| 79 | + smtpClient.Send(mail) |
| 80 | + |
| 81 | + Else |
| 82 | + ' If the APC response is DECLINED email results and investigate |
| 83 | + |
| 84 | + ' Create a mail object |
| 85 | + Dim mail As New Net.Mail.MailMessage() |
| 86 | + |
| 87 | + ' Set SMTP details and port if required |
| 88 | + Dim smtpClient As New Net.Mail.SmtpClient("mail.nochex.com") |
| 89 | + |
| 90 | + ' Add credentials if the SMTP server requires them. |
| 91 | + ' smtpClient.Credentials = Net.CredentialCache.DefaultNetworkCredentials |
| 92 | + |
| 93 | + ' Specify the from and to email address along with the subject and body Strings. |
| 94 | + ' The address of the sender. |
| 95 | + mail.From = New Net.Mail.MailAddress("[email protected]") |
| 96 | + ' The address of the recipient. |
| 97 | + mail.To.Add("" + toEmail + "") |
| 98 | + ' Subject of the email, which displays as Declined. |
| 99 | + mail.Subject = "APC DECLINED - VB" |
| 100 | + ' Contents of the email which will display the response of the APC. The response should be Declined. |
| 101 | + mail.Body = "-- APC Response: " + apcresponse |
| 102 | + |
| 103 | + Dim formvalues As NameValueCollection = Request.Form |
| 104 | + ' Loop through the POST details and attaches them to the email body after the response of the apc. These variables are collected from the APC Post. |
| 105 | + For Each formkey As String In formvalues.AllKeys |
| 106 | + mail.Body += Environment.NewLine + " -- " + formkey + " -- " + formvalues(formkey) + ". " |
| 107 | + Next |
| 108 | + |
| 109 | + ' Sends the complete email. (Headers, Subject, Contents.) |
| 110 | + smtpClient.Send(mail) |
| 111 | + End If |
| 112 | + |
| 113 | + Catch ex As Exception ' If an exception occured. Email the reason for failure |
| 114 | + ' Create a mail objection |
| 115 | + Dim mail As New Net.Mail.MailMessage() |
| 116 | + ' Set SMTP details and port if required |
| 117 | + Dim smtpClient As New Net.Mail.SmtpClient("mail.nochex.com") |
| 118 | + |
| 119 | + ' Add credentials if the SMTP server requires them. |
| 120 | + ' smtpClient.Credentials = Net.CredentialCache.DefaultNetworkCredentials. |
| 121 | + |
| 122 | + ' Specify the from and to email address along with the subject and body Strings. |
| 123 | + ' Sender of the Email. |
| 124 | + mail.From = New Net.Mail.MailAddress("[email protected]") |
| 125 | + ' Receipient of the Email. |
| 126 | + mail.To.Add("" + toEmail + "") |
| 127 | + ' Subject of the email, displayed if the response is not declined or authorised. |
| 128 | + mail.Subject = "Error" |
| 129 | + ' The body of the email will be the error message. |
| 130 | + mail.Body = ex.Message |
| 131 | + ' Sends the complete email. (Headers, Subject, Contents.) |
| 132 | + smtpClient.Send(mail) |
| 133 | + End Try |
| 134 | + End Sub |
| 135 | +End Class |
0 commit comments