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

Checking what payment provider tenant is using before warning payment methods #785

Merged
merged 7 commits into from
Oct 11, 2023
23 changes: 20 additions & 3 deletions src/api/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,26 @@ export const getPaymentMethodsForTenants = async (
let count = 0;

tenants.some((tenantDetail) => {
promises.push(
limiter(() => getTenantPaymentMethods(tenantDetail.tenant))
);
if (
tenantDetail.payment_provider === 'external' ||
!tenantDetail.trial_start
) {
promises.push(
new Promise((resolve) => {
resolve({
data: {
tenant: tenantDetail.tenant,
skipPaymentMethod: true,
},
});
})
);
} else {
promises.push(
limiter(() => getTenantPaymentMethods(tenantDetail.tenant))
);
}

count += 1;
return count >= MAX_TENANTS;
});
Expand Down
8 changes: 7 additions & 1 deletion src/api/tenants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { supabaseClient, TABLES } from 'services/supabase';
import { Tenants } from 'types';

const COLUMNS = ['tasks_quota', 'collections_quota', 'tenant', 'trial_start'];
const COLUMNS = [
'collections_quota',
'payment_provider',
'tasks_quota',
'tenant',
'trial_start',
];

const getTenantDetails = () => {
return supabaseClient
Expand Down
6 changes: 3 additions & 3 deletions src/components/admin/Billing/AddPaymentMethod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function AddPaymentMethod({
stripePromise,
tenant,
}: Props) {
const enableButton =
const enable =
setupIntentSecret !== INTENT_SECRET_LOADING &&
setupIntentSecret !== INTENT_SECRET_ERROR;

Expand All @@ -35,7 +35,7 @@ function AddPaymentMethod({
<Box>
<LoadingButton
loadingPosition="start"
disabled={!enableButton}
disabled={!enable}
loading={setupIntentSecret === INTENT_SECRET_LOADING}
onClick={() => setOpen(true)}
startIcon={<Plus style={{ fontSize: 15 }} />}
Expand All @@ -58,7 +58,7 @@ function AddPaymentMethod({
<DialogTitle>
<FormattedMessage id="admin.billing.addPaymentMethods.title" />
</DialogTitle>
{setupIntentSecret ? (
{enable ? (
<Elements
stripe={stripePromise}
options={{
Expand Down
26 changes: 18 additions & 8 deletions src/hooks/billing/useTenantMissingPaymentMethodWarning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,27 @@ function useTenantMissingPaymentMethodWarning() {

// We have tenant in trial and now need to go through all the payment methods and see if any are missing
if (paymentMethods.responses.length > 0) {
// Go through each tenant to try to find the payment methods
tenantsInTrial.some((tenantInTrial) => {
const currentTenant = tenantInTrial.tenant;

// Find the payment method for this tenant
const paymentMethodForTenant = paymentMethods.responses.find(
(paymentMethod) => {
return currentTenant === paymentMethod.tenant;
// Step through the responses we have to see if the tenant is missing details
// we do not step through the tenants so if any calls to fetch payment methods
// failed we do not show a warning accidently
paymentMethods.responses.some((paymentMethodForTenant) => {
const currentTenant = paymentMethodForTenant.tenant;

const tenantInTrial = tenantsInTrial.find(
(tenantInTrialCurr) => {
return tenantInTrialCurr.tenant === currentTenant;
}
);

// We can skip if there are no tenant details found
// We skip those not in a trial OR have payment methods outside our provider
if (
!tenantInTrial ||
paymentMethodForTenant?.skipPaymentMethod
) {
return false;
}

// We check the methods list and see if one exists. We are not checking if a primary card is set
// at this time (Q4 2023) but that might change based on experience.
const hasPaymentMethod =
Expand Down
9 changes: 6 additions & 3 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,17 @@ export interface StorageMappings {
updated_at: string;
}

export type TenantPaymentProviders = 'external' | 'stripe';

export interface Tenants {
id: string;
tasks_quota: number;
collections_quota: number;
created_at: string;
detail: string;
id: string;
payment_provider: TenantPaymentProviders;
tasks_quota: number;
tenant: string;
trial_start: string;
created_at: string;
updated_at: string;
}

Expand Down