我使用Nodejs创建Stripe Backend。
我要创建客户信用卡使用API
我在参数中输入注册所需的所有卡信息,并访问API http://localhost:5000/stripe-create-card
,
但是返回令牌为空。
我根据参考文献编写了代码,所以我认为没有错。
我如何获得令牌?
const express = require('express');
const stripe = require('stripe')('MYKEY');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.set('port', (process.env.PORT || 5000));
//app.listen(3000);
app.post('/create_customer',(req,res) => {
const email = req.body.email;
console.log(email);
stripe.customers.create({
email : email
},function(err, customer) {
// asynchronously called
res.send(customer);
console.log(customer);
})
});
app.post('/body',(req,res) =>{
//res.setHeader('Content-Type', 'text/plain');
console.log(req.body);
console.log(req.body.email);
});
app.post('/stripe-create-card', (req, res)=> {
const customer_id = req.body.customer_id;
const card_num = req.body.card_num;
const card_month = req.body.card_month;
const card_year = req.body.card_year;
const card_cvc = req.body.card_cvc;
console.log(customer_id,card_num,card_month,card_year,card_cvc);
stripe.tokens.create({
card: {
"number": '4242424242424242',
"exp_month": 12,
"exp_year": 2020,
"cvc": "123"
}
}, function(err, token) {
// asynchronously called
console.log(token);
const params = {
source: token.id
};
stripe.customers.createSource(customer_id, params, function(err, card) {
console.log(card);
});
});
});
app.listen(app.get('port'), function () {
console.log('Node app is running on port', app.get('port'))
});
答案 0 :(得分:1)
我有一个令牌,但这是使用以下代码从客户端获得的:
<form action="/orders/payment/confirmed" method="POST">
<div class="formError"></div>
<div class="formSuccess">Your payment has been successfully submitted, please check your email</div>
<div>
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="paymentData" class="cta blue" >Purchase</button>
</div>
</form>
之后,配置值:
const handler = StripeCheckout.configure({
key: 'your public key',
image: 'your image path',
locale: 'auto',
token: token => {
//Here you receive your token id as token.id, so you can make a request
//to your server with this key
})
之后,您可以使用令牌ID来处理付款。
祝你好运。