Skip to content

Instantly share code, notes, and snippets.

@blakewest
Forked from khempenius/gist:55c6c1ca82163edb4bb2
Last active September 22, 2015 21:02
Show Gist options
  • Save blakewest/ed22e52731f33f53f1f3 to your computer and use it in GitHub Desktop.
Save blakewest/ed22e52731f33f53f1f3 to your computer and use it in GitHub Desktop.
Import Customers from Stripe
# Used when a practice's patients have been transferred to Stripe from
# another payment processing platform and we need to associate their Stripe
# customers with their Hint patients. This is done by matching credit card name
# to patient name or to email, or to address, or hint hid.
def get_customers(practice, params = nil)
response = Stripe::Customer.all(params, practice.stripe_token)
response.data.each do |customer|
stripe_customer_id = customer.id
source_with_name = customer.sources.data.find { |source| source.try(:name) }
source_with_address = customer.sources.data.find { |source| source.try(:address_line1) }
customer_name = source_with_name.try(:name)
customer_email = customer.email
customer_address = source_with_address.try(:address_line1)
customer_hint_id = customer.metadata["hint_id"]
id_match = customer_hint_id && practice.patients.find_by_ident(customer_hint_id)
name_matches = customer_name && practice.patients.where("LOWER(patients.first_name) = ? AND LOWER(patients.last_name) = ?", customer_name.split(' ').first.downcase, customer_name.split(' ').last.downcase)
email_matches = customer_email && practice.patients.where(email: customer_email)
address_matches = customer_address && practice.patients.joins(:address).where('addresses.line1 = ?', customer_address)
if id_match
update_stripe_customer_id_on_patient(id_match, stripe_customer_id)
next
end
if email_matches.try(:count) == 1
update_stripe_customer_id_on_patient(email_matches.first, stripe_customer_id)
next
end
if address_matches.try(:count) == 1
update_stripe_customer_id_on_patient(address_matches.first, stripe_customer_id)
next
end
if name_matches.try(:count) == 1
update_stripe_customer_id_on_patient(name_matches.first, stripe_customer_id)
next
end
puts "#{customer_name} - ERROR: (#{stripe_customer_id}) matches #{email_matches.try(:count)} patient emails, #{address_matches.try(:count)} patient addresses, and #{name_matches.try(:count)} patient names."
end
last_customer = response.data.last.id
params = {starting_after: last_customer}
get_customers(practice, params) if response.has_more
end
def update_stripe_customer_id_on_patient(patient, stripe_customer_id)
if patient.stripe_customer_id.nil?
patient.update_attribute(:stripe_customer_id, stripe_customer_id)
puts "SUCCESS: #{stripe_customer_id} matched with #{patient.name} (#{patient.ident})"
elsif patient.stripe_customer_id == stripe_customer_id
print "."
else
puts "ERROR: #{patient.name} has stripe_customer_id #{patient.stripe_customer_id}, but stripe has someone with the same name or email as having customer id #{stripe_customer_id}"
end
end
# Use this after getting all the customers.
def import_all_cards_from_existing_stripe_customers(practice)
practice.patients.where.not(stripe_id: nil).map { |patient| PaymentProcessor::CustomerImporter.new(patient).import }
end
# Use this if you know a stripe_customer goes with a certain hint patient, but they won't otherwise
# be linked from the above function because, for instance, they match multiple hint patients.
def link_patient_with_stripe(practice, patient_ident, stripe_id)
patient = practice.patients.find_by_ident(patient_ident)
patient.update_attribute(:stripe_customer_id, stripe_id)
PaymentProcessor::CustomerImporter.new(patient).import
end
# Send this an array of arrays. Like... [['pat-3jlk3', 'cus_38jk3lkj'], ...]
def link_list_of_patients_with_stripe(list_of_patients_with_stripe)
list_of_patients_with_stripe.each do |patient_id_stripe_id_pair|
patient_id = patient_id_stripe_id_pair[0]
stripe_id = patient_id_stripe_id_pair[1]
practice = Patient.find_by_ident(patient_id).practice
link_patient_with_stripe(practice, patient_id, stripe_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment