jam-cloud/web/app/assets/javascripts/react-components/Subscription.js.jsx.coffee

162 lines
4.9 KiB
CoffeeScript
Raw Permalink Normal View History

2020-09-15 00:51:01 +00:00
context = window
rest = context.JK.Rest()
logger = context.JK.logger
2020-10-09 22:22:20 +00:00
LocationActions = context.LocationActions
2020-09-15 00:51:01 +00:00
UserStore = context.UserStore
@Subscription = React.createClass({
2020-10-09 22:22:20 +00:00
mixins: [Reflux.listenTo(@LocationStore, "onLocationsChanged")]
2020-09-15 00:51:01 +00:00
getInitialState: () ->
{
2020-10-09 22:22:20 +00:00
clicked: false,
2020-11-21 22:14:37 +00:00
selectedCountry: null,
selectedPlan: null,
subscription: null
2020-09-15 00:51:01 +00:00
}
2020-10-09 22:22:20 +00:00
onLocationsChanged: (countries) ->
2020-11-21 22:14:37 +00:00
console.log("countries in ", countries)
2020-10-09 22:22:20 +00:00
@setState({countries: countries})
onCountryChanged: (e) ->
val = $(e.target).val()
@setState({selectedCountry: val})
currentCountry: () ->
this.state.selectedCountry || this.props.selectedCountry || ''
2020-11-21 22:14:37 +00:00
onPlanChanged: (e) ->
val = $(e.target).val()
@setState({selectedPlan: val})
currentPlan: () ->
this.state.selectedPlan || this.props.selectedPlan || ''
2020-09-15 00:51:01 +00:00
openBrowser: () ->
context.JK.popExternalLink("https://www.jamkazam.com/client#/subscription")
2020-11-21 22:14:37 +00:00
onRecurlyToken: (err, token_data) ->
2020-09-15 00:51:01 +00:00
if err
console.log("error", err)
# handle error using err.code and err.fields
else
# recurly.js has filled in the 'token' field, so now we can submit the
# form to your server
2020-11-21 22:14:37 +00:00
console.log("eintercepted", token_data)
rest.createSubscription({plan_code: @state.selectedPlan, recurly_token: token_data.id})
2020-09-15 00:51:01 +00:00
onFormSubmit: (event) ->
form = event.target
console.log("ok work this", form)
event.preventDefault()
recurly.token(@elements, form, @onRecurlyToken)
configureRecurly: () =>
unless window.configuredRecurly
2020-11-30 00:24:28 +00:00
window.recurly.configure(gon.global.recurly_public_api_key)
2020-09-15 00:51:01 +00:00
window.configuredRecurly = true
2020-11-21 22:14:37 +00:00
2020-09-15 00:51:01 +00:00
componentDidMount: () ->
2020-10-09 22:22:20 +00:00
LocationActions.load()
2020-09-15 00:51:01 +00:00
@configureRecurly()
@elements = recurly.Elements()
cardElement = @elements.CardElement(
inputType: 'mobileSelect',
style: {
fontSize: '1em',
lineHeight: '42px',
placeholder: {
color: 'gray !important',
fontWeight: 'bold',
content: {
number: 'Card number',
cvv: 'CVC'
}
},
backgroundColor: 'white'
invalid: {
fontColor: 'red'
}
}
)
2020-11-30 00:24:28 +00:00
console.log("attaching", $('#subscription-elements'))
2020-09-15 00:51:01 +00:00
cardElement.attach('#subscription-elements')
@root = $(@getDOMNode())
document.querySelector('#subscription-form').addEventListener('submit', @onFormSubmit.bind(this))
2020-10-09 22:22:20 +00:00
defaultText: () ->
'Select Country'
2020-09-15 00:51:01 +00:00
render: () ->
2020-10-09 22:22:20 +00:00
if @state.countries?
countries = [`<option key="" value="">{this.defaultText()}</option>`]
for countryId, countryInfo of @state.countries
countries.push(`<option key={countryId} value={countryId}>{countryInfo.name}</option>`)
country = @state.countries[this.currentCountry()]
else
countries = []
countryJsx = `
<select name="countries" onChange={this.onCountryChanged} value={this.currentCountry()} data-recurly="country" autocomplete="shipping country">{countries}</select>`
2020-11-21 22:14:37 +00:00
plan_codes = [`<option key='' value='' >Select Plan</option>`]
for plan in gon.global.subscription_codes
plan_codes.push(`<option key={plan.id} value={plan.id}>{plan.name} ({plan.price}/month)</option>`)
plansJsx = `
<select name="plan_code" onChange={this.onPlanChanged} value={this.currentPlan()} >{plan_codes}</select>`
`<div>
<h3>Update Payment</h3>
<form id="subscription-form">
2020-09-15 00:51:01 +00:00
<div id="subscription-account-data">
<label for="first_name">First Name:</label>
2020-10-09 22:22:20 +00:00
<input type="text" data-recurly="first_name" required autocomplete="cc-give-name"></input>
2020-09-15 00:51:01 +00:00
<label for="last_name">Last Name:</label>
2020-10-09 22:22:20 +00:00
<input type="text" data-recurly="last_name" required autocomplete="cc-family-name"></input>
2020-09-15 00:51:01 +00:00
<label for="address1">Address 1:</label>
2020-10-09 22:22:20 +00:00
<input type="text" data-recurly="address1" required autocomplete="shipping address-line1"></input>
2020-09-15 00:51:01 +00:00
<label for="address2">Address 2:</label>
2020-11-30 00:24:28 +00:00
<input type="text" data-recurly="address2" required autocomplete="shipping address-line2"></input>
2020-09-15 00:51:01 +00:00
<label for="city">City:</label>
2020-10-09 22:22:20 +00:00
<input type="text" data-recurly="city" required autocomplete="shipping address-level2"></input>
2020-09-15 00:51:01 +00:00
<label for="state">State:</label>
2020-10-09 22:22:20 +00:00
<input type="text" data-recurly="state" required autocomplete="shipping address-level1"></input>
2020-09-15 00:51:01 +00:00
<label for="postal_code">Postal Code:</label>
2020-10-09 22:22:20 +00:00
<input type="text" data-recurly="postal_code" autocomplete="shipping postal-code"></input>
2020-09-15 00:51:01 +00:00
<label for="country">Country:</label>
2020-10-09 22:22:20 +00:00
{countryJsx}
2020-11-30 00:24:28 +00:00
2020-11-21 22:14:37 +00:00
<label for="plan_code">Plan:</label>
{plansJsx}
2020-09-15 00:51:01 +00:00
</div>
2020-11-30 00:24:28 +00:00
<div id="subscription-elements-legacy">
2020-09-15 00:51:01 +00:00
</div>
<input type="hidden" name="recurly-token" data-recurly="token"></input>
2020-11-21 22:14:37 +00:00
<button className="button-orange">SUBMIT</button>
</form>
</div>`
2020-09-15 00:51:01 +00:00
})