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

1、注册Stripe

Sign Up and Create a Stripe Account | Stripe

C#接入Stripe,实现支付宝微信支付_第1张图片

2、测试模式

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

C#接入Stripe,实现支付宝微信支付_第2张图片

 3、支付开发

NuGet 安装 Stripe.net 库

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

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

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

            var options = new SessionCreateOptions
            {
                PaymentMethodTypes = new List { "alipay", "wechat_pay" },
                LineItems = new List
                {
                    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 { { "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回调

C#接入Stripe,实现支付宝微信支付_第3张图片

 C#接入Stripe,实现支付宝微信支付_第4张图片

 C#接入Stripe,实现支付宝微信支付_第5张图片

        /// 
        /// Stripe webhook 支付回调
        /// 官方文档:https://stripe.com/docs/payments/handling-payment-events
        /// 
        /// 
        [HttpPost]
        public async Task 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

你可能感兴趣的:(支付,C#,c#,.netcore)