Skip to content

Commit

Permalink
feat: Open payment modal if a plan was preselected, add annual plans (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-ssg authored Jul 10, 2024
1 parent 7f9cd62 commit 103a94f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 22 deletions.
13 changes: 12 additions & 1 deletion frontend/web/components/ProjectsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -24,4 +27,12 @@ const ProjectsPage: FC<ProjectsPageType> = ({ 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) => (
<WrappedPayment {...props} asyncScriptOnLoad={onPaymentLoad} />
)
88 changes: 67 additions & 21 deletions frontend/web/components/modals/Payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -58,7 +62,9 @@ const Payment = class extends Component {

constructor(props, context) {
super(props, context)
this.state = {}
this.state = {
yearly: true,
}
}

componentDidMount = () => {
Expand Down Expand Up @@ -98,7 +104,7 @@ const Payment = class extends Component {
''
return (
<div className='col-md-12'>
<Row space className='mb-4'>
<Row space className='mb-2'>
<h5>Manage Payment Plan</h5>
{this.props.isDisableAccountText && (
<Row>
Expand All @@ -115,6 +121,28 @@ const Payment = class extends Component {
</Row>
)}
</Row>
<div className='d-flex mb-4 font-weight-medium justify-content-center align-items-center gap-2'>
<h5
className={classNames('mb-0', {
'text-muted': !this.state.yearly,
})}
>
Pay Yearly (Save 10%)
</h5>
<Switch
checked={!this.state.yearly}
onChange={() => {
this.setState({ yearly: !this.state.yearly })
}}
/>
<h5
className={classNames('mb-0', {
'text-muted': this.state.yearly,
})}
>
Pay Monthly
</h5>
</div>
<Row className='pricing-container align-start'>
<Flex className='pricing-panel p-2'>
<div className='panel panel-default'>
Expand Down Expand Up @@ -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) => (
<WrappedPayment
{...props}
asyncScriptOnLoad={() => {
Chargebee.init({
site: Project.chargebee.site,
})
Chargebee.registerAgain()
firstpromoter()
Chargebee.getInstance().setCheckoutCallbacks(() => ({
success: (hostedPageId) => {
AppActions.updateSubscription(hostedPageId)
},
}))
}}
/>
export default (props) => (
<WrappedPayment {...props} asyncScriptOnLoad={onPaymentLoad} />
)
4 changes: 4 additions & 0 deletions frontend/web/components/pages/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 103a94f

Please sign in to comment.