activemerchant undefined method 'month'
-
09-07-2021 - |
Question
I'm building an application that handles different types of transactions for different services such as subscriptions, gift subscriptions and purchases.
I have an issue with the gift transactions and activemerchant. Ill give you a brief overview of how it works.
The user creates a gift subscription and fills out the data for it, it is stored in the db and then shown back to the user for review in a custom "show_view", the user then proceeds to enter credit card information in a separate form and when he submits the data, a method from the controller is called to handle the transaction and here is where Im having issues.
This is the gift_subscription.rb model
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 more clear, from the controller create method, it redirects users to the review action where there's an edit_review in case they want to change something, then they go to transaction_details where they enter creditcard info and finally the method do_gift_transaction is called to actually do the 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've been looking around and I can't seem to find why it doesnt recognize the month... For other subscriptions I have basically the same model (a few diferences) but it works perfectly. Any help here would be much appreciated.
GiftSubscription model 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
Solution
So after a few hours of banging my head it turned out to be quite simple... In the view to enter credit card data, the fields that were supposed to be saved to the db were not being saved because I only had a link_to button to continue and ergo when the credit_card method was called, it was empty.
Thanks to lander16 for pointing this out.