Stripe and tax_percent in my Rails app

空扰寡人 提交于 2019-12-12 05:39:11

问题


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??


回答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 %>


来源:https://stackoverflow.com/questions/38603665/stripe-and-tax-percent-in-my-rails-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!