Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// “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();
}
};
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
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