我正在尝试设置一个简单的购物车。 我已经按照教程设置了购物车,我正在尝试将其连接到Braintree。 购物车功能有效,Braintree支付工作(如果我输入一个数字作为金额),但我遇到问题将购物车总价格变量发送到交易控制器。 到目前为止我的代码:
购物车index.html
<div class="cart-item-container">
<%= link_to 'Empty your cart', cart_clear_path %>
<br>
<br>
<% tokentotal = 0 %>
<% total = 0 %>
<ul> <% @cart.each do | id, quantity | %>
<% product = Product.find_by_id(id) %>
<% if quantity != 0 %>
<li class="cart-items">
<%= link_to product.title, product %>
<p><%= product.description %></p>
<p><%= number_to_currency(product.price, :unit => '€') %></p>
<p>Quantity<%= quantity %></p>
<a href="/cart/remove/<%= product.id %>"> Remove from cart</a>
</li>
<% total += quantity * product.price %>
<% tokentotal += quantity * product.tokens %>
<p>Total <%= number_to_currency(total, :unit => '€') %></p>
<p> Tokens <%= tokentotal %></p>
</ul>
<% end %> <%end%>
</div>
<form id="checkout" method="post" action="/transactions">
<div id="payment-form"></div>
<%= form_tag transactions_path do%>
<%# render 'customer_form' unless current_user.has_payment_info? %>
<div id="dropin"> <p>Please enter your payment details:</p>
</div>
<%=submit_tag "Pay", class: "button mt1" %>
<% end %>
</form>
<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script type="text/javascript">
function setupBT() {
braintree.setup("<%=@client_token%>", 'dropin', {
container: 'dropin'
});
}
if (window.addEventListener)
window.addEventListener("load", setupBT, false);
else if (window.attachEvent)
window.attachEvent("onload", setupBT);
else window.onload = setupBT;
</script>
</script>
显示购物车中节奏的商品的总价。我想要一种传递&lt;%total%&gt;的方法到交易控制器的'金额'部分。我尝试了很多变化,但在这个例子中将它留空了。
交易控制器:
class TransactionsController < ApplicationController
before_action :authenticate_user!
def new
end
def create
unless current_user.has_payment_info?
@result = Braintree::Transaction.sale(
amount: ,
payment_method_nonce: params[:payment_method_nonce],
customer: {
first_name: params[:first_name],
last_name: params[:last_name],
company: params[:company],
email: current_user.email,
phone: params[:phone]
},
options: {
store_in_vault: true
})
else
@result = Braintree::Transaction.sale(
amount: ,
payment_method_nonce: params[:payment_method_nonce])
end
if @result.success?
session[:cart] = nil
Token.create(user_id: current_user.id)
redirect_to new_gig_path, notice: "Congraulations! Your transaction has been successfully!"
else
flash[:alert] = "Something went wrong while processing your transaction. Please try again!"
render :new
end
end
private
def token_params
params.require(:token).permit(:user_id)
end
private
def generate_client_token
if current_user.has_payment_info?
Braintree::ClientToken.generate(customer_id: current_user.braintree_customer_id)
else
Braintree::ClientToken.generate
end
end
end
购物车控制器:
class CartController < ApplicationController
before_action :authenticate_user!
before_action :find_product
def remove
id = params[:id]
if session[:cart] then
cart = session[:cart]
else
session[:cart] = {}
cart = session[:cart]
end
if cart[id] then
cart[id] = cart[id] - 1
else
cart[id] = 1
end
redirect_to :action => :index
end
def add
id = params[:id]
#if cart already created use exisiing one
if session[:cart] then
cart = session[:cart]
else
session[:cart] = {}
cart = session[:cart]
end
#if token already in cart increase value
if cart[id] then
cart[id] = cart[id] + 1
else
cart[id] = 1
end
redirect_to :action => :index
end #end add method
def clearCart
session[:cart] = nil
redirect_to :action => :index
end
def index
if current_user.has_payment_info?
@client_token = Braintree::ClientToken.generate(customer_id: current_user.braintree_customer_id)
else
@client_token = Braintree::ClientToken.generate
end
#if there is a cart, pass it to the page for display else pass empty value
if session[:cart] then
@cart = session[:cart]
else
@cart = {}
end
end
private
def find_product
@product = Product.find_by_id(params[:id])
end
end
感谢您的光临。
答案 0 :(得分:3)
您可以使用表单中的隐藏字段将金额发送给控制器。
<form id="checkout" method="post" action="/transactions">
<div id="payment-form"></div>
<%= form_tag transactions_path do%>
<%= hidden_field_tag 'amount', total %>
<%# render 'customer_form' unless current_user.has_payment_info? %>
<div id="dropin"> <p>Please enter your payment details:</p>
</div>
<%=submit_tag "Pay", class: "button mt1" %>
<% end %>
</form>
答案 1 :(得分:0)
如果您希望@archana
的回答更简洁,您可以将参数传递给button_to
帮助者:
<%= button_to "Pay", transaction_path, params: { amount: total } %>
这将创建一个带有amount
隐藏输入的表单,将值传递给控制器。