0411-39943997
工作时间:周一至周日 8:30-17:30
技术文库
Technical Library

关于Paypal的一些设置和使用方法(三)C#开发代码

标签: C#开发代码 Papal 设置 使用方法   访问: 15442017-09-21

1、原理:Paypal的交易原理很简单,就是将您要支付的页面Post提交到他的服务器上。你的页面上要有他要的必要信息即可。

2、提交URL:


测试用的为:

真实用的为:


3、实例:


<form id="form1" action="https://www.sandbox.paypal.com/cgi-bin/webscrmethod="post">

        <input type="hidden" name="cmd" value="_xclick" />//表示立即支付

        <input type="hidden" name="business" value="youraccount@test.com" />//您的账号

        <input type="hidden" name="item_name" value="order_no" />//订单号

        <input type="hidden" name="amount" value="order_amount" />//订单金额

        <input type="hidden" name="currency_code" value="USD" />//交易币种


        <input type="submit" id="btnSubmit" value="确定提交" />           

</form>



4、提交后的反馈信息:IPN使用

当您收到新的付款交易或者已发生的付款交易的状态发生变化时,paypal都将异步发送付款详细数据到您所指定的url,以便您了解买家付款的具体情况并做出相应的响应。这个过程我们称作即时付款通知(简称 ipn)。


ipn 在以前的文章中以设置好,忘记的同学可以回过头去看看。


Paypal 会将一些交易信息返回给您指定的页面:


接收信息的页面 notify_url.cs 中的代码如下:


//获取参数


public static string GetFormString(string strName)

        {

            if (HttpContext.Current.Request.Form[strName] == null)

                return "";

            return HttpContext.Current.Request.Form[strName];

        }



//向paypal请求验证


private string ValidatePaypalInfo(int payment_id)

        { 

            try

            {

                string strLive = "https://www.sandbox.paypal.com/cgi-bin/webscr";


                string strFormValues;

                string strNewValues;

                string strResponse;


                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;


                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strLive);

                req.Method = "POST";

                req.ContentType = "application/x-www-form-urlencoded";

                byte[] param = HttpContext.Current.Request.BinaryRead(HttpContext.Current.Request.ContentLength);

                strFormValues = Encoding.ASCII.GetString(param);

                strNewValues = strFormValues + "&cmd=_notify-validate";

                req.ContentLength = strNewValues.Length;

                StreamWriter stout = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);

                stout.Write(strNewValues);

                stout.Close();


                StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream());

                strResponse = sr.ReadToEnd();

                sr.Close();


                return strResponse;

            }

            catch (Exception e)

            {

                Utils.WriteLog("paypal", "paypal have some error:" + e.Message + " \r\n" + e.StackTrace);


                return "";

            }

        }



//接收

protected void Page_Load(object sender, EventArgs e)

{

            string trade_no = GetFormString("txn_id"); //交易号

            string order_no = GetFormString("item_name").ToUpper(); //获取订单号

            string total_fee = GetFormString("mc_gross"); //获取总金额

            string trade_status = GetFormString("payment_status"); //交易状态


            string strResponse = ValidatePaypalInfo(site_payment_id);


 if (strResponse.ToUpper() == "VERIFIED")//验证成功

  {


    if (trade_status.ToLower() == "completed")//交易状态 成功

     {

//做你要做的事,比如更新订单。

     }


   }


}


更多信息请参见:


解决方案体验中心



Copyright© 2020 仟亿科技,All rights reserved.