-
-
Notifications
You must be signed in to change notification settings - Fork 301
Add Stripe subscription integration for paid storage upgrades #5810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: unstable
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,331 @@ | ||
| <template> | ||
|
|
||
| <div | ||
| class="subscription-card" | ||
| :style="{ backgroundColor: $themePalette.grey.v_100 }" | ||
| > | ||
| <div | ||
| v-if="loading" | ||
| class="loading" | ||
| > | ||
| <KCircularLoader /> | ||
| </div> | ||
|
|
||
| <div | ||
| v-else-if="isActive" | ||
| class="active-subscription" | ||
| > | ||
| <div class="status-header"> | ||
| <KIcon | ||
| icon="check" | ||
| :color="cancelAtPeriodEnd ? $themeTokens.annotation : $themeTokens.success" | ||
| /> | ||
| <span class="status-text"> | ||
| {{ cancelAtPeriodEnd ? $tr('subscriptionCanceling') : $tr('subscriptionActive') }} | ||
| </span> | ||
| </div> | ||
| <div | ||
| v-if="showSuccessMessage" | ||
| class="success-banner" | ||
| :style="{ backgroundColor: $themePalette.green.v_100, color: $themePalette.green.v_700 }" | ||
| > | ||
| <KIcon | ||
| icon="check" | ||
| :color="$themeTokens.success" | ||
| /> | ||
| <span>{{ $tr('upgradeSuccess', { size: subscriptionGb }) }}</span> | ||
| <KIconButton | ||
| icon="close" | ||
| :ariaLabel="$tr('dismiss')" | ||
| :size="'small'" | ||
| :color="$themePalette.green.v_400" | ||
| class="dismiss-btn" | ||
| @click="showSuccessMessage = false" | ||
| /> | ||
| </div> | ||
| <p | ||
| class="storage-info" | ||
| :style="{ color: $themeTokens.annotation }" | ||
| > | ||
| {{ $tr('storageIncluded', { size: subscriptionGb }) }} | ||
| </p> | ||
| <p | ||
| v-if="formattedPeriodEnd" | ||
| class="period-notice" | ||
| :style="{ color: cancelAtPeriodEnd ? $themeTokens.error : $themeTokens.annotation }" | ||
| > | ||
| {{ | ||
| cancelAtPeriodEnd | ||
| ? $tr('cancelNotice', { date: formattedPeriodEnd }) | ||
| : $tr('renewalNotice', { date: formattedPeriodEnd }) | ||
| }} | ||
| </p> | ||
| <KButton | ||
| :text="$tr('manageSubscription')" | ||
| appearance="basic-link" | ||
| @click="handleManageClick" | ||
| /> | ||
| </div> | ||
|
|
||
| <div | ||
| v-else | ||
| class="upgrade-prompt" | ||
| > | ||
| <h3>{{ $tr('instantUpgrade') }}</h3> | ||
| <p>{{ $tr('upgradeDescription') }}</p> | ||
| <div class="storage-selector"> | ||
| <KTextbox | ||
| v-model="selectedGb" | ||
| type="number" | ||
| :label="$tr('storageAmount')" | ||
| :min="1" | ||
| :max="50" | ||
| :invalid="!isValidGb" | ||
| :invalidText="$tr('storageRange')" | ||
| :showInvalidText="true" | ||
| class="gb-input" | ||
| /> | ||
| <span class="price-display"> | ||
| {{ $tr('annualPrice', { price: validGb * PRICE_PER_GB }) }} | ||
| </span> | ||
| </div> | ||
| <KButton | ||
| :primary="true" | ||
| :disabled="!isValidGb || redirecting" | ||
| class="upgrade-btn" | ||
| @click="handleUpgradeClick" | ||
| > | ||
| <span class="upgrade-btn-content"> | ||
| <KCircularLoader | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/learningequality/studio/pull/5810/changes#r3038962811 (not 100% sure, if I recall there may animation glitch if |
||
| v-if="redirecting" | ||
| :size="24" | ||
| :stroke="3" | ||
| class="upgrade-btn-loader" | ||
| /> | ||
| <span :style="{ visibility: redirecting ? 'hidden' : 'visible' }"> | ||
| {{ $tr('upgradeNow') }} | ||
| </span> | ||
| </span> | ||
| </KButton> | ||
| </div> | ||
|
|
||
| <Banner | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you please use non-Vuetify replacement Note that there will be a visual difference compared to |
||
| v-if="error" | ||
| :value="true" | ||
| :text="$tr('genericError')" | ||
| error | ||
| class="error-banner" | ||
| /> | ||
| </div> | ||
|
|
||
| </template> | ||
|
|
||
|
|
||
| <script> | ||
|
|
||
| import { ref, watch } from 'vue'; | ||
| import { useRoute, useRouter } from 'vue-router/composables'; | ||
| import { useSubscription } from './useSubscription'; | ||
| import { ONE_GB } from 'shared/constants'; | ||
| import Banner from 'shared/views/Banner'; | ||
|
|
||
| const MIN_GB = 1; | ||
| const MAX_GB = 50; | ||
| const PRICE_PER_GB = 15; | ||
|
|
||
| export default { | ||
| name: 'SubscriptionCard', | ||
| components: { | ||
| Banner, | ||
| }, | ||
| setup() { | ||
| const { | ||
| loading, | ||
| redirecting, | ||
| error, | ||
| isActive, | ||
| storageBytes, | ||
| cancelAtPeriodEnd, | ||
| currentPeriodEnd, | ||
| fetchSubscriptionStatus, | ||
| createCheckoutSession, | ||
| createPortalSession, | ||
| } = useSubscription(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yay composable |
||
|
|
||
| const showSuccessMessage = ref(false); | ||
| const selectedGb = ref(10); | ||
|
|
||
| const route = useRoute(); | ||
| const router = useRouter(); | ||
|
|
||
| fetchSubscriptionStatus(); | ||
|
|
||
| watch( | ||
| () => route.query.upgrade, | ||
| val => { | ||
| if (val === 'success') { | ||
| showSuccessMessage.value = true; | ||
| router.replace({ query: {} }); | ||
| } | ||
| }, | ||
| { immediate: true }, | ||
| ); | ||
|
|
||
| const handleUpgradeClick = () => { | ||
| createCheckoutSession(Number(selectedGb.value)); | ||
| }; | ||
|
|
||
| const handleManageClick = () => { | ||
| createPortalSession(); | ||
| }; | ||
|
|
||
| return { | ||
| loading, | ||
| redirecting, | ||
| error, | ||
| isActive, | ||
| storageBytes, | ||
| cancelAtPeriodEnd, | ||
| currentPeriodEnd, | ||
| showSuccessMessage, | ||
| selectedGb, | ||
| PRICE_PER_GB, | ||
| handleUpgradeClick, | ||
| handleManageClick, | ||
| }; | ||
| }, | ||
| computed: { | ||
| subscriptionGb() { | ||
| if (this.storageBytes) { | ||
| return `${Math.round(this.storageBytes / ONE_GB)} GB`; | ||
| } | ||
| return `${MIN_GB} GB`; | ||
| }, | ||
| formattedPeriodEnd() { | ||
| if (!this.currentPeriodEnd) { | ||
| return null; | ||
| } | ||
| return this.$formatDate(this.currentPeriodEnd); | ||
| }, | ||
| validGb() { | ||
| const n = Number(this.selectedGb); | ||
| if (!Number.isInteger(n) || n < MIN_GB || n > MAX_GB) { | ||
| return MIN_GB; | ||
| } | ||
| return n; | ||
| }, | ||
| isValidGb() { | ||
| const n = Number(this.selectedGb); | ||
| return Number.isInteger(n) && n >= MIN_GB && n <= MAX_GB; | ||
| }, | ||
| }, | ||
| $trs: { | ||
| instantUpgrade: 'Instant Storage Upgrade', | ||
| upgradeDescription: 'Purchase additional storage at $15/GB per year.', | ||
| upgradeNow: 'Upgrade Now', | ||
| storageAmount: 'Storage (GB)', | ||
| storageRange: 'Enter a value between 1 and 50', | ||
| annualPrice: '${price}/year', | ||
| subscriptionActive: 'Storage Subscription Active', | ||
| subscriptionCanceling: 'Subscription Canceling', | ||
| cancelNotice: 'Your subscription will expire on {date}. Storage will be removed after that.', | ||
| renewalNotice: 'Your subscription will automatically renew on {date}.', | ||
| storageIncluded: '{size} included with your subscription', | ||
| manageSubscription: 'Manage Subscription', | ||
| upgradeSuccess: 'Storage increased to {size}', | ||
| dismiss: 'Dismiss', | ||
| genericError: 'There was a problem connecting to our payment provider. Please try again.', | ||
| }, | ||
| }; | ||
|
|
||
| </script> | ||
|
|
||
|
|
||
| <style lang="scss" scoped> | ||
|
|
||
| .subscription-card { | ||
| max-width: 500px; | ||
| padding: 24px; | ||
| margin-bottom: 24px; | ||
| border-radius: 8px; | ||
| } | ||
|
|
||
| .loading { | ||
| display: flex; | ||
| justify-content: center; | ||
| padding: 16px; | ||
| } | ||
|
|
||
| .status-header { | ||
| display: flex; | ||
| align-items: center; | ||
| margin-bottom: 8px; | ||
| } | ||
|
|
||
| .status-text { | ||
| margin-left: 8px; | ||
| font-weight: bold; | ||
| } | ||
|
|
||
| .storage-info { | ||
| margin-bottom: 16px; | ||
| } | ||
|
|
||
| .period-notice { | ||
| margin-bottom: 16px; | ||
| font-size: 0.9em; | ||
| } | ||
|
|
||
| .upgrade-prompt h3 { | ||
| margin-top: 0; | ||
| margin-bottom: 8px; | ||
| } | ||
|
|
||
| .upgrade-prompt p { | ||
| margin-bottom: 16px; | ||
| } | ||
|
|
||
| .storage-selector { | ||
| display: flex; | ||
| gap: 12px; | ||
| align-items: flex-start; | ||
| margin-bottom: 16px; | ||
| } | ||
|
|
||
| .gb-input { | ||
| max-width: 120px; | ||
| } | ||
|
|
||
| .price-display { | ||
| padding-top: 24px; | ||
| font-weight: bold; | ||
| } | ||
|
|
||
| .upgrade-btn-content { | ||
| display: inline-grid; | ||
| align-items: center; | ||
| justify-items: center; | ||
| } | ||
|
|
||
| .upgrade-btn-content > * { | ||
| grid-area: 1 / 1; | ||
| } | ||
|
|
||
| .error-banner { | ||
| margin-top: 16px; | ||
| } | ||
|
|
||
| .success-banner { | ||
| display: flex; | ||
| gap: 8px; | ||
| align-items: center; | ||
| padding: 8px 12px; | ||
| margin-bottom: 12px; | ||
| border-radius: 4px; | ||
| } | ||
|
|
||
| .dismiss-btn { | ||
| margin-left: auto; | ||
| } | ||
|
|
||
| </style> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For new loaders, could you use
v-if="show('loader', loading, 400)"to align with https://design-system.learningequality.org/loaders#minimum-visible-time?(old loaders in Studio don't have this, and some newer loaders use wrong value, but 400 is closest to the expected experience ~ we recently fine-tuned with Tomiwa)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's an example which also has transition applied https://design-system.learningequality.org/usekshow#example