Matchu
d97c32b5da
Some important little upgrades but mostly straightforward! Note that there's still a known issue where item searches crash, I was hoping that this was a bug in Rails 4.2 that would be fixed on upgading to 5, but nope, oh well! Also uhh I just got a bit silly and didn't actually mean to go all the way to 5.2 in one go, I had meant to start at 5.0… but tbh the 5.1 and 5.2 changes seem small, and this seems to be working, so. Yeah ok let's roll!
69 lines
1.7 KiB
Ruby
69 lines
1.7 KiB
Ruby
class Donation < ApplicationRecord
|
|
FEATURE_COST = 500 # in cents = $5.00
|
|
|
|
belongs_to :campaign
|
|
belongs_to :user
|
|
has_many :features, class_name: 'DonationFeature'
|
|
|
|
def to_param
|
|
"#{id}-#{secret}"
|
|
end
|
|
|
|
def self.create_from_charge(campaign, user, params)
|
|
amount = (BigDecimal.new(params[:amount]) * 100).floor
|
|
|
|
campaign.progress += amount
|
|
|
|
charge_params = {
|
|
amount: amount,
|
|
description: 'Donation (thank you!)',
|
|
currency: 'usd'
|
|
}
|
|
|
|
if params[:stripe_token_type] == 'card'
|
|
customer = Stripe::Customer.create(
|
|
card: params[:stripe_token]
|
|
)
|
|
charge_params[:customer] = customer.id
|
|
elsif params[:stripe_token_type] == 'bitcoin_receiver'
|
|
charge_params[:card] = params[:stripe_token]
|
|
else
|
|
raise ArgumentError, "unexpected stripe token type #{params[:stripe_token_type]}"
|
|
end
|
|
|
|
charge = Stripe::Charge.create(charge_params)
|
|
|
|
donation = campaign.donations.build
|
|
donation.amount = amount
|
|
donation.charge_id = charge.id
|
|
donation.user = user
|
|
donation.donor_name = user.try(:name)
|
|
donation.donor_email = params[:donor_email]
|
|
donation.secret = new_secret
|
|
|
|
num_features = amount / FEATURE_COST
|
|
features = []
|
|
num_features.times do
|
|
features << donation.features.new
|
|
end
|
|
|
|
Donation.transaction do
|
|
campaign.save!
|
|
donation.save!
|
|
features.each(&:save!)
|
|
end
|
|
|
|
DonationMailer.thank_you_email(donation, donation.donor_email).deliver
|
|
|
|
donation
|
|
end
|
|
|
|
def self.new_secret
|
|
SecureRandom.urlsafe_base64 8
|
|
end
|
|
|
|
def self.from_param(param)
|
|
id, secret = param.split('-', 2)
|
|
self.where(secret: secret).find(id)
|
|
end
|
|
end
|