检索令牌/创build费用 – 条带

这可能是一个愚蠢的问题,但在这里,我们走了。 我已经设置了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参数中的条纹标记。

从我的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> 

用testing卡提交付款,我被redirect到/用404充电。我是新来的,我显然已经复制/粘贴了一些代码,但是我正在努力的把头绕在它周围,我想了解它,不只是使它工作。 我有点得到如何信用卡信息检索工作与JS,但我有点困惑,当涉及到收费/redirect/ 404 /。
我的意思是,这个行动路线指向我的一个不存在的页面,对吧? 我是否需要创build此页面?

  <form action="/charge" method="post" id="payment-form"> 

对不起,这篇文章的长度,请帮我理解这里发生了什么,或者我需要修复。

感谢任何帮助。

你如何服务你的后端—快递?

如果您在将表单提交给/charge时看到404 ,则听起来您可能没有在Express中为app.post /charge app.post设置app.post路由设置。

你可以通过路由指南阅读更多的细节https://expressjs.com/en/guide/routing.html

如果你想看一个简单的工作例子,看看这个(确保用实际的testing键replacepk_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)