import { Elements, StripeProvider, injectStripe } from 'react-stripe-elements'; import { Component } from 'react'; import FormHeader from '../components/FormHeader'; import FormSubmit from '../components/FormSubmit'; import CouponInput from '../components/CouponInput'; import CheckoutForm from '../components/CheckoutForm'; import Form from '../components/Form'; import { IconError } from '../components/icons'; const getCouponData = frameLocation => { const params = new URLSearchParams(frameLocation.query); const coupon = params.get('coupon') || ''; return { coupon }; }; class PaymentForm extends Component { constructor(props) { super(props); } handleSubmit = ({ name, email, password, plan, coupon }) => { // 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, coupon }); }); }; render({frameLocation}) { let label = this.props.showSpinner ? "Confirming payment..." : "Confirm payment"; const { coupon } = getCouponData(frameLocation); return (
this.handleSubmit(data)}> { coupon ? : '' } ); } } const PaymentFormWrapped = injectStripe(PaymentForm); export default class StripeSubscriptionPage extends Component { 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 : ""; const dollarAmount = (amount / 100); return ( ) } changePlan(e) { const plan = this.plans.find(plan => plan.id === e.target.value); this.setState({ selectedPlan: plan }) } renderPlans(plans) { return (
this.changePlan(e)}> { plans.map((plan) => this.renderPlan(plan)) }
); } renderPlansSection() { return (

Billing period

{this.renderPlans(this.plans)}
) } render({ error, handleSubmit, stripeConfig, frameLocation, showSpinner }) { const publicKey = stripeConfig.config.publicKey || ''; return (
{ IconError } We were unable to process your payment, please try again or use different card details.
{ this.renderPlansSection() }

Card details

) } };