Skip to content

Instantly share code, notes, and snippets.

@jeffAwesome
Created August 9, 2015 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffAwesome/9c762d6bf8849280a7df to your computer and use it in GitHub Desktop.
Save jeffAwesome/9c762d6bf8849280a7df to your computer and use it in GitHub Desktop.
class PaymentGateway
def initialize(customer, gatewayCustomer = Balanced::Customer)
@GatewayCustomer = gatewayCustomer
@customer = customer
end
def customer
#return @GatewayCustomer.find(@customer.customer_uri) if @customer.customer_uri
# if balanced customer doesn't already exist... lets make one
return self.create_customer unless @customer.customer_uri
if @customer.customer_uri
customer = @GatewayCustomer.find(@customer.customer_uri)
self.associate_bank unless customer.attributes['customers.bank_accounts']
return customer
end
end
def create_customer
dob = @customer.dob.split('/')
customer = @GatewayCustomer.new(
:name => @customer.first_name + " " + @customer.last_name,
:email => @customer.email,
:ssn_last4 => @customer.ssn.split(//).last(4).join,
:dob_month => dob[0],
:dob_year => dob[2],
:address => {
:city => @customer.city,
:state => @customer.state,
:postal_code => @customer.zip,
:line1 => @customer.street_address
}
)
customer.save
@customer.customer_uri = customer.attributes['href']
@customer.save
customer
end
def admin_customer
#return @GatewayCustomer.find(@customer.customer_uri) if @customer.customer_uri
# if balanced customer doesn't already exist... lets make one
return self.create_admin_customer unless @customer.customer_uri
if @customer.customer_uri
customer = @GatewayCustomer.find(@customer.customer_uri)
self.associate_bank unless customer.attributes['customers.bank_accounts']
return customer
end
end
def create_admin_customer
begin
customer = @GatewayCustomer.new(
:name => @customer.full_name,
:email => @customer.email,
:business_name => @customer.office_name,
:ein => @customer.ein,
:address => {
:city => @customer.city,
:state => @customer.state,
:postal_code => @customer.zip,
:line1 => @customer.street_address
}
)
customer.save
@customer.customer_uri = customer.attributes['href']
@customer.save
customer
rescue => e
puts e
puts 'There was an error saving the admin'
end
end
def set_bank
if @customer.bank_uri.present? && @customer.customer_uri.present?
customer = @GatewayCustomer.find(@customer.customer_uri)
bank_account = Balanced::BankAccount.fetch(@customer.bank_uri)
validate_bank
else
@customer.bank_uri
end
end
def associate_bank
if @customer.bank_uri.present? && @customer.customer_uri.present?
bank_account = Balanced::BankAccount.fetch(@customer.bank_uri)
bank_account.associate_to_customer(@customer.customer_uri)
end
end
def validate_bank
if @customer.bank_uri != nil
bank_account = Balanced::BankAccount.find(@customer.bank_uri)
if @customer.verification_uri.nil?
self.associate_bank
verification = bank_account.verify
@customer.verification_uri = verification.attributes['href']
@customer.save!
verification
end
else
return false
end
end
def bank_verified?
verification = Balanced::BankAccountVerification.fetch(@customer.verification_uri)
if verification.attributes['verification_status'] != 'succeeded'
@customer.bank_verified = verification.attributes['verification_status']
@customer.save!
return false
else
@customer.bank_verified = verification.attributes['verification_status']
@customer.save!
return true
end
end
def verify_bank(v1, v2)
verification = Balanced::BankAccountVerification.fetch(@customer.verification_uri)
verification.confirm(amount_1 = v1.to_i, amount_2 = v2.to_i)
end
def bank_verification
if @customer.has_bank?
bank = Balanced::BankAccount.find(@customer.bank_uri)
verification = Balanced::Verification.find(bank.verification_uri)
end
end
def locked_out
if @customer.has_bank? && @customer.bank_verified == "failed"
@customer.reset_bank
true
else
false
end
end
def is_verified?
if @customer.has_bank?
if @customer.bank_verified == "succeeded"
true
else
false
end
end
end
#payments
def pay(payment, encounter, the_payment)
bank_account = Balanced::BankAccount.fetch(@customer.bank_uri)
#merchant_customer = @GatewayCustomer.fetch(@customer.customer_uri)
office = Admin.find(encounter.office_id)
merchant_customer = @GatewayCustomer.fetch(office.customer_uri)
patient = Patient.find(payment.patient_id)
begin
order = merchant_customer.create_order(
:description => "For Payment Plan #"+encounter.id.to_s
)
payment.order_uri = order.attributes['href']
paymentCall = order.debit_from(
:source => bank_account,
:amount => the_payment,
:appears_on_statment_as => "PaymentOak-pay",
:description => "Payment for procedure " + encounter.title
)
payment.set_payment_attributes(paymentCall)
rescue => e
payment.status = "failed"
payment.attempted_amount = payment.update_attempts
payment.save
PaymentNotificationsWorker.perform_async(payment.patient_id, payment.id)
Error.new({description: e.to_s, patient_id: patient.id, encounter_id: encounter.id, office_id: patient.office_id, payment_id: payment.id }).save()
puts e
e
end
end
def send_payment(payment, payment_amount, encounter, office)
bank_account = Balanced::BankAccount.fetch(office.bank_uri)
begin
merchant_customer = @GatewayCustomer.fetch(office.customer_uri)
#credit = bank_account.credit(
# :amount => payment_amount,
# :description => "Patients payment For Payment Plan #"+encounter.id.to_s
#)
#order = merchant_customer.create_order(
# :description => "Patients payment For Payment Plan #"+encounter.id.to_s,
# :amount => payment_amount,
# :meta => {
# 'credit' => encounter.id.to_s
# }
#)
order = Balanced::Order.fetch(payment.order_uri)
order.credit_to(
:destination => bank_account,
:appears_on_statment_as => "POak-Payment",
:amount => payment_amount
)
payment.set_paid_status
puts "hey this should be good to go"
rescue => e
puts e
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment