C#實(shí)現(xiàn)Stripe支付的方法實(shí)踐
Stripe支付首頁(yè)需要引用Stripe.net框架,我引用的是22.8.0版本,注意.NETFramework的版本為4.5,同時(shí)需要引用Newtonsoft.Json(版本不能低于9.0.1)和System.Collections.Immutable(版本不低于1.5.0)。
一、前端JS代碼如下:
<script src="https://js.stripe.com/v3/"></script> <script src="https://checkout.stripe.com/checkout.js"></script> <script type="text/javascript"> //Stripe支付 var myStripe = { testKey: '<%=ConfigurationManager.AppSettings["pk_liveConfig"] %>', //配置文件中的key 這個(gè)從Stripe中取,我就不截圖展示了 logoImg: "https://stripe.com/img/documentation/checkout/marketplace.png", //抬頭的Logo //換卡 changeHandler: function (f) { return StripeButton.configure({ key: this.testKey, image: f.logoImg || this.logoImg, name: f.title || 'Update Card Detail', panelLabel: f.button || 'Submit', allowRememberMe: false, locale: 'auto', dataKey: this.testKey, token: function (token) { f.email = token.email; f.tokenId = token.id; f.callback(f); } }); }, payHandler: function (f) { layer.closeAll(0); return StripeCheckout.configure({ key: this.testKey, name: f.title || 'Stripe費(fèi)用', email: f.Email || '', currency: f.currency || 'zxx', amount: f.amount || 0, allowRememberMe: false, image: f.logoImg || this.logoImg, locale: 'auto', token: function (token) { f.tokenId = token.id; f.email = token.email; f.callback(f); } }); }, changeCard: function (f) { this.changeHandler(f).open(); }, pay: function (f) { this.payHandler(f).open(); }, SendMsg: function (uid) { var message = {}; message.action = "noticeMember"; message.code = 1; message.uid = uid; message.msg = "<div>已有用戶購(gòu)買(mǎi)了該照片!</div>"; socketApi.sendMessage(message); } } myStripe.pay({ title: 'TEST', currency: 'USD',//幣種:美元(USD)、人民幣(CNY)、港幣(HKD) amount: <%=Convert.ToInt32(acoumt) %> * 100,//金額 callback: function (p) { $.ajax({ type: 'POST', dataType: 'text', url: '/admin/ajax/PCBAOrdersData.ashx', data: 'param=Pay&email=' + this.email + "&amount=" + this.amount + "&tokenId=" + this.tokenId, success: function (data) { if (data == "succeeded") { location.href = "";//支付成功,跳轉(zhuǎn)頁(yè)面 } else { layer.msg(data); } }, error: function () { } }) } }); </script>
效果如圖所示:
二、后端C#代碼如下:
/// <summary> /// Stripe支付 /// </summary> public void Pay() { string Msg = "Payment Failure"; try { string tokenId = _Request.GetString("tokenId", ""); string amount = _Request.GetString("amount", "0"); string email = _Request.GetString("email", ""); Stripe.StripeConfiguration.SetApiKey(ConfigurationManager.AppSettings["pk_liveSecretKey"]); var options = new Stripe.ChargeCreateOptions { Amount = Convert.ToInt64(amount), Currency = "USD",//幣種:美元(USD)、人民幣(CNY)、港幣(HKD) SourceId = tokenId, Description = "Stripe支付",//說(shuō)明 ReceiptEmail = email, }; var service = new Stripe.ChargeService(); Stripe.Charge charge = service.Create(options); Msg = charge.Status; } catch (Exception e) { Msg = e.Message; throw e; } finally { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Write(Msg); HttpContext.Current.Response.End(); } }
三、配置文件代碼如下:
<appSettings> <add key="pk_liveConfig" value="pk_test_XXXXXX"/><!--stripe賬號(hào)公鑰--> <add key="pk_liveSecretKey" value="sk_test_XXXXXX"/><!--stripe賬號(hào)Secret key--> </appSettings>
Stripe支付的流程就是點(diǎn)擊支付按鈕就調(diào)用myStripe.pay函數(shù)去生成token,然后調(diào)用callback方法執(zhí)行后臺(tái)代碼,返回succeeded就是支付成功了
到此這篇關(guān)于C#實(shí)現(xiàn)Stripe支付的方法實(shí)踐的文章就介紹到這了,更多相關(guān)C# Stripe支付內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Visual Studio C#創(chuàng)建windows服務(wù)程序
用Visual C#創(chuàng)建Windows服務(wù)不是一件困難的事,本文就將指導(dǎo)你一步一步創(chuàng)建一個(gè)Windows服務(wù)并使用它,本文主要介紹了Visual Studio C#創(chuàng)建windows服務(wù)程序,感興趣的可以了解一下2024-01-01Unity Shader實(shí)現(xiàn)序列幀動(dòng)畫(huà)效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)序列幀動(dòng)畫(huà)效果 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02C#實(shí)現(xiàn)FTP文件下載及超時(shí)控制詳解
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)FTP文件下載及超時(shí)控制的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03C#使用post發(fā)送和接收數(shù)據(jù)的方法
這篇文章主要介紹了C#使用post發(fā)送和接收數(shù)據(jù)的方法,涉及C#使用post收發(fā)數(shù)據(jù)的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04