我有一个产品页面,当您购买产品时,它将重定向到购买页面(收据和下载页面)。
一切都可以在我的本地计算机上正常运行,但是当购买在远程服务器上进行生产时,会向客户收取费用(使用 Stripe ),但不会在purchase
表中创建一个条目-不允许客户下载其产品。我检查了远程服务器上psql
中的“购买”表,它没有任何行/条目。
它也应该发送电子邮件-链接到购买页面,该页面也仅在开发中起作用。
我在本地计算机和远程服务器上都使用 PostgreSQL 。
为控制器收费:
class ChargesController < ApplicationController
def new
set_meta_tags noindex: true
end
def create
pack = Pack.find(params[:product_id])
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken],
)
# Amount in cents
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => pack.price_in_cents,
:description => 'Product Purchase',
:currency => 'usd',
)
purchase = Purchase.create(
email: params[:stripeEmail],
card: params[:stripeToken],
amount: pack.price_in_cents,
description: charge.description,
currency: charge.currency,
customer_id: customer.id,
product_id: pack.id,
uuid: SecureRandom.uuid,
)
redirect_to purchase
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
purchases_controller :
class PurchasesController < ApplicationController
def show
@title = 'Purchase Receipt';
@purchase = Purchase.find_by_uuid(params[:id])
@pack = Pack.find(@purchase.product_id)
set_meta_tags noindex: true
end
end
购买模式:
class Purchase < ApplicationRecord
attr_accessor :download_token
after_create :email_purchaser
def to_param
uuid
end
def email_purchaser
PurchaseMailer.purchase_receipt(self).deliver
end
def Purchase.new_token
SecureRandom.urlsafe_base64
end
def create_download
self.download_token = Purchase.email.new_token
update_attribute(:download, Purchase.email(download_token))
update_attribute(:download_sent_at, Time.zone.now)
end
end
purchase_mailer:
class PurchaseMailer < ActionMailer::Base
layout 'purchase_mailer'
default from: "First Last <myemail@gmail.com>"
def purchase_receipt purchase
@purchase = purchase
mail to: purchase.email, subject: "Thank you for your purchase. Here's your download link, enjoy!"
end
end
链接到购买。在发送的电子邮件中显示视图:
<%= link_to "DOWNLOAD", purchase_url(@purchase), target: "_blank" %>
这是服务器日志:
I, [2019-01-11T22:04:21.404919 #17222] INFO -- : [14c2eed0-3aae-4ab6-8142-9aa9744819af] Started HEAD "/https://mywebsite.com/" for XX.XXX.XXX.XXX at 2019-01-11 22:04:21 +0000
F, [2019-01-11T22:04:21.405538 #17222] FATAL -- : [14c2eed0-3aae-4ab6-8142-9aa9744819af]
F, [2019-01-11T22:04:21.405585 #17222] FATAL -- : [14c2eed0-3aae-4ab6-8142-9aa9744819af] ActionController::RoutingError (No route matches [HEAD] "/https:/mywebsite.com"):
F, [2019-01-11T22:04:21.405611 #17222] FATAL -- : [14c2eed0-3aae-4ab6-8142-9aa9744819af]
F, [2019-01-11T22:04:21.405648 #17222] FATAL -- : [14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/debug_exceptions.rb:63:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:36:in `call_app'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `block in call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:69:in `block in tagged'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:26:in `tagged'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:69:in `tagged'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/request_id.rb:25:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/method_override.rb:22:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/runtime.rb:22:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/executor.rb:12:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/sendfile.rb:111:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/engine.rb:522:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] /usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:97:in `process_request'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:157:in `accept_and_process_next_request'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:110:in `main_loop'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler.rb:415:in `block (3 levels) in start_threads'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] /usr/lib/ruby/vendor_ruby/phusion_passenger/utils.rb:113:in `block in create_thread_and_abort_on_exception'
这是在计费控制器中将!
添加到purchase = Purchase.create!(
之后的服务器日志:
F, [2019-01-15T21:21:12.869293 #24540] FATAL -- : [932479d4-c710-4ac6-9159-8c3fa5299adc] ActiveModel::UnknownAttributeError (unknown attribute 'uuid' for Purchase.):
这是我的routes.rb文件:
resources :charges
resources :purchases, only: [:show]
由于它破坏了我的代码,我从get 'purchase' => 'purchases#show', as: 'purchase'
中删除了routes.rb
。
正在使URL执行此操作:https://mywebsite.com/purchase.uuid
代替此:https://mywebsite.com/purchase/uuid
这是我的config / environments / production.rb文件:
Rails.application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.js_compressor = :uglifier
config.assets.compile = false
config.log_level = :debug
config.log_tags = [ :request_id ]
config.action_mailer.perform_caching = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'transverseaudio.com'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'transverseaudio.com',
:enable_starttls_auto => true
}
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
config.active_record.dump_schema_after_migration = false
end
运行rails routes
后:
charges GET /charges(.:format) charges#index
POST /charges(.:format) charges#create
new_charge GET /charges/new(.:format) charges#new
edit_charge GET /charges/:id/edit(.:format) charges#edit
charge GET /charges/:id(.:format) charges#show
PATCH /charges/:id(.:format) charges#update
PUT /charges/:id(.:format) charges#update
DELETE /charges/:id(.:format) charges#destroy
purchase GET /purchases/:id(.:format) purchases#show
答案 0 :(得分:1)
我建议您将ChargesController.create
包装在交易中。如果Purchase.create
中发生某些错误(例如,验证),您最终会在db + Stripe中的实际事务中创建一些局部数据。另外,检查purchase
是否已实际创建将是一个很好的改进:
if purchase
redirect_to purchase
else
render :new, error: purchase.errors
end
然后在生产GUI中,您将清楚地看到问题所在。
此外,在您的日志F, [2019-01-15T21:21:12.869293 #24540] FATAL -- : [932479d4-c710-4ac6-9159-8c3fa5299adc] ActiveModel::UnknownAttributeError (unknown attribute 'uuid' for Purchase.):
中,这可能意味着您的迁移未应用到产品中,而您只是在Purchase
中缺少了uuid列。
答案 1 :(得分:0)
看起来您正在收到HEAD请求。您的路线似乎无法处理此问题。不用担心,这很正常。
这是正在进行的CORS飞行前检查。在此处阅读更多信息:https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
和
Why does a cross-origin HEAD request need a preflight check?
对于导轨,请尝试机架Cors: https://github.com/cyu/rack-cors这可能会有帮助。
基本上,机架CORS可使您的应用适当地响应飞行前检查。