Skip to content

Instantly share code, notes, and snippets.

Created March 6, 2013 17:45
Show Gist options
  • Save anonymous/5101366 to your computer and use it in GitHub Desktop.
Save anonymous/5101366 to your computer and use it in GitHub Desktop.
def merchant
after_redirection = "#{root_url}balanced/success"
current_user = User.find(session[:user_id])
begin
existing=Balanced::Account.find_by_email(current_user.email)
if existing.nil?
@market = marketplace_creation
marketplace = @market
bank_account = marketplace.create_bank_account(
:account_number => params[:account_number],
:bank_code => params[:bankcode],
:name => current_user.full_name
)
merchant = marketplace.create_merchant(
:email_address => current_user.email,
:merchant => {
:type => params[:account_type],
:name => current_user.full_name,
:street_address => current_user.street,
:postal_code => current_user.zipcode,
:dob => current_user.date_of_birth,
:phone_number => params[:phone_number],
},
:bank_account_uri => bank_account.uri,
:name => current_user.full_name
)
else
existing.promote_to_merchant(
{
:type => params[:account_type],
:name => current_user.full_name,
:street_address => current_user.street,
:postal_code => current_user.zipcode,
:dob => current_user.date_of_birth,
:phone_number => params[:phone_number],
}
)
end
respond_to do |format|
format.html {
@user=User.find(session[:user_id])
@user.update_attribute("bank_active",true)
redirect_to '/public/bank_info?bank=success'
flash[:success] = "Bank information successfully updated" }
format.json {render :json => {:success => "Bank information successfully updated"}}
end
rescue Balanced::MoreInformationRequired => ex
redirect_to ex.redirect_uri + '?redirect_uri=' + after_redirection
flash[:success] = "Bank info saved"
rescue Balanced::Error => error
puts error.message.inspect
respond_to do |format|
format.html { redirect_to edit_user_path(session[:user_id])
flash[:error] = "merchant account not created, please check your bank info"}
format.json {render :json => {:error => "merchant account not created, please check your bank info"}}
end
end
end
def success
@user=User.find(session[:user_id])
unless params["merchant_uri"].nil?
@market = marketplace_creation
marketplace = @market
existing=Balanced::Account.find_by_email(params["email_address"])
if existing.nil?
merchant = marketplace.create_merchant(
:email_address => params["email_address"]
:merchant => params["merchant_uri"],
:name => current_user.full_name
)
else
existing.promote_to_merchant(params["merchant_uri"])
end
@user.update_attribute("bank_active",true)
redirect_to '/public/bank_info?bank=success'
flash[:success] = "Bank information successfully updated"
else
redirect_to edit_user_path(session[:user_id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment