Class: Spree::BraintreeCheckout
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spree::BraintreeCheckout
- Defined in:
- app/models/spree/braintree_checkout.rb
Constant Summary collapse
- FINAL_STATES =
%w(authorization_expired processor_declined gateway_rejected failed voided settled settlement_declined refunded released).freeze
Class Method Summary collapse
- .create_from_params(params) ⇒ Object
- .create_from_token(token, payment_method_id) ⇒ Object
- .update_states ⇒ Object
Instance Method Summary collapse
- #actions ⇒ Object
- #can_credit?(_payment) ⇒ Boolean
- #can_settle?(_) ⇒ Boolean
- #can_void?(_payment) ⇒ Boolean
- #update_state ⇒ Object
Class Method Details
.create_from_params(params) ⇒ Object
13 14 15 16 17 18 |
# File 'app/models/spree/braintree_checkout.rb', line 13 def self.create_from_params(params) type = braintree_card_type_to_spree(params[:braintree_card_type]) create!(paypal_email: params[:paypal_email], braintree_last_digits: params[:braintree_last_two], braintree_card_type: type) end |
.create_from_token(token, payment_method_id) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'app/models/spree/braintree_checkout.rb', line 20 def self.create_from_token(token, payment_method_id) gateway = Spree::PaymentMethod.find(payment_method_id) vaulted_payment_method = gateway.vaulted_payment_method(token) type = braintree_card_type_to_spree(vaulted_payment_method.try(:card_type)) create!(paypal_email: vaulted_payment_method.try(:email), braintree_last_digits: vaulted_payment_method.try(:last_4), braintree_card_type: type) end |
.update_states ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/spree/braintree_checkout.rb', line 29 def self.update_states braintree = Gateway::BraintreeVzeroBase.first.provider result = { changed: 0, unchanged: 0 } not_in_state(FINAL_STATES).find_each do |checkout| checkout.state = braintree::Transaction.find(checkout.transaction_id).status if checkout.state_changed? result[:changed] += 1 checkout.save else result[:unchanged] += 1 end end result end |
Instance Method Details
#actions ⇒ Object
50 51 52 |
# File 'app/models/spree/braintree_checkout.rb', line 50 def actions %w(void settle credit) end |
#can_credit?(_payment) ⇒ Boolean
62 63 64 |
# File 'app/models/spree/braintree_checkout.rb', line 62 def can_credit?(_payment) %w(settled settling).include? state end |
#can_settle?(_) ⇒ Boolean
58 59 60 |
# File 'app/models/spree/braintree_checkout.rb', line 58 def can_settle?(_) %w(authorized).include? state end |
#can_void?(_payment) ⇒ Boolean
54 55 56 |
# File 'app/models/spree/braintree_checkout.rb', line 54 def can_void?(_payment) %w(authorized submitted_for_settlement).include? state end |
#update_state ⇒ Object
44 45 46 47 48 |
# File 'app/models/spree/braintree_checkout.rb', line 44 def update_state status = Transaction.new(Gateway::BraintreeVzeroBase.first.provider, transaction_id).status payment.send(payment_action(status)) status end |