import React from 'react'; import axios from 'axios'; import Modal from 'react-responsive-modal'; import braintree from 'braintree-web-drop-in'; import BraintreeDropin from 'braintree-dropin-react'; import Help from '../common/Help.jsx'; export default class Upgrade extends React.Component { constructor(props) { super(props); this.state = { fetching: false, plans: null, error: '', modalOpen: false, customerToken: '', plan: null, processingPayment: false, paymentError: null, }; } async componentDidMount() { this.setState({ fetching: true }); try { let request = await axios.get('/upgrade/plans'); this.setState({ plans: request.data.plans }); } catch (e) { this.setState({ error: 'Failed to load plans, try again later.' }); } this.setState({ fetching: false }); } async subscribe(plan) { let tokenRequest = await axios.post('/upgrade/token'); this.setState({ token: tokenRequest.data.token, plan, modalOpen: true }); } closeModal = () => { if (this.state.processingPayment) return; this.setState({ token: '', plan: null, modalOpen: false }); } handlePaymentMethod = async (payload) => { this.setState({ processingPayment: true }); const { plan } = this.state; const planId = plan.id; console.log('payload', payload); this.setState({ paymentError: null }); try { let result = await axios.post('/upgrade/process', { paymentMethodNonce: payload.nonce, planId: planId, }); if (result.data && result.data.success) { this.setState({ processingPayment: false }); this.closeModal(); window.location.href = '/account'; } } catch (err) { this.setState({ paymentError: { ...err.response.data, retry: false } }); setTimeout(_ => { this.setState({ paymentError: { ...this.state.paymentError, retry: true }, processingPayment: false }) }, 5000); } } renderSubmitButton = ({ onClick, isDisabled, text }) => { if (this.state.processingPayment) { text = 'Processing payment...'; } if (this.state.paymentError !== null && this.state.paymentError.retry === false) { text = 'Error processing payment!' } return ( ); } render() { if (this.state.fetching || this.state.plans === null) { return (
Please wait...
{this.state.error}
{this.state.plans.premiumPlans.map(plan => ( | {plan.name} | ))}
---|---|
Number of Servers | {this.state.plans.premiumPlans.map(plan => ({plan.qty} | ))}
Price | {this.state.plans.premiumPlans.map(plan => (
${(plan.price / plan.qty).toFixed(2)} / server
${plan.price} / month
|
))}
{this.state.plans.premiumPlans.map(plan => ( | this.subscribe(plan)} className={'button is-info'}>Subscribe now | ))}
Payment failed! — {this.state.paymentError.error}
Code {this.state.paymentError.processorResponse.code} - {this.state.paymentError.processorResponse.text}
{this.state.paymentError.processorResponse.additional !== '' ? ({this.state.paymentError.processorResponse.additional}
) : ''}Transaction ID: {this.state.paymentError.transaction}
You have not been charged.
Payment failed!
{this.state.paymentError.error}
You have not been charged.
Purchasing {this.state.plan.name} at a monthly recurring cost of USD$ {this.state.plan.price}
) : ''}