首页 文章

更改rails应用程序中条带中项目的价格

提问于
浏览
0

我觉得相当茫然,我正试图在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 . 我觉得这是一个简单的问题,但我无法解决它 . 有什么建议?

1 回答

  • 0

    这很简单 . Stripe只向您展示一个例子 . 您需要添加所需的价格 - 例如从控制器传递全局值 .

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

    问题是: where does the price come from?

    在常规网上商店中,您将拥有一个带有数据列的 Product 模型: price .

    然后你会在充电控制器的show动作中调用它(基于正常的REST-ROUTE Rails应用程序)(我可以想象它被称为类似的东西):

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

    我希望它能引导你朝着正确的方向前进 .

相关问题