Activemerchant undefined method 'month'

I am creating an application that processes various types of transactions for various services such as subscriptions, gift subscriptions and purchases.

I have a problem with gift transactions and activemerchant. I will give you a brief overview of how this works.

The user creates a gift subscription and fills in the data for it, it is stored in db, and then displayed to the user for viewing in the user show_view, then the user proceeds to enter credit card information in a separate form, and when he sends the data for processing transaction method is called from the controller, and there are problems with Im.

This is model gift_subscription.rb

def gift_purchase response = GATEWAY.purchase(price, credit_card, gift_purchase_options) GiftTransaction.create!(:action => "gift_purchase", :amount => price, :response => response) response.success? end private def gift_purchase_options { :ip => ip_address, :billing_address => { :name => name + last_name, :address1 => address1, :city => city, :state => state, :country => "Mexico", :zip => zip } } end def validate_card unless credit_card.valid? credit_card.errors.full_messages.each do |message| errors[:base] << message end end end def credit_card @credit_card = ActiveMerchant::Billing::CreditCard.new( :brand => card_type, :number => card_number, :verification_value => card_verification, :month => card_expires_on.month, :year => card_expires_on.year, :first_name => name, :last_name => last_name ) 

And here is the gift_subscription_controller.rb

 def review @gift_subscription = GiftSubscription.find(params[:id]) end def edit_review @gift_subscription = GiftSubscription.find(params[:id]) end def update_review @gift_subscription = GiftSubscription.find(params[:id]) respond_to do |format| if @gift_subscription.update_attributes(params[:gift_subscription]) format.html { redirect_to "gift_subscriptions/review/#{@gift_subscription.id}", :notice => 'Gift subscription was successfully updated.' } format.json { head :no_content } else format.html { render :action => "edit_review" } format.json { render :json => @gift_subscription.errors, :status => :unprocessable_entity } end end end def do_gift_transaction @gift_subscription = GiftSubscription.find(params[:id]) if @gift_subscription.gift_purchase redirect_to '/thank_you' else redirect_to "/gift_subscriptions/#{@gift_subscription.id}/failed_transaction" end end def failed_transaction @gift_subscription = GiftSubscription.find(params[:id]) @gift_transactions = @gift_subscription.gift_transactions end def transaction_details @gift_subscription = GiftSubscription.find(params[:id]) end 

To make things a little clearer, using the controller creation method, it redirects users to the review action, where there is edit_review in case they want to change something, then they go to transaction_details, where they enter credit card information, and finally , the do_gift_transaction method is called for the actual transaction.

The error I get is the following

 NoMethodError in GiftSubscriptionsController#do_gift_transaction undefined method `month' for nil:NilClass Rails.root: /home/peanut/RubymineProjects/GiftBox Application Trace | Framework Trace | Full Trace app/models/gift_subscription.rb:44:in `credit_card' app/models/gift_subscription.rb:12:in `gift_purchase' app/controllers/gift_subscriptions_controller.rb:113:in `do_gift_transaction' 

I look around and I can’t find out why it doesn’t recognize the month ... For other subscribers I have basically the same model (several differences), but it works fine. Any help here would be greatly appreciated.

GiftSubscription Attribute Attributes

 attr_accessible :response, :name, :last_name, :address1, :address2,:city, :state, :zip, :card_type, :ip_address, :price, :duration, :created_at, :card_expires_on, :card_number, :card_verification, :message has_one :gift_transactions, :class_name => "GiftTransaction" attr_accessor :card_number, :card_verification validate :validate_card, :on => :transaction_details 
+4
source share
1 answer

So, after hitting my head a few hours later, it turned out to be quite simple ... In order to enter credit card data, the fields that should have been saved in db were not saved, because I only had the link_to button to continue and ergo when it was called credit_card method, it was empty.

Thanks to lander16 for pointing this out.

0
source

Source: https://habr.com/ru/post/1440668/


All Articles