I am trying to setup a stripe checkout page on a KeystoneJS webpage.
The view.on('post'... function doesn't seem to work and I am not able to find the source of the issue:
Here is my code:
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()
});
};
I would be grateful for any help :-)
via chdecultot
No comments:
Post a Comment