0

I am setting up stripe connect with the example from https://github.com/rfunduk/rails-stripe-connect-example and am running into a problem using serialize to store stripe_account_status which should be stored as an array.

This is how it should be stored (Generated from the above example link)

enter image description here

{"details_submitted"=>false, "charges_enabled"=>true, "transfers_enabled"=>false, "fields_needed"=>["legal_entity.first_name", "legal_entity.last_name", "legal_entity.dob.day", "legal_entity.dob.month", "legal_entity.dob.year", "legal_entity.address.line1", "legal_entity.address.city", "legal_entity.address.postal_code", "bank_account"], "due_by"=>nil}

And this is how my application is storing it

enter image description here

{:details_submitted=>false, :charges_enabled=>true, :transfers_enabled=>false, :fields_needed=>["legal_entity.first_name", "legal_entity.last_name", "legal_entity.dob.day", "legal_entity.dob.month", "legal_entity.dob.year", "legal_entity.address.line1", "legal_entity.address.city", "legal_entity.address.postal_code", "bank_account"], :due_by=>nil}

As far as I am concerned everything is set up the same. The only difference is that the first example uses

serialize :stripe_account_status, JSON

and my app just has

serialize :stripe_account_status

The reason for this is that when I add JSON I this error:

JSON::ParserError - 795: unexpected token at '':

I have tried finding out the JSON error including changing the config/initializers/cookies_serializer.rb to use :hybrid but this is giving me the same error.

Could someone point me into the right direction of either fixing the JSON issue OR finding a way to make sure the stripe_account_status is stored as an array correctly.

Below is the methods used to store the array:

if @account
  user.update_attributes(
    currency: @account.default_currency,
    stripe_account_type: 'managed',
    stripe_user_id: @account.id,
    secret_key: @account.keys.secret,
    publishable_key: @account.keys.publishable,
    stripe_account_status: account_status
  )
end

def account_status
{
  details_submitted: account.details_submitted,
  charges_enabled: account.charges_enabled,
  transfers_enabled: account.transfers_enabled,
  fields_needed: account.verification.fields_needed,
  due_by: account.verification.due_by
}
end

Thanks I really appreciate any direction you could point me!

MikeHolford
  • 1,851
  • 2
  • 21
  • 32

1 Answers1

1

When you ask Rails to serialize an attribute on a model, it will default to storing the object as YAML string.

You can ask Rails to serialize differently, as you have noticed by providing a class to do the serialization e.g

serialize :stripe_account_status, JSON

The reason why this isn't working when you add it is because you presumably already have a record in the database using the YAML and so Rails can't parse this as a valid JSON string when reading from the DB. If it's just development data that you don't need, you can delete the records and then use JSON, otherwise you will need to convert the current YAML strings to JSON.

Rails will also symbolize the keys of a hash when parsing a serialized string in the database. This is the only difference between the hashes in your question and shouldn't matter in practise. Should you need String keys for some reason, you can use the #stringify_keys method on the hash provided by Rails.

Tom Livesey
  • 401
  • 1
  • 4
  • 10