0

我一直坚持一件看似简单的事情,但已经两天了,我无法弄清楚我错过了什么,我确信这是一件小事。

我的系统有registrations一个registration has_many registration_transactions.

首先创建一个注册,incomplete然后有一个checkout并且注册处于pending状态。在该checkout方法中,registration_transaction应该创建 a 。

registration 和registration_tranasction 都有一个字段payment_method,如果payment_method 是Credit Card并且transaction_type是,payment那么另一个registration_transaction 为相同的注册创建transaction_type 是Fee

这是执行所有这些操作的代码:

if !self.valid?
    errors.add_to_base("Error encountered processing transaction: Invalid Registration Data Entered.")
  else
    #if self.registration_transactions.empty?
      registration_transactions.create(
        :transaction_type   => 'Fee',
        :payment_method_id  => self.payment_method_id,
        :amount             => event_registration_type_membership.price,
        :notes              => "#{event.code} - #{event_registration_type_membership.event_registration_type.name} Registration Fee",
        :ip_address         => self.ip_address,
        :status             => 'Processed',
        :date_1             => Date.current)
    #end


    if payment_method.name.eql? 'Credit Card'
      payment = registration_transactions.find_by_notes("#{event.code} - #{event_registration_type_membership.event_registration_type.name} Registration Fee Initial Payment")
      #Commented the line below and changed status to 'Processed'.
      #Client wanted to process a transaction automatically when a user registers for an event and pays through CC
      #payment = registration_transactions.build(:status => 'Draft') if payment.nil?
      payment = registration_transactions.build(:status => 'Processed') if payment.nil?



      payment.update_attributes(
        :transaction_type             => 'Payment',
        :payment_method_id            => self.payment_method_id,
        :amount                       => event_registration_type_membership.price,
        :notes                        => "#{event.code} - #{event_registration_type_membership.event_registration_type.name} Registration Fee Initial Payment",
        :credit_card_number           => self.credit_card_number,
        :credit_card_security_code    => self.credit_card_security_code,
        :ip_address                   => self.ip_address,
        #:status                      => 'Draft', changed status to processed
        :status                       => 'Processed',

        :credit_card_type_id          => self.credit_card_type_id,
        :credit_card_expiration_date  => self.credit_card_expiration_date,
        :billing_city                 => self.transaction.billing_city,
        :billing_state                => self.transaction.billing_state,
        :billing_postal_code          => self.transaction.billing_postal_code,
        :billing_country              => self.transaction.billing_country,
        :billing_first_name           => self.billing_first_name,
        :billing_last_name            => self.billing_last_name,
        :billing_address_1            => self.transaction.billing_address_1,
        :date_1                       => Date.current)

      result = payment.process(self.ip_address,false)
      logger.info "GCS - attempted to process payment via authorize.net, RESULT=" + result.to_s
      logger.info "GCS - messages from authorize net=" + payment.errors.full_messages.to_s


      logger.info "GCS - result: #{result.inspect}"
      logger.info "GCS - payment: #{payment.inspect}"
      logger.info "GCS - payment.errors: #{payment.errors.full_messages}"
      self.save!

      errors.add_to_base(payment.errors.full_messages) if !result



    else #check payment
      result = true
    end
  end

问题是,当我在运行时放置检查点并签入时,registration_transactions会有两笔交易,一笔用于费用,另一笔用于付款,就像期望的那样。但是当注册完成并且我查看数据库时,只有支付交易。不知何故,费用交易没有得到保存。

4

1 回答 1

0

此行在保存之前不会保存构建关联模型。

registration_transactions.build(:status => 'Processed')

然后,

payment.update_attributes(...)

它更新数据库中的现有记录。您需要使用所有属性构建关联记录,然后保存。

于 2012-07-02T13:12:27.417 回答