secret

The balanced_card_uri attribute disappears when I try to access it in the model.

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.