我能够创建和订购并成功运行并保存它。我正在使用订单模型和 order_transaction 模型。我添加了一个字段,以便在填写订单和订阅时,用户可以上传自己的照片,该照片将在网站上展示。整个过程仍然可以正常工作,除非用户附上照片。发生这种情况时,它只是无法通过信用卡验证。取下附件,然后它再次正常工作。
订单.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
订单控制器.rb
def create
@order = Order.new(params[:order])
if @order.valid? && @order.purchase
render :success and return
else
render :action => 'new'
end
end
好像不知何故,附上一张照片会导致它失去传递信用卡数据的能力。我不确定它在哪里丢失它,但它是。感觉就像回形针中的错误。在与 activemerchant 进行交易时,有人在上传文件时遇到过类似的问题吗?这是一个相当简单的订单类型,有三个订阅选项。
甚至关于如何更好地测试并查看发生了什么的建议也会非常有帮助,这让我现在非常困惑。