Created
January 31, 2019 04:31
-
-
Save harrisonmalone/054d059454b5864fedb353e33f84d81d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const router = express.Router(); | |
const User = require('../models/User.model'); | |
require('dotenv').config(); | |
const stripe = require("stripe")(process.env.REACT_APP_STRIPE_SECRET_KEY) // this is not posting | |
router.post('/api/stripe', (req, res, next) => { | |
const { token, email, selectedOption } = req.body | |
stripe.customers.create({ | |
email: email, | |
source: token.id, | |
}, ((err, customer) => { | |
User.findOne({email}) | |
.then( user => { | |
user.stripeId = customer.id; | |
return user.save(); | |
}) | |
.catch( err => console.log(err)) | |
if (err) { | |
res.send({ | |
success: false, | |
message: err | |
}) | |
} else if (selectedOption === "monthly") { | |
const { id } = customer | |
stripe.subscriptions.create({ | |
customer: id, | |
items: [ | |
{ | |
plan: "plan_EOUE6qieRKFekI", | |
}, | |
], | |
}, function(err, subscription) { | |
if (err) { | |
res.send({ | |
success: false, | |
message: 'Error' | |
}) | |
} | |
else { | |
res.send({ | |
success: true, | |
message: 'Success' | |
}) | |
} | |
}) | |
} | |
else if (selectedOption === "quarterly") { | |
const { id } = customer | |
stripe.subscriptions.create({ | |
customer: id, | |
items: [ | |
{ | |
plan: "hfjsdjkhg", | |
}, | |
], | |
}, function(err, subscription) { | |
if (err) { | |
res.send({ | |
success: false, | |
message: 'Error' | |
}) | |
} | |
else { | |
res.send({ | |
success: true, | |
message: 'Success' | |
}) | |
} | |
}) | |
} | |
}) | |
)} | |
) | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment