Code:
'Post back to either sandbox or live
Dim strSandbox As String = "https://www.sandbox.paypal.com/cgi-bin/webscr"
Dim strLive As String = "https://www.paypal.com/cgi-bin/webscr"
Dim req As HttpWebRequest = CType(WebRequest.Create(strSandbox), HttpWebRequest)
'Set values for the request back
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
Dim Param() As Byte = Request.BinaryRead(HttpContext.Current.Request.ContentLength)
Dim strRequest As String = Encoding.ASCII.GetString(Param)
strRequest = strRequest + "&cmd=_notify-validate"
req.ContentLength = strRequest.Length
'for proxy
'Dim proxy As New WebProxy(New System.Uri("http://url:port#"))
'req.Proxy = proxy
'Send the request to PayPal and get the response
Dim streamOut As StreamWriter = New StreamWriter(req.GetRequestStream(), Encoding.ASCII)
streamOut.Write(strRequest)
streamOut.Close()
Dim streamIn As StreamReader = New StreamReader(req.GetResponse().GetResponseStream())
Dim strResponse As String = streamIn.ReadToEnd()
streamIn.Close()
Dim order_item_no, order_trans_id, order_pay_status As String
order_item_no = Request.Form("item_number1")
order_trans_id = Request.Form("txn_id")
order_pay_status = Request.Form("payment_status")
If strResponse = "VERIFIED" Then
'check the payment_status is Completed
'check that txn_id has not been previously processed
'check that receiver_email is your Primary PayPal email
'check that payment_amount/payment_currency are correct
'process payment
If order_pay_status = "Completed" Then
If Mid(order_item_no, 1, 1) = "Z" Then
order_item_no = order_item_no.Replace("Z", "")
Call GetInvoiceDetails("", "", "", order_item_no, "")
Call DBgeneral("UPDATE tblInvoice SET invPaid='1' WHERE invID=" & order_item_no & ";")
Call DBgeneral("INSERT INTO tblInvoiceInfo(iiInvoiceID,iiType,iiReference,iiNote) VALUES('" & order_item_no & "','1','" & order_trans_id & "','PayPal Payment made!');")
Call SendMail(InvUser(0), "Payment received with thanks!", "1", "payment_received", order_item_no, "")
If InvStatus(0) = "0" Then
Call DBgeneral("UPDATE tblInvoice SET invStatus='1' WHERE invID=" & order_item_no)
End If
End If
Else
End If
Call SendMail("", "Completed:Order PPI", "0", "Order ID:" & order_item_no & " <p>Completed<p>", "", "")
ElseIf strResponse = "INVALID" Then
'log for manual investigation
' Call SendMail("", "Invalid:Order PPI", order_item_no & "<p>" & order_trans_id, "", "")
Call SendMail("", "INVALID:Order PPI Response:" & strResponse, "0", "Order ID:" & order_item_no & " <p>Completed<p>", "", "")
Else
'Response wasn't VERIFIED or INVALID, log for manual investigation
' Call SendMail("", "Other:Order PPI", order_item_no & "<p>" & order_trans_id, "", "")
Call SendMail("", "ELSE:Order PPI", "0", "Order ID:" & order_item_no & " <p>Completed<p>", "", "")
End If
' Call SendMail("", "API LOAD0", "0", "Order ID:" & order_item_no & " <p>Completed<p>", "", "")
Call SendMail("", "API DATA", "0", strResponse, "", "")