balanced / balanced-ruby

Balanced API library in ruby.
MIT License
111 stars 47 forks source link

1.1: Customer DOB is broken on update. #156

Closed ChrisGF closed 10 years ago

ChrisGF commented 10 years ago

I am not sure exactly what is happening but I know that the dob field is being trashed on update.

From what I can tell the fetch is returning the object with the date being broken out into parts. I passed "1974-04-05" as the dob input to both calls. To see what was going on I added some logging to the initialize and save methods. Here is the output from the two calls. Both calls included the same data.

I will keep digging and see if I can find the spot where the dob on update is being initialized with what appears to be an array with the dob and the ssn.

I wrapped the initialize call to see what was happening.

    def initialize(attributes = {})
      Rails.logger.debug("Raw Attributes: #{attributes.inspect}")
      @attributes = Utils.indifferent_read_access attributes
      Rails.logger.debug("Read Attributes: #{@attributes.inspect}")
      @hyperlinks = {}
    end

    ...

    def save
      href = @attributes.delete('href')
      method = :post
      if href.nil?
        href = self.class.collection_path
      elsif !Balanced.is_collection(href)
        method = :put
      end

      attributes_to_submit = self.sanitize
      Rails.logger.debug("Submitting: #{attributes_to_submit.inspect}")
      begin
        @response = Balanced.send(method, href, attributes_to_submit)
      rescue Balanced::Error
        # restore the href on the instance if there was an exception
        # this will allow us to try to fix any attributes and save again
        @attributes['href'] = href
        raise
      end

      reload @response
    end

Create Method:

      @customer = Balanced::Customer.new(
        :name => (balanced_customer.business_contact_name.present? ? balanced_customer.business_contact_name : balanced_customer.legal_name),
        :email => (balanced_customer.business_contact_email.present? ? balanced_customer.business_contact_email : balanced_customer.user.email),
        :address => {
          :line1 => (balanced_customer.address.present? ? balanced_customer.address.address1 : nil),
          :line2 => (balanced_customer.address.present? ? balanced_customer.address.address2 : nil),
          :city => (balanced_customer.address.present? ? balanced_customer.address.city : nil),
          :state => (balanced_customer.address.present? ? balanced_customer.address.state : nil),
          :postal_code => (balanced_customer.address.present? ? balanced_customer.address.zipcode : nil),
          :country => "US",
        },
        :business_name => balanced_customer.business_name,
        :phone => (balanced_customer.phone_number.present? ? balanced_customer.phone_number : nil),
        :ein => (balanced_customer.ein.present? ? balanced_customer.ein : nil),
        :dob => (balanced_customer.dob.present? ? Date.parse(balanced_customer.dob).to_s(:balance) : nil),
        :ssn_last4 => (balanced_customer.ssn_last4.present? ? balanced_customer.ssn_last4 : nil),
        :meta => {
          groundfloor_id: balanced_customer.id,
          groundfloor_created_at: balanced_customer.created_at
        }
      ).save

Create Method output:

Raw Attributes: {:name=>"Date Test", :email=>"billsfannc@gmail.com", :address=>{:line1=>"100 Main Street", :line2=>nil, :city=>"Raleigh", :state=>"NC", :postal_code=>"11111", :country=>"US"}, :business_name=>"", :phone=>"9196513656", :ein=>nil, :dob=>"1974-04-05", :ssn_last4=>"1111", :meta=>{:groundfloor_id=>22, :groundfloor_created_at=>Sun, 23 Feb 2014 14:22:02 UTC +00:00}}

Read Attributes: {"name"=>"Date Test", "email"=>"billsfannc@gmail.com", "address"=>{"line1"=>"100 Main Street", "line2"=>nil, "city"=>"Raleigh", "state"=>"NC", "postal_code"=>"11111", "country"=>"US"}, "business_name"=>"", "phone"=>"9196513656", "ein"=>nil, "dob"=>"1974-04-05", "ssn_last4"=>"1111", "meta"=>{"groundfloor_id"=>22, "groundfloor_created_at"=>Sun, 23 Feb 2014 14:22:02 UTC +00:00}}

Balanced Key: name = Date Test
Balanced Key: email = billsfannc@gmail.com
Balanced Key: address = {"line1"=>"100 Main Street", "line2"=>nil, "city"=>"Raleigh", "state"=>"NC", "postal_code"=>"11111", "country"=>"US"}
Balanced Key: business_name =
Balanced Key: phone = 9196513656
Balanced Key: ein =
Balanced Key: dob = 1974-04-05
Balanced Key: ssn_last4 = 1111
Balanced Key: meta = {"groundfloor_id"=>22, "groundfloor_created_at"=>Sun, 23 Feb 2014 14:22:02 UTC +00:00}

