-
-
Save taylorbrooks/ee0b27bebe14d88417d9 to your computer and use it in GitHub Desktop.
The balanced_card_uri attribute disappears when I try to access it in the model.
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
Error = https://img.skitch.com/20120916-fpmxabwg6m4ys3y84rkti615iq.jpg | |
# customers_controller.rb | |
def create | |
@customer = current_account.customers.find_or_initialize_by_repeat_donator(params[:customer]) | |
if @customer.save | |
puts "Donation successful!" | |
render :thank_you | |
else | |
render :new | |
end | |
end | |
# customer.rb | |
class Customer < ActiveRecord::Base | |
module HandleRepeatDonators | |
# If a customer exists (based on their email address), | |
# we add a new donation record that belongs to the existing customers | |
# Otherwise, we just create everything, straight up. | |
def find_or_initialize_by_repeat_donator attrs | |
# Don't create donation records twice, thanks | |
customer = attrs.dup | |
customer.delete :donations | |
# Note, the ".tap" is important. | |
# Rails only executes the given block for find_or_initialize_by_attr if the record wasn't found, | |
# but we want to create a donation every time, | |
# whether we created a new customer, or found an existing one | |
# | |
# ".tap" is like, take "self" (the current variable) and yield it to this block | |
find_or_initialize_by_email(attrs[:email], customer).tap do |customer| | |
customer.recurring = attrs[:recurring] | |
#raise 'fuck1' | |
# At this point, if we "found" the customer, | |
# then the donation was not created | |
# | |
# but if we "created" the customer, above, then | |
# accepts_nested_attributes_for :donations | |
# took care of the magic for us | |
if customer.recurring | |
customer.recurring_amount = attrs[:donations][:amount] | |
else | |
customer.donations.build(attrs[:donations]) | |
end if attrs[:donations] | |
end | |
end | |
end | |
belongs_to :account | |
has_many :donations | |
# Include default devise modules. Others available are: | |
# :token_authenticatable, :lockable, :timeoutable, :omniauthable, :registerable, | |
devise :database_authenticatable, :encryptable, :recoverable, :trackable, :rememberable, :confirmable, :authentication_keys => [:email, :account_id] | |
# Setup accessible (or protected) attributes for your model | |
attr_accessible :first_name, :last_name, :balanced_card_uri, :email, :password, :password_confirmation, :remember_me | |
attr_protected :account_id | |
attr_accessor :recurring, :recurring_amount, :balanced_card_uri | |
before_save :balanced_customer | |
before_save :handle_recurring_donations, :if => :recurring | |
validates :first_name, :last_name, :email, :presence => true | |
# Devise multi-step signup | |
def password_required? | |
super if confirmed? | |
end | |
def password_match? | |
self.errors[:password] << "can't be blank" if password.blank? | |
self.errors[:password_confirmation] << "can't be blank" if password_confirmation.blank? | |
self.errors[:password_confirmation] << "does not match password" if password != password_confirmation | |
password == password_confirmation && !password.blank? | |
end | |
def name | |
"#{first_name} #{last_name}".strip | |
end | |
def recurring? | |
recurring || plan.present? | |
end | |
def recurring= recurring | |
@recurring = recurring.to_i > 0 | |
end | |
def balanced_customer | |
raise "Balanced Card: #{self.balanced_card_uri} Email: #{self.email}" | |
@balanced_customer ||= balanced_account_uri.blank? ? create_balanced_customer : retrieve_balanced_customer | |
raise 'fuck again' | |
self.balanced_account_uri = @balanced_account.uri | |
self.last_4 = @balanced_customer.cards.first.last_4 rescue nil | |
@balanced_customer | |
end | |
protected | |
def create_balanced_customer | |
card_uri = self.balanced_card_uri.to_s | |
card_url = balanced_card_uri | |
begin | |
buyer = Balanced::Marketplace.my_marketplace.create_buyer(self.email, card_uri) | |
rescue Balanced::Conflict => ex | |
unless ex.category_code == 'duplicate-email-address' | |
raise | |
end | |
raise 'faaaack' | |
# notice extras? it includes some helpful additionals. | |
puts "This account already exists on Balanced! Here it is #{ex.extras[:account_uri]}" | |
buyer = Balanced::Account.find ex.extras[:account_uri] | |
buyer.add_card card_uri | |
rescue Balanced::BadRequest => ex | |
# what exactly went wrong? | |
puts ex | |
raise | |
end | |
end | |
def handle_recurring_donations | |
plan_amount = recurring_amount.to_i/100.00 | |
plan = "Monthly Donation to #{self.account.name} for #{self.name} for #{plan_amount}" | |
Stripe::Plan.create( | |
amount: self.recurring_amount, | |
currency: 'usd', | |
id: plan.parameterize('-'), | |
interval: 'month', | |
name: plan | |
) | |
plan = plan.parameterize('-') | |
c = Stripe::Customer.retrieve("#{self.stripe_customer_id}") | |
c.update_subscription(plan: plan, prorate: false) | |
self.plan = plan | |
rescue Stripe::StripeError => e | |
# plan already exists, but no worries | |
puts "#{e}" | |
puts 'stripe exception' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment