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
require 'digest/md5' | |
class Chargify::HooksController < ApplicationController | |
protect_from_forgery :except => :dispatch_handler | |
before_filter :verify, :only => :dispatch_handler | |
EVENTS = %w[ test signup_success signup_failure renewal_success renewal_failure payment_success payment_failure billing_date_change subscription_state_change subscription_product_change ].freeze | |
def dispatch_handler | |
event = params[:event] |
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
def self.internationalize_phone_number(number, country) | |
c = Country[Country.find_by_name(country)[0]] | |
if c | |
number = Phony.normalize(number) | |
number = "#{c.country_code}#{number}" unless number.starts_with?(c.country_code) | |
end | |
number = Phony.normalize(number) | |
number | |
end |
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
rails generate migration add_birth_date_fields_to_users birth_day:integer birth_month:integer |
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
desc "Updating birth dates from date of birth" | |
task :update_birth_date_fields => :environment do | |
puts "Updating birth dates from date of birth" | |
User.where('date_of_birth is not null').each do |u| | |
u.birth_month = u.date_of_birth.month | |
u.birth_day = u.date_of_birth.day | |
u.save | |
end | |
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
private | |
def update_birth_date_fields | |
if date_of_birth_changed? | |
self.birth_month = date_of_birth ? date_of_birth.month : nil | |
self.birth_day = date_of_birth ? date_of_birth.day : nil | |
end | |
end |
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
before_save :update_birth_date_fields | |
private | |
def update_birth_date_fields | |
if date_of_birth_changed? | |
self.birth_month = date_of_birth ? date_of_birth.month : nil | |
self.birth_day = date_of_birth ? date_of_birth.day : nil | |
end | |
end |
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
def subscription_state_change | |
begin | |
account = Account.find(@subscription.customer.reference) | |
account.subscription_state = @subscription.state | |
account.subscription_billing_date = @subscription.current_period_ends_at | |
account.save(:validate => false) | |
render :nothing => true, :status => 200 | |
rescue Exception => e | |
notify_hoptoad(e) #If you use hoptoad... | |
render :nothing => true, :status => 422 and return |
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
# Amount should be a decimal between 0 and 1. Lower means darker | |
def darken_color(hex_color, amount=0.4) | |
hex_color = hex_color.gsub('#','') | |
rgb = hex_color.scan(/../).map {|color| color.hex} | |
rgb[0] = (rgb[0].to_i * amount).round | |
rgb[1] = (rgb[1].to_i * amount).round | |
rgb[2] = (rgb[2].to_i * amount).round | |
"#%02x%02x%02x" % rgb | |
end | |
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
def contrasting_text_color(hex_color) | |
color = hex_color.gsub('#','') | |
convert_to_brightness_value(color) > 382.5 ? darken_color(color) : lighten_color(color) | |
end | |
def convert_to_brightness_value(hex_color) | |
(hex_color.scan(/../).map {|color| color.hex}).sum | |
end |
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
def plan_change_preview(new_plan_id) | |
api_url = "https://#{Chargify.subdomain}.chargify.com/subscriptions/#{subscription_id}/migrations/preview.json" | |
data = {:migration => {:product_handle => p_handle} } | |
response = HTTParty.post(api_url, :body => data.as_json, :basic_auth => {:username => Chargify.api_key, :password => 'X'}) | |
response.parsed_response.first[1] | |
end |
OlderNewer