Skip to content

Commit

Permalink
Merge pull request #8 from gvaughn/name_in_ship_address
Browse files Browse the repository at this point in the history
supply first and last name in shipping address
  • Loading branch information
kamui committed Nov 3, 2015
2 parents 0514e85 + 2a3cf39 commit 3df2323
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/models/solidus/gateway/braintree_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,14 @@ def handle_result(result)
end

def map_address(addr)
full_name = addr.fetch(:name, "")
*first_name_parts, last_name = full_name.split(" ")
first_name = first_name_parts.join(" ")
last_name ||= ""

{
first_name: first_name,
last_name: last_name,
street_address: addr[:address1],
extended_address: addr[:address2],
locality: addr[:city],
Expand Down
2 changes: 1 addition & 1 deletion lib/solidus_braintree/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SolidusBraintree
VERSION = "0.2.0"
VERSION = "0.2.1"
end
35 changes: 35 additions & 0 deletions spec/solidus/gateway/braintree_gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@
it 'sends a bill address' do
expected_params = {
billing: {
first_name: address.first_name,
last_name: address.last_name,
street_address: "1234 bill address",
extended_address: address.address2,
locality: address.city,
Expand Down Expand Up @@ -408,6 +410,8 @@
options: {},
amount: "5.00",
shipping: {
first_name: address.first_name,
last_name: address.last_name,
street_address: address.address1,
extended_address: address.address2,
locality: address.city,
Expand All @@ -416,6 +420,8 @@
postal_code: address.zipcode,
},
billing: {
first_name: address.first_name,
last_name: address.last_name,
street_address: address.address1,
extended_address: address.address2,
locality: address.city,
Expand All @@ -433,5 +439,34 @@
end
end
end

context "first and last name splitting" do
# since the address_hash comes from Spree::Address#active_merchant_hash which throws away
# information by only giving a concatenated full name in the :name field, we have to do a best
# guess here to split it back out. PayPal actually requires first_name and last_name on the
# shipping address in order to provide seller protection. Having something there is better
# than nothing.
let(:mapped_address) { payment_method.send(:map_address, {name: name}) }

context "simple 2 word name" do
let(:name) { "Luke Skywalker" }

it "splits" do
address = mapped_address
expect(address[:first_name]).to eq "Luke"
expect(address[:last_name]).to eq "Skywalker"
end
end

context "3 word name" do
let(:name) { "Obi Wan Kenobi" }

it "splits" do
address = mapped_address
expect(address[:first_name]).to eq "Obi Wan"
expect(address[:last_name]).to eq "Kenobi"
end
end
end
end
end

0 comments on commit 3df2323

Please sign in to comment.