you weren’t really that far off, it’s just that your routes were wrong, you don’t need to hit the stripe domain since you already have a back-end setup that communicates with the stripe api by using your secret back-end key and the imported stripe package, so whenever you use stripe.charges.create
or any other builtin methods they have, it talks to their own domain, you can just use custom routes.
The one thing I am still not clear about is this https://stripe.com/docs/api/charges/create , it says the source
argument is optional but clearly will not work without it if you want to do a charge, this is the error you get if you don’t provide a source
Error: Must provide source or customer.
at Constructor._Error (C:\....\stripe_test\node_modules\stripe\lib\Error.js:12:16)
at Constructor (C:\....\stripe_test\node_modules\stripe\lib\utils.js:164:17)
1 Like
Thanks a lot for this, just confused as to why my route isn’t showing up.
I have this in app.js and it doesn’t seem to be working:
const stripeRouter = require('./src/routes/stripe')
app.use('/stripe', stripeRouter)
I can’t see what / how you exported your routes file, but if properly done you should be able to access it with just app.use(stripeRouter)
typically your routes file should look something like
const router = require('express').Router();
router.get('/anyroute',(req,res)=>{
res.send('Hello')
})
module.exports = router;
1 Like
Okay, I got it working by taking out ‘/stripe’ in the app.use line.
Now I get the following error in my terminal:
{ code: 'parameter_missing',
doc_url: 'https://stripe.com/docs/error-codes/parameter-missing',
message:
'You must supply either a card, customer, PII data, bank account, or account legal entity to create a token. If you\'re making this request with a library, be sure to pass all of the required parameters for creating a token. If you\'re making this request manually, be sure your POST parameters begin with the token type. For example, a PII token would require `pii[personal_id_number]`, while an account token would require a parameter beginning with `account[legal_entity]`. See the API reference for more information: https://stripe.com/docs/api#token_object',
did you take a look at the github code I posted ? Do you have both stipe api keys backend and frontend ? did you register your phone #?
1 Like
Yeah, I am trying to refactor it just slightly, as I’m putting in the info on the backend. You need the phone registered?
yeah if you don’t register a phone number then you have to use their custom client script, look at the error messages you get in the backend
1 Like
Maybe I’m dumb, but I really don’t see it.
But here’s the code now:
const express = require('express')
const router = express.Router()
require('dotenv').config()
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY)
router.post('/stripe', async (req, res) => {
const stripeToken = req.body.stripeToken
try {
const card = await stripe.tokens.create({
id: req.body.id,
amount: 500,
currency: "usd",
description: "Example Charge",
source: stripeToken
})
const charge = await stripe.charges.create(card)
res.json(charge)
console.log(charge)
} catch (error) {
console.log(error)
}
})
module.exports = router
Frontend:
stripeBtn = async (token) => {
console.log('onToken', token)
await fetch('http://localhost:3000/stripe', {
method: 'POST',
headers: {
'Authorization': `Bearer ${stripeKey}`,
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
stripeToken: token.id,
}),
})
.then(response => response.json())
.then(json => {
console.log('json>>>>', json)
})
}
Where are you getting the token from ?, you got to let stripe generate the token, here is the workflow,
In the front-end, you send your credit number and purchase information, then in the backend, you use the credit card number to generate the token and then use the token.id to make the charge,
First try and make it work with the code i posted and then modify it according to your needs
1 Like
I should just use your code, lmao.
yeah, just to see if it is working then do your mods
1 Like
Okay, so I’m using Stripe Checkout, that’s how I was getting the id that way.
I see, yeah, i opted not to use any of their client scripts so I could understand what was going on, are you getting the id successfully using checkout ?
1 Like
I was, yeah, but wouldn’t get this error. I can still get it to work this way and then change it, right? Make the card info not the test data but whatever they input in the field?
actually, if you are already getting the id successfully then hmmm… let me think about it, ah then you do not need to create another token in the back end, just use that token to make a purchase, try something like this in your server
router.post('/stripe', async (req, res) => {
const stripeToken = req.body.stripeToken
try {
const charge = await stripe.charges.create({
amount: 500,
currency: "usd",
description: "Example Charge",
source: stripeToken
})
res.json(charge)
console.log(charge)
} catch (error) {
console.log(error)
}
})
1 Like
basically the way you had it above you were recreating the token, but you don’t need to
1 Like
Yeah!! That worked! It worked the other way then just refactored it. Thank you so much! I will ask more questions as I work through it. I know I’ll have to store the id of the buyer and the id of the item, too.
basically the confusion was that you were using stripe checkout and I hadn’t realized that
1 Like
Yeah, sorry about that! Thank you so much.