У меня есть страница продукта, которая при покупке продукта будет перенаправлена на страницу покупки (страницу получения и загрузки).
Все отлично работает в разработке на моем локальном компьютере, но когда покупка проходит в производстве на удаленном сервере, он взимает плату с клиента (используя 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.):
Вот мой файл route.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 / environment / 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