Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Paypal IPN and MVC 3 VB.NET when combined with remote hosting can be more than a challenge... I finally got it working... Not by creating a controller function at all... I ended up making a aspx app. for the ipn.. Throwing it in the root folder. Pointing paypal to the aspx file. And then making a short routine in my actual home controller that checks for changes made to the table that the ipn writes to. If it finds any that are new and completed it processes the payment from there. That is the only way I have been able to not get a 500 error... To debug the ipn successfully I actually started with the plain paypal source code for a ipn... Got it saying successful on the IPN tester then from there I added small blocks of code to it for each of the tasks it is to do... Then I would run it through the paypal ipn tester again... If It was successful then I knew I could carry on with a little more code... If it failed with 500 at anytime I new that it was something in what I just added... Not much help I know but I figure my 2 weeks of going from working to failing and not knowing why might help someone else out...</p> <p>I cannot stress how important the last part of that is. Just use a very basic IPN handler at first because debugging is kind of complicated as it is not able to be debugged normally through VS. Start out small and work your way up testing it with the paypal ipn tester after each change to make sure that it is not broken. </p> <p>In the interest of helping others out who find the documentation to be overly poor. The below is a working example of a paypal IPN handler. </p> <p>The asp.net view is just a blank asp.net server page named for this example IPN_Handler.ascx . And is as follows:</p> <pre><code>&lt;%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Ipn_Handler.aspx.vb" Inherits="yourNamespace.Ipn_Handler" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; </code></pre> <p></p> <p>And example for the code behind file is as follows. Please note that this came from a working IPN handler so there are SEVERAL references that you may not need.. </p> <pre><code> Imports System.Net Imports System.IO Imports System.Text Imports System.Collections.Specialized Imports System.Web.Mail Imports MySql.Data.MySqlClient Imports System.Security.Principal Imports System.Data Imports System.Linq Imports System.Web.Mvc Imports System.Reflection Imports System.Data.OleDb Imports System.ComponentModel Public Class Ipn_Handler Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim strFormValues As String = Request.Form.ToString() Dim strNewValue Dim Txn_id As String = Request.Form("txn_id") Dim mc_gross_1 As String = Request.Form("mc_gross_1") Dim mc_gross_2 As String = Request.Form("mc_gross_2") Dim mc_gross_3 As String = Request.Form("mc_gross_3") Dim mc_gross_4 As String = Request.Form("mc_gross_4") Dim num_cart_items As String = Request.Form("num_cart_items") Dim Receiver_email As String = Request.Form("receiver_email") Dim Item_name1 As String = Request.Form("item_name1") Dim Item_name2 As String = Request.Form("item_name2") Dim Item_name3 As String = Request.Form("item_name3") Dim Item_name4 As String = Request.Form("item_name4") Dim Quantity As String = Request.Form("quantity") Dim Invoice As String = Request.Form("invoice") Dim Custom As Integer = Request.Form("custom") Dim transaction_subject As Integer = Request.Form("transaction_subject") Dim Payment_status As String = Request.Form("payment_status") Dim Pending_reason As String = Request.Form("pending_reason") If Payment_status &lt;&gt; "Pending" Then Pending_reason = " " End If Dim Payment_date As String = Request.Form("payment_date") Dim Payment_fee As String = Request.Form("payment_fee") Dim Payment_gross As String = Request.Form("payment_gross") Dim Txn_type As String = Request.Form("txn_type") Dim First_name As String = Request.Form("first_name") Dim Last_name As String = Request.Form("last_name") Dim Address_street As String = Request.Form("address_street") Dim Address_city As String = Request.Form("address_city") Dim Address_state As String = Request.Form("address_state") Dim Address_zip As String = Request.Form("address_zip") Dim Address_country As String = Request.Form("address_country") Dim Address_status As String = Request.Form("address_status") Dim Address_country_code As String = Request.Form("address_country_code") Dim Payer_email As String = Request.Form("payer_email") Dim Payer_status As String = Request.Form("payer_status") Dim Payer_id As Integer = Request.Form("payer_id") Dim Payment_type As String = Request.Form("payment_type") Dim Notify_version As String = Request.Form("notify_version") Dim Verify_sign As String = Request.Form("verify_sign") Dim Ipn_Track_Id As String = Request.Form("ipn_track_id") Dim req As HttpWebRequest = CType(WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr"), _ HttpWebRequest) req.Method = "POST" req.ContentType = "application/x-www-form-urlencoded" strNewValue = strFormValues + "&amp;cmd=_notify-validate" req.ContentLength = strNewValue.Length Dim stOut As StreamWriter = New StreamWriter(req.GetRequestStream(), _ Encoding.ASCII) stOut.Write(strNewValue) stOut.Close() Dim strResponse As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse) Dim ipnResponseStream As Stream = strResponse.GetResponseStream Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8") Dim readStream As New StreamReader(ipnResponseStream, encode) Dim read(256) As [Char] Dim count As Integer = readStream.Read(read, 0, 256) While count &gt; 0 Dim IpnResponse As New [String](read, 0, count) count = readStream.Read(read, 0, 256) If IpnResponse = "VERIFIED" Then '//Logic to handle what to do on Verified response. If Payment_status = "Completed" Then '// The Payment_status variable can be used to trap a completed payment response and do work. End If ElseIf IpnResponse = "INVALID" Then '// This is where possible hacking attempts will be caught by paypal and returned as invalid. Else End If End While readStream.Close() strResponse.Close() End Sub End Class </code></pre> <p>A simple way to test if you have placed this example in the proper location for your setup is to browse to the IPN itself. Maybe placing a simple message in the view markup of the IPN_Handler.ascx page. Whatever url you use to browse to it is the one that you will need to enter at paypal in the setup for IPN URL address. Don't message me about Localhost issues. This needs to be tested on a remote accessible server. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload