I'm not sure if I am phrasing this question correctly. I am using the Stripe API for a Harry Potter themed Rails online store demo app. I followed the Stripe boilerplate code, so I currently have the amount set at a hardcoded value of $1.00. In my shopping cart, there is a method that displays the total cost of all items in the cart. That works fine, but I can't figure out how to pass that value to the Charges controller so that it sets that as the amount of the payment.
我不确定我是否正确地表达了这个问题。我正在使用Stripe API为Harry Potter主题的Rails在线商店演示应用程序。我遵循Stripe样板代码,所以我目前的金额设置为$ 1.00的硬编码值。在我的购物车中,有一种方法可以显示购物车中所有商品的总成本。这工作正常,但我无法弄清楚如何将该值传递给Charges控制器,以便将其设置为付款金额。
I'm fairly new to Rails, so any helpful explanations would be greatly appreciated.
我对Rails相当新,所以任何有用的解释都将不胜感激。
Here is my charges/new.html.erb file:
这是我的charge / new.html.erb文件:
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>Amount: $1.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="100"
data-locale="auto"></script>
<% end %>
Here is my Charges controller:
这是我的Charges控制器:
class ChargesController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:new, :create]
def new
end
def create #METHOD IS CALLED AFTER PAYMENT IS MADE
# Amount in cents
@amount = 100
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Witch or Wizard',
:currency => 'usd'
)
Cart.destroy(session[:cart_id])
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
Here is my carts/show.html.erb file:
这是我的carts / show.html.erb文件:
<p id="notice"><%= notice %></p>
<h2>My Cart</h2>
<table class="table table-responsive table-striped">
<thead>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Total Price in Galleons</th>
<th>Total Price in Muggle Currency</th>
</tr>
<tbody>
<%= render(@cart.line_items) %>
<tr>
<td>Total</td>
<td><%= number_to_currency(@cart.total_price * 7.35) %></td>
<td></td>
<td></td>
</tr>
</tbody>
</thead>
</table>
<br>
<div class="row">
<div class="col-md-3">
<div class="row">
<div class="col-md-4">
<%= link_to 'Back', products_path, :class => 'btn btn-primary whiteText' %>
</div>
<div class="col-md-4">
<%= link_to "Checkout", new_charge_path, :class => 'btn btn-success whiteText' %>
</div>
<div class="col-md-4">
<%= link_to 'Empty Cart', @cart, method: :delete, data: {confirm: 'Are you sure you want to empty your cart?'}, :class => 'btn btn-danger whiteText' %>
</div>
</div>
</div>
<div class="col-md-9"></div>
</div>
Here is my Carts controller:
这是我的推车控制器:
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :edit, :update, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
# GET /carts
# GET /carts.json
def index
@carts = Cart.all
end
# GET /carts/1
# GET /carts/1.json
def show
end
# GET /carts/new
def new
@cart = Cart.new
end
# GET /carts/1/edit
def edit
end
# POST /carts
# POST /carts.json
def create
@cart = Cart.new(cart_params)
respond_to do |format|
if @cart.save
format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
format.json { render :show, status: :created, location: @cart }
else
format.html { render :new }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /carts/1
# PATCH/PUT /carts/1.json
def update
respond_to do |format|
if @cart.update(cart_params)
format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
format.json { render :show, status: :ok, location: @cart }
else
format.html { render :edit }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
# DELETE /carts/1
# DELETE /carts/1.json
def destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to root_path, notice: 'Cart was emptied.' }
format.json { head :no_content }
end
end
def update_quantity
@line_item.update_attribute(:quantity)
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cart
@cart = Cart.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def cart_params
params.fetch(:cart, {})
end
def invalid_cart
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to root_path, notice: 'Invalid cart'
end
end
Here is my Cart model:
这是我的购物车型号:
class Cart < ApplicationRecord
has_many :line_items, dependent: :destroy
def add_product(product)
current_item = line_items.find_by(product_id: product.id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product.id)
end
current_item
end
def total_price
line_items.to_a.sum {|item| item.total_price}
end
def convert_to_muggle(galleons)
line_items.to_a.sum {|item| item.convert_to_muggle}
end
end
And here is my routes file:
这是我的路线文件:
Rails.application.routes.draw do
resources :charges
resources :orders
resources :line_items
resources :carts
devise_for :users
match 'users/:id' => 'users#destroy', :via => :delete, :as => :admin_destroy_user
resources :users, only: [:index, :show, :edit, :update]
root 'products#index'
resources :products
controller :products do
post '/products/destroy' => 'products#destroy', as: :destroy
get '/products/add_to_cart' => 'products#add_to_cart', as: :add_to_cart
get '/products/remove_from_cart' => 'products#remove_from_cart', as: :remove_from_cart
end
controller :line_items do
post '/line_items/increase_quantity' => 'line_items#increase_quantity', as: :increase_quantity
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
1 个解决方案
#1
1
In your charges_controller, change the amount:
在您的charge_controller中,更改金额:
@amount = @cart.total_price * 735
And your charges/new.html.erb:
你的收费/ new.html.erb:
<span>Amount: <%= number_to_currency(@cart.total_price * 7.35) %></span>
and
和
data-amount="<%= @cart.total_price * 735 %>"
Let me know if that helps.
如果有帮助,请告诉我。
#1
1
In your charges_controller, change the amount:
在您的charge_controller中,更改金额:
@amount = @cart.total_price * 735
And your charges/new.html.erb:
你的收费/ new.html.erb:
<span>Amount: <%= number_to_currency(@cart.total_price * 7.35) %></span>
and
和
data-amount="<%= @cart.total_price * 735 %>"
Let me know if that helps.
如果有帮助,请告诉我。