Skip to content

Commit

Permalink
Braintree WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed May 13, 2017
1 parent 4cc4fd4 commit 6403db0
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ gemspec
# your gem to rubygems.org.

# To use a debugger
# gem 'byebug', group: [:development, :test]
gem 'byebug', group: [:development, :test]
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ GEM
tzinfo (~> 1.1)
arel (7.1.4)
builder (3.2.3)
byebug (9.0.6)
concurrent-ruby (1.0.5)
dante (0.2.0)
domain_name (0.5.20170223)
Expand Down Expand Up @@ -132,7 +133,7 @@ PLATFORMS
ruby

DEPENDENCIES
activemodel-associations
byebug
pay!
sqlite3
stripe-ruby-mock (~> 2.4)
Expand Down
3 changes: 3 additions & 0 deletions lib/pay/billable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def customer(token=nil)
end

def create_subscription(name="default", processor="stripe")
return if subscribed?(name)
update_card(card_token) if card_token.present?

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

Expand Down
59 changes: 59 additions & 0 deletions lib/pay/billable/braintree.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
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

end
end
end
3 changes: 0 additions & 3 deletions lib/pay/billable/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ def stripe_customer(token=nil)
end

def create_stripe_subscription(name="default")
return if subscribed?(name)

update_card(card_token) if card_token.present?
stripe_sub = stripe_customer.subscriptions.create(plan: plan)

subscription = subscriptions.create(
Expand Down
1 change: 1 addition & 0 deletions pay.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Gem::Specification.new do |s|

s.add_dependency "rails", ">= 4.2"
s.add_dependency "stripe", "~> 1.0"
s.add_dependency "braintree", "~> 2.75"

s.add_development_dependency 'sqlite3'
s.add_development_dependency 'stripe-ruby-mock', '~> 2.4'
Expand Down
10 changes: 5 additions & 5 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
ActiveSupport::TestCase.fixtures :all
end

require 'stripe_mock'
Braintree::Configuration.environment = :development
Braintree::Configuration.merchant_id = "integration_merchant_id"
Braintree::Configuration.public_key = "integration_public_key"
Braintree::Configuration.private_key = "integration_private_key"

require 'stripe_mock'
class DbTest < ActiveSupport::TestCase
setup do
StripeMock.start
Expand All @@ -37,7 +41,3 @@ class DbTest < ActiveSupport::TestCase
StripeMock.stop
end
end

class User < ActiveRecord::Base
include Pay::Billable
end

0 comments on commit 6403db0

Please sign in to comment.