Created
June 25, 2012 17:11
-
-
Save mmalivuk/2989943 to your computer and use it in GitHub Desktop.
Payment processor sample code
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 PaymentProcessor | |
# used for the initial signup flow | |
def self.hosted_signup_page_for(account_type, user) | |
"https://#{self.subdomain}.chargify.com/h/#{self.product_id(account_type)}/subscriptions/new?first_name=#{user.first_name}&last_name=#{user.last_name}&email=#{user.email}&reference=#{user.id}" | |
end | |
# used to send a user to their unique payment page in | |
# the event that they need to update their payment details | |
def self.update_payment_url_for(account) | |
return nil if (subscriber_id = account.chargify_subscriber_id).blank? | |
"https://#{self.subdomain}.chargify.com/update_payment/#{subscriber_id}/#{self.secure_digest(["update_payment", subscriber_id, self.site_key].join("--"))[0..9]}" | |
end | |
private | |
def self.chargify_config | |
YAML::load_file(File.join(Rails.root, 'config', 'chargify.yml')) | |
end | |
def self.site_key | |
self.chargify_config[Rails.env]['site_key'] | |
end | |
def self.subdomain | |
self.chargify_config[Rails.env]['subdomain'] | |
end | |
def self.product_id(account_type) | |
if account_type == "monthly" | |
103279 | |
elsif account_type == "annual" | |
107396 | |
elsif account_type == "lifetime" | |
107415 | |
elsif account_type == "free" | |
107395 | |
end | |
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
class User < ActiveRecord::Base | |
# Include default devise modules. Others available are: | |
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable | |
has_many :leads, :dependent => :destroy | |
has_many :listings, :dependent => :destroy | |
validates :about, :length => { :maximum => 2000 } | |
validates :quote, :length => { :maximum => 225 } | |
# Setup accessible (or protected) attributes for your model | |
attr_accessible :email, :first_name, :last_name, :city, :phone, :company, :quote, :about, :password, :password_confirmation, :remember_me, :avatar | |
mount_uploader :avatar, AvatarUploader | |
def create_subscription_and_setup_account!(account_type) | |
@account_type = params[:account_type] | |
@product_id = PaymentProcessor.product_id(@account_type) | |
json_data = { | |
:subscription => { | |
:product_id => @product_id, | |
:customer_attributes => { | |
:first_name => self.first_name, | |
:last_name => self.last_name, | |
:email => self.email, | |
:reference => self.id | |
} | |
} | |
} | |
subscription = Chargify::Subscription.create(json_data[:subscription]) | |
Account.init!(subscription) unless subscription.blank? | |
end | |
ACTIVE_STATES = ['pending', 'trialing', 'assessing', 'active', 'soft_failure', 'past_due'] | |
def self.init!(subscription, account_state = 'pending') | |
user_id = subscription.customer.reference if subscription.customer | |
product_handle = subscription.product.handle if subscription.product | |
if user_id && product_handle | |
user = User.find_by_id(user_id) | |
unless user.activated? | |
user.apply_default_settings | |
user.activate! | |
end | |
end | |
end | |
def chargify_subscriber_id | |
@subscriber_id ||= Chargify::Subscription.find_by_customer_reference(user_id).id | |
end | |
def active? | |
ACTIVE_STATES.include?(self.state) || self.exempt? | |
end | |
def inactive? | |
!self.active? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment