Rails : Run/Test Stripe Integrated Production database in Localhost

Its not that easy to run Stripe Integrated Rails app’s production database in localhost. Yes you can export-Import the database. You can see this blog post to understand how to load production database to localhost.

Things you need to know

  • You don’t have the logins for users other than you
  • You would be using ‘Test-API Key‘ for stripe in local but the customer objects you stored were created using ‘Live Keys‘ which wont be available using ‘Test Keys
  • There is risk of sending Email/SMS to the production users which is not good.

How to proceed then?

I am assuming we are using Devise for authentication. Since we don’t have passwords to get into those accounts, we need to forcefully update the passwords with our own password.

You remember there is a field called ‘:encrypted_password‘ in Users table. You can batch update the passwords like

new_password_digest = User.new(:password => 'password').encrypted_password
User.all.update_all(encrypted_password: new_password_digest)

This will set all passwords to ‘password‘. Now you will be able to login to all the accounts.

Next Issue: Original Email / SMS

Still there are original email addresses involved. In case of any changes there is still chances of sending Email/SMSes to original customer. So we need to batch update email/phone_numbers as well.

my_email = 'email@shivabhusal.com'
my_phone_number = '9843492222'

non_admin_users = User.joins(:roles).where.not({roles: {name: 'admin'}})
non_admin_users.each do |user|
  email_split = my_email.split('@')

  # downcase is important!
  addition = user.first_name.downcase.gsub(' ', '') rescue ''
  addition << "_#{user.last_name.downcase.gsub(' ', '')}" rescue ''
  addition << "+#{user.roles.first.name}" rescue ''
  email_split.insert(1, "+#{addition}+#{user.id}@")

  user.phone = my_phone_number

  user.email = email_split.join()
  user.save validate: false

Next Issue : Stripe Customer / Subscription Objects


How to solve?

I prefer to create a new customer object for every customer in database and also all the subscription objects, card object to be replaced by new card objects, subscription objects and all.




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s