Update Method:

      @customer = Balanced::Customer.find(balanced_customer.uri)
      @customer.name = (balanced_customer.business_contact_name.present? ? balanced_customer.business_contact_name : balanced_customer.legal_name)
      @customer.email = (balanced_customer.business_contact_email.present? ? balanced_customer.business_contact_email : balanced_customer.user.email)
      @customer.address["line1"] = (balanced_customer.address.present? ? balanced_customer.address.address1 : nil)
      @customer.address["line2"] = (balanced_customer.address.present? ? balanced_customer.address.address2 : nil)
      @customer.address["city"] = (balanced_customer.address.present? ? balanced_customer.address.city : nil)
      @customer.address["state"] = (balanced_customer.address.present? ? balanced_customer.address.state : nil)
      @customer.address["postal_code"] = (balanced_customer.address.present? ? balanced_customer.address.zipcode : nil)
      @customer.address["country"] = "US"
      @customer.business_name = (balanced_customer.business_name.present? ? balanced_customer.business_name : nil)
      @customer.phone = (balanced_customer.phone_number.present? ? balanced_customer.phone_number : nil)
      @customer.ein = (balanced_customer.ein.present? ? balanced_customer.ein : nil)
      @customer.dob = (balanced_customer.dob.present? ? Date.parse(balanced_customer.dob).to_s(:balance) : nil),
      @customer.ssn_last4 = (balanced_customer.ssn_last4.present? ? balanced_customer.ssn_last4 : nil)
      @customer.save

Update Call Output:

Raw Attributes: {"name"=>"Date Test", "links"=>{"source"=>nil, "destination"=>nil}, "updated_at"=>"2014-02-23T14:22:06.731072Z", "created_at"=>"2014-02-23T14:22:06.333409Z", "dob_month"=>nil, "id"=>"CU6NxIVrgxAcBHGPt1XOi7Z8", "phone"=>"9196513656", "href"=>"/customers/CU6NxIVrgxAcBHGPt1XOi7Z8", "merchant_status"=>"no-match", "meta"=>{"groundfloor_id"=>"22", "groundfloor_created_at"=>"2014-02-23T14:22:02.758Z"}, "dob_year"=>nil, "address"=>{"city"=>"Raleigh", "line2"=>nil, "line1"=>"100 Main Street", "state"=>"NC", "postal_code"=>"11111", "country_code"=>nil}, "business_name"=>"", "ssn_last4"=>nil, "email"=>"billsfannc@gmail.com", "ein"=>nil}

Read Attributes: {"name"=>"Date Test", "links"=>{"source"=>nil, "destination"=>nil}, "updated_at"=>"2014-02-23T14:22:06.731072Z", "created_at"=>"2014-02-23T14:22:06.333409Z", "dob_month"=>nil, "id"=>"CU6NxIVrgxAcBHGPt1XOi7Z8", "phone"=>"9196513656", "href"=>"/customers/CU6NxIVrgxAcBHGPt1XOi7Z8", "merchant_status"=>"no-match", "meta"=>{"groundfloor_id"=>"22", "groundfloor_created_at"=>"2014-02-23T14:22:02.758Z"}, "dob_year"=>nil, "address"=>{"city"=>"Raleigh", "line2"=>nil, "line1"=>"100 Main Street", "state"=>"NC", "postal_code"=>"11111", "country_code"=>nil}, "business_name"=>"", "ssn_last4"=>nil, "email"=>"billsfannc@gmail.com", "ein"=>nil}

Balanced Key: name = Date Test
Balanced Key: links = {"source"=>nil, "destination"=>nil}
Balanced Key: updated_at = 2014-02-23T14:22:06.731072Z
Balanced Key: created_at = 2014-02-23T14:22:06.333409Z
Balanced Key: dob_month =
Balanced Key: id = CU6NxIVrgxAcBHGPt1XOi7Z8
Balanced Key: phone = 9196513656
Balanced Key: merchant_status = no-match
Balanced Key: meta = {"groundfloor_id"=>"22", "groundfloor_created_at"=>"2014-02-23T14:22:02.758Z"}
Balanced Key: dob_year =
Balanced Key: address = {"city"=>"Raleigh", "line2"=>nil, "line1"=>"100 Main Street", "state"=>"NC", "postal_code"=>"11111", "country_code"=>nil, "country"=>"US"}
Balanced Key: business_name =
Balanced Key: ssn_last4 = 1111
Balanced Key: email = billsfannc@gmail.com
Balanced Key: ein =
Balanced Key: dob = ["1974-04-05", "1111"]
ChrisGF commented 10 years ago

This might not be a gem issue.. I am writing an irb script to reproduce.

ChrisGF commented 10 years ago

Nevermind it was a poorly placed comma in the code: