KeystoneJS - 条纹集成
KeystoneJS - Stripe Integration
我正在尝试在 KeystoneJS 网页上设置条带结帐页面。
view.on('post'... 功能似乎不起作用,我无法找到问题的根源:
这是我的代码:
public/js/stripe.js
// Handle form submission
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the user if there was an error
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server
onReceiveToken(result.token);
}
});
});
var onReceiveToken = function(token, args) {
// Submit token to server so it can charge the card
$.ajax({
url: '/checkout',
type: 'POST',
data: {
action: 'charge',
stripeToken: token.id
},
success: function(data) {
console.log('Returns the HTML content of checkout.html');
}
})
};
routes/views/checkout.js
var keystone = require('keystone');
var stripe = require('stripe')("STRIPE_KEY");
exports = module.exports = function (req, res) {
var view = new keystone.View(req, res);
var locals = res.locals;
// locals.section is used to set the currently selected
// item in the header navigation.
locals.section = 'checkout';
// Render the view
view.render('checkout');
console.log(JSON.stringify(req.body)); <-- Is rendered correctly
//Create Subscription
view.on('post', { "action":"charge" }, function (next) {
console.log(JSON.stringify(req.body)); <-- Isn't rendered
stripe.customers.create({
description: 'Customer for elizabeth.williams@example.com',
source: res.token,
}, function(err, customer) {
// asynchronously called
});
next()
});
};
如有任何帮助,我将不胜感激:-)
刚写完题就找到了答案:
您必须最后渲染视图。
var keystone = require('keystone');
var stripe = require('stripe')("STRIPE_KEY");
exports = module.exports = function (req, res) {
var view = new keystone.View(req, res);
var locals = res.locals;
// locals.section is used to set the currently selected
// item in the header navigation.
locals.section = 'checkout';
//Create Subscription
view.on('post', { "action":"charge" }, function (next) {
console.log(JSON.stringify(req.body)); <-- Isn't rendered
stripe.customers.create({
description: 'Customer for elizabeth.williams@example.com',
source: res.token,
}, function(err, customer) {
// asynchronously called
});
next()
});
// Render the view
view.render('checkout');
};
我正在尝试在 KeystoneJS 网页上设置条带结帐页面。
view.on('post'... 功能似乎不起作用,我无法找到问题的根源:
这是我的代码:
public/js/stripe.js
// Handle form submission
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the user if there was an error
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server
onReceiveToken(result.token);
}
});
});
var onReceiveToken = function(token, args) {
// Submit token to server so it can charge the card
$.ajax({
url: '/checkout',
type: 'POST',
data: {
action: 'charge',
stripeToken: token.id
},
success: function(data) {
console.log('Returns the HTML content of checkout.html');
}
})
};
routes/views/checkout.js
var keystone = require('keystone');
var stripe = require('stripe')("STRIPE_KEY");
exports = module.exports = function (req, res) {
var view = new keystone.View(req, res);
var locals = res.locals;
// locals.section is used to set the currently selected
// item in the header navigation.
locals.section = 'checkout';
// Render the view
view.render('checkout');
console.log(JSON.stringify(req.body)); <-- Is rendered correctly
//Create Subscription
view.on('post', { "action":"charge" }, function (next) {
console.log(JSON.stringify(req.body)); <-- Isn't rendered
stripe.customers.create({
description: 'Customer for elizabeth.williams@example.com',
source: res.token,
}, function(err, customer) {
// asynchronously called
});
next()
});
};
如有任何帮助,我将不胜感激:-)
刚写完题就找到了答案:
您必须最后渲染视图。
var keystone = require('keystone');
var stripe = require('stripe')("STRIPE_KEY");
exports = module.exports = function (req, res) {
var view = new keystone.View(req, res);
var locals = res.locals;
// locals.section is used to set the currently selected
// item in the header navigation.
locals.section = 'checkout';
//Create Subscription
view.on('post', { "action":"charge" }, function (next) {
console.log(JSON.stringify(req.body)); <-- Isn't rendered
stripe.customers.create({
description: 'Customer for elizabeth.williams@example.com',
source: res.token,
}, function(err, customer) {
// asynchronously called
});
next()
});
// Render the view
view.render('checkout');
};