Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace :stripe_uuids do
- desc "populates stripe uuids for all payment resources"
- task populate: :environment do
- File.open("creditcards.txt", "a") do |file|
- # customers = CustomerAccount.all
- # customers.each do |c|
- # return if c.user.nil?
- # begin
- # #populate customers
- # balanced_customer = Balanced::Customer.find(c.href)
- # if balanced_customer && balanced_customer.meta.present?
- # if balanced_customer.meta["stripe.customer_id"].present?
- # customer_id = balanced_customer.meta["stripe.customer_id"]
- # if stripe_customer = Stripe::Customer.retrieve(customer_id)
- # file.write "found stripe customer #{customer_id} for customer #{c.user.email}\n"
- # # c.stripe_uuid = customer_id
- # end
- # end
- # #bank accounts
- # #populate owner's stripe accounts
- # if balanced_customer.meta["stripe.account_id"].present?
- # account_id = balanced_customer.meta["stripe.account_id"]
- # if stripe_account = Stripe::Account.retrieve(account_id)
- # file.write "found stripe account #{balanced_customer.meta["stripe.account_id"]} for #{c.user.email}\n"
- # # c.account = account_id
- # if stripe_account.bank_accounts.present? && stripe_account.bank_accounts.data.present? #bank accounts
- # stripe_bank_account_data = stripe_account.bank_accounts.data
- # bank_account = c.bank_accounts.first
- # file.write "updating bank account #{bank_account.id} with stripe uuid #{stripe_bank_account_data.first.id} for #{c.user.email}\n"
- # # bank_account.update(
- # # bank_name: stripe_bank_account_data.first.bank_name,
- # # account_number_last_four: stripe_bank_account_data.first.last4,
- # # stripe_uuid: stripe_bank_account_data.first.id,
- # # )
- # c.bank_accounts.each do |other|
- # unless other == bank_account
- # file.write "deleting bank account #{other.id} for #{c.user.email}\n"
- # # other.destroy!
- # end
- # end
- # end
- # end
- # end
- # end
- # rescue Balanced::NotFound => e
- # file.write "could not find a stripe resource for #{c.id}\n"
- # rescue => e
- # file.write("ERROR: " + e.message + " for #{c.id}\n")
- # end
- # end
- #would you like to delete all non primary credit cards? yes.
- cards = CreditCard.where(stripe_uuid: nil).where(primary: true)
- cards.each do |card|
- begin
- stripe_card_id= nil
- # balanced_card = Balanced::Card.find(card.href)
- # if balanced_card.meta.present? && balanced_card.meta["stripe_customer.funding_instrument.id"].present?
- # card_id = balanced_card.meta["stripe_customer.funding_instrument.id"]
- stripe_customer = Stripe::Customer.retrieve(card.customer_account.stripe_uuid)
- stripe_customer.sources.data.each do |source|
- stripe_card_id = source.id if source.last4 = card.last_four
- end
- if stripe_card_id.present?
- file.write "found stripe cc #{card.id} with stripe id #{stripe_card_id} for customer #{stripe_customer.id}\n"
- puts "found stripe cc #{card.id} with stripe id #{stripe_card_id} for customer #{stripe_customer.id}\n"
- card.stripe_uuid = stripe_card_id
- card.save
- end
- # end
- rescue => e
- puts "ERROR: #{e.message} for card #{card.id}\n"
- file.write "ERROR: #{e.message} for card #{card.id}\n"
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement