ducky/web

Paddy 2015-06-30 Parent:62e0c0df28bb Child:21f80f56cda9

9:e9e0a28a7419 Go to Latest

ducky/web/src/pages/payment.jsx

Update to use plans instead of PWYW. If we're going to lean on Stripe for most of our subscription processing, we need to use plans, instead of pay what you want. This updates the page to replace our amount input with a plan select box. It also removes the nonsense about finding your first charge date, because Stripe forced us into a simpler, but harder to predict, billing model. We also updated our CSS to work with select boxes, as well as text inputs.

History
1 import app from 'ampersand-app'
2 import React from 'react'
3 import ScriptLoaderMixin from 'react-script-loader'
4 import LaddaButton from 'react-ladda'
5 import LaddaCSS from '../../node_modules/ladda/dist/ladda.min.css'
6 import HeroUnit from '../components/hero'
7 import ValidationError from '../components/validation-error'
8 import onboardStyles from '../styles/onboarding.scss'
9 import config from '../config'
11 export default React.createClass({
12 displayName: 'PaymentMethodPage',
13 mixins: [ScriptLoaderMixin.ReactScriptLoaderMixin, React.addons.LinkedStateMixin],
14 getScriptURL () {
15 return 'https://js.stripe.com/v2/'
16 },
18 getInitialState () {
19 return {
20 stripeLoading: true,
21 stripeFailedToLoad: false,
22 active: false,
23 errors: [],
24 number: null,
25 name: null,
26 cvc: null,
27 plan: this.getDefaultPlan(),
28 expireMonth: null,
29 expireYear: null,
30 }
31 },
33 getDefaultPlan () {
34 if (app.me && app.me.subscription && app.me.subscription.plan) {
35 return app.me.subscription.plan
36 }
37 return 'basic_monthly'
38 },
40 nameValidationOutputs: {
41 },
42 numberValidationOutputs: {
43 'invalid_value': 'Are you sure this is right? That number didn’t work.',
44 'invalid_format': 'That’s not a valid credit card number.',
45 },
46 cvcValidationOutputs: {
47 'invalid_format': 'That’s not a valid security code.',
48 'invalid_value': 'That’s not the correct security code.',
49 },
50 expirationValidationOutputs: {
51 'invalid_format': 'That’s not a valid expiration date.',
52 'invalid_value': 'That card appears to be expired.',
53 },
54 balanceValidationOutputs: {
55 'insufficient': 'Your card was declined.',
56 },
57 planValidationOutputs: {
58 'invalid_value': 'That’s not a valid plan!',
59 },
61 catchAllValidationOutputs: {
62 },
64 onScriptLoaded () {
65 Stripe.setPublishableKey(config.stripeKey)
66 this.setState({stripeLoading: false})
67 },
69 onScriptError () {
70 this.setState({stripeFailedToLoad: true, errors: [{'error': 'act_of_god'}]})
71 },
73 addCard (e) {
74 e.preventDefault()
75 if (this.state.stripeLoading || this.state.stripeFailedToLoad) {
76 return
77 }
78 this.setState({active: true})
79 const t = this
80 const errors = []
81 Stripe.card.createToken({
82 number: this.state.number,
83 cvc: this.state.cvc,
84 exp_month: this.state.expireMonth,
85 exp_year: this.state.expireYear,
86 name: this.state.name,
87 }, function(status, response) {
88 if (response.error) {
89 if (response.error.type == 'card_error') {
90 switch (response.error.code) {
91 case 'incorrect_number':
92 errors.push({'error': 'invalid_value', 'field': '/number'})
93 break
94 case 'invalid_number':
95 errors.push({'error': 'invalid_format', 'field': '/number'})
96 break
97 case 'invalid_expiry_month':
98 errors.push({'error': 'invalid_format', 'field': '/expiration'})
99 break
100 case 'invalid_expiry_year':
101 errors.push({'error': 'invalid_format', 'field': '/expiration'})
102 break
103 case 'invalid_cvc':
104 errors.push({'error': 'invalid_format', 'field': '/cvc'})
105 break
106 case 'expired_card':
107 errors.push({'error': 'invalid_value', 'field': '/expiration'})
108 break
109 case 'incorrect_cvc':
110 errors.push({'error': 'invalid_value', 'field': '/cvc'})
111 break
112 case 'incorrect_zip':
113 errors.push({'error': 'invalid_value', 'field': '/zip'})
114 break
115 case 'card_declined':
116 errors.push({'error': 'insufficient', 'field': '/balance'})
117 break
118 case 'missing':
119 errors.push({'error': 'missing', 'field': '/customer/card'})
120 break
121 case 'processing_error':
122 errors.push({'error': 'act_of_god', 'field': '/'})
123 break
124 case 'rate_limit':
125 errors.push({'error': 'access_denied', 'field': '/rate'})
126 break
127 default:
128 errors.push({'error': 'act_of_god', 'field': '/'})
129 break
130 }
131 } else {
132 console.log('Error:', response.error.message)
133 }
134 } else {
135 console.log('Sending '+response.id+' to server to create customer')
136 }
137 t.setState({active: false, errors: errors})
138 })
139 },
141 render () {
142 return (
143 <div className='container'>
144 <HeroUnit title='Set Up Your Subscription'>Gotta pay those bills.</HeroUnit>
145 <article className='onboarding payment'>
146 <p>Ducky costs money to run, and to keep it improving. We pass these costs on to you. There’s no parent company, no ads, nothing but people making software they want you to enjoy. Also, you get a
147 free 31 day trial. Cancel before it ends, and we won’t charge you at all.</p>
148 <form onSubmit={this.addCard}>
149 <div>
150 <label htmlFor='name'>Cardholder Name</label>
151 <input id='name' type='text' placeholder='This is the name on your card' valueLink={this.linkState('name')} />
152 <ValidationError errors={this.state.errors} field='/name' outputs={this.nameValidationOutputs} />
154 <label htmlFor='cardNumber'>Card Number</label>
155 <input id='cardNumber' type='text' placeholder='4242 4242 4242 4242' valueLink={this.linkState('number')} />
156 <ValidationError errors={this.state.errors} field='/number' outputs={this.numberValidationOutputs} />
158 <label htmlFor='cvc'>Security Code / CVC</label>
159 <input id='cvc' className='cvc' type='password' placeholder='123' valueLink={this.linkState('cvc')} />
161 <label htmlFor='expireMonth' className='expiration'>Expires</label>
162 <input id='expireMonth' className='expiration month' type='text' placeholder='01' valueLink={this.linkState('expireMonth')} />
163 <input id='expireYear' className='expiration year' type='text' placeholder='15' valueLink={this.linkState('expireYear')} />
164 <ValidationError errors={this.state.errors} field='/cvc' outputs={this.cvcValidationOutputs} />
165 <ValidationError errors={this.state.errors} field='/expiration' outputs={this.expirationValidationOutputs} />
167 <label htmlFor='plan'>Plan</label>
168 <select id='plan' className='plan' defaultValue={this.getDefaultPlan()}>
169 <option value='basic_monthly'>Basic Monthly - $2/month</option>
170 <option value='basic_yearly'>Basic Yearly - $20/year</option>
171 <option value='supporter_monthly'>Supporter Monthly - $5/month</option>
172 <option value='supporter_yearly'>Supporter Yearly - $50/year</option>
173 </select>
174 <ValidationError errors={this.state.errors} field='/plan' outputs={this.planValidationOutputs} />
175 <p>There’s no difference between the Supporter and Basic tiers. The Supporter tier is just for people who love Ducky and want to see it grow and improve.</p>
177 <ValidationError errors={this.state.errors} field='/balance' outputs={this.balanceValidationOutputs} />
178 <ValidationError errors={this.state.errors} notFields={['/name', '/number', '/cvc', '/expiration', '/plan', '/balance']} notParams={[]} notHeaders={[]} outputs={this.catchAllValidationOutputs} />
180 <div className='actionbuttons'>
181 <LaddaButton style='expand-right' active={this.state.active}>
182 <button type='submit' className='primary' disabled={this.state.stripeLoading || this.state.stripeFailedToLoad || this.state.active || this.state.errors.length}>Add Card</button>
183 </LaddaButton>
184 </div>
185 </div>
186 </form>
187 </article>
188 </div>
189 )
190 }
191 })