Class: Pay::Braintree::Customer
- Inherits:
-
Customer
- Object
- ApplicationRecord
- Customer
- Pay::Braintree::Customer
- Defined in:
- app/models/pay/braintree/customer.rb
Instance Method Summary collapse
- #add_payment_method(token, default: false) ⇒ Object
-
#api_record ⇒ Object
Retrieve the Braintree::Customer object.
-
#api_record_attributes ⇒ Object
Returns a hash of attributes for the Stripe::Customer object.
- #card_details_for_braintree_transaction(transaction) ⇒ Object
- #charge(amount, options = {}) ⇒ Object
- #gateway ⇒ Object
- #save_payment_method(payment_method, default:) ⇒ Object
- #save_transaction(transaction) ⇒ Object
- #subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options) ⇒ Object
-
#update_api_record(**attributes) ⇒ Object
Syncs name and email to Braintree::Customer You can also pass in other attributes that will be merged into the default attributes.
Methods inherited from Customer
#active?, #customer_name, #deleted?, #has_incomplete_payment?, #on_generic_trial?, #on_trial?, #on_trial_or_subscribed?, #retry_past_due_subscriptions!, #subscribed?, #subscription, #update_payment_method
Instance Method Details
#add_payment_method(token, default: false) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'app/models/pay/braintree/customer.rb', line 104 def add_payment_method(token, default: false) result = gateway.payment_method.create( customer_id: processor_id || api_record.id, payment_method_nonce: token, options: { make_default: default, verify_card: true } ) raise Pay::Braintree::Error, result unless result.success? pay_payment_method = save_payment_method(result.payment_method, default: default) # Update existing subscriptions to the new payment method subscriptions.each do |subscription| if subscription.active? gateway.subscription.update(subscription.processor_id, {payment_method_token: token}) end end pay_payment_method rescue ::Braintree::AuthorizationError => e raise Pay::Braintree::AuthorizationError, e rescue ::Braintree::BraintreeError => e raise Pay::Braintree::Error, e end |
#api_record ⇒ Object
Retrieve the Braintree::Customer object
-
If no processor_id is present, creates a Customer.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/models/pay/braintree/customer.rb', line 25 def api_record if processor_id? gateway.customer.find(processor_id) else result = gateway.customer.create(api_record_attributes) raise Pay::Braintree::Error, result unless result.success? update!(processor_id: result.customer.id) result.customer end rescue ::Braintree::AuthorizationError => e raise Pay::Braintree::AuthorizationError, e rescue ::Braintree::BraintreeError => e raise Pay::Braintree::Error, e end |
#api_record_attributes ⇒ Object
Returns a hash of attributes for the Stripe::Customer object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/models/pay/braintree/customer.rb', line 10 def api_record_attributes attributes = case owner.class.pay_braintree_customer_attributes when Symbol owner.send(owner.class.pay_braintree_customer_attributes, self) when Proc owner.class.pay_braintree_customer_attributes.call(self) end first_name, last_name = customer_name.split(" ", 2) {email: email, first_name: first_name, last_name: last_name}.merge(attributes || {}) end |
#card_details_for_braintree_transaction(transaction) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'app/models/pay/braintree/customer.rb', line 212 def card_details_for_braintree_transaction(transaction) case transaction.payment_instrument_type when "android_pay_card", "apple_pay_card", "credit_card", "google_pay_card", "samsung_pay_card", "visa_checkout_card" # Lookup the attribute with the payment method details by name attribute_name = transaction.payment_instrument_type # The attribute name for Apple and Google Pay don't include _card for some reason if ["apple_pay_card", "google_pay_card"].include?(transaction.payment_instrument_type) attribute_name = attribute_name.split("_card").first # Android Pay was renamed to Google Pay, but test nonces still use android_pay_card elsif attribute_name == "android_pay_card" attribute_name = "google_pay" end # Retrieve payment method details from transaction payment_method = transaction.send(:"#{attribute_name}_details") { payment_method_type: :card, brand: payment_method.card_type, last4: payment_method.last_4, exp_month: payment_method.expiration_month, exp_year: payment_method.expiration_year } when "paypal_account" { payment_method_type: :paypal, brand: "PayPal", email: transaction.paypal_details.payer_email, last4: transaction.paypal_details.payer_email, exp_month: nil, exp_year: nil } when "venmo_account" { payment_method_type: :venmo, brand: "Venmo", username: transaction.venmo_account_details.username, last4: transaction.venmo_account_details.username, exp_month: nil, exp_year: nil } else {payment_method_type: "unknown"} end end |
#charge(amount, options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/pay/braintree/customer.rb', line 47 def charge(amount, = {}) args = { amount: amount.to_i / 100.0, customer_id: processor_id || api_record.id, options: {submit_for_settlement: true}, custom_fields: .delete(:metadata) }.merge() result = gateway.transaction.sale(args) raise Pay::Braintree::Error, result unless result.success? save_transaction(result.transaction) rescue ::Braintree::AuthorizationError => e raise Pay::Braintree::AuthorizationError, e rescue ::Braintree::BraintreeError => e raise Pay::Braintree::Error, e end |
#gateway ⇒ Object
155 156 157 |
# File 'app/models/pay/braintree/customer.rb', line 155 def gateway Pay.braintree_gateway end |
#save_payment_method(payment_method, default:) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'app/models/pay/braintree/customer.rb', line 159 def save_payment_method(payment_method, default:) attributes = case payment_method when ::Braintree::CreditCard, ::Braintree::ApplePayCard, ::Braintree::GooglePayCard, ::Braintree::SamsungPayCard, ::Braintree::VisaCheckoutCard { payment_method_type: :card, brand: payment_method.card_type, last4: payment_method.last_4, exp_month: payment_method.expiration_month, exp_year: payment_method.expiration_year } when ::Braintree::PayPalAccount { payment_method_type: :paypal, brand: "PayPal", email: payment_method.email } when ::Braintree::VenmoAccount { payment_method_type: :venmo, brand: "Venmo", username: payment_method.username } when ::Braintree::UsBankAccount { payment_method_type: "us_bank_account", bank: payment_method.bank_name, last4: payment_method.last_4 } else { payment_method_type: payment_method.class.name.demodulize.underscore, brand: payment_method.try(:card_type), last4: payment_method.try(:last_4), exp_month: payment_method.try(:expiration_month), exp_year: payment_method.try(:expiration_year), bank: payment_method.try(:bank_name), username: payment_method.try(:username), email: payment_method.try(:email) } end pay_payment_method = payment_methods.where(processor_id: payment_method.token).first_or_initialize payment_methods.update_all(default: false) if default pay_payment_method.update!(attributes.merge(default: default)) # Reload the Rails association reload_default_payment_method if default pay_payment_method end |
#save_transaction(transaction) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/models/pay/braintree/customer.rb', line 131 def save_transaction(transaction) attrs = card_details_for_braintree_transaction(transaction) attrs[:amount] = transaction.amount.to_f * 100 attrs[:metadata] = transaction.custom_fields attrs[:currency] = transaction.currency_iso_code attrs[:application_fee_amount] = transaction.service_fee_amount attrs[:created_at] = transaction.created_at # Associate charge with subscription if we can if transaction.subscription_id pay_subscription = subscriptions.find_by(processor_id: transaction.subscription_id) pay_subscription ||= Pay::Braintree::Subscription.sync(transaction.subscription_id) if pay_subscription attrs[:subscription] = pay_subscription attrs[:metadata] = pay_subscription. end end charge = charges.find_or_initialize_by(processor_id: transaction.id) charge.update!(attrs) charge end |
#subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/models/pay/braintree/customer.rb', line 65 def subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **) token = api_record.payment_methods.find(&:default?).try(:token) raise Pay::Error, "Customer has no default payment method" if token.nil? # Standardize the trial period options if (trial_period_days = .delete(:trial_period_days)) && trial_period_days > 0 .merge!(trial_period: true, trial_duration: trial_period_days, trial_duration_unit: :day) end = .delete(:metadata) = .merge(payment_method_token: token, plan_id: plan) result = gateway.subscription.create() raise Pay::Braintree::Error, result unless result.success? # Braintree returns dates without time zones, so we'll assume they're UTC trial_end_date = result.subscription.trial_period.present? ? result.subscription.first_billing_date.end_of_day : nil subscription = subscriptions.create!( name: name, processor_id: result.subscription.id, processor_plan: plan, status: :active, trial_ends_at: trial_end_date, ends_at: nil, metadata: ) if (charge = result.subscription.transactions.first) save_transaction(charge) end subscription rescue ::Braintree::AuthorizationError => e raise Pay::Braintree::AuthorizationError, e rescue ::Braintree::BraintreeError => e raise Pay::Braintree::Error, e end |
#update_api_record(**attributes) ⇒ Object
Syncs name and email to Braintree::Customer You can also pass in other attributes that will be merged into the default attributes
42 43 44 45 |
# File 'app/models/pay/braintree/customer.rb', line 42 def update_api_record(**attributes) api_record unless processor_id? gateway.customer.update(processor_id, api_record_attributes.merge(attributes)) end |