Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fbohz/913c363623ccff6f82e8d5ee35bc79e3 to your computer and use it in GitHub Desktop.
Save fbohz/913c363623ccff6f82e8d5ee35bc79e3 to your computer and use it in GitHub Desktop.
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const path = require('path');
if (process.env.NODE_ENV !== 'production') require('dotenv').config();
// remember to have your .env file with your STRIPE_SECRET_KEY defined.
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, '../frontend/build')));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, '../frontend/build', 'index.html'));
});
}
app.listen(port, error => {
if (error) throw error;
console.log('Server running on port ' + port);
});
// post route for Stripe API
app.post('/payment', (req, res) => {
const body = {
source: req.body.token.id,
amount: req.body.amount,
currency: 'usd'
};
stripe.charges.create(body, (stripeErr, stripeRes) => {
if (stripeErr) {
res.status(500).send({ error: stripeErr });
} else {
res.status(200).send({ success: stripeRes });
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment