Search code examples
ruby-on-railsruby-on-rails-3recurly

Rails and Recurly credit card form not updating expiration month and year info


I was creating a form to interact with Recurly on Rails.

I have this view and this form:

<h1><%= t('payments.edit') %> </h1>


<%= render(:partial => 'shared/new_recurly_form', :locals => {:success_url => "#{APP_URL}#{users_credit_card_path}"}) %>

<%= semantic_form_for current_user, :url => users_credit_card_path, :html => {:method => :put, :class => 'credit_card'} do |form| %>

    <%= form.inputs do %>
      <div class="field">
        <%= form.label :name %>
        <%= form.text_field :name %>
      </div>  
      <div class="field">
        <%= form.label :first_surname %>
        <%= form.text_field :surnames %>
      </div>  
      <div class="field">
          <%= label_tag :number, "Credit Card Number " %>
          <%= text_field_tag :number %>
      </div>
      <div class="field">
          <%= label_tag :verification_value, "Security Code on Card (CVV)" %>
          <%= text_field_tag :verification_value %>
      </div>
      <div class="field">
      <%= label_tag :month, "Expiration month" %>
      <%= select_month(Date.today, :use_month_numbers => true) %>
      </div>
      <div class="field">
      <%= label_tag :year, "Expiration year" %>
      <%= select_year(Date.today, :start_year => Time.now.year, :end_year => Time.now.year + 20) %>
      </div>
    <% end %>

  <%= form.buttons do %>
    <%= form.commit_button  t('confirm') %>
  <% end %>

<% end %>

and its respective controller, credit_card_controller.rb

class Users::CreditCardsController < ApplicationController

  skip_before_filter :check_deactivated_user
  protect_from_forgery :except => :create

  def create
    update
  end

  def edit
    @user = current_user
    @price_plan = current_user.price_plans.active.first
  end

  def update
    begin
        update_new_recurly_subscription
    rescue Exceptions::RecurlyAccountCreateInvalidAccountException => e
        puts "CreditCardsController Exceptions::RecurlyAccountCreateInvalidAccountException"
        puts e.message
    rescue Recurly::Resource::NotFound => e
        puts "CreditCardsController Recurly::Resource::NotFound Creating new account for user"  
        puts e.message  
        begin
            create_new_recurly_account
            create_new_recurly_subscription         
        rescue Exceptions::RecurlyAccountCreateInvalidAccountException => e
            puts "CreditCardsController inside find Recurly::Resource::NotFound"
            puts e.message
            update_new_recurly_account
            create_new_recurly_subscription
        end 
    end

    account = update_account_billing_info
    account.billing_info = {
        :first_name         => current_user.name,
        :last_name          => "#{current_user.surnames}",
        :number             => params[:number],
        :verification_value => params[:verification_value],
        :month              => params[:month],
        :year               => params[:year]
    }
    puts "Users::CreditCardsController update Billing info: #{account.billing_info.inspect}"
    account.billing_info.save
    if current_user.credit_card_store!(account.billing_info)
      if current_user.is_debtor?
        flash[:alert] = t('flash.price_plans.payed_after_unpay')
        render :edit
      else
        flash[:alert] = t('flash.credit_card.update.success')
        render :success
      end

    else
      flash[:failure] = t('flash.credit_card.update.failure')
      render :edit
    end

  end

end

When I update the form, using the update function on the former controller, I am able to change the credit card number but I am not able to change the expiration month and the expiration year.

What am I missing here? Why can I change the credit card number and not the month and the year interacting with Recurly?


Solution

  • You're pulling the incorrect parameters (nil values). Based on your logs, try:

    account.billing_info = {
        ...
        :month              => params[:date][:month],
        :year               => params[:date][:year]
    }
    

    which corresponds to "date"=>{"month"=>"7", "year"=>"2017"}