Class: Spree::Gateway::BraintreeVzeroBase::Utils

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/gateway/braintree_vzero_base/utils.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gateway, order) ⇒ Utils

Returns a new instance of Utils.



7
8
9
10
11
12
13
14
# File 'app/models/spree/gateway/braintree_vzero_base/utils.rb', line 7

def initialize(gateway, order)
  @order = order
  begin
    @customer = gateway.provider::Customer.find(order.user.id) if order.user
  rescue
  end
  @gateway = gateway
end

Instance Attribute Details

#customerObject (readonly)

Returns the value of attribute customer.



5
6
7
# File 'app/models/spree/gateway/braintree_vzero_base/utils.rb', line 5

def customer
  @customer
end

#gatewayObject (readonly)

Returns the value of attribute gateway.



5
6
7
# File 'app/models/spree/gateway/braintree_vzero_base/utils.rb', line 5

def gateway
  @gateway
end

#orderObject (readonly)

Returns the value of attribute order.



5
6
7
# File 'app/models/spree/gateway/braintree_vzero_base/utils.rb', line 5

def order
  @order
end

Instance Method Details

#address_data(address_type, target) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/spree/gateway/braintree_vzero_base/utils.rb', line 48

def address_data(address_type, target)
  address = target.send("#{address_type}_address")
  country = address.country

  {
    company: address.company,
    country_code_alpha2: country.iso,
    country_code_alpha3: country.iso3,
    country_code_numeric: country.numcode,
    country_name: country.name,
    first_name: address.first_name,
    last_name: address.last_name,
    locality: address.city,
    postal_code: address.zipcode,
    region: address.state.try(:abbr),
    street_address: address.address1,
    extended_address: address.address2
  }
end

#customer_data(user) ⇒ Object



68
69
70
# File 'app/models/spree/gateway/braintree_vzero_base/utils.rb', line 68

def customer_data(user)
  address_data('billing', user).slice(:first_name, :last_name, :company, :phone).merge!(id: user.id, email: user.email)
end

#customer_payment_methods(payment_method_type) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/spree/gateway/braintree_vzero_base/utils.rb', line 72

def customer_payment_methods(payment_method_type)
  payment_methods = @customer.try(:payment_methods) || []

  if payment_method_type.eql?('custom')
    payment_methods.select { |pm| pm.is_a?(Braintree::CreditCard) }
  elsif payment_method_type.eql?('paypal')
    payment_methods.select { |pm| pm.is_a?(Braintree::PayPalAccount) }
  else
    payment_methods
  end
end

#get_address(address_type) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/spree/gateway/braintree_vzero_base/utils.rb', line 16

def get_address(address_type)
  if order.user && (address = order.user.send("#{address_type}_address"))
    braintree_address = BraintreeVzeroBase::Address.new(gateway.provider, order)
    vaulted_duplicate = Spree::Address.vaulted_duplicates(address).first

    if vaulted_duplicate && braintree_address.find(vaulted_duplicate.braintree_id)
      { "#{address_type}_address_id" => vaulted_duplicate.braintree_id }
    else
      { address_type => address_data(address_type, order.user) }
    end
  else
    { address_type => address_data(address_type, order) }
  end
end

#get_customerObject



31
32
33
34
35
36
37
# File 'app/models/spree/gateway/braintree_vzero_base/utils.rb', line 31

def get_customer
  if @customer
    { customer_id: @customer.id }
  else
    { customer: (payment_in_vault[:store_shipping_address_in_vault] && order.user) ? customer_data(order.user) : {} }
  end
end

#map_payment_status(braintree_status) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/spree/gateway/braintree_vzero_base/utils.rb', line 97

def map_payment_status(braintree_status)
  case braintree_status
  when 'authorized', 'settlement_pending'
    'pending'
  when 'voided'
    'void'
  when 'settled', 'submitted_for_settlement', 'settling'
    'completed'
  else
    'failed'
  end
end

#order_data(identifier, amount) ⇒ Object



39
40
41
42
43
44
45
46
# File 'app/models/spree/gateway/braintree_vzero_base/utils.rb', line 39

def order_data(identifier, amount)
  identifier.merge(
    amount: amount,
    order_id: order.number,
    line_items: collect_line_items,
    shipping_amount: order_shipping
  )
end

#payment_in_vault(data = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/spree/gateway/braintree_vzero_base/utils.rb', line 84

def payment_in_vault(data = {})
  store_ship_address = data['shipping_address_id'].blank?

  case gateway.preferred_store_payments_in_vault.to_s
  when 'store_only_on_success'
    { store_in_vault_on_success: true, store_shipping_address_in_vault: store_ship_address }
  when 'store_all'
    { store_in_vault: true, store_shipping_address_in_vault: store_ship_address }
  else
    { store_in_vault: false }
  end
end