将 PayPal 与基于 WebMatrix 的网站集成 (Razor/C#)
我花了无数个小时阅读 PayPal 网站及其 x.com 网站上的文档,并在整个网络上搜索了这一点,但不幸的是,我需要一些帮助。
我在我的网站上提供一项服务,我想做的就是将客户发送到贝宝网站(我可以做到),并让他们付款(他们可以做到),然后让他们一旦付款被批准/完成(这就是发生的情况),就会被重定向回我的网站,但这就是一切停止的地方......
我已经尝试了很多教程和示例,但这里变得相当困难。我需要能够在我的网站上进行简单的检查(IPN 的事情)。这样我就知道是否有人通过 PayPal 支付了该服务的费用,这样我就知道是否应该在我的网站上为该特定客户激活该服务。
我知道有一个 PayPal Helper,但在这种情况下这对我没有帮助,因为它不处理 IPN 内容。
注:本服务不是订阅服务,只是一次性付费。
我非常感谢您对此的帮助,我真的很困惑,不知道下一步该怎么做。
更新
所以我测试了@Bobby 在他的答案中提供的脚本,并且我收到一个错误:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>
注意:关闭 CustomErrors 后,我仍然收到此消息。
更新 #2
当我尝试输出 string ipnPost = strRequest; 的结果时它显示一条消息“状态无效 - form_charset=UTF8”
这就是它的全部内容。我不知道为什么..
最新更新 -
我现在使用以下代码收到以下错误:
“/”应用程序中的服务器错误。
您必须先将 ContentLength 字节写入请求流 调用 [Begin]GetResponse。描述:未处理的异常 发生在当前 Web 请求执行期间。请 查看堆栈跟踪以获取有关错误及其位置的更多信息 它起源于代码。
异常详细信息:System.Net.ProtocolViolationException:您必须 在调用之前将 ContentLength 字节写入请求流 [开始]获取响应。
来源错误:
第 34 行:第 35 行:第 36 行:StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());第 37 行:
字符串 strResponse = StreamIn.ReadToEnd();streamOut.Close();第 38 行: StreamIn.Close();源文件: c:\HostingSpaces\sssssssss\sssssss.com\wwwroot\Checkout\Status.cshtml 行数:36
堆栈跟踪:
[ProtocolViolationException:您必须将 ContentLength 字节写入 调用 [Begin]GetResponse 之前的请求流。]
System.Net.HttpWebRequest.GetResponse() +7769822
ASP._Page_Checkout_Status_cshtml.Execute() 中 c:\HostingSpaces\ssssss\ssssssss.com\wwwroot\Checkout\Status.cshtml:36 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
System.Web.WebPages.WebPage.ExecutePageHierarchy() +369
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext、TextWriter writer、WebPageRenderingBase startPage) +157
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContext 上下文)+294------------------------------------------------------------ --------------------------------- 版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET版本:4.0.30319.1
@using System.Collections.Generic
@using System.Text
@using System.Web
@using System.Web.UI
@using System.Web.UI.HtmlControls
@using System.Web.UI.WebControls
@using System.ComponentModel
@{
Layout = "~/_SiteLayout.cshtml";
Page.Title = "Checkout | sssss";
string postUrl = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(postUrl);
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = System.Text.Encoding.UTF8.GetString(param);
string ipnPost = strRequest;
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
//for proxy
//WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
//req.Proxy = proxy;
//Send the request to PayPal and get the response
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(),
System.Text.Encoding.UTF8);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
/*/ logging ipn messages... be sure that you give write
// permission to process executing this code
string logPathDir = ResolveUrl("Messages");
string logPath = string.Format("{0}\\{1}.txt",
Server.MapPath(logPathDir), DateTime.Now.Ticks);
File.WriteAllText(logPath, ipnPost);
/*/
}
@if (strResponse == "VERIFIED")
{
/*---------------- WILL DO OTHER CHECKS LATER ------------------*/
//check the payment_status is Completed
<p>status is verified</p>
//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
}
else if (strResponse == "INVALID")
{
//log for manual investigation
<p>status is invalid.</p>
<p>@ipnPost</p>
}
else
{
//log response/ipn data for manual investigation
<p>status is invalid.</p>
<p>@ipnPost</p>
}
I have spent countless hours reading over the docs on PayPal's site and also on their x.com site, and have searched all over the web for this, but unfortunately, I am in need of some help.
I have a service that I offer on my site, and all I am trying to do is to send the customer off to the paypal site (which I can do), and let them pay (which they can do), and then have them be redirected back to my site once the payment has been approved/made (which is what happens), but that's where it all stops...
I have tried so many tutorials and samples out there, and it's becoming quite difficult here. I need to be able to do simple checks on my site (IPN thing). So that I know whether or not someone has paid for the service at paypal, so I know whether or not I should activate the service on my site for that particular customer.
I know there is a PayPal Helper, but that does not help me in this situation since it does not deal with the IPN stuff.
Note: This service is not a subscription service, it is just a one-off payment.
I would really appreciate your help on this, I really am stuck and don't know what to do next.
UPDATE
So I have tested the script which @Bobby provided in his answer, and I am receiving an error:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>
NOTE: After turning CustomErrors OFF, I still receive this message.
UPDATE #2
When I try to output the result of string ipnPost = strRequest; it displays a message "status is invalid - form_charset=UTF8"
That's all it says. I don't know why..
LATEST UPDATE -
I am now receiving the following error using the following code:
Server Error in '/' Application.
You must write ContentLength bytes to the request stream before
calling [Begin]GetResponse. Description: An unhandled exception
occurred during the execution of the current web request. Please
review the stack trace for more information about the error and where
it originated in the code.Exception Details: System.Net.ProtocolViolationException: You must
write ContentLength bytes to the request stream before calling
[Begin]GetResponse.Source Error:
Line 34: Line 35: Line 36: StreamReader streamIn = new
StreamReader(req.GetResponse().GetResponseStream()); Line 37:
string strResponse = streamIn.ReadToEnd();streamOut.Close(); Line 38:
streamIn.Close();Source File:
c:\HostingSpaces\sssssssss\sssssss.com\wwwroot\Checkout\Status.cshtml
Line: 36Stack Trace:
[ProtocolViolationException: You must write ContentLength bytes to the
request stream before calling [Begin]GetResponse.]
System.Net.HttpWebRequest.GetResponse() +7769822
ASP._Page_Checkout_Status_cshtml.Execute() in
c:\HostingSpaces\ssssss\ssssssss.com\wwwroot\Checkout\Status.cshtml:36
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
System.Web.WebPages.WebPage.ExecutePageHierarchy() +369
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext
pageContext, TextWriter writer, WebPageRenderingBase startPage) +157
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContext
context) +294-------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.1
@using System.Collections.Generic
@using System.Text
@using System.Web
@using System.Web.UI
@using System.Web.UI.HtmlControls
@using System.Web.UI.WebControls
@using System.ComponentModel
@{
Layout = "~/_SiteLayout.cshtml";
Page.Title = "Checkout | sssss";
string postUrl = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(postUrl);
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = System.Text.Encoding.UTF8.GetString(param);
string ipnPost = strRequest;
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
//for proxy
//WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
//req.Proxy = proxy;
//Send the request to PayPal and get the response
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(),
System.Text.Encoding.UTF8);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
/*/ logging ipn messages... be sure that you give write
// permission to process executing this code
string logPathDir = ResolveUrl("Messages");
string logPath = string.Format("{0}\\{1}.txt",
Server.MapPath(logPathDir), DateTime.Now.Ticks);
File.WriteAllText(logPath, ipnPost);
/*/
}
@if (strResponse == "VERIFIED")
{
/*---------------- WILL DO OTHER CHECKS LATER ------------------*/
//check the payment_status is Completed
<p>status is verified</p>
//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
}
else if (strResponse == "INVALID")
{
//log for manual investigation
<p>status is invalid.</p>
<p>@ipnPost</p>
}
else
{
//log response/ipn data for manual investigation
<p>status is invalid.</p>
<p>@ipnPost</p>
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论