Advertisement
Guest User

Untitled

a guest
May 25th, 2015
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. namespace :stripe_uuids do
  2. desc "populates stripe uuids for all payment resources"
  3. task populate: :environment do
  4. File.open("creditcards.txt", "a") do |file|
  5.  
  6. # customers = CustomerAccount.all
  7. # customers.each do |c|
  8. # return if c.user.nil?
  9. # begin
  10. # #populate customers
  11. # balanced_customer = Balanced::Customer.find(c.href)
  12. # if balanced_customer && balanced_customer.meta.present?
  13.  
  14. # if balanced_customer.meta["stripe.customer_id"].present?
  15. # customer_id = balanced_customer.meta["stripe.customer_id"]
  16. # if stripe_customer = Stripe::Customer.retrieve(customer_id)
  17. # file.write "found stripe customer #{customer_id} for customer #{c.user.email}\n"
  18. # # c.stripe_uuid = customer_id
  19. # end
  20. # end
  21.  
  22. # #bank accounts
  23.  
  24. # #populate owner's stripe accounts
  25. # if balanced_customer.meta["stripe.account_id"].present?
  26. # account_id = balanced_customer.meta["stripe.account_id"]
  27. # if stripe_account = Stripe::Account.retrieve(account_id)
  28. # file.write "found stripe account #{balanced_customer.meta["stripe.account_id"]} for #{c.user.email}\n"
  29. # # c.account = account_id
  30. # if stripe_account.bank_accounts.present? && stripe_account.bank_accounts.data.present? #bank accounts
  31. # stripe_bank_account_data = stripe_account.bank_accounts.data
  32. # bank_account = c.bank_accounts.first
  33.  
  34. # file.write "updating bank account #{bank_account.id} with stripe uuid #{stripe_bank_account_data.first.id} for #{c.user.email}\n"
  35. # # bank_account.update(
  36. # # bank_name: stripe_bank_account_data.first.bank_name,
  37. # # account_number_last_four: stripe_bank_account_data.first.last4,
  38. # # stripe_uuid: stripe_bank_account_data.first.id,
  39. # # )
  40.  
  41. # c.bank_accounts.each do |other|
  42. # unless other == bank_account
  43. # file.write "deleting bank account #{other.id} for #{c.user.email}\n"
  44. # # other.destroy!
  45. # end
  46. # end
  47. # end
  48. # end
  49. # end
  50.  
  51. # end
  52. # rescue Balanced::NotFound => e
  53. # file.write "could not find a stripe resource for #{c.id}\n"
  54. # rescue => e
  55. # file.write("ERROR: " + e.message + " for #{c.id}\n")
  56. # end
  57. # end
  58.  
  59. #would you like to delete all non primary credit cards? yes.
  60. cards = CreditCard.where(stripe_uuid: nil).where(primary: true)
  61. cards.each do |card|
  62. begin
  63. stripe_card_id= nil
  64. # balanced_card = Balanced::Card.find(card.href)
  65. # if balanced_card.meta.present? && balanced_card.meta["stripe_customer.funding_instrument.id"].present?
  66. # card_id = balanced_card.meta["stripe_customer.funding_instrument.id"]
  67. stripe_customer = Stripe::Customer.retrieve(card.customer_account.stripe_uuid)
  68. stripe_customer.sources.data.each do |source|
  69. stripe_card_id = source.id if source.last4 = card.last_four
  70. end
  71.  
  72. if stripe_card_id.present?
  73. file.write "found stripe cc #{card.id} with stripe id #{stripe_card_id} for customer #{stripe_customer.id}\n"
  74. puts "found stripe cc #{card.id} with stripe id #{stripe_card_id} for customer #{stripe_customer.id}\n"
  75. card.stripe_uuid = stripe_card_id
  76. card.save
  77. end
  78. # end
  79. rescue => e
  80. puts "ERROR: #{e.message} for card #{card.id}\n"
  81. file.write "ERROR: #{e.message} for card #{card.id}\n"
  82. end
  83. end
  84. end
  85. end
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement