Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class User < ActiveRecord::Base
# new columns need to be added here to be writable through mass assignment
attr_accessible :username, :email, :password, :password_confirmation,
:admin, :stripe_card_token, :stripe_card_type,
:admin, :stripe_user_id, :stripe_card_token, :stripe_card_type,
:stripe_card_digits, :stripe_card_expiry

attr_accessor :stripe_card_token

has_many :invoices

attr_accessor :password
Expand Down Expand Up @@ -44,7 +46,7 @@ def create_signup_token
end

def has_card?
stripe_card_token.present?
stripe_user_id.present?
end

private
Expand Down
1 change: 0 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "signup_token"
t.string "stripe_card_token"
t.string "stripe_user_id"
t.string "stripe_card_type"
t.string "stripe_card_digits"
Expand Down
5 changes: 3 additions & 2 deletions features/support/timeout.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
def retry_on_timeout(n = 3, &block)
def retry_on_timeout(n = 20, &block)
block.call
rescue Capybara::TimeoutError, Capybara::ElementNotFound => e
rescue Exception => e
if n > 0
puts "Catched error: #{e.message}. #{n-1} more attempts."
sleep(0.1) #stops it from timing out so easily on a slower computer
retry_on_timeout(n - 1, &block)
else
raise
Expand Down
7 changes: 4 additions & 3 deletions lib/user_activator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ def initialize(signup_token, params)

def activate
@user.signup_token = nil
@user.stripe_user_id = create_stripe_customer if @params[:stripe_card_token]
card_token = @params.delete(:stripe_card_token)
@user.stripe_user_id = create_stripe_customer(card_token) unless card_token.blank?
@user.update_attributes(@params)
@user
end

private

def create_stripe_customer
def create_stripe_customer(card_token)
response = Stripe::Customer.create(
:description => "#{@user.username}",
:email => "#{@user.email}",
:card => @params.delete(:stripe_card_token) )
:card => card_token )
response.id
end

Expand Down
1 change: 1 addition & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
end

factory :user_with_card do
stripe_user_id "cus_15MdST1exra0SF"
stripe_card_token "tok_sometoken"
stripe_card_type "Visa"
stripe_card_digits "4242"
Expand Down
5 changes: 4 additions & 1 deletion spec/lib/user_activator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
end

context "when no card data is provided" do
let(:user_params) { { password: '123456', password_confirmation: '123456'} }
let(:user_params) { { password: '123456', password_confirmation: '123456',
stripe_card_token: "",
stripe_card_type: "", stripe_card_digits: "",
stripe_card_expiry: ""} }

it 'should not try to create the stripe user' do
UserActivator.any_instance.expects(:create_stripe_customer).never
Expand Down