Attribute Accessor
Attribute Accessor
Attribute Accessor
Attribute Accessor
To deduct amount from user's credit card
# File app/models/payment.rb, line 101 def self.charge user, amount_cents, resource = nil return true unless Configurations::BalancedPayment.enabled? total = ((1.029 * amount_cents) + 30).round(0) if resource.is_a? Transaction resource_description = "charged against DirectTransaction - #{resource.id}" elsif resource.is_a? Invoice resource_description = "charged against manual invoice - #{resource.id}" else resource_description = "For Session - #{resource.class.name} - #{resource.id}" end begin response = user.credit_card.balanced_card.debit( :amount => total.to_i, :description => (resource ? "#{Configurations::General.application_name} credits. #{resource_description}" : "#{Configurations::General.application_name} credits.") ) AccountBalance.credit(user, resource, false, amount_cents.round(0), user.credit_card) save_payment!(response, user, resource) rescue nil return true rescue Exception => e save_failed_payment!(e, user, total, 'debit', resource) return false end end
It will create the student for Balanced.
# File app/models/payment.rb, line 75 def self.create_new_student(user) return true unless Configurations::BalancedPayment.enabled? student = Balanced::Student.new( :name => user.name, :email => user.email ).save return student end
To find the student if not present it will create the student for Balanced.
# File app/models/payment.rb, line 60 def self.find_or_create_student(user) return true unless Configurations::BalancedPayment.enabled? if user.balanced_student_uri? begin Rails.logger.debug("finding student with uri- #{user.balanced_student_uri}") return Balanced::Student.find(user.balanced_student_uri) rescue create_new_student(user) end else create_new_student(user) end end
To charge user from his account balance when sufficient balance is there else charge it from credit card.
# File app/models/payment.rb, line 128 def self.populate_credits resource return true unless Configurations::BalancedPayment.enabled? begin if resource.is_a? TutoringSession rate = resource.cost_in_cents else rate = resource.chargeable_amount.cents end payer = resource.user has_amount = payer.balance_amount.cents if has_amount < rate has_amount = payer.total_balance.cents if has_amount < rate if !(charge payer, (rate-has_amount), resource) raise "Couldn't charge card." end end end AccountBalance.debit(payer, resource, nil, rate) rescue => e return false end end
To save failed payment details
# File app/models/payment.rb, line 162 def self.save_failed_payment!(response, user, amount, type, resource = nil) payment = Payment.new payment.payment_for = resource if resource.present? payment.user = user payment.status = 'failed' payment.amount = amount payment._type = type payment.update_payment_attributes(response) payment.save(:validate => false) end
To save payment details
# File app/models/payment.rb, line 153 def self.save_payment!(response, user, resource = nil) payment = Payment.new payment.payment_for = resource if resource.present? payment.user = user payment.update_payment_attributes(response) payment.save(:validate => false) end
To withdraw amoun from users account
# File app/models/payment.rb, line 85 def self.withdraw(user, amount , desc) return true unless Configurations::BalancedPayment.enabled? payee = user begin response = payee.bank_account.balanced_bank_account.credit( :amount => (amount.cents - 25), :description => "#{Configurations::General.application_name} - #{desc}" ) save_payment!(response, payee) rescue nil rescue => e save_failed_payment!(e, payee, amount.cents, 'credit') return false end end
To add New bank account of user.
# File app/models/payment.rb, line 38 def add_new_bank_for(user) return true unless Configurations::BalancedPayment.enabled? begin student = self.class.find_or_create_student(user) account_hash = {} [:name, :bank_code, :account_number, :type].each do |key| account_hash.merge!(key => self.send(key)) end bank = Balanced::BankAccount.new(account_hash).save student.add_bank_account(bank) user.balanced_student_uri = student.uri user.bank_uri = bank.uri user.save(:validate => false) return true rescue => e puts e.inspect self.errors.add(:base, 'Invalid Bank details.') return false end end
Returns true if failed transaction is eligible for repayment
# File app/models/payment.rb, line 212 def can_be_sent_for_repayment? self.failed_credit? and self.payment_for.present? and self.payment_for.invoice.paid? end
Returns true when payment type is credit
# File app/models/payment.rb, line 189 def credit? self._type == 'credit' end
Returns true when payment type is debit
# File app/models/payment.rb, line 194 def debit? self._type == 'debit' end
Returns true is status of credit is failed
# File app/models/payment.rb, line 217 def failed_credit? self.credit? and self.status == 'failed' end
To reload transaction from balanced payment
# File app/models/payment.rb, line 199 def reload_from_balanced_payments! if self.credit? @transaction = Balanced::Credit.find(self.uri) elsif self.debit? @transaction = Balanced::Debit.find(self.uri) end if @transaction self.update_payment_attributes(response) self.save end end
Update table if failed transaction successfully sent for repayment
# File app/models/payment.rb, line 222 def resend! self.payment_for.invoice.update_column(:paid, nil) if self.can_be_sent_for_repayment? return true end
To update payment attributes
# File app/models/payment.rb, line 174 def update_payment_attributes(response) if response.is_a?(Balanced::Credit) || response.is_a?(Balanced::Debit) self._type = response.is_a?(Balanced::Credit) ? 'credit' : 'debit' self.uri = response.attributes["href"] self.amount = response.attributes["amount"] self.status = response.attributes["status"] self.transaction_id = response.attributes["id"] self.description = response.attributes["description"] elsif response.is_a? Balanced::PaymentRequired self.transaction_id = response.request_id self.description = "#{response.category_code} - #{response.status_code} : #{response.additional}" end end