贝宝ASP.NET C#获取引用者 [英] ASP.NET C# Get referer from Paypal

查看:179
本文介绍了贝宝ASP.NET C#获取引用者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的麻烦。我做了一个电子商务网站的许多个月前,现在我想做的事情,一个用户买了之后,贝宝重定向一个网站,确认买了一个电子邮件用户。

I have a big trouble. I've done an e-commerce site many month ago, now i wanna do that after an user has bought, paypal redirect the user in a website that confirm the bought with an e-mail.

显然,返回的页面有refererpage的控制,因为它不能发送确认邮件到所有写网页的地址,但该命令:

Obviously, the return page has a control of refererpage, because it can't send a confirmation mail to all that write the page address, but the command:

Request.ServerVariables["HTTP_REFERER"]

不行的,因为贝宝是HTTPS网页。所以,我怎么能解决呢?

Doesn't work, because paypal is an https webpage. So, how i can solve it?

在谢谢!

推荐答案

您好,你可以试试下面code此:

hi you can try below code for this:

首先设置notify_url在您的网站

First set the "notify_url" in your website

 <input type="hidden" name="notify_url" value="http://www.your-website-url.com/notifypaypal.aspx" />

之后,创建notifypaypal.aspx页。

after that create notifypaypal.aspx Page.

notifypaypal.aspx:这里不需要任何code ..

notifypaypal.aspx : here not required any code ..

notifypaypal.aspx.cs:

notifypaypal.aspx.cs :

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Threading;
using System.Net;
using BLL;

public partial class notifypaypal : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {


        //Post back to either sandbox or live
       // string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";//For localhost
        string strLive = "https://www.paypal.com/cgi-bin/webscr";//For live server
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strLive);

        //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 = Encoding.ASCII.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.ASCII);
        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")
        {

            #region [Update Order Status]
            string txn_id = HttpContext.Current.Request["txn_id"]; //txn_id=    Unique transaction number.
            string payment_status = HttpContext.Current.Request["payment_status"];   //payment_status=Payment state(Completed,Pending,Failed,Denied,Refunded)
            string pending_reason = HttpContext.Current.Request["pending_reason"];   //pending_reason=Reason of payment delay(echeck,multi_currency,intl,verify,...)
            string item_number = HttpContext.Current.Request["item_number"];  //item_number=order number


            if (HttpContext.Current.Request["payment_status"].ToString() == "Completed")
            {
                try
                {
//update in database that particular "item_number"(Order number) is successfully  Completed

                }
                catch
                {
                }
            }
            else
            {
                if (HttpContext.Current.Request["payment_status"].ToString() == "Pending")
                {
                    try
                    {
                        //update in database that particular "item_number"(Order number) is Pending
                    }
                    catch
                    {
                    }
                }
                else
                {

                }
            }
            #endregion


        }
        else if (strResponse == "INVALID")
        {

        }
        else
        {


        }
    }     

}

这篇关于贝宝ASP.NET C#获取引用者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