【发布时间】:2018-03-19 21:13:49
【问题描述】:
这可能是一个愚蠢的问题,但我们开始吧。 我已经设置了 Stripe Elements (https://stripe.com/docs/elements) 来收集信用卡信息并对其进行加密。 现在我正在尝试设置费用,但我不确定如何设置我的“服务器端”代码。
在我的 controller.js 中提交表单:
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();
}
https://stripe.com/docs/charges: “在您的服务器上,获取表单提交的 POST 参数中的 Stripe 令牌。”
来自我的 Nodecharge.js:
// 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
var stripe = require("stripe")("sk_test_111111111111111111");
// Token is created using Stripe.js or Checkout!
// Get the payment token ID submitted by the form:
var token = request.body.stripeToken; // Using Express
// Charge the user's card:
stripe.charges.create({
amount: 1000,
currency: "sek",
description: "Example charge",
source: token,
}, function(err, charge) {
// asynchronously called
});
我的 HTML 表单:
<form action="/charge" method="post" id="payment-form">
<div class="form-row">
<label for="card-element">
Credit or debit card
</label>
<div id="card-element">
<!-- a Stripe Element will be inserted here. -->
</div>
<!-- Used to display form errors -->
<div id="card-errors" role="alert"></div>
</div>
<button>Submit Payment</button>
</form>
使用测试卡提交付款后,我被重定向到 /charge 并显示 404。
我是新手,显然我已经复制/粘贴了一些代码,但我正在努力理解它,我想理解它,而不仅仅是让它工作。
我有点了解信用卡信息检索如何与 js 配合使用,但在收费/重定向/404/ 方面我有点困惑。
我的意思是,这条行动线将我指向一个不存在的页面,对吗?我需要创建这个页面吗?
<form action="/charge" method="post" id="payment-form">
抱歉这篇文章的长度,请帮助我了解这里发生了什么,或者我需要修复什么。
感谢任何帮助。
【问题讨论】:
标签: javascript jquery html node.js stripe-payments