C#接入Stripe,实现支付宝微信支付

2 篇文章 0 订阅
本文介绍了如何注册和使用Stripe服务,包括在测试模式下进行支付流程,通过Stripe.net库进行App和Web支付的开发示例,以及设置Webhooks处理支付回调事件。同时,提到了Stripe的手续费信息。
摘要由CSDN通过智能技术生成

1、注册Stripe

Sign Up and Create a Stripe Account | Stripe

2、测试模式

Stripe提供测试模式,通过测试模式可以实现支付流程

 3、支付开发

NuGet 安装 Stripe.net 库

        /// <summary>
        /// APP支付
        /// 官方文档:https://stripe.com/docs/payments/alipay/accept-a-payment?platform=mobile&ui=android
        /// </summary>
        /// <param name="amount">金额</param>
        /// <param name="orderId">订单号</param>
        /// <returns></returns>
        public string AppPay(decimal amount, string orderId)
        {
            //密钥
            StripeConfiguration.ApiKey = "sk_test_********";

            var options = new PaymentIntentCreateOptions
            {
                Amount = (long)amount * 100,
                Currency = "cny",
                PaymentMethodTypes = new List<string> { "alipay", "wechat_pay" },
                Metadata = new Dictionary<string, string> { { "order_id", orderId } },
            };
            var service = new PaymentIntentService();
            var intent = service.Create(options);

            //将 client secret 发送给您的客户端以安全地完成支付过程
            return intent.ClientSecret;
        }
        /// <summary>
        /// 电脑网站支付
        /// 官方文档:https://stripe.com/docs/payments/alipay/accept-a-payment?platform=web
        /// </summary>
        /// <param name="amount">金额</param>
        /// <param name="orderId">订单号</param>
        /// <param name="returnUrl">跳转地址</param>
        /// <returns></returns>
        public static string WebPay(decimal amount, string orderId, string returnUrl)
        {
            //密钥
            StripeConfiguration.ApiKey = "sk_test_********";

            var options = new SessionCreateOptions
            {
                PaymentMethodTypes = new List<string> { "alipay", "wechat_pay" },
                LineItems = new List<SessionLineItemOptions>
                {
                    new SessionLineItemOptions
                    {
                        PriceData = new SessionLineItemPriceDataOptions
                        {
                            UnitAmount =  (long)amount * 100,
                            Currency = "cny",
                            ProductData = new SessionLineItemPriceDataProductDataOptions()
                            {
                                Name = "产品名称",
                            },
                        },
                        Quantity = 1,
                    },
                },
                Currency = "cny",
                PaymentIntentData = new SessionPaymentIntentDataOptions()
                {
                    Metadata = new Dictionary<string, string> { { "order_id", orderId } },
                },
                Mode = "payment",
                SuccessUrl = returnUrl,
                CancelUrl = returnUrl,
            };
            options.AddExtraParam("payment_method_options[wechat_pay][client]", "web");
            var service = new SessionService();
            var session = service.Create(options);
            //返回支付链接以完成支付过程
            return session.Url;
        }

相关文档

APP支付:Accept an Alipay payment | Stripe Documentation

电脑网站支付:https://stripe.com/docs/payments/alipay/accept-a-payment?platform=web

4、Webhooks回调

 

 

        /// <summary>
        /// Stripe webhook 支付回调
        /// 官方文档:https://stripe.com/docs/payments/handling-payment-events
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> StripeCallBack()
        {
            var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

            try
            {
                //webhook => 密钥签名
                var endpointSecret = "whsec_*******";
                var stripeEvent = EventUtility.ConstructEvent(json, Request.Headers["Stripe-Signature"], endpointSecret);

                // Handle the event
                if (stripeEvent.Type == Events.PaymentIntentSucceeded)
                {
                    var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                    Console.WriteLine(paymentIntent.Metadata["order_id"]);
                    Console.WriteLine("PaymentIntent was successful!");
                }
                // ... handle other event types
                else
                {
                    Console.WriteLine("Unhandled event type: {0}", stripeEvent.Type);
                }

                return Ok();
            }
            catch (StripeException)
            {
                return BadRequest();
            }
        }

5、手续费

https://stripe.com/zh-cn-ca/pricing/local-payment-methods

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值