Reputation: 493
I am trying to implement Stripe in my backend which is in node js. While calling it from front end I am getting this error:
You did not provide an API key, though you did set your Authorization header to "null". Using Bearer auth, your Authorization header should look something like 'Authorization: Bearer YOUR_SECRET_KEY'. See https://stripe.com/docs/api#authentication for details, or we can help at https://support.stripe.com/.
Here is my code:
const stripe = require("stripe")(process.env.keySecret);
if(req.body.type == 'stripe') {
const token = req.body.token;
const amount = req.body.amount;
let charge = stripe.charges.create({
amount: amount,
source: token,
currency: 'USD'
},(err, chargeData) => {
if(err) {
console.log('error in buy premium api error... ', err);
return;
}
let payment = [];
payment.push({
amount: chargeData.amount,
address: chargeData.billing_details.address,
email: chargeData.billing_details.email,
payment_method_details: chargeData.payment_method_details.card
})
console.log('charge... ', charge);
if(charge) {
register.findByIdAndUpdate({ _id: therapist._id },
{
$set:
{
payment_details: payment
}
}, {new:true}, (e1, updatedUser) => {
if(e1) {
return;
}
resolve(updatedUser);
})
}
})
}
else {
let err = 'No payment type define..';
return reject(err);
}
My req.body is:
{
"token": "tok_1F2XPBG04BYg8nGtLoWnQwRU", // coming from front end
"amount": "250",
"type":"stripe"
}
The secret key is test secret key which is like sk_test_xxxxxxxxxxxxxxxx
.
My front end is in Angular 6 and from front end I am passing my test publishable key i.e. pk_test_xxxxxxxxxxxxxxxxx
. I am passing proper data to the both front end as well as backend. Where am I mistaken?
Upvotes: 4
Views: 2536
Reputation: 341
Since you are calling .env
file, you have to include the following line before requiring stripe:
require ("dotenv").config();
const stripe = require("stripe")(process.env.keySecret);
Upvotes: 1