Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def canceled_subscriptions
canceled_subscriptions = []
offset = 0
subscriptions = Moip::Subscription.new.load offset
while subscriptions.any?
subscriptions.each do |subscription|
if subscription["status"] == "CANCELED"
canceled_subscriptions << subscription["code"]
end
end
offset += 100
subscriptions = Moip::Subscription.new.load offset
end
canceled_subscriptions
end
def active_subscriptions
active_subscriptions = []
offset = 0
subscriptions = Moip::Subscription.new.load offset
while subscriptions.any?
subscriptions.each do |subscription|
if subscription["status"] == "ACTIVE"
active_subscriptions << subscription["code"]
end
end
offset += 100
subscriptions = Moip::Subscription.new.load offset
end
active_subscriptions
end
def update_subscription_state code
moip_subscription = Moip::Subscription.new.find code
if moip_subscription.present?
puts "Updating #{code}..."
subscription = Subscription.find_by_code(code)
subscription.update_attribute :state, moip_subscription["status"].downcase
end
end
def all_moip_subscriptions
subscriptions = []
offset = 0
list = Moip::Subscription.new.load offset
while list.any?
subscriptions.concat list
offset += 100
list = Moip::Subscription.new.load offset
end
subscriptions
end
def missing_subscriptions
missing_subscriptions = []
offset = 0
subscriptions = Moip::Subscription.new.load offset
while subscriptions.any?
subscriptions.each do |subscription|
if Subscription.find_by_code(subscription["code"]).nil?
missing_subscriptions << subscription["code"]
end
end
offset += 100
subscriptions = Moip::Subscription.new.load offset
end
missing_subscriptions
end
def create_subscription codes
codes.each do |code|
puts "Processing #{code}..."
subscription = Moip::Subscription.new.find code
if Subscription.find_by_code(code).nil?
user = User.find_by_email(subscription["customer"]["email"])
if subscription["plan"]["code"].match(/Mensal/) || subscription["plan"]["code"] == "009" || subscription["plan"]["code"] == "017" || subscription["plan"]["code"] == "015" || subscription["plan"]["code"] == "050"
plan = 'monthly'
end
params = {
project_id: 4,
user_id: user.id,
value: subscription["amount"].to_f/100,
payment_option: "creditcard",
plan: plan
}
puts "Creating #{code}..."
new_subscription = Subscription.create!(params)
new_subscription.update_attribute :code, code
end
end
end
def ghost_scubscriptions
list = []
Subscription.all.each do |subscription|
puts subscription.id
if Moip::Subscription.new.find(subscription.code).nil?
list << subscription
end
end
list
end
def fix_subscriptions_owners codes
codes.each do |code|
puts "Processing #{code}"
moip_subscription = Moip::Subscription.new.find(code)
subscription = Subscription.find_by_code(code)
if subscription.present?
puts "Changing user #{subscription.user.email} to #{moip_subscription["customer"]["email"]}"
subscription.user = User.find_by_email(moip_subscription["customer"]["email"])
subscription.save!
end
end
end
def fix_subscriptions_codes codes
codes.each do |code|
puts "Processing #{code}"
subscription = Moip::Subscription.new.find(code)
user = User.find_by_email(subscription["customer"]["email"])
next if user.nil? || user.subscriptions.size == 0
# Check if all the user's subscriptions have different dates
subscriptions_dates = user.subscriptions.map{|s| s.created_at.strftime("%d%m%Y")}
next if subscriptions_dates.uniq.size != user.subscriptions.size
user.subscriptions.each do |user_subscription|
if(user_subscription.created_at.day == subscription["creation_date"]["day"] &&
user_subscription.created_at.month == subscription["creation_date"]["month"] &&
user_subscription.created_at.year == subscription["creation_date"]["year"] &&
user_subscription.value.to_i * 100 == subscription["amount"])
puts "Updating #{user_subscription.code} to #{code}"
user_subscription.update_attribute :code, code
end
end
end
end
def migrate_invoices code
i = Moip::Invoice.new
i.subscription_code = code
i.invoices.each do |invoice|
Invoice.create(
uid: invoice["id"],
subscription_id: Subscription.find_by_code(code).id,
value: invoice["amount"].to_f/100,
occurrence: invoice["occurrence"],
status: Invoice::STATUS[invoice["status"]["code"].to_s]
)
end
end
def migrate_retry_payment_link_to_mailchimp payment
include Rails.application.routes.url_helpers
if payment.subscription.present? && !payment.subscription.creditcard?
puts "Migrating #{payment.id}..."
payment.update_user_data retry_link: retry_payment_url(payment)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.