From d97ed180f53bd80816b32f3014b350ffdec53237 Mon Sep 17 00:00:00 2001 From: Jonathan Barrow Date: Sat, 25 Jun 2022 19:55:39 -0400 Subject: [PATCH 1/3] Change tier --- public/assets/css/account.css | 8 ++++-- src/routers/account.js | 48 +++++++++++++++++++++++++++----- views/account/account.handlebars | 17 +++++++---- views/account/upgrade.handlebars | 24 ++++++++-------- 4 files changed, 70 insertions(+), 27 deletions(-) diff --git a/public/assets/css/account.css b/public/assets/css/account.css index dbb4739..42a8dbe 100644 --- a/public/assets/css/account.css +++ b/public/assets/css/account.css @@ -31,7 +31,8 @@ border-radius: 100%; background: var(--btn-secondary); } -.account-sidebar .user #download-cemu-files { +.account-sidebar .user #download-cemu-files, +.account-sidebar .user #account-upgrade { display: flex; flex-flow: column; align-items: center; @@ -40,7 +41,8 @@ margin: 24px 0 0; text-decoration: none; } -.account-sidebar .user #download-cemu-files p.download-caption { +.account-sidebar .user #download-cemu-files p.caption, +.account-sidebar .user #account-upgrade p.caption { margin: 15px 0 0; } .account-sidebar .user p.cemu-warning { @@ -152,7 +154,7 @@ fieldset:disabled form label { color: var(--text); } -.setting-card #remove-discord-connection { +.setting-card #link-discord-account { width: 100%; padding: 12px 48px; cursor: pointer; diff --git a/src/routers/account.js b/src/routers/account.js index e6c6873..a8d0596 100644 --- a/src/routers/account.js +++ b/src/routers/account.js @@ -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'); } diff --git a/views/account/account.handlebars b/views/account/account.handlebars index 870300b..b39ad53 100644 --- a/views/account/account.handlebars +++ b/views/account/account.handlebars @@ -12,9 +12,13 @@

PNID: {{account.username}}

-

Download account files

+

Download account files

(will not work on Nintendo Network)

+ + +

Upgrade Account

+
@@ -52,8 +56,8 @@
-

Beta access

- {{#if isTester}} +

Server Environment

+ {{#if isTester}}
@@ -81,10 +85,13 @@

Connected to Discord as {{ discordUser.username }}#{{ discordUser.discriminator }}

{{else}} -

To gain access to the developer chats, run /command in the server.

+ + + {{/if}} {{else}} -

Beta servers are exclusive to testers. To upgrade to a higher tier, click here.

+

Account is not a beta tester, locked to production servers.

+

Beta servers are exclusive to beta testers.
To become a beta tester, upgrade to a higher account tier.

{{/if}}
diff --git a/views/account/upgrade.handlebars b/views/account/upgrade.handlebars index 55e7d23..e269420 100644 --- a/views/account/upgrade.handlebars +++ b/views/account/upgrade.handlebars @@ -26,43 +26,43 @@ - +

Upgrade

Subscribing to a tier gives you access to our developer chats and, depending on the tier, to our beta servers. Please someone reword this, it sucks. No fr.

- - {{#each tiers}} + + {{#each tiers}} - + {{/each}}
- - + +