Last active
October 3, 2017 22:05
-
-
Save homeyer/8f3dbf48c047656ff640fdea0b95b69c to your computer and use it in GitHub Desktop.
This file contains 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
// “InstallationURL” configured in marketplace | |
app.get('/marketplace/purchase', | |
// mark as a marketplace purchase | |
(req, res, next) => { | |
req.session.isMarketplacePurchase = true; | |
next(); | |
}, | |
//send users through your normal GitHub Oauth | |
passport.authenticate('github') | |
); | |
// oauth callback | |
app.get('/auth/github/callback', | |
passport.authenticate('github', { failureRedirect: '/login' }), | |
handleMarketplacePurchase, | |
(req, res) => { | |
delete req.session.isMarketplacePurchase; | |
// Successful authentication, redirect home. | |
res.redirect('/'); | |
} | |
); | |
handleMarketplacePurchase: async (req, res, next) => { | |
// a user might be the owner for more than one purchased subscription (multiple orgs, or org and personal) | |
const purchases = await request.get('/user/marketplace_purchases'); | |
for(let purchase of purchases){ | |
let account = await Account.findOne({owner: purchase.account.login}); | |
account = account || new Account({ | |
owner: purchase.account.login, | |
type: purchase.account.type | |
}); | |
account.marketplacePlan = purchase.plan.name; | |
// "subscriptionEndDate" is a consistent way between Stripe and Marketplace to toggle free vs paid features | |
if (purchase.plan.price_model.toLowerCase() === 'free') { | |
account.subscriptionEndDate = undefined; | |
} else { | |
account.subscriptionEndDate = new Date('2517'); | |
} | |
// cancel an existing plan | |
if (account.stripeId) { | |
await StripeHelper.deleteSubscriptionInStripe({ account }); | |
} | |
await account.save(); | |
} | |
}; |
This file contains 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
app.post('/marketplace/receive’, (req, res, next) => | |
switch | |
when 'canceled', 'cancelled' | |
account.cancelAt = effective_date + 29 days | |
when 'changed' | |
account.marketplacePlan = marketplace_purchase.plan.name | |
account.subscriptionEndDate = new Date("2517") | |
if ‘free’ | |
account.subscriptionEndDate = effective_date |
This file contains 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
app.get('/auth/github', | |
passport.authenticate('github') | |
); | |
app.get('/auth/github/callback', | |
passport.authenticate('github', { failureRedirect: '/login' }), | |
(req, res) => { | |
// Successful authentication, redirect home. | |
res.redirect('/'); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment