From 50934a392d904bbb7fed3b3c5caf4b6ced1ee5e8 Mon Sep 17 00:00:00 2001 From: Flavio Auciello Date: Fri, 9 Apr 2021 08:54:15 +0200 Subject: [PATCH] Fix via custom SolidusPaypalbraintree::Address::Name service class that's because we can't rely on Spree::Address::Name, it is not present on Solidus < 2.11. --- .../spree/address_decorator.rb | 23 -------- .../braintree_checkout_helper.rb | 5 +- .../solidus_paypal_braintree/address.rb | 58 ++++++++++++++++++- .../solidus_paypal_braintree/configuration.rb | 4 +- .../transaction_address.rb | 6 +- .../testing_support/factories.rb | 2 +- 6 files changed, 66 insertions(+), 32 deletions(-) delete mode 100644 app/decorators/models/solidus_paypal_braintree/spree/address_decorator.rb diff --git a/app/decorators/models/solidus_paypal_braintree/spree/address_decorator.rb b/app/decorators/models/solidus_paypal_braintree/spree/address_decorator.rb deleted file mode 100644 index 5405c42d..00000000 --- a/app/decorators/models/solidus_paypal_braintree/spree/address_decorator.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -module SolidusPaypalBraintree - module Spree - module AddressDecorator - def firstname - name.split(/[[:space:]]/, 2).first - end - - def lastname - name.split(/[[:space:]]/, 2)[1..-1].join(' ') - end - - def full_name - name - end - - unless ::Spree::Address.methods.include?(:firstname) - ::Spree::Address.prepend self - end - end - end -end diff --git a/app/helpers/solidus_paypal_braintree/braintree_checkout_helper.rb b/app/helpers/solidus_paypal_braintree/braintree_checkout_helper.rb index d81f2992..06c0cc12 100644 --- a/app/helpers/solidus_paypal_braintree/braintree_checkout_helper.rb +++ b/app/helpers/solidus_paypal_braintree/braintree_checkout_helper.rb @@ -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 diff --git a/app/models/solidus_paypal_braintree/address.rb b/app/models/solidus_paypal_braintree/address.rb index ac5d2072..595b479d 100644 --- a/app/models/solidus_paypal_braintree/address.rb +++ b/app/models/solidus_paypal_braintree/address.rb @@ -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 @@ -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 @@ -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 diff --git a/app/models/solidus_paypal_braintree/configuration.rb b/app/models/solidus_paypal_braintree/configuration.rb index 11e038eb..789be61c 100644 --- a/app/models/solidus_paypal_braintree/configuration.rb +++ b/app/models/solidus_paypal_braintree/configuration.rb @@ -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' diff --git a/app/models/solidus_paypal_braintree/transaction_address.rb b/app/models/solidus_paypal_braintree/transaction_address.rb index 0a23ec32..12574ba8 100644 --- a/app/models/solidus_paypal_braintree/transaction_address.rb +++ b/app/models/solidus_paypal_braintree/transaction_address.rb @@ -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)" diff --git a/lib/solidus_paypal_braintree/testing_support/factories.rb b/lib/solidus_paypal_braintree/testing_support/factories.rb index 91e0fa76..1f02d430 100644 --- a/lib/solidus_paypal_braintree/testing_support/factories.rb +++ b/lib/solidus_paypal_braintree/testing_support/factories.rb @@ -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 }