C#實現(xiàn)Stripe支付的方法實踐
Stripe支付首頁需要引用Stripe.net框架,我引用的是22.8.0版本,注意.NETFramework的版本為4.5,同時需要引用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 這個從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#代碼如下:
/// <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();
}
}
三、配置文件代碼如下:
<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函數(shù)去生成token,然后調用callback方法執(zhí)行后臺代碼,返回succeeded就是支付成功了
到此這篇關于C#實現(xiàn)Stripe支付的方法實踐的文章就介紹到這了,更多相關C# Stripe支付內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Visual Studio C#創(chuàng)建windows服務程序
用Visual C#創(chuàng)建Windows服務不是一件困難的事,本文就將指導你一步一步創(chuàng)建一個Windows服務并使用它,本文主要介紹了Visual Studio C#創(chuàng)建windows服務程序,感興趣的可以了解一下2024-01-01
C#使用post發(fā)送和接收數(shù)據(jù)的方法
這篇文章主要介紹了C#使用post發(fā)送和接收數(shù)據(jù)的方法,涉及C#使用post收發(fā)數(shù)據(jù)的相關技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04

