Skip to content

Instantly share code, notes, and snippets.

@ahmed1490
Forked from tanookiben/BalancedPayments.md
Created February 21, 2014 18:19
Show Gist options
  • Save ahmed1490/9140032 to your computer and use it in GitHub Desktop.
Save ahmed1490/9140032 to your computer and use it in GitHub Desktop.
= form_tag "/store_bank_account", :method => :post, :id => "bank-account-form", :class => "form-horizontal" do
.control-group
= label_tag "ba-name", "Account Holder's Name", :class => "control-label"
.controls
= text_field_tag "ba-name", nil, :placeholder => "Account Holder's Name", :class => "ba-name", :autocomplete => "off"
.control-group
= label_tag "ba-rn", "Routing Number", :class => "control-label"
.controls
= text_field_tag "ba-rn", nil, :placeholder => "Routing Number", :class => "ba-rn", :autocomplete => "off"
.control-group
= label_tag "ba-an", "Account Number", :class => "control-label"
.controls
= text_field_tag "ba-an", nil, :placeholder => "Account Number", :class => "ba-an", :autocomplete => "off"
.control-group
= label_tag "ba-type", "Type", :class => "control-label"
.controls
= select_tag "ba-type", "<option value='' disabled selected style='display:none;'>Select Account Type</option><option value=\"checking\">CHECKING</option><option value=\"savings\">SAVINGS</option>".html_safe
.control-group
.controls
= submit_tag "Submit", :class => "btn btn-primary btn-large"
= form_tag "/store_credit_card", :method => :post, :id => "credit-card-form", :class => "form-horizontal" do
.control-group
= label_tag "cc-number", "Card Number", :class => "control-label"
.controls
= text_field_tag "cc-number", nil, :placeholder => "Enter Credit Card Number", :class => "cc-number", :autocomplete => "off"
.control-group
= label_tag "cc-em", "Expiration", :class => "control-label"
.controls
= text_field_tag "cc-em", nil, :placeholder => "Expiration Month", :class => "cc-em", :autocomplete => "off"
= text_field_tag "cc-em", nil, :placeholder => "Expiration Year", :class => "cc-ey", :autocomplete => "off"
.control-group
= label_tag "cc-csc", "Security Code", :class => "control-label"
.controls
= text_field_tag "cc-csc", nil, :placeholder => "Security Code", :class => "cc-csc", :autocomplete => "off"
.control-group
.controls
= submit_tag "Submit", :class => "btn btn-primary btn-large"
class BalancedController < ApplicationController
def create_balanced_account
current_user.balanced_account_uri = Balanced::Marketplace.my_marketplace.create_account(:email => current_user.email, :type => 'person').uri
current_user.save
# add a redirect to the desired path
end
def store_credit_card
balanced_account = Balanced::Account.find(current_user.balanced_account_uri)
balanced_account.add_card(params[:balancedCreditCardURI])
# add a redirect to the desired path
end
def store_bank_account
balanced_account = Balanced::Account.find(current_user.balanced_account_uri)
balanced_account.add_card(params[:balancedBankAccountURI])
# add a redirect to the desired path
end
def process_payment
balanced_account = Balanced::Account.find(current_user.balanced_account_uri)
account.debit(
:amount => 1000, # or params[:amount]
:description => "Balanced Payments transaction",
:appears_on_statement_as => "Balanced Payments")
# add a redirect to the desired path
end
end

Getting Balanced Payments up and running.

Files

  • balanced_controller.rb
  • _bank_account_form.html.haml
  • _credit_card_form.html.haml
  • bank_account_submission.js
  • credit_card_submission.js

Requirements

  • a User model with a field for a :balanced_account_uri string
  • appropriate routes for the methods in balanced_controller.rb
  • replacement of the placeholder text in the .js files for the Balanced marketplace uri
var marketplaceUri = 'YOUR MARKETPLACE URI HERE';
var requestBinUrl = '/store_bank_account'
var debug = function(tag, content) {
$('<' + tag + '>' + content + '</' + tag + '>').appendTo('#result');
};
try {
balanced.init(marketplaceUri);
} catch (e) {
debug('code', 'balanced.init error!');
}
function balancedCallback(response) {
var tag = (response.status < 300) ? 'pre' : 'code';
debug(tag, JSON.stringify(response));
switch(response.status) {
case 201:
console.log(response.data);
var $form = $("#bank-account-form");
var bank_account_uri = response.data['uri'];
$('<input>').attr({
type: 'hidden',
value: bank_account_uri,
name: 'balancedBankAccountURI'
}).appendTo($form);
$form.attr({action: requestBinUrl});
$form.get(0).submit();
break;
case 400:
console.log(response.error);
break;
case 404:
console.log(response.error);
break;
}
}
var tokenizeBankAccount = function(e) {
e.preventDefault();
var $form = $('#bank-account-form');
var bankAccountData = {
name: $form.find('.ba-name').val(),
account_number: $form.find('.ba-an').val(),
bank_code: $form.find('.ba-rn').val(),
type: $form.find('select').val()
};
balanced.bankAccount.create(bankAccountData, balancedCallback);
};
$(function(){
$('#bank-account-form').submit(tokenizeBankAccount);
});
var marketplaceUri = 'YOUR MARKETPLACE URI HERE';
var requestBinUrl = '/store_credit_card'
var debug = function(tag, content) {
$('<' + tag + '>' + content + '</' + tag + '>').appendTo('#result');
};
try {
balanced.init(marketplaceUri);
} catch (e) {
debug('code', 'balanced.init error!');
}
function balancedCallback(response) {
var tag = (response.status < 300) ? 'pre' : 'code';
debug(tag, JSON.stringify(response));
switch(response.status) {
case 201:
console.log(response.data);
var $form = $("#credit-card-form");
var card_token_uri = response.data['uri'];
$('<input>').attr({
type: 'hidden',
value: card_token_uri,
name: 'balancedCreditCardURI'
}).appendTo($form);
$form.attr({action: requestBinUrl});
$form.get(0).submit();
break;
case 400:
console.log(response.error);
break;
case 404:
console.log(response.error);
break;
}
}
var tokenizeCreditCard = function(e) {
e.preventDefault();
var $form = $('#credit-card-form');
var creditCardData = {
card_number: $form.find('.cc-number').val(),
expiration_month: $form.find('.cc-em').val(),
expiration_year: $form.find('.cc-ey').val(),
security_code: $form.find('.cc-csc').val()
};
balanced.card.create(creditCardData, balancedCallback);
};
$(function(){
$('#credit-card-form').submit(tokenizeCreditCard);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment