Skip to content

Commit

Permalink
Continues implementiong new Stripe module
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncharnes committed Jun 11, 2017
1 parent 880e84f commit 130052a
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 140 deletions.
18 changes: 18 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
AllCops:
Exclude:
- db/migrate/*
- lib/pay/version.rb
- test/dummy/**/*
- test/test_helper.rb

Documentation:
Enabled: false

ClassAndModuleChildren:
Enabled: false

ClassVars:
Enabled: false

SpecialGlobalVars:
Enabled: false
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ gemspec
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.

# To use a debugger
gem 'byebug', group: [:development, :test]
13 changes: 8 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
PATH
remote: .
specs:
pay (0.0.19)
pay (0.0.0)
braintree (~> 2.75)
rails (>= 4.2)
stripe (~> 1.0)

Expand Down Expand Up @@ -47,6 +48,8 @@ GEM
tzinfo (~> 1.1)
arel (8.0.0)
ast (2.3.0)
braintree (2.76.0)
builder (>= 2.0.0)
builder (3.2.3)
byebug (9.0.6)
coderay (1.1.1)
Expand All @@ -62,19 +65,19 @@ GEM
i18n (0.8.4)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.5)
mail (2.6.6)
mime-types (>= 1.16, < 4)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
mini_portile2 (2.2.0)
minitest (5.10.2)
multi_json (1.12.1)
netrc (0.11.0)
nio4r (2.1.0)
nokogiri (1.7.2)
mini_portile2 (~> 2.1.0)
nokogiri (1.8.0)
mini_portile2 (~> 2.2.0)
parser (2.4.0.0)
ast (~> 2.2)
powerpack (0.1.1)
Expand Down
7 changes: 1 addition & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end

APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
APP_RAKEFILE = File.expand_path('../test/dummy/Rakefile', __FILE__)
load 'rails/tasks/engine.rake'


load 'rails/tasks/statistics.rake'



require 'bundler/gem_tasks'

require 'rake/testtask'
Expand All @@ -33,10 +30,8 @@ Rake::TestTask.new(:test) do |t|
t.verbose = false
end


task default: :test


task :console do
require 'irb'
require 'irb/completion'
Expand Down
9 changes: 1 addition & 8 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class Subscription < ApplicationRecord

# Associations
belongs_to :owner, class_name: Pay.billable_class, foreign_key: :owner_id

Expand Down Expand Up @@ -52,16 +51,10 @@ def resume
end

def processor_subscription
Stripe::Subscription.retrieve(processor_id)
owner.processor_subscription(processor_id)
end

def find_trial_ends_at(subscription)
subscription.trial_end.present? ? Time.at(subscription.trial_end) : nil
end

private

def new_stripe_subscription
owner.processor_customer.subscriptions.create(plan: processor_plan)
end
end
Empty file added db/development.sqlite3
Empty file.
22 changes: 8 additions & 14 deletions lib/pay/billable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,17 @@ module Billable
attribute :card_token, :string
end

def customer(token=nil)
def customer(token = nil)
send("#{processor}_customer", token)
end

def subscribe(name = 'default', plan = 'default', processor = 'stripe')
return if subscribed?(name)
update_card(card_token) if card_token.present?

send("create_#{processor}_subscription", name)
end

def processor_customer(token = nil)
if processor_id?
customer = Stripe::Customer.retrieve(processor_id)
update_card(token) if token.present?
end

customer
self.processor = processor
send("create_#{processor}_subscription", name, plan)
end

def update_card(token)
raise StandardError, 'No processor selected' unless processor
send("update_#{processor}_card", token)
end

Expand All @@ -52,5 +42,9 @@ def subscribed?(name = 'default', plan = nil)
def subscription(name = 'default')
subscriptions.where(name: name).last
end

def processor_subscription(subscription_id)
send("#{processor}_subscription", subscription_id)
end
end
end
110 changes: 51 additions & 59 deletions lib/pay/billable/braintree.rb
Original file line number Diff line number Diff line change
@@ -1,65 +1,57 @@
module Pay
module Billable
module Braintree

def braintree_customer(token=nil)
if processor_id?
result = Braintree::PaymentMethod.create(
customer_id: processor_id,
payment_method_nonce: card_token,
options: {make_default: true}
)

if result.success?
raise StandardError, result.inspect
else
customer = Braintree::Customer.find(processor_id)
end

else
result = Braintree::Customer.create(
email: email,
payment_method_nonce: card_token,
)

if result.success?
customer = result.customer
update(processor: "braintree", processor_id: customer.id)
else
raise StandardError, result.inspect
end

customer
end
end

def create_braintree_subscription(name="default")
token = braintree_customer.payment_methods.find{ |pm| pm.default? }.token
result = Braintree::Subscription.create(
payment_method_token: token,
plan_id: plan,
)

if result.success?
subscription = subscriptions.create(
name: name || "default",
processor: processor,
processor_id: result.subscription.id,
processor_plan: plan,
trial_ends_at: stripe_sub.trial_end.present? ? Time.at(stripe_sub.trial_end) : nil,
quantity: quantity || 1,
ends_at: nil
)
else
raise StandardError, result.inspect
end

subscription
end

def update_braintree_card(token)
end

# def braintree_customer(token = nil)
# if processor_id?
# result = ::Braintree::PaymentMethod.create(
# customer_id: processor_id,
# payment_method_nonce: token,
# options: { make_default: true }
# )

# raise StandardError, result.inspect unless result.success?
# ::Braintree::Customer.find(processor_id)
# else
# result = ::Braintree::Customer.create(
# email: email,
# payment_method_nonce: token
# )

# raise StandardError, result.inspect unless result.success?
# update(processor: 'braintree', processor_id: result.customer.id)

# result.customer
# end
# end

# def create_braintree_subscription(name = 'default')
# token = braintree_customer.payment_methods.find(&:default?).token

# result = ::Braintree::Subscription.create(
# payment_method_token: token,
# plan_id: plan
# )

# if result.success?
# subscription = subscriptions.create(
# name: name || 'default',
# processor: processor,
# processor_id: result.subscription.id,
# processor_plan: plan,
# trial_ends_at: ,
# quantity: quantity || 1,
# ends_at: nil
# )
# else
# raise StandardError, result.inspect
# end

# subscription
# end

# def update_braintree_card(token)
# # Placeholder
# end
end
end
end
69 changes: 42 additions & 27 deletions lib/pay/billable/stripe.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,64 @@
module Pay
module Billable
module Stripe
def stripe_customer(token=nil)
def stripe_customer
if processor_id?
customer = ::Stripe::Customer.retrieve(processor_id)
update_card(token) if token.present?
else
customer = ::Stripe::Customer.create(email: email, source: token)
update(processor: "stripe", processor_id: customer.id)
customer = ::Stripe::Customer.create(email: email, source: card_token)
update(processor: 'stripe', processor_id: customer.id)
end

customer
end

def create_stripe_subscription(name="default")
stripe_sub = stripe_customer.subscriptions.create(plan: plan)
def create_stripe_subscription(name, plan)
stripe_sub = stripe_customer.subscriptions.create(plan: plan)
subscription = create_subscription(stripe_sub, 'stripe', name, plan)
subscription
end

def update_stripe_card(token)
customer = stripe_customer
token = ::Stripe::Token.retrieve(token)

return if token.card.id == customer.default_source
save_stripe_card(token, customer)
end

def stripe_subscription(subscription_id)
Stripe::Subscription.retrieve(subscription_id)
end

private

subscription = subscriptions.create(
name: name || "default",
def save_stripe_card(token, customer)
card = customer.sources.create(source: token.id)
customer.default_source = card.id
customer.save

update(
card_brand: card.brand,
card_last4: card.last4,
card_exp_month: card.exp_month,
card_exp_year: card.exp_year
)
end

def create_subscription(subscription, processor, name, plan)
subscriptions.create(
name: name || 'default',
processor: processor,
processor_id: stripe_sub.id,
processor_id: subscription.id,
processor_plan: plan,
trial_ends_at: stripe_sub.trial_end.present? ? Time.at(stripe_sub.trial_end) : nil,
trial_ends_at: trial_end_date(subscription),
quantity: quantity || 1,
ends_at: nil
)
subscription
end

def update_stripe_card(token)
customer = stripe_customer
token = ::Stripe::Token.retrieve(token)

if token.card.id != customer.default_source
card = customer.sources.create(source: token.id)
customer.default_source = card.id
customer.save

update(
card_brand: card.brand,
card_last4: card.last4,
card_exp_month: card.exp_month,
card_exp_year: card.exp_year
)
end
def trial_end_date(stripe_sub)
stripe_sub.trial_end.present? ? Time.at(stripe_sub.trial_end) : nil
end
end
end
Expand Down
Loading

0 comments on commit 130052a

Please sign in to comment.