Stripe支付首页需要引用Stripe.net框架,我引用的是22.8.0版本,注意.NETFramework的版本为4.5,同时需要引用Newtonsoft.Json(版本不能低于9.0.1)和System.Collections.Immutable(版本不低于1.5.0)。
一、前端JS代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
<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 这个从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费用' , 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>已有用户购买了该照片!</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 = "" ; //支付成功,跳转页面 } else { layer.msg(data); } }, error: function () { } }) } }); </script> |
效果如图所示:
二、后端C#代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
/// <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支付" , //说明 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(); } } |
三、配置文件代码如下:
1
2
3
4
|
< appSettings > < add key = "pk_liveConfig" value = "pk_test_XXXXXX" /> <!--stripe账号公钥--> < add key = "pk_liveSecretKey" value = "sk_test_XXXXXX" /> <!--stripe账号Secret key--> </ appSettings > |
Stripe支付的流程就是点击支付按钮就调用myStripe.pay函数去生成token,然后调用callback方法执行后台代码,返回succeeded就是支付成功了
到此这篇关于C#实现Stripe支付的方法实践的文章就介绍到这了,更多相关C# Stripe支付内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_44547599/article/details/122886515