Note that there are some explanatory texts on larger screens.

plurals
  1. PODifferent options in subscription selection form
    primarykey
    data
    text
    <p>Working on a Rails project that allows users to sign up and then select a subscription, which is handled via Stripe.</p> <p>So how it works is they create an account (using Devise), which then routes them to a page where they can select either a free plan or premium plan in the same form.</p> <p>What I'm trying to accomplish is this: If they select the free plan, no credit card info is required when they hit submit (and I don't have a 'free' plan inside Stripe, so I don't need to worry about that interaction).</p> <p>If they select the premium plan, I want the app to know that they have to enter in the credit card info, or it won't go through.</p> <p>I followed Ryan Bates' Railscast on the subject, and modified his code to suit my situation. I believe the answer is in changing up the CoffeeScript form, but my skills here are a bit rusty. I've tried several different solutions, but can't get it to work. Curious if someone can let me know how to organize the logic in such a way to accomplish this.</p> <p>The current result in the file below allows me to create a free subscription only if I select that first and hit submit. If I select the premium button and then switch back, it doesn't work.</p> <p>my coffeescript form in subscriptions.js.coffee:</p> <pre><code>jQuery -&gt; $('#plan_id_2').change -&gt; if $(this).is(':checked') Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content')) subscription.setupForm() else true subscription = setupForm: -&gt; $('#new_subscription').submit -&gt; $('input[type=submit]').prop('disabled', true) subscription.processCard() false processCard: -&gt; card = number: $('#card_number').val() cvc: $('#card_code').val() expMonth: $('#card_month').val() expYear: $('#card_year').val() Stripe.createToken(card, subscription.handleStripeResponse) handleStripeResponse: (status, response) -&gt; if status == 200 $('#subscription_stripe_card_token').val(response.id) $('#new_subscription')[0].submit() else $('#stripe_error').text(response.error.message) $('input[type=submit]').prop('disabled', false) </code></pre> <p>my subscription form</p> <pre><code>&lt;h1&gt;Select a plan&lt;/h1&gt; &lt;div class="row"&gt; &lt;%= form_for @subscription, url: user_subscriptions_path, method: :post, html: { class: 'form-horizontal' } do %&gt; &lt;div class="col-sm-4"&gt; &lt;div class="form-group"&gt; &lt;%= label_tag "Free" %&gt; &lt;%= radio_button_tag :plan_id, @free_plan.id, false %&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="col-sm-4"&gt; &lt;div class="form-group"&gt; &lt;%= label_tag "Premium" %&gt; &lt;%= radio_button_tag :plan_id, @premium_plan.id, false %&gt; &lt;/div&gt; &lt;div id="premium-form"&gt; &lt;%= hidden_field_tag :stripe_card_token %&gt; &lt;div class="form-group"&gt; &lt;%= label_tag :card_number, "Credit Card Number" %&gt; &lt;%= text_field_tag :card_number, nil, name: nil %&gt; &lt;/div&gt; &lt;div class="form-group"&gt; &lt;%= label_tag :card_code, "Security Code on Card (CVV)" %&gt; &lt;%= text_field_tag :card_code, nil, name: nil %&gt; &lt;/div&gt; &lt;div class="form-group"&gt; &lt;%= label_tag :card_month, "Card Expiration" %&gt; &lt;%= select_month nil, {add_month_numbers_true: true}, {name: nil, id: "card_month"}%&gt; &lt;%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"}%&gt; &lt;/div&gt; &lt;div id="stripe_error"&gt; &lt;noscript&gt;JavaScript is not enabled and is required for this form. First enable it in your web browser settings.&lt;/noscript&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;%= submit_tag 'Choose Plan' %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload