C#实现Stripe支付的方法实践

作者:新鑫S 时间:2023-01-22 01:46:47 

Stripe支付首页需要引用Stripe.net框架,我引用的是22.8.0版本,注意.NETFramework的版本为4.5,同时需要引用Newtonsoft.Json(版本不能低于9.0.1)和System.Collections.Immutable(版本不低于1.5.0)。

C#实现Stripe支付的方法实践

一、前端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#实现Stripe支付的方法实践

二、后端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函数去生成token,然后调用callback方法执行后台代码,返回succeeded就是支付成功了

来源:https://blog.csdn.net/weixin_44547599/article/details/122886515

标签:C#,Stripe,支付
0
投稿

猜你喜欢

  • java中javamail收发邮件实现方法

    2022-12-15 02:03:03
  • Android使用WindowManager构造悬浮view

    2022-08-03 00:43:13
  • java导出生成word的简单方法

    2023-11-23 23:07:13
  • springboot FeignClient注解及参数

    2021-07-09 21:59:07
  • IDEA配置maven环境的详细教程(Unable to import maven project报错问题的解决)

    2022-04-09 13:53:40
  • spring cloud整合ribbon问题及解决方案

    2023-07-25 04:24:49
  • jsp、struts、spring、mybatis实现前端页面功能模块化拆分的方案

    2023-11-25 07:38:00
  • 利用C#实现在Word中更改字体颜色

    2021-12-25 12:14:09
  • 轻松掌握Java建造者模式

    2023-11-06 15:24:38
  • 基于spring security实现登录注销功能过程解析

    2023-11-29 06:09:05
  • 聊聊在Servlet中怎么上传文件

    2022-03-07 17:56:44
  • RestTemplate自定义请求失败异常处理示例解析

    2021-12-03 22:13:17
  • Android 自定义输入支付密码的软键盘实例代码

    2021-08-09 11:41:43
  • 详解利用spring-security解决CSRF问题

    2023-07-31 14:31:19
  • 浅试仿 mapstruct实现微服务编排框架详解

    2022-07-12 13:20:44
  • Android编程设置屏幕亮度的方法

    2022-07-02 16:40:52
  • Android仿IOS自定义AlertDialog提示框

    2022-08-23 21:08:44
  • java中利用List的subList方法实现对List分页(简单易学)

    2022-06-18 23:33:09
  • java中为何重写equals时必须重写hashCode方法详解

    2022-10-24 01:44:39
  • Spring Security自定义认证逻辑实例详解

    2023-02-28 19:19:18
  • asp之家 软件编程 m.aspxhome.com