Stripe API: There is no token with ID X (stripe.charges.create) - javascript

Fairly new to node/express, and I'm trying to get a simple dummy charge working with Stripe 3.3.2 and checkout.js. Once the checkout form is submitted, the token makes its way back to the backend without issue; using that token to create a charge, however, results in the following: There is no token with ID tok_xxxxxxxxxxxxxxxxxxxxxxxx
What am I missing here?
/views/premium.mustache
<a id="subscribe" href="#">Pay with Card</a>
<script>
// Define handler to be called when Stripe returns a card token
function onReceiveToken(token, args) {
// Submit token to server so it can charge the card
$.ajax({
url: '/premium/charge',
type: 'POST',
data: {
    stripeToken: token
    }
    });
   }
// Configure Checkout
var checkout = StripeCheckout.configure({
key: 'xx_xxxx_xxxxxxxxxxxxxxxxxxxx',
token: onReceiveToken,
image: '/images/logo/stripe.png',
name: 'Test App',
description: 'Premium Sounds',
amount: 500
});
// Open Checkout when the link is clicked
$('#subscribe').on('click', function() {
checkout.open();
return false;
});
</script>
/routes/stripe/charge.js
var dateUtils = require('date-utils'),
config = require('../../../config/config.js'),
stripe = require('stripe')(config.keys.stripe.test.secret_key),
User = require('../../models/User');
module.exports = function(router, app, passport) {
router.get('/premium/charge', function(req, res) {
res.redirect('/premium');
});
router.post('/premium/charge', function(req, res) {
var token = req.body.stripeToken.id,
card = req.body.stripeToken.card.id;
stripe.charges.create({
amount: 500,
currency: 'USD',
source: token,
description: 'Dummy customer'
}, function(err, charge) {
if (err) {
console.log(err);
} else {
console.log(charge);
}
});
});
}
req.body
{
stripeToken: {
id: 'tok_15jsAF2eZvKYlo2CmvgETnJg',
livemode: 'false',
created: '1427204031',
used: 'false',
object: 'token',
type: 'card',
card: {
id: 'card_15jsAF2eZvKYlo2CdXDcaI0h',
object: 'card',
last4: '4242',
brand: 'Visa',
funding: 'credit',
exp_month: '9',
exp_year: '2018',
country: 'US',
name: 'sd#smm.com',
address_line1: '',
address_line2: '',
address_city: '',
address_state: '',
address_zip: '',
address_country: '',
cvc_check: 'pass',
address_line1_check: '',
address_zip_check: '',
dynamic_last4: ''
},
email: 'sd#smm.com',
verification_allowed: 'true',
client_ip: '74.110.163.89'
}
}
Stripe response
{
[Error: There is no token with ID tok_xxxxxxxxxxxxxxxxxx.]
type: 'StripeInvalidRequest',
stack: 'Error: There is no token with ID (node.js:442:13)',
rawType: 'invalid_request_error',
code: undefined,
param: 'source',
message: 'There is no token with ID tok_xxxxxxxxxxxxxxxxxx.',
detail: undefined,
raw: {
type: 'invalid_request_error',
message: 'There is no token with ID tok_xxxxxxxxxxxxxxxxxx.',
param: 'source'
}
}

Stripe support is awesome. Will mark this as the correct answer for those of you who stumble onto this in the future.
99.9% of the time you see this error it's because you aren't creating the token with your public key. Check to make sure it's correct/set.

Related

DiscordAPIError[40060]: Interaction has already been acknowledged. Error

