diff --git a/.rubocop.yml b/.rubocop.yml index f74dba89..ecfe7207 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -43,6 +43,10 @@ RSpec/VerifiedDoubles: # Sometimes you really need an "anything" double IgnoreSymbolicNames: true +Rspec/Naming/VariableNumber: + # PayPal uses snake_case, we use normal_case ¯\_(ツ)_/¯ + Enabled: false + Style/FrozenStringLiteralComment: Exclude: - spec/**/* diff --git a/app/models/solidus_paypal_commerce_platform/paypal_address.rb b/app/models/solidus_paypal_commerce_platform/paypal_address.rb index f16572f8..0ca25eeb 100644 --- a/app/models/solidus_paypal_commerce_platform/paypal_address.rb +++ b/app/models/solidus_paypal_commerce_platform/paypal_address.rb @@ -19,7 +19,7 @@ def simulate_update(paypal_address) end def update(paypal_address) - formatted_address = format_address(paypal_address) + formatted_address = address_attributes(paypal_address[:updated_address], paypal_address[:recipient]) new_address = @order.ship_address.dup || ::Spree::Address.new new_address.assign_attributes(formatted_address) @@ -51,36 +51,35 @@ def find_state(state_name, country) end def format_simulated_address(paypal_address) - country = ::Spree::Country.find_by(iso: paypal_address[:country_code]) - # Also adds fake information for a few fields, so validations can run + # Adds fake information for a few fields, so validations can run + paypal_address[:address_line_1] = "123 Fake St." + ::Spree::Address.new( - city: paypal_address[:city], - state: find_state(paypal_address[:state], country), - state_name: paypal_address[:state], - zipcode: paypal_address[:postal_code], - country: country, - address1: "123 Fake St.", - phone: "123456789", - firstname: "Fake" + address_attributes(paypal_address, { name: { given_name: "Fake" } }) ) end - def format_address(paypal_address) - address = paypal_address[:updated_address] - recipient = paypal_address[:recipient] + def address_attributes(address, recipient) country = ::Spree::Country.find_by(iso: address[:country_code]) - { + attributes = { address1: address[:address_line_1], address2: address[:address_line_2], - state: find_state(address[:admin_area_1], country), - state_name: address[:admin_area_1], - city: address[:admin_area_2], + state: find_state(address[:admin_area_1] || address[:state], country), + state_name: address[:admin_area_1] || address[:state], + city: address[:admin_area_2] || address[:city], country: country, zipcode: address[:postal_code], - firstname: recipient[:name][:given_name], - lastname: recipient[:name][:surname] } + + if SolidusSupport.combined_first_and_last_name_in_address? + attributes[:name] = "#{recipient[:name][:given_name]} #{recipient[:name][:surname]}" + else + attributes[:firstname] = recipient[:name][:given_name] + attributes[:lastname] = recipient[:name][:surname] + end + + attributes end end end diff --git a/solidus_paypal_commerce_platform.gemspec b/solidus_paypal_commerce_platform.gemspec index 302b3757..14117c2e 100644 --- a/solidus_paypal_commerce_platform.gemspec +++ b/solidus_paypal_commerce_platform.gemspec @@ -30,7 +30,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'deface', '~> 1.5' spec.add_dependency 'solidus_core', ['>= 2.0.0', '< 3'] - spec.add_dependency 'solidus_support', [">= 0.5.1", "< 1"] + spec.add_dependency 'solidus_support', [">= 0.8.0", "< 1"] spec.add_dependency 'solidus_webhooks', '~> 0.2' spec.add_dependency 'paypal-checkout-sdk' diff --git a/spec/models/solidus_paypal_commerce_platform/paypal_address_spec.rb b/spec/models/solidus_paypal_commerce_platform/paypal_address_spec.rb index c74849a2..b5b63896 100644 --- a/spec/models/solidus_paypal_commerce_platform/paypal_address_spec.rb +++ b/spec/models/solidus_paypal_commerce_platform/paypal_address_spec.rb @@ -3,7 +3,7 @@ RSpec.describe SolidusPaypalCommercePlatform::PaypalAddress, type: :model do let(:order) { create(:order) } let(:original_address) { create(:address) } - let(:address) { create(:address) } + let(:address) { create(:address, name_attributes) } let(:params) { { updated_address: { @@ -16,8 +16,8 @@ }, recipient: { name: { - given_name: address.firstname, - surname: address.lastname + given_name: "Alexander", + surname: "Hamilton" } } } @@ -37,8 +37,12 @@ expect(subject.address2).to eq address.address2 expect(subject.zipcode).to eq address.zipcode expect(subject.country).to eq address.country - expect(subject.firstname).to eq address.firstname - expect(subject.lastname).to eq address.lastname + if SolidusSupport.combined_first_and_last_name_in_address? + expect(subject.name).to eq address.name + else + expect(subject.firstname).to eq address.firstname + expect(subject.lastname).to eq address.lastname + end expect(subject.phone).to eq original_address.phone end @@ -52,4 +56,12 @@ end end end + + def name_attributes + if SolidusSupport.combined_first_and_last_name_in_address? + { name: "Alexander Hamilton" } + else + { firstname: "Alexander", lastname: "Hamilton" } + end + end end diff --git a/spec/requests/solidus_paypal_commerce_platform/orders_controller_spec.rb b/spec/requests/solidus_paypal_commerce_platform/orders_controller_spec.rb index fc32a97a..32b7eab2 100644 --- a/spec/requests/solidus_paypal_commerce_platform/orders_controller_spec.rb +++ b/spec/requests/solidus_paypal_commerce_platform/orders_controller_spec.rb @@ -15,7 +15,7 @@ get solidus_paypal_commerce_platform.verify_total_path, params: params - expect(response).to have_http_status(200) + expect(response).to have_http_status(:ok) end end @@ -29,7 +29,7 @@ get solidus_paypal_commerce_platform.verify_total_path, params: params - expect(response).to have_http_status(400) + expect(response).to have_http_status(:bad_request) end end end diff --git a/spec/requests/solidus_paypal_commerce_platform/wizard_controller_spec.rb b/spec/requests/solidus_paypal_commerce_platform/wizard_controller_spec.rb index 9a934538..68edff4a 100644 --- a/spec/requests/solidus_paypal_commerce_platform/wizard_controller_spec.rb +++ b/spec/requests/solidus_paypal_commerce_platform/wizard_controller_spec.rb @@ -38,7 +38,7 @@ expect(payment_method.preferred_client_id).to eq("CLIENT-ID") expect(payment_method.preferred_client_secret).to eq("CLIENT-SECRET") - expect(response).to have_http_status(201) + expect(response).to have_http_status(:created) end end end