Skip to content
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

Billing-fixes-2.5.25 #1158

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions apps/api/src/app/controllers/billing.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,18 @@ const createCheckoutSessionHandler = createRoute(
}
);

const processCheckoutSuccessHandler = createRoute(
routeDefinition.processCheckoutSuccess.validators,
async ({ user: sessionUser, query }, req, res) => {
ensureStripeIsInitialized();
const { subscribeAction, sessionId } = query;

if (!subscribeAction || !sessionId) {
return redirect(res, `${ENV.JETSTREAM_CLIENT_URL}/settings/billing`);
}
const processCheckoutSuccessHandler = createRoute(routeDefinition.processCheckoutSuccess.validators, async ({ user, query }, req, res) => {
ensureStripeIsInitialized();
const { subscribeAction, sessionId } = query;

const user = await userDbService.findByIdWithSubscriptions(sessionUser.id);
if (!user.subscriptions?.[0]?.customerId) {
return redirect(res, `${ENV.JETSTREAM_CLIENT_URL}/settings/billing`);
}
if (!subscribeAction || !sessionId) {
return redirect(res, `${ENV.JETSTREAM_CLIENT_URL}/settings/billing`);
}

await stripeService.saveSubscriptionFromCompletedSession({ sessionId });
await stripeService.saveSubscriptionFromCompletedSession({ sessionId });

redirect(res, `${ENV.JETSTREAM_CLIENT_URL}/settings/billing`);
}
);
redirect(res, `${ENV.JETSTREAM_CLIENT_URL}/settings/billing`);
});

