Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Fix via custom SolidusPaypalbraintree::Address::Name service class
Browse files Browse the repository at this point in the history
that's because we can't rely on Spree::Address::Name, it is not
present on Solidus < 2.11.
  • Loading branch information
Flavio Auciello committed Apr 9, 2021
1 parent b810856 commit 50934a3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 32 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
module SolidusPaypalBraintree
module BraintreeCheckoutHelper
def braintree_3ds_options_for(order)
ship_address = order.ship_address
bill_address = order.bill_address

ship_address = SolidusPaypalBraintree::Address.new(order.ship_address)
bill_address = SolidusPaypalBraintree::Address.new(order.bill_address)
{
nonce: nil, # populated after tokenization
bin: nil, # populated after tokenization
Expand Down
58 changes: 57 additions & 1 deletion app/models/solidus_paypal_braintree/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,39 @@

module SolidusPaypalBraintree
class Address
class Name
attr_reader :first_name, :last_name, :value

def initialize(*components)
@value = components.join(' ').strip
initialize_name_components(components)
end

def to_s
@value
end

private

def initialize_name_components(components)
if components.size == 2
@first_name = components[0].to_s
@last_name = components[1].to_s
else
@first_name, @last_name = @value.split(/[[:space:]]/, 2)
end
end
end

delegate :address1,
:address2,
:city,
:country,
:phone,
:state,
:zipcode,
to: :spree_address

def initialize(spree_address)
@spree_address = spree_address
end
Expand All @@ -14,7 +47,7 @@ def to_json(*_args)
postalCode: spree_address.zipcode,
countryCode: spree_address.country.iso,
phone: spree_address.phone,
recipientName: spree_address.full_name
recipientName: name
}

if ::Spree::Config.address_requires_state && spree_address.country.states_required
Expand All @@ -23,8 +56,31 @@ def to_json(*_args)
address_hash.to_json
end

def name
@name ||= name_evaluator.to_s
end
alias_method :full_name, :name

def firstname
name_evaluator.first_name
end
alias_method :first_name, :firstname

def lastname
name_evaluator.last_name
end
alias_method :last_name, :lastname

private

def name_evaluator
if SolidusSupport.combined_first_and_last_name_in_address?
Name.new(spree_address.name)
else
Name.new(spree_address.firstname, spree_address.last_name)
end
end

attr_reader :spree_address
end
end
4 changes: 3 additions & 1 deletion app/models/solidus_paypal_braintree/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class Configuration < ::Spree::Base
messaging: { availables: %w[true false], default: 'false' }
}.freeze

include ::Spree::Preferences::Persistable
unless respond_to?(:preference)
include ::Spree::Preferences::Persistable
end

belongs_to :store, class_name: 'Spree::Store'

Expand Down
6 changes: 3 additions & 3 deletions app/models/solidus_paypal_braintree/transaction_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def to_spree_address
else
::Spree::Deprecation.warn("first_name and last_name are deprecated. Use name instead.", caller)
if first_name.nil?
a = ::Spree::Address.new(name: name)
address.firstname = a.firstname
address.lastname = a.lastname || "(left blank)"
name_value = ::SolidusPaypalBraintree::Address::Name.new(name)
address.firstname = name_value.first_name
address.lastname = name_value.last_name || "(left blank)"
else
address.firstname = first_name
address.lastname = last_name || "(left blank)"
Expand Down
2 changes: 1 addition & 1 deletion lib/solidus_paypal_braintree/testing_support/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
name { 'John Doe' }
else
transient do
name { "John Doe" }
name { 'John Doe' }
end

firstname { SolidusPaypalBraintree::Address::Name.new(name).first_name }
Expand Down

0 comments on commit 50934a3

Please sign in to comment.