Here’s the code for the client, then the client error I’m getting, and the section of code the error is referring to. Please note, I have not properly configured the backend portion yet, just trying to get this part somewhat functional and help understanding the guide I’m trying to follow over at Stripe.
Template.shopping_cart_template.onRendered(async function shopping_cart_template_onRendered() {
const template = Template.instance();
template.autorun(() => {
template.data.dictionary.set('local_cart', JSON.parse(get_local_data('local_cart')) || []);
template.data.dictionary.set('local_cart_item_count', get_local_data('local_cart_item_count') || 0);
template.data.dictionary.set('local_cart_total', local_cart_total(template.data.dictionary.get('local_cart'))
.total_without_taxes || []);
template.data.dictionary.get('local_cart');
template.data.dictionary.get('local_cart_item_count');
template.data.dictionary.get('local_cart_total');
template.not_ready_to_checkout.get();
template.item_quantity.get();
Session.set('account_navigation_active', 'Profile');
if (template.data.dictionary.get('local_cart')
.length) {
template.data.dictionary.set('local_cart_ready', true);
}
if (Session.get('local_cart_reload')) {
template.data.dictionary.set('local_cart', JSON.parse(get_local_data('local_cart')) || []);
template.not_ready_to_checkout.set(true);
template.checkout_process.set('step', 'cart');
Session.set('local_cart_reload', false);
}
});
const stripe = await loadStripe(Meteor.settings.public.stripe.testPublishableKey);
const elements = stripe.elements();
const cardElement = elements.create('card');
console.log('### stripe: ', stripe);
console.log('### elements: ', elements);
console.log('### cardElement: ', cardElement);
cardElement.mount('#card-element');
console.log('### cardElement.mount: ', cardElement);
const form = document.getElementById("payment-form");
console.log('### form: ', form);
const resultContainer = document.getElementById('payment-result');
cardElement.on('change', function (event) {
if (event.error) {
resultContainer.textContent = event.error.message;
console.log('### resultContainer.textContent ERROR: ', resultContainer.textContent);
} else {
resultContainer.textContent = '';
console.log('### resultContainer.textContent: ', resultContainer.textContent);
}
});
form.addEventListener('submit', async event => {
event.preventDefault();
console.log('### event: ', event);
resultContainer.textContent = '';
console.log('### cardElement: ', cardElement);
const result = await stripe.createPaymentMethod({
type: 'card',
card: cardElement,
});
console.log('### result: ', result);
handlePaymentMethodResult(result);
});
const handlePaymentMethodResult = async ({
paymentMethod,
error
}) => {
if (error) {
// An error happened when collecting card details, show error in payment form
resultContainer.textContent = result.error.message;
} else {
// Send paymentMethod.id to your server (see Step 3)
const response = await fetch("/pay", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
payment_method_id: paymentMethod.id
})
});
const responseJson = await response.json();
handleServerResponse(responseJson);
}
};
const handleServerResponse = async responseJson => {
if (responseJson.error) {
// An error happened when charging the card, show it in the payment form
resultContainer.textContent = responseJson.error;
} else {
// Show a success message
resultContainer.textContent = 'Success!';
}
};
});
Client error I’m receiving:
shopping-cart.js:139 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
async function (async)
handlePaymentMethodResult @ shopping-cart.js:125
(anonymous) @ shopping-cart.js:113
async function (async)
(anonymous) @ shopping-cart.js:108
Portion the error is pointing to that’s problematic:
form.addEventListener('submit', async event => {
event.preventDefault();
console.log('### event: ', event);
resultContainer.textContent = '';
console.log('### cardElement: ', cardElement);
const result = await stripe.createPaymentMethod({
type: 'card',
card: cardElement,
});
console.log('### result: ', result);
handlePaymentMethodResult(result);
});
const handlePaymentMethodResult = async ({
paymentMethod,
error
}) => {
if (error) {
// An error happened when collecting card details, show error in payment form
resultContainer.textContent = result.error.message;
} else {
// Send paymentMethod.id to your server (see Step 3)
const response = await fetch("/pay", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
payment_method_id: paymentMethod.id
})
});
const responseJson = await response.json();
handleServerResponse(responseJson);
}
};
Let me know what else you need and I’ll provide it as soon as I can. Thank you very much for your assistance!