0

So I've got my rails app selling subscriptions to access content. I'm temporarily inserting a hidden field in the check-out form to pass tax_percent to Stripe's API, but it keeps showing up in the Stripe dashboard as null. The tax is only for NJ and is a straight 7% if the buyer is also in NJ. I'm calculating the sales tax via a javascript call (Calculating Sales Tax for a US State in Javascript), which then pushes the value to the subscription#create action. Except it ain't working.

Here's what the line looks like in the form:

<input type="hidden" id="tax_percent" name="tax_percent">
<%= f.hidden_field :plan_id %>

Yes, I need to build a Ruby method to calculate this on the server side, but for now, this is fine.

Here's what shows up in the logs:

Processing by Koudoku::SubscriptionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"TokenValueHere", "tax_percent"=>"7", "subscription"=>{"plan_id"=>"5", "credit_card_token"=>"OtherTokenValue", "last_four"=>"undefined", "card_type"=>"undefined"}, "owner_id"=>"72"}

So you see that tax_percent appears to be outside the subscription object, yet plan_id is not, but they're right next to each other in the checkout form.

How do I get this tax_percent to hit the Stripe API properly??

Community
  • 1
  • 1
JohnOHFS
  • 174
  • 14

1 Answers1

1

Why is your f.hidden_field inside the subscription object while your manual <input> is not, you ask? It’s because your manual input doesn’t use the naming convention for form fields that hidden_field automatically applies.

As the docs for #hidden_field show, your hidden field plan_id actually has a name attribute of subscription[plan_id], not plan_id. Another hidden field has to use the same subscription[…] convention for its value to be automatically put inside the hash at params["subscription"].

You can automatically make your tax_percent hidden field adhere to this naming convention by using the standalone form helper hidden_field:

<%= hidden_field :subscription, :tax_percent %>
<%= f.hidden_field :plan_id %>
Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
  • Can I use my javascript calculator output, which is document.getElementById('tax_percent').value = tax_percent; to inject that tax percent value into the ruby hidden_field: <%= hidden_field :subscription, :tax_percent %> ?? – JohnOHFS Jul 27 '16 at 12:15
  • I get an error when I make this change: undefined method `tax_percent' for Subcription – JohnOHFS Jul 27 '16 at 12:18
  • @JohnOHFS Ah, I didn’t notice that the default `value` for that hidden field would be `@subscription.tax_percent`. If you don’t want to define a `tax_percent` method for your `Subscription` model, you can give the field a different value such as `0.07` by writing `<%= hidden_field :subscription, :tax_percent, value: '0.07' %>`. – Rory O'Kane Jul 29 '16 at 02:22
  • @JohnOHFS And yes, setting the hidden field’s `value` with JavaScript should work like any other ``. The browser will submit the hidden field’s last-set value along with other form values when it submits the form to the URL at the `
    `’s `action` attribute.
    – Rory O'Kane Jul 29 '16 at 02:27
  • Thanks, @Rory-Okane – JohnOHFS Jul 29 '16 at 14:28