码迷,mamicode.com
首页 > Web开发 > 详细

【网站国际化必备】Asp.Net MVC 集成Paypal(贝宝)快速结账 支付接口 ,附源码demo

时间:2017-02-18 12:07:12      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:dll   ted   baidu   允许   end   back   pie   off   sch   

     开篇先给大家讲段历史故事,博主是湖北襄阳人。襄阳物华天宝,人杰地灵,曾用名襄樊。在2800多年的历史文化中出现了一代名相诸葛亮(卧龙),三国名士庞统(凤雏),魏晋隐士司马徽(水镜先生),唐代大诗人孟浩然(孟襄阳),张继、杜审言,文学家皮日休,北宋著名书画家米芾(米襄阳),“允冠百王”的光武帝刘秀,东方圣人释道安等一大批历史文化名人。小说《三国演义》120回故事中有30多回提到襄阳。

    相传诸葛亮的老婆黄月英黄头发黑皮肤,但知识广博。诸葛亮发明木牛流马,就是从黄月英的传授的技巧上发展出来。不仅如此黄月英还有一大很重要的发明,就是大头菜,传承多年到现在已经成为湖北襄阳的一道特产,也是一项重要的饮食文化,早几年有关部门根据诸葛孔明的赫赫大名为大头菜取了个比较国际化的名字:孔明菜(广告语:吃孔明菜,谋天下事!)。

    正是因为孔明菜响亮的声誉美名,近几年家乡的孔明菜事业繁荣发展,畅销五大洲,海内外。为了更好的打造国外市场,博主有幸得令,成为襄阳市孔明菜国际互联网电子商务平台打造任务的一分子,负责对接国际支付接口Paypal。

    以上故事只为引题,部分虚构,如有雷同,不胜荣幸。如果你正在或将来打算赚赚老外的钱,那就跟着博主的节奏一起来国际化吧!

    怎么对接Paypal支付呢?博主选择了其中的一种对接方式即(快速结账),也是当前的主流对接方式,具体步骤和详细参见官方介绍:https://www.paypal-biz.com/developer/internet/ec/,博主友情提示,操作步骤需要遵循,至于每个步骤的文档可以粗略的看看。

    因为博主一出道就从MVC干起,传统的webForm先天性驾驭不了,也没那闲时间去回头学习,官方提供的源码下载下来一直未调通(如有大神调通,望不吝赐教!)。所以博主潜心研究,取其技术精髓,整合出了一个MVC3版本下的demo供各位看官参考。

    MVC对接Paypal怎么做呢?我们需要如下准备材料:

  1. 在Paypal官方注册个高级帐号:https://www.paypal.com/c2/webapps/mpp/get-started
    然后在开发者中心里面去创建至少一个卖家和一个买家帐号(供您在上线之前做测试用,官方称之为Sandbox帐号,同可以理解为测试环境帐号):https://developer.paypal.com/webapps/developer/applications/accounts,如下图,红色代表卖家帐号,绿色代表买家帐号
    技术分享

  2. 在项目中加入官方dll文件,引用Paypal类库paypal_base.dll和log4net.dll,已在源码中附加。

  3. 获取卖家帐号API配置项,(配置项的关键需要从您创建的某个卖家帐号中获取,获取方式如下图):
    技术分享

  4. 配置卖家帐号API到您的网站:
    技术分享
    <!--Paypal支付账号配置-->
        <add key="APIUserName" value="wen_api1.shipfinder.cn"/>
        <add key="APIPassword" value="1376359127"/>
        <add key="APISinature" value="An5ns1Kso7MWUdW4ErQKJJJ4qi4-AKqBVNoNVjFOrjJUxaTCiRAvn7V4"/>
        <add key="Environment" value="sandbox"/>
        <!--测试环境:https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token= -->
        <!--真实环境:https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token= -->
        <add key="RedirectURL" value="https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&amp;token="/>
    技术分享

     

  5. SetExpressCheckout (向paypal提交订单,自己的网站做订单入库操作)核心代码如下:

    public ActionResult SetExpressCheckout() {
    string currency = Request.Form["currencyCodeType"];
    string name = Request.Form["NAME"];
    string SHIPTOSTREET = Request.Form["SHIPTOSTREET"];
    string SHIPTOCITY = Request.Form["SHIPTOCITY"];
    string SHIPTOSTATE = Request.Form["SHIPTOSTATE"];
    string SHIPTOCOUNTRYCODE = Request.Form["SHIPTOCOUNTRYCODE"];
    string SHIPTOZIP = Request.Form["SHIPTOZIP"];
    string L_NAME1 = Request.Form["L_NAME1"];
    string L_AMT1 = Request.Form["L_AMT1"];
    string L_QTY1 = Request.Form["L_QTY1"];
    string L_NAME0 = Request.Form["L_NAME0"];
    string L_AMT0 = Request.Form["L_AMT0"];
    string L_QTY0 = Request.Form["L_QTY0"];

    string hots = Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port + "/";

    NVPCodec encoder = new NVPCodec();
    encoder.Add("PAYMENTACTION", "Sale");

    //不允许客户改地址
    encoder.Add("ADDROVERRIDE", "1");
    encoder.Add("CANCELURL", hots + "/Pay/Index");
    encoder.Add("CURRENCYCODE", currency);
    encoder.Add("SHIPTONAME", name);
    encoder.Add("SHIPTOSTREET", SHIPTOSTREET);
    encoder.Add("SHIPTOCITY", SHIPTOCITY);
    encoder.Add("SHIPTOSTATE", SHIPTOSTATE);
    encoder.Add("SHIPTOCOUNTRYCODE", SHIPTOCOUNTRYCODE);
    encoder.Add("SHIPTOZIP", SHIPTOZIP);
    encoder.Add("L_NAME0", L_NAME0);
    encoder.Add("L_NUMBER0", "1000");
    encoder.Add("L_DESC0", "Size: 8.8-oz");
    encoder.Add("L_AMT0", L_AMT0);
    encoder.Add("L_QTY0", L_QTY0);
    encoder.Add("L_NAME1", L_NAME1);
    encoder.Add("L_NUMBER1", "10001");
    encoder.Add("L_DESC1", "Size: Two 24-piece boxes");
    encoder.Add("L_AMT1", L_AMT1);
    encoder.Add("L_QTY1", L_QTY1);
    encoder.Add("L_ITEMWEIGHTVALUE1", "0.5");
    encoder.Add("L_ITEMWEIGHTUNIT1", "lbs");

    double ft = double.Parse(L_QTY0) * double.Parse(L_AMT0) + double.Parse(L_QTY1) * double.Parse(L_AMT1);
    encoder.Add("ITEMAMT", ft.ToString());
    encoder.Add("TAXAMT", "2.00");

    double amt = System.Math.Round(ft + 5.00f + 2.00f + 1.00f, 2);
    double maxamt = System.Math.Round(amt + 25.00f, 2);

    encoder.Add("SHIPDISCAMT","-3.00");
    encoder.Add("AMT",amt.ToString());

    string returnURL = hots + "/Pay/GetExpressCheckoutDetails?amount="+amt.ToString();//获取订单详情的地址,也是此步骤之后的地址

    encoder.Add("RETURNURL",returnURL);
    encoder.Add("SHIPPINGAMT", "8.00");
    encoder.Add("MAXAMT", maxamt.ToString());

    encoder.Add("INSURANCEOPTIONOFFERED", "true");
    encoder.Add("INSURANCEAMT", "1.00");

    encoder.Add("LOCALECODE", "US");

    encoder.Add("NOSHIPPING", "1");

    encoder.Add("L_SHIPPINGOPTIONISDEFAULT0", "false");
    encoder.Add("L_SHIPPINGOPTIONNAME0", "Ground");
    encoder.Add("L_SHIPPINGOPTIONLABEL0", "UPS Ground 7 Days");
    encoder.Add("L_SHIPPINGOPTIONAMOUNT0", "3.00");
    encoder.Add("L_SHIPPINGOPTIONISDEFAULT1", "true");
    encoder.Add("L_SHIPPINGOPTIONNAME1", "UPS Air");
    encoder.Add("L_SHIPPINGOPTIONlABEL1", "UPS Next Day Air");
    encoder.Add("L_SHIPPINGOPTIONAMOUNT1", "8.00");

    encoder.Add("CALLBACKTIMEOUT", "4");

    NVPCodec decoder = PaypalProvider.SetExpressCheckout(encoder);

    string ack = decoder["ACK"];
    string L_ERRORCODE0 = decoder["L_ERRORCODE0"];
    string L_SHORTMESSAGE0 = decoder["L_SHORTMESSAGE0"];
    string L_LONGMESSAGE0 = decoder["L_LONGMESSAGE0"];
    string L_SEVERITYCODE0 = decoder["L_SEVERITYCODE0"];

    if (!string.IsNullOrEmpty(ack) &&
    (ack.Equals("Success", System.StringComparison.OrdinalIgnoreCase) || ack.Equals("SuccessWithWarning", System.StringComparison.OrdinalIgnoreCase))
    )
    {
    // 发送电子邮件
    //new MailManager(email, "订单", "订单信息").Send();
    Session["TOKEN"] = decoder["token"];
    return Redirect(ConfigurationManager.AppSettings["RedirectURL"] + decoder["token"]);

    }
    else
    {
    return Redirect("/Pay/Index");
    }
    }


    技术分享
    public ActionResult SetExpressCheckout() {
                string currency = Request.Form["currencyCodeType"];
                string name = Request.Form["NAME"];
                string SHIPTOSTREET = Request.Form["SHIPTOSTREET"];
                string SHIPTOCITY = Request.Form["SHIPTOCITY"];
                string SHIPTOSTATE = Request.Form["SHIPTOSTATE"];
                string SHIPTOCOUNTRYCODE = Request.Form["SHIPTOCOUNTRYCODE"];
                string SHIPTOZIP = Request.Form["SHIPTOZIP"];
                string L_NAME1 = Request.Form["L_NAME1"];
                string L_AMT1 = Request.Form["L_AMT1"];
                string L_QTY1 = Request.Form["L_QTY1"];
                string L_NAME0 = Request.Form["L_NAME0"];
                string L_AMT0 = Request.Form["L_AMT0"];
                string L_QTY0 = Request.Form["L_QTY0"];
    
                string hots = Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port + "/";
    
                NVPCodec encoder = new NVPCodec();
                encoder.Add("PAYMENTACTION", "Sale");
    
                //不允许客户改地址
                encoder.Add("ADDROVERRIDE", "1");
                encoder.Add("CANCELURL", hots + "/Pay/Index");
                encoder.Add("CURRENCYCODE", currency);
                encoder.Add("SHIPTONAME", name);
                encoder.Add("SHIPTOSTREET", SHIPTOSTREET);
                encoder.Add("SHIPTOCITY", SHIPTOCITY);
                encoder.Add("SHIPTOSTATE", SHIPTOSTATE);
                encoder.Add("SHIPTOCOUNTRYCODE", SHIPTOCOUNTRYCODE);
                encoder.Add("SHIPTOZIP", SHIPTOZIP);
                encoder.Add("L_NAME0", L_NAME0);
                encoder.Add("L_NUMBER0", "1000");
                encoder.Add("L_DESC0", "Size: 8.8-oz");
                encoder.Add("L_AMT0", L_AMT0);
                encoder.Add("L_QTY0", L_QTY0);
                encoder.Add("L_NAME1", L_NAME1);
                encoder.Add("L_NUMBER1", "10001");
                encoder.Add("L_DESC1", "Size: Two 24-piece boxes");
                encoder.Add("L_AMT1", L_AMT1);
                encoder.Add("L_QTY1", L_QTY1);
                encoder.Add("L_ITEMWEIGHTVALUE1", "0.5");
                encoder.Add("L_ITEMWEIGHTUNIT1", "lbs");
    
                double ft = double.Parse(L_QTY0) * double.Parse(L_AMT0) + double.Parse(L_QTY1) * double.Parse(L_AMT1);
                encoder.Add("ITEMAMT", ft.ToString());
                encoder.Add("TAXAMT", "2.00");
    
                double amt = System.Math.Round(ft + 5.00f + 2.00f + 1.00f, 2);
                double maxamt = System.Math.Round(amt + 25.00f, 2);
    
                encoder.Add("SHIPDISCAMT","-3.00");
                encoder.Add("AMT",amt.ToString());
    
                 string returnURL = hots + "/Pay/GetExpressCheckoutDetails?amount="+amt.ToString();//获取订单详情的地址,也是此步骤之后的地址
    
                encoder.Add("RETURNURL",returnURL);
                encoder.Add("SHIPPINGAMT", "8.00");
                encoder.Add("MAXAMT", maxamt.ToString());
    
                encoder.Add("INSURANCEOPTIONOFFERED", "true");
                encoder.Add("INSURANCEAMT", "1.00");
    
                encoder.Add("LOCALECODE", "US");
    
                encoder.Add("NOSHIPPING", "1");
    
                encoder.Add("L_SHIPPINGOPTIONISDEFAULT0", "false");
                encoder.Add("L_SHIPPINGOPTIONNAME0", "Ground");
                encoder.Add("L_SHIPPINGOPTIONLABEL0", "UPS Ground 7 Days");
                encoder.Add("L_SHIPPINGOPTIONAMOUNT0", "3.00");
                encoder.Add("L_SHIPPINGOPTIONISDEFAULT1", "true");
                encoder.Add("L_SHIPPINGOPTIONNAME1", "UPS Air");
                encoder.Add("L_SHIPPINGOPTIONlABEL1", "UPS Next Day Air");
                encoder.Add("L_SHIPPINGOPTIONAMOUNT1", "8.00");
    
                encoder.Add("CALLBACKTIMEOUT", "4");
    
                NVPCodec decoder = PaypalProvider.SetExpressCheckout(encoder);
    
                string ack = decoder["ACK"];
                string L_ERRORCODE0 = decoder["L_ERRORCODE0"];
                string L_SHORTMESSAGE0 = decoder["L_SHORTMESSAGE0"];
                string L_LONGMESSAGE0 = decoder["L_LONGMESSAGE0"];
                string L_SEVERITYCODE0 = decoder["L_SEVERITYCODE0"];
               
                if (!string.IsNullOrEmpty(ack) &&
                    (ack.Equals("Success", System.StringComparison.OrdinalIgnoreCase) || ack.Equals("SuccessWithWarning", System.StringComparison.OrdinalIgnoreCase))
                    )
                {
                    // 发送电子邮件
                    //new MailManager(email, "订单", "订单信息").Send();
                    Session["TOKEN"] = decoder["token"];
                    return Redirect(ConfigurationManager.AppSettings["RedirectURL"] + decoder["token"]);
    
                }
                else
                {
                    return Redirect("/Pay/Index");
                }
            }
    技术分享

     

  6. GetExpressCheckoutDetails (根据提交订单后返回的Token,获取当前订单详情 ),核心代码如下:
    技术分享
      //得到支付详细信息
                string token = Session["TOKEN"].ToString();
                NVPCodec nvp = new NVPCodec();
                nvp.Add("TOKEN", token);
                nvp.Add("CURRENCYCODE", "USD");
    
    
                NVPCodec responsenvp = PaypalProvider.GetExpressCheckoutDetails(nvp);
                string ack = responsenvp["ACK"];
    
                if (string.IsNullOrEmpty(ack) ||
                    (
                        !ack.Equals("Success", System.StringComparison.OrdinalIgnoreCase) &&
                        !ack.Equals("SuccessWithWarning", System.StringComparison.OrdinalIgnoreCase))
                    )
                {
                  
                    return Redirect("/Pay/Index");
                }
                string payerstatus = responsenvp["PAYERSTATUS"];
                string email = responsenvp["EMAIL"];
                string payerid = responsenvp["PAYERID"];
                string firstname = responsenvp["FIRSTNAME"];
                string lastname = responsenvp["LASTNAME"];
    技术分享

     

  7. DoExpressCheckoutPayment(根据订单详情进行验证并支付,然后自己的网站更新支付状态,订单状态),核心代码如下:
    技术分享
    //开始支付
    
                NVPCodec donvp = new NVPCodec();
                donvp.Add("TOKEN", token);
                donvp.Add("AMT", amount);
                donvp.Add("PAYERID", payerid);
                donvp.Add("PAYMENTACTION", "Sale");
                donvp.Add("CURRENCYCODE", "USD");
    
                NVPCodec response_do = PaypalProvider.DoExpressCheckoutPayment(donvp);
                string do_ack = response_do["ACK"];
    
                string L_ERRORCODE0 = response_do["L_ERRORCODE0"];
                string L_SHORTMESSAGE0 = response_do["L_SHORTMESSAGE0"];
                string L_LONGMESSAGE0 = response_do["L_LONGMESSAGE0"];
                string L_SEVERITYCODE0 = response_do["L_SEVERITYCODE0"];
                if (string.IsNullOrEmpty(do_ack) ||
                    (
                        !do_ack.Equals("Success", System.StringComparison.OrdinalIgnoreCase) &&
                        !do_ack.Equals("SuccessWithWarning", System.StringComparison.OrdinalIgnoreCase))
                    )
                {
                    return Redirect("/Pay/Index");
                }
                else {
    
                    string paymenttype = response_do["PAYMENTTYPE"];
                    string transactionID = response_do["TRANSACTIONID"];
                    string note = response_do["NOTE"];
                    string ordertime = response_do["ORDERTIME"];
                    string paymentstatus = response_do["PAYMENTSTATUS"];
                    string reason=response_do["PENDINGREASON"];
                    double feeamt = double.Parse(response_do["FEEAMT"]);
                    return Content("恭喜您支付成功!");
                }
    技术分享

     

  8. 真正的核心就是5、6、7步骤,用Paypal技术人员熟练的口气说就是Set,Get,Do,最后一步成功后就是处理我们网站业务逻辑的过程。博主这里提供个买家帐号供大家测试(帐号:a@bc.cn,密码:11111111),下面附上demo运行效果:
    技术分享
    技术分享
    技术分享
    技术分享
    技术分享

       

      源码下载地址

【网站国际化必备】Asp.Net MVC 集成Paypal(贝宝)快速结账 支付接口 ,附源码demo

标签:dll   ted   baidu   允许   end   back   pie   off   sch   

原文地址:http://www.cnblogs.com/android-blogs/p/6412728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!