開篇先給你們講段歷史故事,博主是湖北襄陽人。襄陽物華天寶,人傑地靈,曾用名襄樊。在2800多年的歷史文化中出現了一代名相諸葛亮(臥龍),三國名士龐統(鳳雛),魏晉隱士司馬徽(水鏡先生),唐代大詩人孟浩然(孟襄陽),張繼、杜審言,文學家皮日休,北宋著名書畫家米芾(米襄陽),「允冠百王」的光武帝劉秀,東方聖人釋道安等一大批歷史文化名人。小說《三國演義》120回故事中有30多回提到襄陽。web
相傳諸葛亮的老婆黃月英黃頭髮黑皮膚,但知識廣博。諸葛亮發明木牛流馬,就是從黃月英的傳授的技巧上發展出來。不只如此黃月英還有一大很重要的發明,就是大頭菜,傳承多年到如今已經成爲湖北襄陽的一道特產,也是一項重要的飲食文化,早幾年有關部門根據諸葛孔明的赫赫大名爲大頭菜取了個比較國際化的名字:孔明菜(廣告語:吃孔明菜,謀天下事!)。express
正是由於孔明菜響亮的聲譽美名,近幾年家鄉的孔明菜事業繁榮發展,暢銷五大洲,海內外。爲了更好的打造國外市場,博主有幸得令,成爲襄陽市孔明菜國際互聯網電子商務平臺打造任務的一分子,負責對接國際支付接口Paypal。api
以上故事只爲引題,部分虛構,若有雷同,不勝榮幸。若是你正在或未來打算賺賺老外的錢,那就跟着博主的節奏一塊兒來國際化吧!app
怎麼對接Paypal支付呢?博主選擇了其中的一種對接方式即(快速結帳),也是當前的主流對接方式,具體步驟和詳細參見官方介紹:https://www.paypal-biz.com/developer/internet/ec/,博主友情提示,操做步驟須要遵循,至於每一個步驟的文檔能夠粗略的看看。webapp
由於博主一出道就從MVC幹起,傳統的webForm先天性駕馭不了,也沒那閒時間去回頭學習,官方提供的源碼下載下來一直未調通(若有大神調通,望不吝賜教!)。因此博主潛心研究,取其技術精髓,整合出了一個MVC3版本下的demo供各位看官參考。ide
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"); } }
//獲得支付詳細信息 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("恭喜您支付成功!"); }
源碼下載地址測試
感謝閱讀,若是您以爲此文對你有用,不要吝嗇右邊的推薦哦,你們的支持是我分享智慧的動力,後面的文章打算聊聊如何對接支付寶,有興趣能夠經過關注本博客或個人微博動態!網站
正在看本人博客的這位童鞋,我看你氣度不凡,談吐間隱隱有王者之氣,往後必有一番做爲!旁邊有「推薦」二字,你就順手把它點了吧,相得準,我分文不收;相不許,你也好回來找我!spa