这可能是一个愚蠢的问题,但我们继续。我已经设置了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>
使用测试卡提交付款,我使用404重定向到/ charge。这是我的新手,我显然已经复制/粘贴了一些代码,但是我正在努力将其包裹起来,我想了解它,而不仅仅是使它起作用。我有点了解信用卡信息检索与js一起工作的方式,但是在计费/重定向/ 404 /方面我有点困惑。
我的意思是,此动作行将我指向了我当前不存在的页面,对吗?我需要创建此页面吗?
<form action="/charge" method="post" id="payment-form">
抱歉,本文过长,请帮助我了解这里发生的事情或需要解决的问题。
感谢任何帮助。
您如何服务后端--- Express?
如果您在404
向表单提交表单时看到“ a” ,/charge
这听起来可能是您在Express中未app.post
设置路线的/charge
。
您可以通读有关路由的指南,以了解更多详细信息https://expressjs.com/en/guide/routing.html
如果您想看一个简单的工作示例,请看一下它(确保用实际的测试键替换pk_test和sk_test):
var stripe = require("stripe")("sk_test_xxxyyyzzz");
var express = require('express'), bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false })
var app = express();
app.get('/',function(req, res) {
// for kicks, just sending checkout
res.send('<form action="/charge" method="POST">Buy it !<script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="pk_test_xxxyyyyzzzz"></script></form>')
});
app.post('/charge',urlencodedParser, function(req, res) {
// grab a token
var token = req.body.stripeToken;
// creating a charge, for real use add things like error handling
stripe.charges.create({
amount: 2000,
currency: "usd",
source: token, // obtained with Stripe.js
description: "Charge"
}, function(err, charge) {
res.send("You made a charge: "+ charge.id);
});
});
app.listen(5000)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句