0

一見単純なことに行き詰まっていますが、2日経ちましたが、何が欠けているのかわかりません。それは小さなことだと確信しています。

私のシステムにはregistrationsregistration has_many registration_transactions.

登録が最初に作成され、incomplete次に がcheckoutあり、登録がpending状態になります。checkoutメソッドでは、 aがregistration_transaction作成されることになっています。

registration と registration_tranasction の両方に fieldpayment_methodがあり、 payment_method がCredit Cardtransaction_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_transactions2 つのトランザクションが発生することです。1 つは手数料用、もう 1 つは支払い用です。しかし、登録が完了してデータベースを見ると、支払いトランザクションしかありません。どういうわけか手数料トランザクションが保存されません。

4

1 に答える 1

0

この行は、ビルドに関連付けられたモデルを保存するまで保存しません。

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

それで、

payment.update_attributes(...)

DB の既存のレコードを更新します。関連するレコードをすべての属性で作成してから保存する必要があります。

于 2012-07-02T13:12:27.417 に答える