From 103a94fe45c6e9ec677dd2aa14ab2009f5f0b44b Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Wed, 10 Jul 2024 12:27:14 +0100 Subject: [PATCH] feat: Open payment modal if a plan was preselected, add annual plans (#4110) --- frontend/web/components/ProjectsPage.tsx | 13 +++- frontend/web/components/modals/Payment.js | 88 +++++++++++++++++------ frontend/web/components/pages/HomePage.js | 4 ++ 3 files changed, 83 insertions(+), 22 deletions(-) diff --git a/frontend/web/components/ProjectsPage.tsx b/frontend/web/components/ProjectsPage.tsx index 8bbaeb703a4e..dbeefb34a4a1 100644 --- a/frontend/web/components/ProjectsPage.tsx +++ b/frontend/web/components/ProjectsPage.tsx @@ -2,6 +2,9 @@ import React, { FC } from 'react' import ProjectManageWidget from './ProjectManageWidget' import OrganisationProvider from 'common/providers/OrganisationProvider' import ConfigProvider from 'common/providers/ConfigProvider' +import Project from 'common/project' +import { onPaymentLoad } from './modals/Payment' +import makeAsyncScriptLoader from 'react-async-script' type ProjectsPageType = { match: { @@ -24,4 +27,12 @@ const ProjectsPage: FC = ({ match }) => { ) } -export default ConfigProvider(ProjectsPage) +const InnerComponent = ConfigProvider(ProjectsPage) +const WrappedPayment = Project.chargebee?.site + ? makeAsyncScriptLoader('https://js.chargebee.com/v2/chargebee.js', { + removeOnUnmount: true, + })(InnerComponent) + : InnerComponent +export default (props) => ( + +) diff --git a/frontend/web/components/modals/Payment.js b/frontend/web/components/modals/Payment.js index b2f5a95f799d..6a9be59bef6c 100644 --- a/frontend/web/components/modals/Payment.js +++ b/frontend/web/components/modals/Payment.js @@ -6,6 +6,10 @@ import Constants from 'common/constants' import InfoMessage from 'components/InfoMessage' import Icon from 'components/Icon' import firstpromoter from 'project/firstPromoter' +import Utils from 'common/utils/utils' +import AccountProvider from 'common/providers/AccountProvider' +import classNames from 'classnames' +import Switch from 'components/Switch' const PaymentButton = (props) => { const activeSubscription = AccountStore.getOrganisationPlan( @@ -58,7 +62,9 @@ const Payment = class extends Component { constructor(props, context) { super(props, context) - this.state = {} + this.state = { + yearly: true, + } } componentDidMount = () => { @@ -98,7 +104,7 @@ const Payment = class extends Component { '' return (
- +
Manage Payment Plan
{this.props.isDisableAccountText && ( @@ -115,6 +121,28 @@ const Payment = class extends Component { )}
+
+
+ Pay Yearly (Save 10%) +
+ { + this.setState({ yearly: !this.state.yearly }) + }} + /> +
+ Pay Monthly +
+
@@ -744,29 +772,47 @@ const Payment = class extends Component { } } +Payment.propTypes = {} +export const onPaymentLoad = () => { + if (!Project.chargebee?.site) { + return + } + const planId = API.getCookie('plan') + let link + if (planId && Utils.getFlagsmithHasFeature('payments_enabled')) { + ;(function () { + // Create a link element with data-cb-plan-id attribute + link = document.createElement('a') + link.setAttribute('data-cb-type', 'checkout') + link.setAttribute('data-cb-plan-id', planId) + link.setAttribute('href', 'javascript:void(0)') + // Append the link to the body + document.body.appendChild(link) + })() + } + Chargebee.init({ + site: Project.chargebee.site, + }) + Chargebee.registerAgain() + firstpromoter() + Chargebee.getInstance().setCheckoutCallbacks(() => ({ + success: (hostedPageId) => { + AppActions.updateSubscription(hostedPageId) + }, + })) + if (link) { + link.click() + document.body.removeChild(link) + API.setCookie('plan', null) + } +} + const WrappedPayment = makeAsyncScriptLoader( 'https://js.chargebee.com/v2/chargebee.js', { removeOnUnmount: true, }, )(ConfigProvider(Payment)) - -Payment.propTypes = {} - -module.exports = (props) => ( - { - Chargebee.init({ - site: Project.chargebee.site, - }) - Chargebee.registerAgain() - firstpromoter() - Chargebee.getInstance().setCheckoutCallbacks(() => ({ - success: (hostedPageId) => { - AppActions.updateSubscription(hostedPageId) - }, - })) - }} - /> +export default (props) => ( + ) diff --git a/frontend/web/components/pages/HomePage.js b/frontend/web/components/pages/HomePage.js index bb32be331c91..e58a5bc88572 100644 --- a/frontend/web/components/pages/HomePage.js +++ b/frontend/web/components/pages/HomePage.js @@ -65,6 +65,10 @@ const HomePage = class extends React.Component { } componentDidMount() { + const plan = Utils.fromParam().plan + if (plan) { + API.setCookie('plan', plan) + } if ( Project.albacross && this.props.location.pathname.indexOf('signup') !== -1