-
-
Save khempenius/55c6c1ca82163edb4bb2 to your computer and use it in GitHub Desktop.
Import Customers from Stripe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. | |
def get_customers(practice, params=nil) | |
Stripe.api_key = practice.stripe_token | |
response = Stripe::Customer.all(params) | |
response.data.each do |customer| | |
stripe_customer_id = customer.id | |
source_with_name = customer.sources.data.find{ |source| source.try(:name) } | |
customer_name = source_with_name.name | |
if customer_name.nil? | |
puts "ERROR: No name found in the sources for #{stripe_customer_id}" | |
next | |
end | |
matches = practice.patients.where(first_name: customer_name.split(" ").first, last_name: customer_name.split(" ").last) | |
if matches.count != 1 | |
puts "ERROR: #{stripe_customer_id} matches #{matches.count} patients" | |
next | |
end | |
patient = matches.first | |
if patient.stripe_customer_id.nil? | |
patient.update_attribute(:stripe_customer_id, stripe_customer_id) | |
puts "#{stripe_customer_id} matched with #{patient.name} (#{patient.ident})" | |
end | |
end | |
last_customer = response.data.last.id | |
params = {starting_after: last_customer} | |
get_customers(practice.id, params) if response.has_more | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment