开篇先给大家讲段历史故事,博主是湖北襄阳人。襄阳物华天宝,人杰地灵,曾用名襄樊。在2800多年的历史文化中出现了一代名相诸葛亮(卧龙),三国名士庞统(凤雏),魏晋隐士司马徽(水镜先生),唐代大诗人孟浩然(孟襄阳),张继、杜审言,文学家皮日休,北宋著名书画家米芾(米襄阳),“允冠百王”的光武帝刘秀,东方圣人释道安等一大批历史文化名人。小说《三国演义》120回故事中有30多回提到襄阳。
相传诸葛亮的老婆黄月英黄头发黑皮肤,但知识广博。诸葛亮发明木牛流马,就是从黄月英的传授的技巧上发展出来。不仅如此黄月英还有一大很重要的发明,就是大头菜,传承多年到现在已经成为湖北襄阳的一道特产,也是一项重要的饮食文化,早几年有关部门根据诸葛孔明的赫赫大名为大头菜取了个比较国际化的名字:孔明菜(广告语:吃孔明菜,谋天下事!)。
正是因为孔明菜响亮的声誉美名,近几年家乡的孔明菜事业繁荣发展,畅销五大洲,海内外。为了更好的打造国外市场,博主有幸得令,成为襄阳市孔明菜国际互联网电子商务平台打造任务的一分子,负责对接国际支付接口Paypal。
以上故事只为引题,部分虚构,如有雷同,不胜荣幸。如果你正在或将来打算赚赚老外的钱,那就跟着博主的节奏一起来国际化吧!
怎么对接Paypal支付呢?博主选择了其中的一种对接方式即(快速结账),也是当前的主流对接方式,具体步骤和详细参见官方介绍:https://www.paypal-biz.com/developer/internet/ec/,博主友情提示,操作步骤需要遵循,至于每个步骤的文档可以粗略的看看。
因为博主一出道就从MVC干起,传统的webForm先天性驾驭不了,也没那闲时间去回头学习,官方提供的源码下载下来一直未调通(如有大神调通,望不吝赐教!)。所以博主潜心研究,取其技术精髓,整合出了一个MVC3版本下的demo供各位看官参考。
MVC对接Paypal怎么做呢?我们需要如下准备材料:
<!--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&token="/>
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"); } }View Code
//得到支付详细信息 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"];
//开始支付 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("恭喜您支付成功!"); }
源码下载地址
感谢阅读,如果您觉得此文对你有用,不要吝啬右边的推荐哦,大家的支持是我分享智慧的动力,后面的文章打算聊聊如何对接支付宝,有兴趣可以通过关注本博客或我的微博动态!
正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!旁边有“推荐”二字,你就顺手把它点了吧,相得准,我分文不收;相不准,你也好回来找我!