2019-02-14 19:59:41 +03:00
|
|
|
import { Elements, StripeProvider, injectStripe } from 'react-stripe-elements';
|
|
|
|
import { Component } from 'react';
|
|
|
|
import FormHeader from '../components/FormHeader';
|
|
|
|
import FormSubmit from '../components/FormSubmit';
|
|
|
|
import FormHeaderCTA from '../components/FormHeaderCTA';
|
2019-02-26 06:09:16 +03:00
|
|
|
import { IconClose } from '../components/icons';
|
2019-02-14 19:59:41 +03:00
|
|
|
import NameInput from '../components/NameInput';
|
|
|
|
import EmailInput from '../components/EmailInput';
|
|
|
|
import PasswordInput from '../components/PasswordInput';
|
|
|
|
import CheckoutForm from '../components/CheckoutForm';
|
|
|
|
import Form from '../components/Form';
|
|
|
|
|
|
|
|
class PaymentForm extends Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit = ({ name, email, password, plan }) => {
|
|
|
|
// Within the context of `Elements`, this call to createToken knows which Element to
|
|
|
|
// tokenize, since there's only one in this group.
|
|
|
|
plan = this.props.selectedPlan ? this.props.selectedPlan.name : "";
|
|
|
|
this.props.stripe.createToken({ name: name }).then(({ token }) => {
|
|
|
|
this.props.handleSubmit({
|
|
|
|
adapter: 'stripe',
|
|
|
|
plan: plan,
|
|
|
|
stripeToken: token.id,
|
|
|
|
name, email, password
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-02-26 06:09:16 +03:00
|
|
|
render({frameLocation}) {
|
2019-02-14 19:59:41 +03:00
|
|
|
return (
|
2019-02-26 06:09:16 +03:00
|
|
|
<Form onSubmit={(data) => this.handleSubmit(data)}>
|
2019-02-14 19:59:41 +03:00
|
|
|
<CheckoutForm />
|
2019-02-26 06:09:16 +03:00
|
|
|
|
|
|
|
<FormSubmit label="Confirm Payment" />
|
2019-02-14 19:59:41 +03:00
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const PaymentFormWrapped = injectStripe(PaymentForm);
|
|
|
|
|
2019-02-26 06:09:16 +03:00
|
|
|
export default class StripeSubscriptionPage extends Component {
|
2019-02-14 19:59:41 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.plans = props.stripeConfig.config.plans || [];
|
|
|
|
this.state = {
|
|
|
|
selectedPlan: this.plans[0] ? this.plans[0] : ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPlan({ currency, amount, id, interval, name }) {
|
|
|
|
const selectedPlanId = this.state.selectedPlan ? this.state.selectedPlan.id : "";
|
2019-02-26 06:09:16 +03:00
|
|
|
const dollarAmount = (amount / 100);
|
2019-02-14 19:59:41 +03:00
|
|
|
return (
|
2019-02-26 06:09:16 +03:00
|
|
|
<label for={ id }>
|
|
|
|
<div className={ (selectedPlanId === id ? "gm-plan selected" : "gm-plan") }>
|
|
|
|
<input type="radio" id={ id } name="radio-group" value={ id } defaultChecked={ id === selectedPlanId } />
|
|
|
|
<span className="gm-amount">{ `$${dollarAmount}` }</span>
|
|
|
|
<span className="gm-interval"><span className="gm-currency">{ `${currency}` }</span> { `${interval}` }</span>
|
|
|
|
</div>
|
|
|
|
</label>
|
2019-02-14 19:59:41 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
changePlan(e) {
|
|
|
|
const plan = this.plans.find(plan => plan.id === e.target.value);
|
|
|
|
this.setState({
|
|
|
|
selectedPlan: plan
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPlans(plans) {
|
|
|
|
return (
|
2019-02-26 06:09:16 +03:00
|
|
|
<div className="mt3" onChange={(e) => this.changePlan(e)}>
|
2019-02-14 19:59:41 +03:00
|
|
|
{
|
|
|
|
plans.map((plan) => this.renderPlan(plan))
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPlansSection() {
|
|
|
|
return (
|
2019-02-15 20:16:30 +03:00
|
|
|
<div className="gm-plans-container">
|
2019-02-26 06:09:16 +03:00
|
|
|
<h2 className="gm-form-section">Billing period</h2>
|
2019-02-14 19:59:41 +03:00
|
|
|
{this.renderPlans(this.plans)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-02-15 11:35:03 +03:00
|
|
|
render({ error, handleSubmit, stripeConfig }) {
|
2019-02-14 19:59:41 +03:00
|
|
|
const publicKey = stripeConfig.config.publicKey || '';
|
|
|
|
return (
|
2019-02-26 06:09:16 +03:00
|
|
|
<div class="gm-upgrade-page">
|
2019-02-15 20:16:30 +03:00
|
|
|
<div className="gm-modal-form gm-subscribe-form">
|
2019-02-26 06:09:16 +03:00
|
|
|
<FormHeader title="Upgrade" error={error} errorText="Unable to confirm payment" />
|
|
|
|
<div className="flex flex-column justfiy-stretch mt7">
|
|
|
|
{ this.renderPlansSection() }
|
|
|
|
<div className="mt4 nb3">
|
|
|
|
<h2 className="gm-form-section">Card details</h2>
|
|
|
|
</div>
|
|
|
|
<StripeProvider apiKey={publicKey}>
|
|
|
|
<Elements>
|
|
|
|
<PaymentFormWrapped handleSubmit={handleSubmit} publicKey={publicKey} selectedPlan={this.state.selectedPlan} />
|
|
|
|
</Elements>
|
|
|
|
</StripeProvider>
|
|
|
|
</div>
|
2019-02-14 19:59:41 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|