Created
June 20, 2011 17:37
-
-
Save jch/1036093 to your computer and use it in GitHub Desktop.
chargify_mess.rb
This file contains hidden or 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 add_to_chargify(user, credit_card_attributes) | |
credit_card_attributes.symbolize_keys! | |
customer = Chargify::Customer.create({ | |
:first_name => credit_card_attributes[:first_name], | |
:last_name => credit_card_attributes[:last_name], | |
:email => user.email, | |
:reference => self.id # one payer per account, so Account is customer_reference, not User | |
}) | |
# TODO: major error handling needed here... | |
puts customer.errors.full_messages | |
puts '========' | |
subscription = Chargify::Subscription.create({ | |
:customer_reference => customer.reference, | |
:product_handle => 'social-spring' | |
}) | |
# have to do this in 2 separate steps or else chargify fails | |
subscription.credit_card_attributes = credit_card_attributes | |
subscription.save | |
puts subscription.errors.full_messages | |
puts '=========' | |
# UGH: can't create components before subscription has an id | |
quantity = self.users.count | |
subscription.components = self.apps.map {|app| | |
{ | |
:component_id => app.id, | |
:allocated_quantity => quantity | |
} | |
} | |
subscription.save | |
puts subscription.errors.full_messages | |
puts '=======' | |
# UGH: the allocated_quantity is ignored the first time you create it | |
# have to re-save before it takes effect | |
s.components.each do |c| | |
c.allocated_quantity = quantity | |
c.save | |
end | |
subscription | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment