簡體   English   中英

在Rails應用程序中更改帶區項目的價格

[英]Changing the price of items in stripe in a rails application

我感到無所適從,我試圖在Rails應用程序中使用條紋簽出創建一個簡單的店面。 我一直在學習條紋教程,並且可以正常工作,但是他們在控制器中硬編碼了價格。

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。 我覺得這是一個簡單的問題,但我無法解決。 有什么建議么?

這很簡單。 Stripe僅向您顯示示例。 您需要添加所需的價格-例如,通過從控制器傳遞全局值。

...
<span>Amount: <%= @price %></span>
...
data-amount="<%= @price %>"

問題是: 價格從哪里來?

在常規的網上商店中,您將擁有一個帶有數據列的Product模型: price

然后,您可以在charges控制器中的show action中調用它(基於正常的REST-ROUTE Rails應用程序)(我可以想象它像這樣):

#controller
def show
  @price = Product.find(params[:id]).price
end

我希望它能引導您朝正確的方向發展。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM