使用条纹付款几乎完成了我的应用程序,我可以处理信用卡详细信息,但是当我想要它显示收据时。我在ChargesController #create中得到错误NameError。未定义的局部变量或#
的方法`product'Charges_controller.rb
class ChargesController < ApplicationController
def create
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => params[:amount],
:description => product.full_description, # <---- error
:currency => 'gbp'
)
purchase = Purchase.create(email: params[:stripeEmail],
card: params[:stripeToken], amount: product.price_in_cents,
description: charge.description, currency: charge.currency,
customer_id: customer.id, product_id: product.id, uuid: SecureRandom.uuid)
redirect_to purchase
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
end
product.rb
class Product < ActiveRecord::Base
def full_description
"#{self.title} #{self.subtitle}"
end
def price_in_cents
(self.price * 100).to_i
end
end
home.html.erb
<section id="content-details" class="row clearfix">
<article id="details-article" class="col-sm-8">
<h1 id="article-title"><%= @product.title %><span class="font-weight-light"><%= @product.title %></span></h1>
<h3><small>By <a href="#" data-toggle="modal" data-target="#author-modal"><%= @product.author %></a></small></h3>
<%= raw @product.description %>
</article>
<aside id="details-aside" class="col-sm-4">
<%= form_tag charges_path, id:'chargeForm' do %>
<script src="https://checkout.stripe.com/checkout.js"></script>
<%= hidden_field_tag 'stripeToken' %>
<%= hidden_field_tag 'stripeEmail' %>
<%= hidden_field_tag 'amount', @product.price_in_cents %>
<button id='btn-buy' type='button' class="btn btn-success btn-lg btn-block"><span class="glyphicon glyphicon-ok"></span>I want this!</button>
<script>
var handler = StripeCheckout.configure({
key: '<%= Rails.configuration.stripe[:publishable_key] %>',
token: function(token, arg) {
document.getElementById("stripeToken").value = token.id;
document.getElementById("stripeEmail").value = token.email;
document.getElementById("chargeForm").submit();
}
});
document.getElementById('btn-buy').addEventListener('click', function(e) {
handler.open({
name: 'Open Cinema',
description: '<%= @product.title %> <%= @product.subtitle %> ($<%= @product.price %>)',
amount: document.getElementById("amount").value
});
e.preventDefault();
})
</script>
<% end %>
<ul id="details-infobox" class="list-group">
<li class="list-group-item active clearfix">DETAILS</li>
<li class="list-group-item"><%= @product.details %></li>
<li class="list-group-item clearfix">
<span class="pull-left content-qualifier">Length</span>
<span class="pull-right"><%= @product.length %></span>
</li>
</ul>
</aside>
</section>
</div>
<footer id="site-footer">
Made by <a href="http://onemonth.com" target="_blank">
<%= image_tag('chris.jpg', id: 'one-month-footer-logo', alt: 'Open cinema') %>
</footer>
<!-- Modal -->
<div class="modal fade" id="author-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title"><span class="glyphicon glyphicon-user"></span>About </h4>
</div>
<div class="modal-body clearfix">
<div id="modal-left" class="pull-left">
<p><strong><%= @product.author %></strong></p>
<%= raw @product.description %>
</div>
<div id="modal-right" class="pull-right">
<%= image_tag( @product.author_image_name, alt: @product.author) %>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
向product.db添加字段
class AddingFieldsToProduct < ActiveRecord::Migration
def change
add_column :products, :length, :string
add_column :products, :author_description, :text
add_column :products, :author_image_name, :string
end
end
产品表
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :title
t.string :subtitle
t.string :author
t.text :description
t.string :sku
t.decimal :price
t.timestamps
end
end
end
有3个html文件 一个用于home.html.erb,见上文
然后购买show.html.erb
section id="content-receipt" class="clearfix">
<div id="receipt-container">
<div class="dotmatrix left"></div>
<div id="receipt-content">
<h4 id="receipt-title">Thank You!</h4>
<p><strong>RECEIPT</strong></p>
<div id="receipt-details" class="row clearfix">
<div class="col-sm-8 receipt-left">
<p><strong class="font-weight-heavy"><%= @product.title %>xxx</strong><br/> <span class="font-weight-light"><%= @product.subtitle %></span></p><%= @product.discription %>
</div>
<div class="col-sm-4">
$<%= @product.price %>
</div>
</div>
<a href="<%= @product.download_url %>"><button type="button" class="btn btn-success btn-xs">DOWNLOAD</button></a>
<footer id="receipt-footer">
<small>Your purchase is complete. We hope you enjoy it!</small>
</footer>
</div>
<div class="dotmatrix right"></div>
</div>
<div id="receipt-shadow"></div>
</section>
然后purchase_recipts.html.erb
<p>Thanks!</p>
Thanks for buying a ticket to the movie!
<a href="<%= @product.download_url %>"><butoon type="button"
class="btn btn-sucess btn-xs">Download</button></a>
更新
将金额更改为product.price_in_cents但获得相同的错误
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => product.price_in_cents,
:description => product.full_description,
:currency => 'gbp'
)
答案 0 :(得分:2)
将@ product.id作为隐藏字段添加到show.html.erb
中的表单 <%= hidden_field_tag :product_id, @product.id %>
然后在charge_controller.rb中添加:
product = Product.find(params[:product_id])
位于#create方法的顶部。
答案 1 :(得分:0)
您的代码中存在可能阻止其运行的拼写错误。查看你的&#34; purchases_recipts.html.erb&#34;文件。
<a href="<%= @product.download_url %>"><butoon type="button"
class="btn btn-sucess btn-xs">Download</button></a>
应该是:
<a href="<%= @product.download_url %>"><button type="button"
class="btn btn-success btn-xs">Download</button></a>