client.on('interactionCreate', async interaction => {
if(interaction.isChatInputCommand()) {
console.log('Hello World.');
console.log(interaction.options.getString('help2'))
await interaction.reply({ content: 'Secret Pong!', ephemeral: true });
}
});
async function main() {
const commands = [
{
name: 'help',
description: 'Gives Information about bot. ',
options:[
{
name: 'help2',
description : 'helps2',
type: 3,
required : true,
},
],
},
];
that bloody code gives "DiscordAPIError[40060]: Interaction has already been acknowledged." that error pls help me.
Solution .. . .. .. .

Is there a way to delay execution of an Axios request?

I'm trying to delay the execution of an Axios request. So far I've tried to use set timeout to delay the request with no luck. Ideally what should be happening is when a request hits this route it will read the status code from the body and then send the axios post below after 10 minutes if the code is 0. This all works fine but it is sending instantly. How should I go about this?
//STATUS CAKE ROUTE
app.post('/status', (req, res) => {
res.sendStatus(200);
console.log('recieved');
let Name = req.body.Name;
let Code = req.body.StatusCode;
console.log(Code);
//How we're handling status code 0 for a downed server
if (Code === '0') {
//Create reboot prompt
const RebootPrompt = {
channel: '******',
blocks: [
{
type: 'section',
text: {
type: 'plain_text',
text: `${Name}`,
emoji: true,
},
},
{
type: 'actions',
elements: [
{
type: 'button',
text: {
type: 'plain_text',
emoji: true,
text: 'Reboot!',
},
style: 'primary',
value: `${Name},true`,
},
{
type: 'button',
text: {
type: 'plain_text',
emoji: true,
text: 'Leave it alone',
},
style: 'danger',
value: 'false',
},
],
},
],
};
//Send the prompt to reboot if we get a 0 status code to the channel.
setTimeout(() => {
axios.post(
'https://hooks.slack.com/services/********/********/**********',
RebootPrompt
);
}, 600000);
}
});
function myFunction() {
setTimeout(async function() {
try {
let res = await fetch("https://reqres.in/api/users?page=2");//This executes after 3 seconds.
let resJson = await res.json();
alert(resJson.data.length)
} catch (error) {
console.log(error);
}
}, 3000);
}
<!DOCTYPE html>
<html>
<body>
<p>Click the button to wait 3 seconds, then the api gets triggered.</p>
<button onclick="myFunction()">Try it</button>
</body>
</html>

Amount not showing in PayPal payment in Braintree

Hi the amount does not display in paypal when making payment. The JavaScript code is shown below:
var form = document.querySelector('#payment-form');
var client_token = '{{ client_token }}';
braintree.dropin.create({
authorization: client_token,
container: '#bt-dropin',
card: {
cardholderName: {
required: true
}
},
applePay: {
displayName: 'Merchant Name',
paymentRequest: {
label: 'Localized Name',
total: '10.00'
}
},
paypal: {
flow: 'checkout',
amount: '10.00',
currency: 'USD'
},
paypalCredit: {
flow: 'checkout',
amount: '10.00',
currency: 'USD'
},
venmo: true
}, function (createErr, instance) {
if (createErr) {
// Handle any errors that might've occurred when creating Drop-in
console.error(err);
return;
}
form.addEventListener('submit', function (event) {
event.preventDefault();
instance.requestPaymentMethod(function (err, payload) {
if (err) {
console.log('Error', err);
return;
}
// Add the nonce to the form and submit
document.querySelector('#nonce').value = payload.nonce;
form.submit();
});
});
});
From the code, the amount is $10, but does not display in the PayPal payment page. Please what am i doing wrong?
Unless you pass line item information, an amount will not show during the PayPal checkout approval process.
very simple. I included the following to paypal option in the code :
commit: true,

Execute JS function after successful stripe payment

I've decided to use the custom button code supplied by Stripe for accepting payment on a single product I sell. It looks like this:
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="customButton">Hire Bike (1 Day)</button>
<script>
var handler = StripeCheckout.configure({
key: 'MY_KEY',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'auto',
token: function(token) {
// You can access the token ID with `token.id`.
// Get the token ID to your server-side code for use.
}
});
document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options:
handler.open({
name: 'Bike Company',
description: '1 Day Bike Hire',
currency: 'usd',
amount: 25000
});
e.preventDefault();
});
// Close Checkout on page navigation:
window.addEventListener('popstate', function() {
handler.close();
});
</script>
While it does work (when I use my actual public API key of course), what I can't find a solution for is a way to execute some of my own JS when the payment is successful.
I can't find an answer in the documentation, so looking for suggestions from the SO community.
You can run code in the token callback:
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="customButton">Hire Bike (1 Day)</button>
<script>
var handler = StripeCheckout.configure({
key: 'MY_KEY',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'auto',
token: function(token) {
// DO STUFF HERE
alert("Wahoo! You paid!")
}
});
document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options:
handler.open({
name: 'Bike Company',
description: '1 Day Bike Hire',
currency: 'usd',
amount: 25000
});
e.preventDefault();
});
// Close Checkout on page navigation:
window.addEventListener('popstate', function() {
handler.close();
});
</script>

orm sequelize query from two different tables in a single page load

I have this problem: need to display data on one page(page.hbs) different tables. Am using nodejs orm sequelize, mysql database, Handlebars. When the get method to the page page.hbs need to get data from two different tables. Here is my code:
models:
var roof_type = sequelize.define('roof_type', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false
},
description: {
type: DataTypes.STRING(50),
allowNull: true
}
});
return roof_type;
var garret_type = sequelize.define('garret_type', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false
},
description: {
type: DataTypes.STRING(50),
allowNull: true
}
});
return garret_type;
authcontroller.js:
exports.page_admin = function (req, res) {
db.roof_type.findAll({
description: 'description ASC'
}).then(function (data) {
var hbsObject = {
roof_types: data
};
res.render('page_admin', hbsObject);
});
}
exports.page_admin = function (req, res) {
db.garret_type.findAll({
description: 'description ASC'
}).then(function (data) {
var hbsObject = {
garret_types: data
};
res.render('page_admin', hbsObject);
});
}
routes:
module.exports = function (app, passport) {
app.get('/page_admin', isLoggedIn, authController.page_admin);
............
Please help(I do not really understand how to get one query to access two tables, as I understand it you need to fix authcontroller.js(merge requests) ) thank you in Advance for your help.
Use Promises
Promise.all([db.findAll(tableA), db.findAll(tableB)])
.then((data) => {
//data[0] is response from tableA find
// data[1] is from tableB
})

Categories