PayPal支付功能实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Yubu_/article/details/74710427

系统要添加PayPal支付功能,使用最简单的方式,在网页上添加一个PayPal的支付按钮,进入PayPal支付页面,支付成功后返回系统。

1、支付按钮添加

在支付页面创建一个Form表单,包含以下重要字段

    <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_xclick">
        <input type="hidden" name="business" value="收款账号邮箱">
        <input type="hidden" name="item_name" value="商品描述">
        <input type="hidden" name="item_number" value="商品编号">
        <input type="hidden" name="currency_code" value="货币单位,如USD,EUR等">
        <input type="hidden" name="amount" value="支付金额100">
        <input type="hidden" name="notify_url" value="。。。" />  <!--交易后paypal返回网站地址-->
        <input type="hidden" name="cancel_return" value="。。。" /> <!--客户取消交易后返回地址-->
        <input type="hidden" name="return" value="。。。" />  <!--交易后返回地址-->
        <input type="submit" value="PayPal">
    </form>

各字段的意思如上。Post以上表单,就会跳转到PayPal支付页面进行支付了。

由于我们这个系统使用WEBFORM开发的,支付页面已经有一个Form表单,Form不能嵌套,就将Form表单改成了DIV,通过js提交form表单。代码如下


<div class="" id="formDiv">
 <input type="hidden" runat="server" id="cmd" name="cmd">
<input type="hidden" runat="server" id="business" name="business">
<input type="hidden" runat="server" id="item_name" name="item_name">
<input type="hidden" runat="server" id="item_number" name="item_number">
<input type="hidden" runat="server" id="currency_code" name="currency_code">
<input type="hidden" runat="server" id="amount" name="amount">
<input type="hidden" runat="server" id="invoice" name="invoice" />
<input type="hidden" runat="server" id="notify_url" name="notify_url" />
<input type="hidden" runat="server" id="cancel_return" name="cancel_return" />
<input type="hidden" runat="server" id="return" name="return" />
<input type="button" id="btn_PayPal" value="PayPal支付" class="zf-paypai"/>
</div>

$("#btn_PayPal").click(function () {

                var formDivInputNodes = document.getElementById("formDiv").getElementsByTagName("input");

                var tempForm = document.createElement("form");
                tempForm.action = "https://www.sandbox.paypal.com/cgi-bin/webscr";
                tempForm.method = "Post";
                tempForm.target = "_self";
                tempForm.style.display = "none";
                

                for (var i = formDivInputNodes.length-1; i >=0; i--) {
                    tempForm.appendChild(formDivInputNodes[i]);
                }

                tempForm.acceptCharset = "GBK";
                document.charset = "GBK";
                document.body.appendChild(tempForm);
                tempForm.submit();
                document.body.removeChild(tempForm);
            })

说明:此处将form的编码格式设置成了GBK,是因为商品名称中有中文,设置成其它格式提交到PayPal页面会出现乱码的情况。

另外:此时PayPal支付出现的支付页面应该是这样子的


请注意红色框,这是地址栏。由于我们的系统已经生成订单,并且在生成订单的时候已经由客户输入并确认的订单地址,所以此处的地址显示会对客户造成误解。并且此处的地址是从客户的PayPal账号中获取的地址,和客户在我们系统中的地址难以统一,因此就想把这个删掉。很简单,在上面的Form提交的时候,加一个参数进去就好了。(找这个参数花了好长时间的 抓狂 安静)取消后如图

 <%--取消支付页面的地址设置 value=0 可以设置地址  value=1 不可以设置地址  --%>
                 <input type="hidden" runat="server" id="no_shipping" name="no_shipping" value="1" /> 





第二步 及时付款通知IPN,就是上面的  notify_url 的内容

买家付款结束,PayPal异步发送付款详细数据到notify_url,此时要检查付款的具体情况并且做出响应。


IPN通知示意图

(官方文档)
1)客户点击付款按钮进行付款
2)PayPal接受客户付款后,向notify_url通过Post方式发送IPN
3)服务器收到IPN后,必须将收到的POST信息原样返回给PayPal进行验证,PayPal通过词方法防范欺骗或“中间人”攻击
4)PayPal返回验证信息,通过验证为VERIFIED,不通过为INVALD
5)根据验证信息处理付款明细


IPN数据包含了付款过程的详细信息,通过获取并分析可以:
自定义网站对客户购物进行实时回复:以Email或者其它方式通知客户付款的状态;
自动履行相关操作:当收到IPN数据并确认付款状态已经完成后,可以立刻启动发货流程,或者虚拟货币的充值;
记录交易信息到数据库中。

以上是文档对IPN的主要介绍,下面上代码(C#)

try
            {
                //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 strNewRequest = strRequest + "&cmd=_notify-validate" ;
                req.ContentLength = strNewRequest.Length;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                //Send the request to PayPal and get the response
                StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
                streamOut.Write(strNewRequest);
                streamOut.Close();
                StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
                string strResponse = streamIn.ReadToEnd();
                streamIn.Close();

                string strItemName = Request["item_name"].ToString().Trim();
                string strItemNumber = Request["item_number"].ToString().Trim();
                string strPaymentStatus = Request["payment_status"].ToString().Trim();
                string strPaymentAmount = Request["mc_gross"].ToString().Trim();
                string strPaymentCurrency = Request["mc_currency"].ToString().Trim();
                string strTxnId = Request["txn_id"].ToString().Trim();
                string strReceiverEmail = Request["receiver_email"].ToString().Trim();
                string strPayerEmail = Request["payer_email"].ToString().Trim();
                string strInvoice = Request["invoice"].ToString().Trim();
                
                
                if (strResponse == "VERIFIED")
                {
                    if (strPaymentStatus.Equals("Pending"))
                    {
                        string strPendingReason = Request["pending_reason"].ToString().Trim();
                        WriteLog.Write("PayPal", "PayPal Payment: Pengding. Reason:"+strPendingReason, 0, "PayPal");
                        return;
                    }
                    // 检查订单状态(payment_status)是否为完成(Completed)
                    if (!"Completed".Equals(strPaymentStatus))
                    {
                        //do sth.
                        
                    }
                    // 如果订单状态为已完成,将PayPal唯一交易号(txn_id)与已经处理的PayPal交易对照检查确保不重复
                   //code 

                    // 检查收款人地址receiver_email is your Primary PayPal email
                  //code

	
	           // 检查价格和币种check that payment_amount/payment_currency are correct
                   //code

	          // process payment                    
                  //code                                        
                   }                
                  else if (strResponse == "INVALID")                
                 {                    
                       //do somethings                    
                       //code                
                 }                
                 else 
                {
                    //log response/ipn data for manual investigation 
                  //code                
                 }
            } 
           catch(Exception ex)
            { 
               //code
            }

	







猜你喜欢

转载自blog.csdn.net/Yubu_/article/details/74710427