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

Commit

Permalink
Merge pull request #43 from solidusio/apple-pay-address-import
Browse files Browse the repository at this point in the history
Map country names to ISO codes
  • Loading branch information
cbrunsdon authored Nov 21, 2016
2 parents f37a5c7 + 4efb2a7 commit 05aff07
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ window.SolidusPaypalBraintree = {

buildAddress: function(shippingContact) {
var addressHash = {
country_name: shippingContact.country,
country_code: shippingContact.countryCode,
first_name: shippingContact.givenName,
last_name: shippingContact.familyName,
Expand Down
10 changes: 10 additions & 0 deletions app/models/solidus_paypal_braintree/transaction_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module SolidusPaypalBraintree
class TransactionAddress
include ActiveModel::Model
include ActiveModel::Validations::Callbacks
include SolidusPaypalBraintree::CountryMapper

attr_accessor :country_code, :last_name, :first_name,
:city, :zip, :state_code, :address_line_1, :address_line_2
Expand All @@ -18,6 +19,15 @@ class TransactionAddress
validates :spree_country, presence: true
validates :spree_state, presence: true, if: :should_match_state_model?

def initialize(attributes = {})
country_name = attributes.delete(:country_name) || ""
if attributes[:country_code].blank?
attributes[:country_code] = iso_from_name(country_name)
end

super(attributes)
end

def spree_country
country_code && (@country ||= Spree::Country.find_by(iso: country_code.upcase))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class InvalidImportError < StandardError; end
:phone,
:email,
address_attributes: [
:country_code, :last_name, :first_name,
:country_code, :country_name, :last_name, :first_name,
:city, :zip, :state_code, :address_line_1, :address_line_2
]
]
Expand Down
1 change: 1 addition & 0 deletions lib/solidus_paypal_braintree.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'solidus_core'
require 'solidus_paypal_braintree/engine'
require 'solidus_paypal_braintree/country_mapper'

module SolidusPaypalBraintree
def self.table_name_prefix
Expand Down
35 changes: 35 additions & 0 deletions lib/solidus_paypal_braintree/country_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module SolidusPaypalBraintree
module CountryMapper
extend ActiveSupport::Concern

USA_VARIANTS = [
"the united states of america",
"united states of america",
"the united states",
"united states",
"us of a",
"u.s.a.",
"usa",
"u.s.",
"us"
]

CANADA_VARIANTS = [
"canada",
"ca"
]

# Generates a hash mapping each variant of the country name to the same ISO
# ie: { "usa" => "US", "united states" => "US", "canada" => "CA", ... }
COUNTRY_MAP = {
USA_VARIANTS => "US",
CANADA_VARIANTS => "CA"
}.flat_map { |variants, iso| variants.map { |v| [v, iso] } }.to_h

included do
def iso_from_name(country_name)
COUNTRY_MAP[country_name.downcase.strip]
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@
end
end

context "and no country ISO was provided" do
before do
params[:transaction][:address_attributes][:country_code] = ""
params[:transaction][:address_attributes][:country_name] = "United States"
end

it "creates a new address, looking up the ISO by country name" do
order
expect { post_create }.to change { Spree::Address.count }.by(1)
expect(Spree::Address.last.country.iso).to eq "US"
end
end

context "and the transaction does not have an address" do
before { params[:transaction].delete(:address_attributes) }

Expand Down
31 changes: 31 additions & 0 deletions spec/models/solidus_paypal_braintree/transaction_address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@
end
end

describe "#attributes=" do
subject { described_class.new(attrs) }

context "when an ISO code is provided" do
let(:attrs) { { country_code: "US" } }

it "uses the ISO code provided" do
expect(subject.country_code).to eq "US"
end
end

context "when the ISO code is blank" do
context "and a valid country name is provided" do
let!(:canada) { create :country, name: "Canada", iso: "CA" }
let(:attrs) { { country_name: "Canada" } }

it "looks up the ISO code by the country name" do
expect(subject.country_code).to eq "CA"
end
end

context "and no valid country name is provided" do
let(:attrs) { { country_name: "Neverland" } }

it "leaves the country code blank" do
expect(subject.country_code).to be_nil
end
end
end
end

describe '#spree_country' do
subject { described_class.new(country_code: country_code).spree_country }

Expand Down

0 comments on commit 05aff07

Please sign in to comment.