const getSubscriptionsHandler = createRoute(routeDefinition.getSubscriptions.validators, async ({ user }, req, res) => {
ensureStripeIsInitialized();
Expand Down
26 changes: 19 additions & 7 deletions apps/api/src/app/services/stripe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ export async function handleStripeWebhook({ signature, payload }: { signature?:
logger.info({ eventId: event.id, eventType: event.type }, '[STRIPE]: Handling event %s', event.type);

switch (event.type) {
case 'entitlements.active_entitlement_summary.updated':
{
await updateEntitlements(event.data.object);
}
case 'entitlements.active_entitlement_summary.updated': {
await updateEntitlementsFromWebhook(event.data.object);
break;
}
case 'customer.subscription.created':
case 'customer.subscription.deleted':
case 'customer.subscription.paused':
Expand All @@ -60,6 +59,10 @@ export async function fetchCustomerWithSubscriptionsById({ customerId }: { custo
});
}

export async function fetchCustomerEntitlements({ customerId }: { customerId: string }) {
return await stripe.entitlements.activeEntitlements.list({ customer: customerId });
}

export async function fetchCustomerWithSubscriptionsByJetstreamId({ userId }: { userId: string }) {
const customerWithSubscriptions = await stripe.customers.search({
query: `metadata["userId"]:"${userId}"`,
Expand Down Expand Up @@ -134,9 +137,13 @@ export async function createCustomer(user: UserProfile) {
});
}

export async function updateEntitlements(eventData: Stripe.Entitlements.ActiveEntitlementSummary) {
export async function updateEntitlementsFromWebhook(eventData: Stripe.Entitlements.ActiveEntitlementSummary) {
const { customer, entitlements } = eventData;
const entitlementAccess = entitlements.data.reduce(
await updateEntitlements(customer, entitlements.data);
}

export async function updateEntitlements(customerId: string, entitlements: Stripe.Entitlements.ActiveEntitlement[]) {
const entitlementAccess = entitlements.reduce(
(entitlementAccess: EntitlementsAccess, { lookup_key }) => {
if (lookup_key in entitlementAccess) {
entitlementAccess[lookup_key] = true;
Expand All @@ -150,7 +157,7 @@ export async function updateEntitlements(eventData: Stripe.Entitlements.ActiveEn
}
);

await subscriptionDbService.updateUserEntitlements(customer, entitlementAccess);
await subscriptionDbService.updateUserEntitlements(customerId, entitlementAccess);
}

export async function saveSubscriptionFromCompletedSession({ sessionId }: { sessionId: string }) {
Expand All @@ -160,9 +167,14 @@ export async function saveSubscriptionFromCompletedSession({ sessionId }: { sess
throw new Error('Invalid checkout session - a customer is required to be associated with the session');
}

// Update customer subscriptions - will also be updated via webhook
const customerOrId = session.customer;
const customer = await fetchCustomerWithSubscriptionsById({ customerId: isString(customerOrId) ? customerOrId : customerOrId.id });
await saveOrUpdateSubscription({ customer });

// Update customer entitlements - will also be updated via webhook
const entitlements = await fetchCustomerEntitlements({ customerId: customer.id });
await updateEntitlements(customer.id, entitlements.data);
}

/**
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions apps/api/src/assets/images/jetstream-logo-pro.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion apps/jetstream-web-extension/src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Jetstream",
"description": "Jetstream is a set of tools that supercharge your administration of Salesforce.com.",
"version": "0.0.2",
"version": "0.1.0",
"manifest_version": 3,
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmPjyzVG3Kpm0wyjDECyDLnwPlyEhlNmfzcPVybZz9gFKiHTm0qEuUYqGqOSAOWE6KnOR8RbiNkyrROGeEbGmhZIXxS02yKPTpJsA4qflKtTUGoeT7XcOm9cHWw3+BrVuqABCdXEwv/Do1/UX2vu9WeOMHQAr911nVl77JrhhaMQvpu0ruJjD+sZ9QbfjdLNeVeU1+hzgpuvpDnMihDIfZv47GaqvTTwxT67P09loBySjXMmx2mrtOpyLzsATLwQr6kmQQAX5X23ykw3PxY4NWBDldqXxKqTomMkwa3x6mh/4jmI5h6p6Wt6Gaf9FcG1pBOXTrUHNfdFzuK6ahYBsRQIDAQAB",
"icons": {
Expand Down
35 changes: 20 additions & 15 deletions apps/jetstream/src/app/components/billing/Billing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ScopedNotification,
Spinner,
} from '@jetstream/ui';
import { useAmplitude } from '@jetstream/ui-core';
import { JetstreamLogoPro, useAmplitude } from '@jetstream/ui-core';
import { useCallback, useEffect, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { environment } from '../../../environments/environment';
Expand Down Expand Up @@ -72,7 +72,7 @@ export const Billing = () => {
<Page testId="billing-page">
<PageHeader>
<PageHeaderRow>
<PageHeaderTitle icon={{ type: 'standard', icon: 'your_account' }} label="Billing" />
<PageHeaderTitle icon={{ type: 'standard', icon: 'billing' }} label="Billing" />
</PageHeaderRow>
</PageHeader>
<AutoFullHeightContainer className="slds-p-around_small slds-scrollable_none" bufferIfNotRendered={HEIGHT_BUFFER}>
Expand Down Expand Up @@ -109,24 +109,29 @@ export const Billing = () => {
</div>
)}

<div className="slds-box slds-box_small slds-m-bottom_small">
{(!customerWithSubscriptions || !subscriptionStatus.hasActiveSubscriptions) && (
<JetstreamLogoPro width="250px" className="slds-m-bottom_x-small" />
)}
<p className="slds-text-heading_medium">Jetstream Professional Includes:</p>
<ul className="slds-list_dotted slds-m-bottom_small">
<li>
Access to the{' '}
<a href={APP_ROUTES.CHROME_EXTENSION.ROUTE} target="_blank" className="slds-text-heading_x-small" rel="noreferrer">
Chrome Extension
</a>
</li>
<li>Save query history across devices</li>
<li>Save downloads to Google Drive</li>
<li>Load data from Google Drive</li>
</ul>
</div>

<div className="slds-box slds-box_small">
{customerWithSubscriptions && subscriptionStatus.hasActiveSubscriptions ? (
<BillingExistingSubscriptions customerWithSubscriptions={customerWithSubscriptions} />
) : (
<form method="POST" action="/api/billing/checkout-session">
<p className="slds-text-title_bold">Upgrading to a Jetstream Professional plan includes</p>
<ul className="slds-list_dotted slds-m-bottom_small">
<li>
Access to the{' '}
<a href={APP_ROUTES.CHROME_EXTENSION.ROUTE} target="_blank" className="slds-text-heading_x-small" rel="noreferrer">
Chrome Extension
</a>
</li>
<li>Save query history across devices</li>
<li>Save downloads to Google Drive</li>
<li>Load data from Google Drive</li>
</ul>

<fieldset className="slds-form-element" role="radiogroup">
<legend className="slds-form-element__legend slds-form-element__label">Select a plan</legend>
<div className="slds-form-element__control">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { APP_ROUTES } from '@jetstream/shared/ui-router';
import { StripeUserFacingCustomer } from '@jetstream/types';
import { FeedbackLink } from '@jetstream/ui';
import { useState } from 'react';
import { environment } from '../../../environments/environment';
import BillingPlanCard from './BillingPlanCard';
Expand Down Expand Up @@ -38,13 +38,15 @@ export const BillingExistingSubscriptions = ({ customerWithSubscriptions }: Bill
priceDescription="Billed Annually"
onChange={setSelectedPlan}
/>

{/* <BillingPlanCard descriptionTitle="Team - Annual" price="Coming Soon" priceDescription="Billed Annually" disabled /> */}
</div>
<p className="slds-text-body_small slds-m-bottom_x-small">To change plans, visit the billing portal.</p>
<a href={APP_ROUTES.CHROME_EXTENSION.ROUTE} target="_blank" className="slds-text-heading_x-small" rel="noreferrer">
Visit the Chrome Store to install the Chrome Extension
</a>
<form method="POST" action="/api/billing/portal" target="_blank" className="slds-m-bottom_x-small">
<p className="slds-text-body_small">
To change plans, visit the
<button className="slds-button slds-m-left_xx-small">billing portal</button>.
</p>
</form>
<FeedbackLink type="EMAIL" omitInNewWindowIcon label="Have questions or need help? Send us an email." />
</fieldset>
</div>
);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading