1
1
module Pay
2
2
module Braintree
3
- class Billable
4
- attr_reader :pay_customer
5
-
6
- delegate :processor_id ,
7
- :processor_id? ,
8
- :email ,
9
- :customer_name ,
10
- to : :pay_customer
11
-
12
- def initialize ( pay_customer )
13
- @pay_customer = pay_customer
14
- end
3
+ class Customer < Pay ::Customer
4
+ has_many :charges , dependent : :destroy , class_name : "Pay::Braintree::Charge"
5
+ has_many :subscriptions , dependent : :destroy , class_name : "Pay::Braintree::Subscription"
6
+ has_many :payment_methods , dependent : :destroy , class_name : "Pay::Braintree::PaymentMethod"
7
+ has_one :default_payment_method , -> { where ( default : true ) } , class_name : "Pay::Braintree::PaymentMethod"
15
8
16
9
# Returns a hash of attributes for the Stripe::Customer object
17
- def customer_attributes
18
- owner = pay_customer . owner
19
-
10
+ def api_record_attributes
20
11
attributes = case owner . class . pay_braintree_customer_attributes
21
12
when Symbol
22
- owner . send ( owner . class . pay_braintree_customer_attributes , pay_customer )
13
+ owner . send ( owner . class . pay_braintree_customer_attributes , self )
23
14
when Proc
24
- owner . class . pay_braintree_customer_attributes . call ( pay_customer )
15
+ owner . class . pay_braintree_customer_attributes . call ( self )
25
16
end
26
17
27
- # Guard against attributes being returned nil
28
- attributes ||= { }
29
-
30
18
first_name , last_name = customer_name . split ( " " , 2 )
31
- { email : email , first_name : first_name , last_name : last_name } . merge ( attributes )
19
+ { email : email , first_name : first_name , last_name : last_name } . merge ( attributes || { } )
32
20
end
33
21
34
22
# Retrieve the Braintree::Customer object
35
23
#
36
24
# - If no processor_id is present, creates a Customer.
37
- def customer
25
+ def api_record
38
26
if processor_id?
39
27
gateway . customer . find ( processor_id )
40
28
else
41
- result = gateway . customer . create ( customer_attributes )
29
+ result = gateway . customer . create ( api_record_attributes )
42
30
raise Pay ::Braintree ::Error , result unless result . success?
43
- pay_customer . update! ( processor_id : result . customer . id )
31
+ update! ( processor_id : result . customer . id )
44
32
result . customer
45
33
end
46
34
rescue ::Braintree ::AuthorizationError => e
@@ -51,15 +39,15 @@ def customer
51
39
52
40
# Syncs name and email to Braintree::Customer
53
41
# You can also pass in other attributes that will be merged into the default attributes
54
- def update_customer! ( **attributes )
55
- customer unless processor_id?
56
- gateway . customer . update ( processor_id , customer_attributes . merge ( attributes ) )
42
+ def update_api_record ( **attributes )
43
+ api_record unless processor_id?
44
+ gateway . customer . update ( processor_id , api_record_attributes . merge ( attributes ) )
57
45
end
58
46
59
47
def charge ( amount , options = { } )
60
48
args = {
61
49
amount : amount . to_i / 100.0 ,
62
- customer_id : customer . id ,
50
+ customer_id : processor_id || api_record . id ,
63
51
options : { submit_for_settlement : true } ,
64
52
custom_fields : options . delete ( :metadata )
65
53
} . merge ( options )
@@ -75,7 +63,7 @@ def charge(amount, options = {})
75
63
end
76
64
77
65
def subscribe ( name : Pay . default_product_name , plan : Pay . default_plan_name , **options )
78
- token = customer . payment_methods . find ( &:default? ) . try ( :token )
66
+ token = api_record . payment_methods . find ( &:default? ) . try ( :token )
79
67
raise Pay ::Error , "Customer has no default payment method" if token . nil?
80
68
81
69
# Standardize the trial period options
@@ -89,12 +77,15 @@ def subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **opt
89
77
result = gateway . subscription . create ( subscription_options )
90
78
raise Pay ::Braintree ::Error , result unless result . success?
91
79
92
- subscription = pay_customer . subscriptions . create! (
80
+ # Braintree returns dates without time zones, so we'll assume they're UTC
81
+ trial_end_date = result . subscription . trial_period . present? ? result . subscription . first_billing_date . end_of_day : nil
82
+
83
+ subscription = subscriptions . create! (
93
84
name : name ,
94
85
processor_id : result . subscription . id ,
95
86
processor_plan : plan ,
96
87
status : :active ,
97
- trial_ends_at : trial_end_date ( result . subscription ) ,
88
+ trial_ends_at : trial_end_date ,
98
89
ends_at : nil ,
99
90
metadata : metadata
100
91
)
@@ -111,10 +102,8 @@ def subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **opt
111
102
end
112
103
113
104
def add_payment_method ( token , default : false )
114
- customer unless processor_id?
115
-
116
105
result = gateway . payment_method . create (
117
- customer_id : processor_id ,
106
+ customer_id : processor_id || api_record . id ,
118
107
payment_method_nonce : token ,
119
108
options : {
120
109
make_default : default ,
@@ -126,7 +115,7 @@ def add_payment_method(token, default: false)
126
115
pay_payment_method = save_payment_method ( result . payment_method , default : default )
127
116
128
117
# Update existing subscriptions to the new payment method
129
- pay_customer . subscriptions . each do |subscription |
118
+ subscriptions . each do |subscription |
130
119
if subscription . active?
131
120
gateway . subscription . update ( subscription . processor_id , { payment_method_token : token } )
132
121
end
@@ -139,16 +128,6 @@ def add_payment_method(token, default: false)
139
128
raise Pay ::Braintree ::Error , e
140
129
end
141
130
142
- def trial_end_date ( subscription )
143
- return unless subscription . trial_period
144
- # Braintree returns dates without time zones, so we'll assume they're UTC
145
- subscription . first_billing_date . end_of_day
146
- end
147
-
148
- def processor_subscription ( subscription_id , options = { } )
149
- gateway . subscription . find ( subscription_id )
150
- end
151
-
152
131
def save_transaction ( transaction )
153
132
attrs = card_details_for_braintree_transaction ( transaction )
154
133
attrs [ :amount ] = transaction . amount . to_f * 100
@@ -159,7 +138,7 @@ def save_transaction(transaction)
159
138
160
139
# Associate charge with subscription if we can
161
140
if transaction . subscription_id
162
- pay_subscription = pay_customer . subscriptions . find_by ( processor_id : transaction . subscription_id )
141
+ pay_subscription = subscriptions . find_by ( processor_id : transaction . subscription_id )
163
142
pay_subscription ||= Pay ::Braintree ::Subscription . sync ( transaction . subscription_id )
164
143
165
144
if pay_subscription
@@ -168,7 +147,7 @@ def save_transaction(transaction)
168
147
end
169
148
end
170
149
171
- charge = pay_customer . charges . find_or_initialize_by ( processor_id : transaction . id )
150
+ charge = charges . find_or_initialize_by ( processor_id : transaction . id )
172
151
charge . update! ( attrs )
173
152
charge
174
153
end
@@ -219,13 +198,13 @@ def save_payment_method(payment_method, default:)
219
198
}
220
199
end
221
200
222
- pay_payment_method = pay_customer . payment_methods . where ( processor_id : payment_method . token ) . first_or_initialize
201
+ pay_payment_method = payment_methods . where ( processor_id : payment_method . token ) . first_or_initialize
223
202
224
- pay_customer . payment_methods . update_all ( default : false ) if default
203
+ payment_methods . update_all ( default : false ) if default
225
204
pay_payment_method . update! ( attributes . merge ( default : default ) )
226
205
227
206
# Reload the Rails association
228
- pay_customer . reload_default_payment_method if default
207
+ reload_default_payment_method if default
229
208
230
209
pay_payment_method
231
210
end
0 commit comments