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'; import NameInput from '../components/NameInput'; import EmailInput from '../components/EmailInput'; import CouponInput from '../components/CouponInput'; import PasswordInput from '../components/PasswordInput'; import CheckoutForm from '../components/CheckoutForm'; import Form from '../components/Form'; 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 ? "Signing up..." : "Confirm payment"; const { coupon } = getCouponData(frameLocation); return (
this.handleSubmit(data)}> { coupon ? : '' } ); } } const PaymentFormWrapped = injectStripe(PaymentForm); export default class StripePaymentPage 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, title, iconStyle) { return (
this.changePlan(e)}>

{title}

Subscription
{ plans.map((plan) => this.renderPlan(plan)) }
); } renderPlansSection(title, iconStyle) { return (
{this.renderPlans(this.plans, title, iconStyle)}
) } render({ error, handleSubmit, stripeConfig, siteConfig, showSpinner, frameLocation }) { const publicKey = stripeConfig.config.publicKey || ''; let iconUrl = siteConfig && siteConfig.icon; let title = (siteConfig && siteConfig.title) || "Ghost Publication"; let iconStyle = iconUrl ? { backgroundImage: `url(${iconUrl})`, backgroundSize: `44px` } : {}; return ( ) } };