使用VB MVC 3与Razor…我可以在任何控制器中创建PayPal ipn吗?比如我把它放到它自己的控制器里,比如IPN。URL不应该是http://www.testsite.com/IPN吗?如果这是正确的。我是否需要创建一个名为Ipn的空白视图页面,或者Ipn动作名称是什么?在这个视图中,它需要包含任何razor语法吗?我已经回到了我能找到的关于IPN的每一个帖子,但是没有MVC 3 vb.net。这是经过编辑的…我已经得到返回状态200的IPN处理程序。当我通过测试器运行它时,我获得了成功……然而,实际的测试事务没有被输入到数据库中……有人知道为什么是????吗Ipn控制器的功能如下:
End Function
<AcceptVerbs(HttpVerbs.Post)>
Function IPN_Handler(ByVal sender As Object, ByVal e As System.EventArgs)
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 <> "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 + "&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 > 0
Dim IpnResponse As New [String](read, 0, count)
count = readStream.Read(read, 0, 256)
If IpnResponse = "VERIFIED" Then
Dim db As New mysql_31309_schoolEntities
Dim _payment_tracker As New payment_tracker
_payment_tracker.txn_id = Txn_id
_payment_tracker.Class_1_ID = Item_name1
_payment_tracker.Class_2_ID = Item_name2
_payment_tracker.Class_3_ID = Item_name3
_payment_tracker.Class_4_ID = Item_name4
_payment_tracker.num_cart_items = num_cart_items
_payment_tracker.reciever_email = Receiver_email
_payment_tracker.payer_id = Custom
_payment_tracker.payment_status = Payment_status
_payment_tracker.payment_date = Payment_date
_payment_tracker.first_name = First_name
_payment_tracker.last_name = Last_name
_payment_tracker.address1 = Address_street
_payment_tracker.city = Address_city
_payment_tracker.state = Address_state
_payment_tracker.zipcode = Address_zip
_payment_tracker.payment_fee = Payment_fee
_payment_tracker.payment_gross = Payment_gross
If Payment_status <> "Pending" Then
_payment_tracker.pending_reason = " "
Else
_payment_tracker.payment_status = Payment_status
End If
_payment_tracker.ipn_track_id = Ipn_Track_Id
db.payment_tracker.AddObject(_payment_tracker)
db.SaveChanges()
Else
If Payment_status = "Completed" Then
Dim reg As reg_info = db.reg_info.Single(Function(f) f.id = Payer_id)
reg.paid = "Paid"
reg.date_paid = Payment_date
reg.payment_method = Payment_type
db.SaveChanges()
Else
If IpnResponse = "INVALID" Then
Return Nothing
End If
End If
End If
End While
readStream.Close()
strResponse.Close()
Return Nothing
End Function
Paypal IPN和MVC 3vb。NET与远程托管相结合时,可能不仅仅是一个挑战…我终于把它修好了……而不是通过创建一个控制器函数…我最终为ipn制作了一个aspx应用程序。把它丢到根文件夹里。指向paypal的aspx文件。然后在我实际的主控制器中做一个简短的例程检查对ipn写入的表所做的更改。如果它发现任何新的和已完成的,它就从那里处理付款。这是我能够不得到500错误的唯一方法……为了成功调试ipn,我实际上从一个ipn的朴素paypal源代码开始…让它在IPN测试器上显示成功,然后从那里我为它要做的每个任务添加了一小块代码。然后我会再次通过paypal ipn测试器进行测试……如果它成功了,那么我知道我可以继续编写更多的代码……如果在任何时候500都失败了,我认为这是我刚刚添加的东西造成的……我知道没有多大帮助,但我想我从工作到失败的两周,不知道为什么可能会帮助别人…
我不能强调最后一部分有多重要。刚开始只使用一个非常基本的IPN处理程序,因为调试有点复杂,因为它不能通过vs正常调试,从小的开始,并在每次更改后使用paypal IPN测试器进行测试,以确保它没有损坏。
为了帮助那些发现文档过于贫乏的人。下面是一个paypal IPN处理器的工作示例。
这个asp.net视图只是一个空白的asp.net服务器页面,以这个例子命名为IPN_Handler。ascx。和如下:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Ipn_Handler.aspx.vb" Inherits="yourNamespace.Ipn_Handler" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
文件背后的代码示例如下。请注意,这来自一个工作的IPN处理程序,所以有几个引用,你可能不需要。
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 <> "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 + "&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 > 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
一种简单的方法可以测试您是否将这个示例放置在适合您的设置的适当位置,那就是浏览到IPN本身。也许在IPN_Handler的视图标记中放置一个简单的消息。ascx页面。无论你用什么url来浏览它是一个你需要在paypal的设置中输入IPN url地址。不要给我发关于Localhost问题的消息。这需要在远程可访问的服务器上进行测试。