我觉得很茫然,我正在尝试在 rails 应用程序中使用 stripe 结账创建一个简单的店面。我一直在遵循条纹教程,并让它工作,但他们在控制器中硬编码价格。
def create
#amounts are in American cents
@amount= 500
#making a customer
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
#a customer has an email and a token, provided by stripe
#making a charge
charge= Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe Customer',
:currency => 'usd'
)
# a charge has a customer, found by customer id, an amount, a description, and a currency
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
和视图,该视图将对多个产品重复,每个产品的成本不同
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>ITEM 1</span>
<span>Amount: $5.00</span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500"
data-billingAddress="true"
data-shippingAddress="true"
data-locale="auto"></script>
<% end %>
</div>
当然,一切都是5.00。我觉得这是一个简单的问题,但我就是无法解决它。有什么建议吗?
这很简单。条纹只是向您展示一个例子。您需要添加所需的价格 - 例如,从控制器传递全局值。
...
<span>Amount: <%= @price %></span>
...
data-amount="<%= @price %>"
问题是:价格从何而来?
在常规网上商店中,您将拥有一个带有数据列的Product
模型:price
。
然后,您将在收费控制器的显示操作中调用它(基于普通的REST-ROUTE Rails应用程序((我可以想象它被称为类似的东西(:
#controller
def show
@price = Product.find(params[:id]).price
end
我希望它能引导你朝着正确的方向前进。