Module: Spree::BraintreeVzero::OrderDecorator

Defined in:
app/models/spree/braintree_vzero/order_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



4
5
6
# File 'app/models/spree/braintree_vzero/order_decorator.rb', line 4

def self.prepended(base)
  base.state_machine.before_transition to: :complete, do: :process_paypal_express_payments
end

Instance Method Details

#confirmation_required?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
# File 'app/models/spree/braintree_vzero/order_decorator.rb', line 79

def confirmation_required?
  Spree::Config[:always_include_confirm_step] ||
    payments.valid.map(&:payment_method).compact.any?(&:payment_profiles_supported?) ||
    # setting payment_profiles_supported? for braintree gateways would require few additional changes in payments profiles system
    braintree_confirmation_required? || state == 'confirm'
end

#invalidate_paypal_express_paymentsObject



94
95
96
97
98
99
100
# File 'app/models/spree/braintree_vzero/order_decorator.rb', line 94

def invalidate_paypal_express_payments
  return unless paid_with_paypal_express?

  payments.valid.each do |payment|
    payment.invalidate! if payment.payment_method.is_a?(Spree::Gateway::BraintreeVzeroPaypalExpress)
  end
end

#no_phone_number?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
# File 'app/models/spree/braintree_vzero/order_decorator.rb', line 102

def no_phone_number?
  [ship_address, bill_address].each do |address|
    return true if address.try(:phone).eql?(I18n.t('braintree.phone_number_placeholder'))
  end
  false
end

Returns:

  • (Boolean)


86
87
88
# File 'app/models/spree/braintree_vzero/order_decorator.rb', line 86

def paid_with_braintree?
  payments.valid.map(&:payment_method).compact.any? { |p| p.is_a?(Spree::Gateway::BraintreeVzeroBase) }
end

Returns:

  • (Boolean)


90
91
92
# File 'app/models/spree/braintree_vzero/order_decorator.rb', line 90

def paid_with_paypal_express?
  payments.valid.map(&:payment_method).compact.any? { |p| p.is_a?(Spree::Gateway::BraintreeVzeroPaypalExpress) }
end

#payment_required?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
# File 'app/models/spree/braintree_vzero/order_decorator.rb', line 72

def payment_required?
  # default payment processing requires order to have state == payment
  # so there is no need to divide this method for checkout steps and actual payment processing
  return false if paid_with_paypal_express?
  total.to_f > 0.0
end

#remove_phone_number_placeholderObject



109
110
111
112
113
# File 'app/models/spree/braintree_vzero/order_decorator.rb', line 109

def remove_phone_number_placeholder
  [ship_address, bill_address].each do |address|
    address.update_column(:phone, nil) if address.try(:phone).eql?(I18n.t('braintree.phone_number_placeholder'))
  end
end

#save_paypal_address(type, address_hash) ⇒ Object



8
9
10
11
12
13
14
# File 'app/models/spree/braintree_vzero/order_decorator.rb', line 8

def save_paypal_address(type, address_hash)
  return if address_hash.blank?
  address = Spree::Address.new(prepare_address_hash(address_hash))
  return unless address.save

  update_column("#{type}_id", address.id)
end

#save_paypal_payment(options) ⇒ Object



16
17
18
19
# File 'app/models/spree/braintree_vzero/order_decorator.rb', line 16

def save_paypal_payment(options)
  options[:source] = Spree::BraintreeCheckout.create!(options.slice(:paypal_email, :advanced_fraud_data))
  payments.create(options.slice(:braintree_nonce, :payment_method_id, :source))
end

#set_billing_addressObject



21
22
23
24
25
26
27
# File 'app/models/spree/braintree_vzero/order_decorator.rb', line 21

def set_billing_address
  return if bill_address_id
  return unless ship_address_id

  address = Spree::Address.create(shipping_address.attributes.except('id', 'updated_at', 'created_at', 'braintree_id'))
  update_column(:bill_address_id, address.try(:id))
end

#update_from_params(params, permitted_params, request_env = {}) ⇒ Object

override needed to add braintree source attribute



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/spree/braintree_vzero/order_decorator.rb', line 30

def update_from_params(params, permitted_params, request_env = {})
  success = false
  @updating_params = params
  run_callbacks :updating_from_params do
    # Set existing card after setting permitted parameters because
    # rails would slice parameters containg ruby objects, apparently
    existing_card_id = @updating_params[:order] ? @updating_params[:order].delete(:existing_card) : nil

    attributes = @updating_params[:order] ? @updating_params[:order].permit(permitted_params).delete_if { |_k, v| v.nil? } : {}
    payment_attributes = attributes[:payments_attributes].first if attributes[:payments_attributes].present?

    if existing_card_id.present?
      credit_card = Spree::CreditCard.find existing_card_id
      if credit_card.user_id != user_id || credit_card.user_id.blank?
        raise Spree::Core::GatewayError, Spree.t(:invalid_credit_card)
      end

      credit_card.verification_value = params[:cvc_confirm] if params[:cvc_confirm].present?

      attributes[:payments_attributes].first[:source] = credit_card
      attributes[:payments_attributes].first[:payment_method_id] = credit_card.payment_method_id
      attributes[:payments_attributes].first.delete :source_attributes
    end

    if payment_attributes.present?
      payment_attributes[:request_env] = request_env

      if (token = payment_attributes[:braintree_token]).present?
        payment_attributes[:source] = Spree::BraintreeCheckout.create_from_token(token, payment_attributes[:payment_method_id])
      elsif payment_attributes[:braintree_nonce].present?
        payment_attributes[:source] = Spree::BraintreeCheckout.create_from_params(params)
      end
    end

    success = update(attributes)
    set_shipments_cost if shipments.any?
  end

  @updating_params = nil
  success
end