Created
May 17, 2012 13:34
-
-
Save glideranderson/2718965 to your computer and use it in GitHub Desktop.
Full Payments Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PaymentsController < ApplicationController | |
before_filter :authenticate_member! | |
autocomplete :company, :name, :full => true, :extra_data => [:address1, :address2, :city, :state, :zip, :business_category_id, :revenue_id], :scopes => [:inactive] | |
respond_to :json, :only => [:dues] | |
def index | |
end | |
def new | |
@payment = Payments.new(:member_id => current_member) | |
@company = Company.new | |
end | |
def create | |
credit_card | |
retrieve_company | |
amount = calculate_cost | |
if @credit_card.valid? | |
response = Rails.application.config.payment_gateway.recurring( | |
amount, | |
@credit_card, | |
:description => 'Save Our Workforce Annual Membership payment', | |
:start_date => Time.now, | |
:period => 'Year', | |
:frequency => 1 ) | |
if response.success? | |
save_payment(response) | |
save_company | |
flash[:success] = 'Thank you for your payment.' | |
#redirect_to member_path | |
redirect_to member_payments_path | |
else | |
flash[:error] = response.message | |
render :action => 'new' | |
end | |
else | |
flash[:errors] = @credit_card.errors.full_messages | |
render :action => 'new' | |
end | |
end | |
def show | |
@payments = Payment.where( :member_id => current_member.id ) | |
@history = payment_history | |
end | |
def update | |
end | |
def dues | |
if params[:revenue] and params[:btype] | |
render :json => calculate_dues(params[:btype].to_i, params[:revenue].to_i) | |
# render :json => 0 | |
end | |
end | |
private | |
def calculate_cost | |
return convert_to_cents(calculate_dues(@company.btype, @company.rev)) | |
end | |
def calculate_dues(btype, rev) | |
dues = { | |
2 => 100, | |
3 => 300, | |
4 => 1000, | |
5 => 2000, | |
6 => 6000 | |
} | |
return dues[rev] if btype == 2 and dues.has_key?(rev) | |
return 0 if btype == 4 or btype == 5 | |
return 39 | |
end | |
def convert_to_cents(money) | |
return (money.to_i * 100).round | |
end | |
def credit_card | |
@credit_card = ActiveMerchant::Billing::CreditCard.new( | |
:type => params[:payments][:credit_card][:type].to_s, | |
:number => params[:payments][:credit_card][:number], | |
:verification_value => params[:payments][:credit_card][:verification], | |
:first_name => params[:payments][:credit_card][:first_name], | |
:last_name => params[:payments][:credit_card][:last_name], | |
:year => params[:payments][:credit_card]["card_expires_on(1i)"], | |
:month => params[:payments][:credit_card]["card_expires_on(2i)"] | |
) | |
end | |
def save_payment(response) | |
profile_id = response.params['profile_id'] | |
@payment = Payments.new(:member_id => current_member, :profile_id => profile_id) | |
@payment.save | |
if (params[:payments][:renew][:auto] == '0') | |
suspend_recurring_payment(profile_id) | |
end | |
end | |
def retrive_company | |
if (params[:payments][:identity][:cid] != '0' && params[:payments][:identity][:cid] != '') | |
@company = Company.find(params[:payments][:identity][:cid]) | |
else | |
@company = Company.new(params[:payments][:company]) | |
end | |
end | |
def save_company | |
@company.member_id = current_member.id | |
@company.save | |
end | |
def suspend_recurring_payment(profile_id) | |
response = Rails.application.config.payment_gateway.suspend_recurring(profile_id) | |
end | |
def reactivate_recurring_payment(profile_id) | |
response = Rails.application.config.payment_gateway.reactivate_recurring(profile_id) | |
end | |
def payment_history | |
history = [] | |
@payments.each do |p| | |
response = Rails.application.config.payment_gateway.status_recurring(p.profile_id) | |
history << { | |
:active => (response.params['profile_status'] == 'ActiveProfile') ? 'yes' : 'no', | |
:start => p.created_at, | |
:amount => response.params['amount'], | |
:card_type => response.params['credit_card_type'], | |
:card_number => response.params['credit_card_number'] | |
} if response.success? | |
end | |
history | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment