.Net集成PayPal的Demo

.Net集成PayPal的Demo

近来项目中需要使用Paypal(贝宝)支付,研究了一下接口,真是吐血,花了一个下午+一个晚上,屡败屡战,海淘了若干文档,终于尝试成功了,分享一下,希望对将要使用paypal的朋友有所帮助。

paypal提供3种类型的对外付款接口,可参考:https://cms.paypal.com/c2/cgi-bin/?cmd=_render-content&content_ID=pages_c2/profile_comparison_new&fli=true

我们这里采用标准的网页版本,其中标准的网页版本又分为三种实现方式:

1.按钮方式,也就是一个商品一个按钮

2.购物车方式,让paypal托管你的购物车

3.你自己的购物网站有购物车,付款时候将购物车数据提交到paypal即可付款

考虑自由度问题,当然我们需要第三种方式。

准备工作

1.到www.paypal.com中申请一个正式的账号,注意银行卡目前只能用信用卡和借记卡,并且需要支持不同币种的,否则可能开通失败。

2.用刚才申请的账号到https://developer.paypal.com/ 中创建开发者模拟账号,为啥?因为开发者账号测试不需要收费

这里需要创建两个账号,买家和卖家,默认系统会给你创建卖家的。

账号创建好了之后,注意修改每个账号的登录密码,在profile里改。

哦,忘记了,创建买家账号的时候,千万别忘记充值哦,充值金额<10000刀。

3.设置好了之后,我们到沙箱里登录卖家账号,单击链接 Enter Sandbox site,用Business类型账号登录,然后做如下设置

 IPN即时付款通知的意义是什么呢?交互原理如下:

我们提交购物车到paypal,完成付款以后,paypal会想我们设置好的IPN代理,发送消息,从而使我们自己的程序得知是否付款成功,以备后续操作,而且这个操作是在后台调用的,其主要目的就是为了防止用户主动关闭浏览器,造成程序订单状态无法更新的问题。

付款自动返回设置,指的是完成付款之后跳转到我们自己网站的界面,这个是显示给用户看的,而且这个接收界面,paypal会发送相关订单数据,付款状态等到这个页面上,也就是“付款数据传输(可选)”这项,勾上之后在返回URL界面就可接收到相关数据了(用户付款完成后,恶意关闭的情况除外)

4.当然上面两个值在账号里面设置是全局的,你可以在具体的提交按钮里面设置

5.在我们的购物网站,实现购物车界面代码:

复制代码
    <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_cart">
        <input type="hidden" name="upload" value="1">
        <input type="hidden" name="business" value="jacsonwu-facilitator@gmail.com">
<input type="hidden" name="item_name_1" value="San Francisco Bay(32'X32')"> <input type="hidden" name="amount_1" value="1.00"> <input type="hidden" name="quantity_1" value="2">
<input type="hidden" name="item_name_2" value="Mount Hamilton(24'x15')"> <input type="hidden" name="amount_2" value="1.00"> <input type="hidden" name="quantity_2" value="1">
<input type="hidden" name="currency_code" value="CNY">
     <!--这里重写url,将覆盖全局设置--> <input type="hidden" name="return" value="http://127.0.0.1:56508/ok.aspx"> <input type="Hidden" name="notify_url" value="http://127.0.0.1:56508/pp.aspx" />
<input type="submit" value="Upload Cart" alt="Make payments with PayPal-it's fastfree and secure!" /> </form>
复制代码

6.付款成功界面代码,也就是return

复制代码
string strFormValues = Request.Params.ToString();
            string strNewValue;
            string strResponse;
            string serverURL = "https://www.sandbox.paypal.com/cgi-bin/webscr";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(serverURL);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            strNewValue = "cmd=_notify-validate";
            strNewValue = "cmd=_notify-synch&tx=" + Request.QueryString["tx"] + "&at=access_token你的";
            //req.ContentLength = strNewValue.Length;

            StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
            stOut.Write(strNewValue);
            stOut.Close();

            StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
            strResponse = stIn.ReadToEnd();
            stIn.Close();
            if (strResponse.StartsWith("SUCCESS"))
            {
            }
            else
            {
            }
复制代码

 7.IPN后台数据接收

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

        //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("");
        string logPath = string.Format("{0}\{1}.txt",
                         Server.MapPath(logPathDir), DateTime.Now.Ticks);
        File.WriteAllText(logPath, ipnPost + " " + strResponse);
        //

        if (strResponse == "VERIFIED")
        {
            //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
        }
        else if (strResponse == "INVALID")
        {
            //log for manual investigation
        }
        else
        {
            //log response/ipn data for manual investigation
        }
        Response.Write(strResponse);
复制代码

 

原文地址:https://www.cnblogs.com/Leo_wl/p/3764908.html