我能够创建并订购并成功运行并保存。我正在使用订单模型和order_transaction模型。我添加了一个字段,以便在填写订单和订阅时,用户可以上传将在网站上显示的自己的照片。除了用户附加照片的情况外,整个过程仍然可以正常工作。当发生这种情况时,它只是未通过信用卡验证。取下附件,然后再次正常工作。
order.rb
class Order < ActiveRecord::Base
has_many :transactions, :class_name => "OrderTransaction"
attr_accessor :card_number, :card_verification, :card_expires_on, :response
after_create :log_transaction
attr_accessible :first_name,
:last_name,
:address_one,
:address_two,
:city,
:state,
:zip,
:country,
:email,
:verify_email,
:term,
:shirt_color,
:shirt_size,
:card_type,
:card_number,
:card_verification,
:transactions_attributes,
:card_expires_on_month,
:card_expires_on_year,
:photo
has_attached_file :photo,
:styles => { :medium => "320x320>", :thumb => "100x100#" }
validates :first_name, :last_name, :address_one, :city, :state, :zip, :country, :presence = > true
validate :validate_card, :on => :create
validate :require_shirt_size, :on => :create
def purchase
self.response = GATEWAY.purchase(price_in_cents, credit_card)
errors.add(:base, response.message) unless self.response.success?
self.response.success?
end
def credit_card
@credit_card ||= ActiveMerchant::Billing::CreditCard.new(
:type => card_type,
:number => card_number,
:verification_value => card_verification,
:month => card_expires_on_month,
:year => card_expires_on_year,
:first_name => first_name,
:last_name => last_name
)
end
def price_in_cents
total = 0
if term == '1'
total = 2000
elsif term == '2'
total = 3500
elsif term == '3'
total = 4500
end
unless shirt_color == 'none'
total += 1599
end
total
end
def require_shirt_size
unless shirt_color == 'none'
if shirt_size == ''
errors.add(:base, "Shirt Size required if ordering a shirt.")
end
end
end
private
def log_transaction
self.transactions.create!(:action => "purchase", :amount => price_in_cents, :response = > self.response)
end
def validate_card
unless credit_card.valid?
credit_card.errors.full_messages.each do |message|
errors.add(:base, message)
end
end
end
end
order_transaction.rb
class OrderTransaction < ActiveRecord::Base
attr_accessible :action, :amount, :response
belongs_to :order
serialize :params
def response=(response)
self.success = response.success?
self.authorization = response.authorization
self.message = response.message
self.params = response.params
rescue ActiveMerchant::ActiveMerchantError => e
self.success = false
self.authorization = nil
self.message = e.message
self.params = {}
end
end
orders_controller.rb
def create
@order = Order.new(params[:order])
if @order.valid? && @order.purchase
render :success and return
else
render :action => 'new'
end
end
好像不知何故,附上照片会导致它失去传递信用卡数据的能力。我不确定它在哪里丢失,但确实如此。感觉就像回形针中的一个错误。在与activemerchant进行交易时,任何人都有与上传文件类似的问题?这是一个相当简单的订单类型,有三种订阅选项。
即使是关于如何更好地测试和查看正在发生的事情的建议也会非常有帮助,现在让我感到非常困难。