-
Notifications
You must be signed in to change notification settings - Fork 6
/
demo.rb
41 lines (34 loc) · 994 Bytes
/
demo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class User < ActiveRecord::Base
def set_up_subscription_in_braintree!
create_customer_in_braintree && create_subscription_in_braintree
end
def create_customer_in_braintree
customer_result = BraintreeWrapper.create_customer_for(self)
if customer_result.success?
self.braintree_credit_card_token = customer_result.credit_card_token
else
add_charging_error
false
end
end
def create_subscription_in_braintree
subscription_result = BraintreeWrapper.create_subscription_for(self)
if subscription_result.success?
self.braintree_subscription_token = subscription_result.subscription_token
else
add_charging_error
false
end
end
def has_credit_card?
braintree_credit_card_token.present?
end
def credit_card
if has_credit_card?
Braintree::CreditCard.find(braintree_credit_card_token)
end
end
def add_charging_error
errors.add(:base, "We couldn't charge your credit card")
end
end