简体   繁体   English

C#如何从Form.Submit中检索Stripe Token

[英]C# how to retrieve Stripe Token from the Form.Submit

Can I ask how to retrieve the token from the coding/server side based on this script? 我可以问一下如何根据此脚本从编码/服务器端检索令牌吗?

    function stripeTokenHandler(token) {
        // Insert the token ID into the form so it gets submitted to the server
        var form = document.getElementById('payment-form');
        var hiddenInput = document.createElement('input');
        hiddenInput.setAttribute('type', 'hidden');
        hiddenInput.setAttribute('name', 'stripeToken');
        hiddenInput.setAttribute('value', token.id);
        form.appendChild(hiddenInput);

        // Submit the form
        form.submit();
    }

Thank you 谢谢

Here is a basic example of submitting a webform with javascript and accessing the form collection on the server. 这是使用javascript提交Web表单并访问服务器上的表单集合的基本示例。 I have hard-coded the stripe token value, I'm assuming you have that part covered. 我已经假设条带标记值是硬编码的,我假设您已经覆盖了那部分。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication11.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <button onclick="stripeTokenHandler('some token value');">Submit Me</button>
    </div>
    </form>
</body>
    <script>
        function stripeTokenHandler(token) {
            var form = document.getElementById('form1');
            var hiddenInput = document.createElement('input');
            hiddenInput.setAttribute('type', 'hidden');
            hiddenInput.setAttribute('name', 'stripetoken');
            hiddenInput.setAttribute('value', token);
            form.appendChild(hiddenInput);

            // Submit the form
            form.submit();
        }
    </script>
</html>

Code Behind: 背后的代码:

using System;
using System.Diagnostics;

namespace WebApplication11
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                //any form inputs can be obtained with Request.Form[]
                Debug.WriteLine(Request.Form["stripetoken"]);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM