我知道它有一些基本的问题,但我在网络表格上没有实践。我是第一次使用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?或者,如果这是正确的,如何在按钮单击事件中获取表单发布数据?
答案 0 :(得分:6)
感谢评论中的提示。在仔细阅读了互联网和拔毛之后,我走了一段路,然后带着这个解决方案回来了。
代码背后:
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库文档来获取令牌并处理付款。
答案 1 :(得分:4)
我想在评论中发表评论,但我还是不允许。所以,这不是一个真正的答案,更多的是对OP自己的发现的回应。
我正在使用Stripe.js做同样的事情。我可以使用Request.Form简单地获取stripeToken,并在代码隐藏中以通常的方式获取所有其他非c / c相关字段(例如int Total = PaymentTotal.Value;);但是我注意到的并不是抓取数据,而是你 在Page_Load上处理它(或者在页面生命周期的某个后期点),因为提交按钮的onclick事件永远不会被触发,因为表单实际上是使用js而不是按钮提交的。
因此,无论是文字还是有点回答原始问题,你都无法在按钮点击事件中实际获取数据,而不会在回发时手动触发该事件(我猜测)。
如果他们来到这里寻求使Stripe和.NET协同工作,希望能够节省一些时间来解决这个问题。