Skip to content
This repository was archived by the owner on Feb 24, 2023. It is now read-only.

Commit 4af09de

Browse files
authored
Merge pull request #95 from scale8/publishabe-keys-from-api
Get publishable keys from the api.
2 parents b920a32 + 9ab96a2 commit 4af09de

File tree

11 files changed

+55
-16
lines changed

11 files changed

+55
-16
lines changed

api/src/backends/configuration/abstractions/BaseConfig.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ export default abstract class BaseConfig {
258258
return (await this.getConfigEntryOrElse('CAPTCHA_ENABLED', 'true')) === 'true';
259259
}
260260

261+
public async getCaptchaPublishable(): Promise<string> {
262+
return await this.getConfigEntryThrows('CAPTCHA_PUBLISHABLE');
263+
}
264+
261265
public async getCaptchaSecret(): Promise<string> {
262266
return await this.getConfigEntryThrows('CAPTCHA_SECRET');
263267
}
@@ -270,6 +274,10 @@ export default abstract class BaseConfig {
270274
return await this.getConfigEntryThrows('AIRBRAKE_KEY');
271275
}
272276

277+
public async getStripePublishableKey(): Promise<string> {
278+
return await this.getConfigEntryThrows('STRIPE_PUBLISHABLE_KEY');
279+
}
280+
273281
public async getStripeSecretKey(): Promise<string> {
274282
return await this.getConfigEntryThrows('STRIPE_SECRET_KEY');
275283
}

api/src/gql/ResolverRegister.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ export default class ResolverRegister {
227227
use_github_sso: await this.config.gitHubSsoEnabled(),
228228
use_email: await this.config.emailServerEnabled(),
229229
is_audit_enabled: await this.config.isAuditEnabled(),
230+
stripe_publishable: this.config.isCommercial()
231+
? await this.config.getStripePublishableKey()
232+
: null,
233+
captcha_publishable: this.config.isCommercial()
234+
? await this.config.getCaptchaPublishable()
235+
: null,
230236
is_dev: this.config.isDevelopment(),
231237
};
232238
},

api/src/gql/TypeDefRegister.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,14 @@ export default class TypeDefRegister {
811811
"""
812812
is_audit_enabled: Boolean
813813
"""
814+
Stripe Publishable Key
815+
"""
816+
stripe_publishable: String
817+
"""
818+
Captcha Publishable Key
819+
"""
820+
captcha_publishable: String
821+
"""
814822
The backend started in developer mode
815823
"""
816824
is_dev: Boolean

ui/src/components/atoms/Captcha.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import HCaptcha from '@hcaptcha/react-hcaptcha';
22
import { FC, RefObject } from 'react';
3-
import { getCaptchaKey } from '../../utils/ConfigUtils';
3+
import { useConfigState } from '../../context/AppContext';
44

55
type CaptchaProps = {
66
captcha: RefObject<HCaptcha>;
77
setCaptchaToken: (token: string) => void;
88
};
99

1010
const Captcha: FC<CaptchaProps> = (props: CaptchaProps) => {
11+
const { captchaPublishable } = useConfigState();
12+
1113
return (
1214
<HCaptcha
1315
ref={props.captcha}
14-
sitekey={getCaptchaKey()}
16+
sitekey={captchaPublishable}
1517
onVerify={(token) => props.setCaptchaToken(token)}
1618
/>
1719
);

ui/src/components/atoms/StripeCheckout.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { FC } from 'react';
22
import { loadStripe } from '@stripe/stripe-js/pure';
33
import { Elements, useStripe } from '@stripe/react-stripe-js';
44
import { ApolloError } from '@apollo/client/errors';
5-
import { getStripeKey } from '../../utils/ConfigUtils';
6-
import { useLoggedInState } from '../../context/AppContext';
5+
import { useConfigState, useLoggedInState } from '../../context/AppContext';
76

87
const SubscriptionStripe: FC<{ session: string }> = (props: { session: string }) => {
98
const { templateInteractions } = useLoggedInState();
@@ -29,8 +28,9 @@ const SubscriptionStripe: FC<{ session: string }> = (props: { session: string })
2928

3029
const StripeCheckout: FC<{ session: string }> = (props: { session: string }) => {
3130
const { session } = props;
31+
const { stripePublishable } = useConfigState();
3232

33-
const stripePromise = loadStripe(getStripeKey());
33+
const stripePromise = loadStripe(stripePublishable);
3434
return (
3535
<Elements stripe={stripePromise}>
3636
<SubscriptionStripe session={session} />

ui/src/context/ConfigState.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export type ConfigState = {
99
useGithubSSO: boolean;
1010
useTwoFactorAuth: boolean;
1111
isAuditEnabled: boolean;
12+
stripePublishable: string;
13+
captchaPublishable: string;
1214
isConfigured: boolean;
1315
isDev: boolean;
1416
consentPurposes: { id: number; name: string }[];
@@ -26,6 +28,8 @@ export const configInitialState = {
2628
isAuditEnabled: false,
2729
isConfigured: false,
2830
isDev: false,
31+
stripePublishable: '',
32+
captchaPublishable: '',
2933
consentPurposes: [],
3034
consentVendors: [],
3135
tagManagerProducts: [],
@@ -39,6 +43,8 @@ export const configStateFromData = (data?: ConfigQueryData): ConfigState => ({
3943
useGithubSSO: data?.config.use_github_sso ?? false,
4044
useTwoFactorAuth: data?.config.use_two_factor_auth ?? false,
4145
isAuditEnabled: data?.config.is_audit_enabled ?? false,
46+
stripePublishable: data?.config.stripe_publishable ?? '',
47+
captchaPublishable: data?.config.captcha_publishable ?? '',
4248
isConfigured: data?.config.is_configured ?? false,
4349
isDev: data?.config.is_dev ?? false,
4450
consentPurposes: data?.config.consent_purposes ?? [],

ui/src/gql/generated/ConfigQueryData.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ export interface ConfigQueryData_config {
8383
* Whether the application will record an audit trail
8484
*/
8585
is_audit_enabled: boolean | null;
86+
/**
87+
* Stripe Publishable Key
88+
*/
89+
stripe_publishable: string | null;
90+
/**
91+
* Captcha Publishable Key
92+
*/
93+
captcha_publishable: string | null;
8694
/**
8795
* A list of consent purposes
8896
*/

ui/src/gql/generated/LoggedUser.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ export interface LoggedUser_config {
175175
* Whether the application will record an audit trail
176176
*/
177177
is_audit_enabled: boolean | null;
178+
/**
179+
* Stripe Publishable Key
180+
*/
181+
stripe_publishable: string | null;
182+
/**
183+
* Captcha Publishable Key
184+
*/
185+
captcha_publishable: string | null;
178186
/**
179187
* A list of consent purposes
180188
*/

ui/src/gql/queries/ConfigQuery.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const ConfigQuery = gql`
1111
use_github_sso
1212
use_two_factor_auth
1313
is_audit_enabled
14+
stripe_publishable
15+
captcha_publishable
1416
consent_purposes {
1517
id
1618
name

ui/src/gql/queries/LoggedUserQuery.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const LoggedUserQuery = gql`
3535
use_github_sso
3636
use_two_factor_auth
3737
is_audit_enabled
38+
stripe_publishable
39+
captcha_publishable
3840
consent_purposes {
3941
id
4042
name

0 commit comments

Comments
 (0)