c# - 如何在 asp.net webform 中获取 js POST?

标签 c# javascript .net webforms stripe-payments

我知道这是一个基本问题,但我没有练习网络表单。我是第一次使用 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>

JS 中的最后一次调用创建了一个 JSON 对象,我想知道如何在单击按钮时到达 C# 页面:

protected void submit_Click(object sender, EventArgs e)
{
....
}

我想执行 javascript 实现以避免必须遵守 PCI。我是在错误地处理这个问题吗?是否应该全部由 Stripe.net 来处理所有内容,并完全跳过 js?或者如果这是正确的,我怎样才能在按钮点击事件中获取表单发布数据?

最佳答案

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

  1. 使按钮成为标准的 html 输入(而不是 asp:Button)
  2. 像这样在 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 库文档获取 token 并处理付款即可。

关于c# - 如何在 asp.net webform 中获取 js POST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18038867/

相关文章:

c# - IsChanged with IChangeTracking Not Firing on Window Close WPF MVVM C#

javascript - Vimeo iframe 导致闪烁

c# - 如何提高 double 类型对于大数的精度?

c# - 如何在 C++/CLI 中将事件处理程序分配给事件?

c# - 阅读器关闭时调用 Read 的尝试无效?

C# vs C++ 三元运算符

javascript - 使用三个js创建环面图

c# - 使用项目中缺少 DLL 自身的依赖项

c# - 正则表达式获取我的文件的父目录在字符串文件路径中

javascript - Angular 不会更新 DOM 但会将数据记录到控制台