Skip to content

Instantly share code, notes, and snippets.

@xn
Created May 24, 2011 19:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xn/989475 to your computer and use it in GitHub Desktop.
Save xn/989475 to your computer and use it in GitHub Desktop.
braintree setup
def add_credit_card_data(url)
begin
Braintree::TransparentRedirect.create_credit_card_data({
:redirect_url => url + "/user/result",:credit_card => {
:customer_id => customer_id}})
rescue Exception => exc
warn exc.message
return exc
end
end
//using this as a global variable to check in the console
var $mullet = "bob";
$('#account_create').live('click',function() {
var form = $('form#register_form');
var userdata = form.find('#user_fields input,#user_fields select').serialize();
var action = form.attr('action');
$.ajax({
cache : false,
type : 'post',
url : '/user',
datatype : 'json',
data : userdata,
success : function(data) {
$mullet = data
var transactionform = form.clone().prepend($("<input name='tr_data' type='hidden'>").attr('value', data.tr_data));
transactionform.find('#user_fields').detach();
$.ajax({
cache : false,
type : 'post',
url : action,
datatype : 'json',
data : transactionform.serialize(),
success : function(tdata) {
$.get("/user/subscribe");
return false;
}
});
return false;
},
error : function(edata) { alert("fail");}
});
return false;
});
post '/user' do
warn params.inspect
redirect(to_dashboard) if logged_in?
new_user = User.new(params[:user])
if new_user.save
new_user.login!
logged_user = login! new_user
else
errors = error_messages_for(new_user)
warn errors.inspect
errors = 'You should provide some information' if
errors.include?("can't be empty")
end
case request_type?
when :mobile then
if logged_user
redirect to_dashboard
else
flash[:error] = errors
redirect '#new'
end
else
if logged_user
[200, {:success => true, :tr_data => new_user.add_credit_card_data(env['SERVER_NAME'])}.to_json]
else
[401, { :success => false, :error => errors}.to_json]
end
end
end
.modal-info-window
%h1 Register your Account
%form#register_form{:action => Braintree::TransparentRedirect.url, :method => "post"}
%fieldset
#user_fields
%p
%label{:for => :user_email} Email
%input{:id => :user_email,
:name => 'user[email]',
:placeholder => "your@address.com",
:type => 'text', :value => ""}
%p
%label{:for => :user_password} Password
%input{:id => :user_password, :name => 'user[password]',
:placeholder => "Password",
:type => :password, :value => ""}
%p
%label{:for => :user_password_confirmation} Confirm your Password
%input.enter2submit{:id => :user_password_confirmation,
:placeholder => "Password",
:name => 'user[password_confirmation]', :type => :password,
:value => "" }
%p.full
%label
Your Subscription
%select{:name =>"user[subscription_key]"}
%option{:value => "basic", :selected => plan == "basic" ? true : false} Basic $9.95/mo
%option{:value => "premium", :selected => plan == "premium" ? true : false} Super $19.95/mo
%option{:value => "super", :selected => plan == "super" ? true : false} Premium $19.95/mo
%p
%a{:href => "/pricing", :target => "_blank", :style => "float:right"} Learn more about pricing...
#cc_fields{:style => "clear:both"}
%p.full
%label
Cardholder Name
%img{:src =>"/images/icon-lock.png"}
Secure
%input{:type =>"text", :name =>"credit_card[cardholder_name]"}
%p.full
%label
Card Number
%input{:type =>"text", :name =>"credit_card[number]", :size => 16}
%p.full
%label
Card Expiration Date
%select{:name =>"credit_card[expiration_month]"}
- Date::MONTHNAMES.compact.each_with_index do |m,i|
%option{:value => i + 1, :selected => nil}= "#{i + 1} - #{m}"
%select{:type =>"select", :name =>"credit_card[expiration_year]"}
- ((Time.now.year)..(10.years.from_now.year)).each do |y|
%option{:value => y, :selected => nil}= y
%p
%label
I accept the
%a{:href => "/about/tos", :target => "_blank"}
Terms of Service
%input{:type=>"checkbox", :name=> "tos", :value=>1, :checked=>"true"}
.buttons
%a#account_create{:href => "#", :class => "button"} CREATE AN ACCOUNT
:javascript
$(document).ready(function() {
if(!Modernizr.input.placeholder){
$("input").each(
function(){
if($(this).val()=="" && $(this).attr("placeholder")!=""){
$(this).val($(this).attr("placeholder"));
$(this).focus(function(){
if($(this).val()==$(this).attr("placeholder")) $(this).val("");
});
$(this).blur(function(){
if($(this).val()=="") $(this).val($(this).attr("placeholder"));
});
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment