Skip to content

Instantly share code, notes, and snippets.

@JamesDullaghan
Created July 28, 2014 19:28
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 JamesDullaghan/ff23b92421cbfe4c2e9e to your computer and use it in GitHub Desktop.
Save JamesDullaghan/ff23b92421cbfe4c2e9e to your computer and use it in GitHub Desktop.
Balanced Bank Account Customer issue
# Model:
def create_business_customer(data)
customer = Balanced::Customer.new(
address: {
line1: data[:address],
city: data[:city],
state: data[:state],
postal_code: data[:state]
},
business_name: data[:business_name],
ein: data[:ein],
email: data[:email],
phone: data[:phone]
)
bank_account = fetch_bank_account
bank_account.associate_to_customer(customer.href)
self.balanced_account = customer.href
self.save
end
def setup_bank_account(data)
unstore_bank_account if balanced_bank_account_uri.present?
create_bank_account(data)
verify_bank_account
end
def create_bank_account(data)
bank_account = Balanced::BankAccount.fetch(data[:bank_token])
self.balanced_bank_account_uri = bank_account.href
self.save
end
def fetch_bank_account
balanced_bank_account_uri ? Balanced::BankAccount.fetch(balanced_bank_account_uri) : nil
end
def unstore_bank_account
bank_account = fetch_bank_account
if bank_account.present?
bank_account.unstore
self.balanced_bank_account_uri = nil
self.balanced_verification_uri = nil
self.bank_account_verified = false
self.save
end
end
# Controller:
def create
@kennel.setup_bank_account(params[:bank])
@kennel.create_business_customer(customer_params)
if @kennel.save
redirect_to dashboard_bankings_path, notice: "Successfully added bank account. You still need to verify the account."
else
flash[:error] = @kennel.errors.full_messages
render :new
end
end
# bank account uri and bank account verification uri are stored.
# customer returns
#<Balanced::Customer:0x007fea9ed2c798 @attributes={"address"=>{"line1"=>"1234 Main St.", "city"=>"San Diego", "state"=>"CA", "postal_code"=>"92024"}, "business_name"=>"Testing", "ein"=>"123456789", "phone"=>"8881231234", :href=>"customers"}, @hyperlinks={}>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment