【问题标题】:Integrating a stripe checkout with .net将条带结帐与 .net 集成
【发布时间】:2020-04-17 15:29:54
【问题描述】:

我需要您的帮助,我想将条带结帐与 .net 集成 我一步一步按照 Stripe 教程进行操作,但 Stripe Checkout 重定向不起作用。

这是我的代码:

   <script src="https://js.stripe.com/v3/"></script>
  <asp:Button runat="server" OnClick="btnPay_Click" Text="S'inscrire" CssClass="btn btn-submit" id="btnPay" />
<script>
    var stripe = Stripe('pk_test_fuKFopc9GVK4QgRD4c3CDZng00OI6sMSyU');

    var element = document.getElementById('btnPay');

    element.addEventListener('click', function () {

        stripe.redirectToCheckout({
            // Make the id field from the Checkout Session creation API response
            // available to this file, so you can provide it as parameter here
            // instead of the {{CHECKOUT_SESSION_ID}} placeholder.
            sessionId: '<%= sessionid%>'
        }).then(function (result) {
            // If `redirectToCheckout` fails due to a browser or network
            // error, display the localized error message to your customer
            // using `result.error.message`.
        });
    });
</script>

还有我的服务器代码:

 protected void btnPay_Click(object sender, EventArgs e)
        {
            // Set your secret key: remember to change this to your live secret key in production
            // See your keys here: https://dashboard.stripe.com/account/apikeys
            StripeConfiguration.ApiKey = WebConfigurationManager.AppSettings["StripeSecretKey"];

            var options = new SessionCreateOptions
            {
                CustomerEmail = "customer@hotmail.com",
                PaymentMethodTypes = new List<string> {
                                    "card",
                                },
                LineItems = new List<SessionLineItemOptions> {
                                    new SessionLineItemOptions {
                                        Name = "TEST",
                                        Description = "my Description",
                                        Amount = 99,
                                        Currency = "eur",
                                        Quantity = 1,
                                    },
                                },
                SuccessUrl = "https://example.com/success?session_id={CHECKOUT_SESSION_ID}",
                CancelUrl = "https://example.com/cancel",
            };

            var service = new SessionService();
            Session session = service.Create(options);

重定向到 Checkout 不起作用 :( :( :(

【问题讨论】:

  • 你在使用clientIdMode="static"吗?您是否收到 JS 错误?
  • 请注意您的successurl 字面意思是 success?session_id={CHECKOUT_SESSION_ID}
  • 我看不到你的服务器端代码是如何被调用的,看起来你只是在调用 javascript 函数。看页面源码,sessionid 评价了吗?使用浏览器开发工具跟踪网络请求并查看发生了什么。
  • @Crowcoder “我不知道你的服务器端代码是如何被调用的” - 该按钮有一个OnClick,尽管我同意似乎没有是客户端和服务器之间的任何“对话”。

标签: javascript c# stripe-payments


【解决方案1】:

我的 asp.net 表单有点生锈,但我 90% 确定我知道您的问题。话虽如此,请务必阅读 cmets 和此链接:https://meta.stackexchange.com/questions/147616/what-do-you-mean-it-doesnt-work,以便您将来可以写出更好的问题。

现在..

Asp.net 表单喜欢将自己的唯一 ID 恶作剧添加到对象中。因此,即使您为&lt;asp:btn&gt; 指定了一个 ID,它的末尾也会有一个 guid 或一些卡住的东西。要确认这一点,请在实际单击按钮之前检查页面,并查看 HTML 中的实际 ID 值。

将ClientIDMode="Static" 添加到您的asp:btn 是一种解决方案,另一种解决方案是使用css 类或其他查询选择器在您的javascript 代码中实际选择asp:btn。

【讨论】:

【解决方案2】:

谢谢大家,仅供参考: - 当我再次单击按钮时,重定向有效。

  • 当我在页面加载上使用结帐服务器代码时效果很好!太奇怪了。

我已按照以下步骤操作: https://stripe.com/docs/payments/checkout/one-time

 protected void Page_Load(object sender, EventArgs e)
        {
 // Set your secret key: remember to change this to your live secret key in production
            // See your keys here: https://dashboard.stripe.com/account/apikeys
            StripeConfiguration.ApiKey = WebConfigurationManager.AppSettings["StripeSecretKey"];



            var options = new SessionCreateOptions
            {
                CustomerEmail = "mehdizerouali@hotmail.com",
                PaymentMethodTypes = new List<string> {
                                    "card",
                                },
                LineItems = new List<SessionLineItemOptions> {
                                    new SessionLineItemOptions {
                                        Name = "BPENLIGNE",
                                        Description = "Formule Smart",
                                        Amount = 99,
                                        Currency = "eur",
                                        Quantity = 1,
                                    },
                                },
                SuccessUrl = "https://example.com/success?session_id={CHECKOUT_SESSION_ID}",
                CancelUrl = "https://example.com/cancel",
            };

            var service = new SessionService();
            Session session = service.Create(options);

            sessionid = session.Id;
        }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 2023-04-03
  • 2022-01-02
  • 2012-06-19
  • 2020-09-05
  • 2021-03-26
  • 2019-09-15
相关资源
最近更新 更多