Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp.net webform中获取js帖子?_C#_Javascript_.net_Webforms_Stripe Payments - Fatal编程技术网

C# 如何在asp.net webform中获取js帖子?

C# 如何在asp.net webform中获取js帖子?,c#,javascript,.net,webforms,stripe-payments,C#,Javascript,.net,Webforms,Stripe Payments,我知道这是一个基本的问题,但我在webforms上没有实践。我第一次使用Stripe.js,希望将它与Stripe.net结合使用来处理客户端。以下是客户端代码: <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="StripePage.aspx.cs" Inherits="StripePage.StripePage" %> <asp

我知道这是一个基本的问题,但我在webforms上没有实践。我第一次使用Stripe.js,希望将它与Stripe.net结合使用来处理客户端。以下是客户端代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="StripePage.aspx.cs" Inherits="StripePage.StripePage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
    // This identifies your website in the createToken call below
    // You need to put your real publish key here.
    Stripe.setPublishableKey('pk_test_1nDJ3hA1Mv2Sy9bUoYcBMXmm');
    // ...
    // I am using jquery to process the payment. It knows what form to 
    // process it on based on the name 'payment-form'
    jQuery(function ($) {
        //payment submission
        $('#payment-form').submit(function (event) {
            var $form = $(this);

            // Disable the submit button to prevent repeated clicks
            $form.find('button').prop('disabled', true);

            Stripe.createToken($form, stripeResponseHandler);

            // Prevent the form from submitting with the default action
            return false;
        });

        //if there is a error, it is displayed on the page if there was
        //no error this is where it gets sent to the server.
        var stripeResponseHandler = function (status, response) {
            var $form = $('#payment-form');

            if (response.error) {
                // Show the errors on the form
                $form.find('.payment-errors').text(response.error.message);
                $form.find('button').prop('disabled', false);
            } else {
                // token contains id, last4, and card type
                var token = response.id;
                // Insert the token into the form so it gets submitted to the server
                $form.append($('<input type="hidden" name="stripeToken" />').val(token));
                // and submit
                $form.get(0).submit();
            }
        };
    });
</script>

<form method="POST" id="paymentForm" runat="server">
    <span class="payment-errors" runat="server"></span>

    <div class="form-row">
        <label>
            <span>Card Number</span>
            <br />
            <input id="number" type="text" data-stripe="number" clientidmode="Static" />
            <input type="text" size="20" data-stripe="number" runat="server" />
        </label>
    </div>

    <div class="form-row">
        <label>
            <span>CVC</span>
            <br />
            <input type="text" size="4" data-stripe="cvc" runat="server" />
        </label>
    </div>

    <div class="form-row">
        <label>
            <span>Expiration (MM/YYYY)</span>
            <br />
            <input type="text" size="2" data-stripe="exp-month" runat="server" />
        </label>
        <br />
        <input type="text" size="4" data-stripe="exp-year" runat="server" />
    </div>
    <asp:Button ID="submit" ClientIDMode="Static" runat="server" Text="SubmitPayment" OnClick="submit_Click" />
</form>
</asp:Content>

我希望执行javascript实现,以避免执行PCI遵从性。我是不是处理得不对?是否应该全部使用Stripe.net来处理所有内容,并完全跳过js?或者,如果这是正确的,如何在按钮单击事件中获取表单post数据?

感谢评论中的提示。在仔细阅读了互联网和头发拉扯之后,我离开了一会儿,带着这个解决方案回来了

  • 使按钮仅为标准html输入(而不是asp:按钮)
  • 在Page_Load事件中获取通过JavaScript发送的回发信息,如下所示
  • 代码隐藏:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            StripeConfiguration.SetApiKey("[API Secret Key");
            NameValueCollection nvc = Request.Form;
            string amount = nvc["amount"];
            var centsArray = amount.Split('.');
            int dollarsInCents = Convert.ToInt32(centsArray[0]) * 100;
            int remainingCents = Convert.ToInt32(centsArray[1]);
            string tokenId = nvc["stripeToken"];
    
            var tokenService = new StripeTokenService();
            StripeToken stripeToken = tokenService.Get(tokenId);
    
    
            var myCharge = new StripeChargeCreateOptions
            {
                TokenId = tokenId, 
                AmountInCents = dollarsInCents + remainingCents,
                Currency = "usd"
            };
            var chargeService = new StripeChargeService();
            StripeCharge stripeCharge = chargeService.Create(myCharge);
        }
    }
    

    似乎使用NameValueCollection(位于System.Collections.Specialized命名空间中)使我能够通过变量名从Request.Form中提取所需内容。由于我添加了新的变量名,只需获取它们,然后按照Stripe.NET库文档获取令牌并处理付款

    我只想对答案发表评论,但目前还不允许发表评论。因此,这并不是一个真正的答案,更多的是对OP自身发现的回应

    我也在做同样的事情,使用Stripe.js。我可以简单地使用Request.Form获取stripeToken,并以代码隐藏中的常见方式获取所有其他与c/c无关的字段(例如int-Total=PaymentTotal.Value;);但我要指出的不是如何获取数据,而是您必须在页面加载时(或在页面生命周期的稍后某个点)处理数据,因为Submit按钮的onclick事件从未实际触发,因为表单实际上是使用js而不是按钮提交的

    因此,由于完全是字面意义上的,并且有点回答了最初的问题,您实际上无法获取按钮单击事件中的数据,而不是在回发时手动触发该事件(我猜是这样的)


    如果其他人来这里是想让Stripe和.NET一起工作,希望能节省一些时间来弄清楚这一点。

    这是我们推荐的方法。您的表单将像普通表单一样由JavaScript提交,就好像您的用户在根本没有JS的情况下单击了“提交”。因此,您可以像处理普通表单提交一样处理它。如果它是ASP.NET webforms,您可能需要在初始加载中添加一个隐藏的文本字段,并对其ClientID进行硬编码。然后在条带回调中,使用JQuery将conrols值设置为您的令牌,并触发回发事件,以便ASP.NET可以重新绑定并找到它的服务器端。事实上,我从一天开始就在逃避同样的问题。那个么,这是一种只在页面加载时而不是在代码背后的按钮单击事件中处理此问题的方法吗?因为我有将近50个字段以及用于内部用户注册过程的条带所需的字段。这似乎只在点击按钮时可行。另外,每次回发时,它都会执行条带处理。补救方法可能是在按钮单击时设置标志,并在ispostback检查中在页面加载时将其与按钮单击标志一起处理。
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            StripeConfiguration.SetApiKey("[API Secret Key");
            NameValueCollection nvc = Request.Form;
            string amount = nvc["amount"];
            var centsArray = amount.Split('.');
            int dollarsInCents = Convert.ToInt32(centsArray[0]) * 100;
            int remainingCents = Convert.ToInt32(centsArray[1]);
            string tokenId = nvc["stripeToken"];
    
            var tokenService = new StripeTokenService();
            StripeToken stripeToken = tokenService.Get(tokenId);
    
    
            var myCharge = new StripeChargeCreateOptions
            {
                TokenId = tokenId, 
                AmountInCents = dollarsInCents + remainingCents,
                Currency = "usd"
            };
            var chargeService = new StripeChargeService();
            StripeCharge stripeCharge = chargeService.Create(myCharge);
        }
    }