mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-09-29 12:06:40 -05:00
Change tier
This commit is contained in:
@@ -29,7 +29,7 @@ router.get('/', async (request, response) => {
|
||||
}
|
||||
|
||||
const { upgrade_success } = request.query;
|
||||
console.log(upgrade_success);
|
||||
|
||||
const stripe = {};
|
||||
if (upgrade_success === 'true') {
|
||||
stripe.showNotice = true;
|
||||
@@ -193,6 +193,8 @@ router.get('/', async (request, response) => {
|
||||
renderData.discordAuthURL = discordAuthURL;
|
||||
}
|
||||
|
||||
renderData.isTester = false;
|
||||
|
||||
response.render('account/account', renderData);
|
||||
});
|
||||
|
||||
@@ -616,16 +618,48 @@ router.post('/checkout/:priceId', async (request, response) => {
|
||||
|
||||
await database.PNID.updateOne({ pid }, {
|
||||
$set: {
|
||||
connections: {
|
||||
stripe: {
|
||||
customer_id: customer.id // ensure PNID always has latest customer ID
|
||||
}
|
||||
}
|
||||
'connections.stripe.customer_id': customer.id // ensure PNID always has latest customer ID
|
||||
}
|
||||
}, { upsert: true }).exec();
|
||||
|
||||
const priceId = request.params.priceId;
|
||||
|
||||
const pnid = await database.PNID.findOne({ pid });
|
||||
|
||||
if (pnid.get('access_level') >= 2) {
|
||||
response.cookie('error', 'Staff members do not need to purchase tiers', { domain: '.pretendo.network' });
|
||||
return response.redirect('/account');
|
||||
}
|
||||
|
||||
const stripeConnection = pnid.get('connections.stripe');
|
||||
|
||||
if (stripeConnection.subscription_id) {
|
||||
// User already has a subscription with us
|
||||
if (stripeConnection.price_id === priceId) {
|
||||
response.cookie('error', 'Already subscribed to this tier', { domain: '.pretendo.network' });
|
||||
return response.redirect('/account');
|
||||
}
|
||||
|
||||
try {
|
||||
const subscription = await stripe.subscriptions.retrieve(stripeConnection.subscription_id);
|
||||
stripe.subscriptions.update(stripeConnection.subscription_id, {
|
||||
cancel_at_period_end: false,
|
||||
proration_behavior: 'always_invoice',
|
||||
items: [{
|
||||
id: subscription.items.data[0].id,
|
||||
price: priceId,
|
||||
}]
|
||||
});
|
||||
|
||||
return response.redirect('/account?upgrade_success=true');
|
||||
} catch (error) {
|
||||
// Maybe we need a dedicated error page?
|
||||
// Or handle this as not cookies?
|
||||
response.cookie('error', error.message, { domain: '.pretendo.network' });
|
||||
return response.redirect('/account');
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
line_items: [
|
||||
@@ -643,7 +677,7 @@ router.post('/checkout/:priceId', async (request, response) => {
|
||||
return response.redirect(303, session.url);
|
||||
} catch (error) {
|
||||
// Maybe we need a dedicated error page?
|
||||
// O handle this as not cookies?
|
||||
// Or handle this as not cookies?
|
||||
response.cookie('error', error.message, { domain: '.pretendo.network' });
|
||||
return response.redirect('/account');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user