Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace :premium_users do
- desc 'Get the user id & data in a hash'
- task get_users: :environment do
- users = Medic.includes(:payment_methods).where.not payment_methods: { period_end_date: nil }
- request_hash = {}
- users.each do |user|
- period_end_date = user.payment_methods.last.period_end_date
- if Date.today - period_end_date <= 172_800
- openpay_customer_id = user.payment_methods.last.openpay_customer_id
- openpay_subscription_id = user.payment_methods.last.openpay_subscription_id
- medic_id = user.payment_methods.last.medic_id
- user_hash = {}
- user_hash[medic_id] = [openpay_customer_id, openpay_subscription_id]
- else
- user_hash = {}
- end
- request_hash.merge!(user_hash) # If no users is empty => {}
- if request_hash.empty?
- puts 'No User to check!'
- else
- request_hash.each_value do |value|
- openpay_customer_id = value[0]
- openpay_subscription_id = value[1]
- @openpay = OpenpayApi.new(ENV['MERCHANT_ID_OPENPAY'], ENV['SECRET_KEY_OPENPAY'])
- @subscriptions = @openpay.create(:subscriptions)
- response_hash = @subscriptions.get(openpay_subscription_id, openpay_customer_id)
- @charge_date, @creation_date, @current_period_number, @period_end_date, @status, @trial_end_date = response_hash.values_at('charge_date', 'creation_date', 'current_period_number', 'period_end_date', 'status', 'trial_end_date')
- case @status
- when 'past_due'
- @premium = false
- @cancelled = Time.now
- when 'unpaid'
- @premium = false
- @cancelled = Time.now
- when 'cancelled'
- @premium = false
- @cancelled = Time.now
- else
- @premium = true
- @cancelled = nil
- end
- end
- @payment_method = PaymentMethod.update(
- premium: @premium,
- charge_date: @charge_date,
- creation_date: @creation_date,
- current_period_number: @current_period_number,
- period_end_date: @period_end_date,
- status: @status,
- trial_end_date: @trial_end_date,
- cancelled_on: @cancelled
- )
- puts "User #{user.email} update, Complete!"
- end
- end
- end
- end
- class Medic < ApplicationRecord
- has_many :payment_methods, dependent: :destroy
- end
- class PaymentMethod < ApplicationRecord
- belongs_to :medic, optional: true
- end
- SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:15:13', `updated_at` = '2018-06-15 21:01:19' WHERE `payment_methods`.`id` = 1
- (11.9ms) COMMIT
- (0.1ms) BEGIN
- SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:15:13', `updated_at` = '2018-06-15 21:01:19' WHERE `payment_methods`.`id` = 2
- (2.7ms) COMMIT
- (0.1ms) BEGIN
- SQL (0.2ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:15:13', `updated_at` = '2018-06-15 21:01:19' WHERE `payment_methods`.`id` = 3
- (2.6ms) COMMIT
- User medic1@mail.com update, Complete!
- PaymentMethod Load (0.3ms) SELECT `payment_methods`.* FROM `payment_methods`
- (0.1ms) BEGIN
- SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:16:24', `updated_at` = '2018-06-15 21:01:20' WHERE `payment_methods`.`id` = 1
- (11.9ms) COMMIT
- (0.1ms) BEGIN
- SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:16:24', `updated_at` = '2018-06-15 21:01:20' WHERE `payment_methods`.`id` = 2
- (2.7ms) COMMIT
- (0.1ms) BEGIN
- SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:16:24', `updated_at` = '2018-06-15 21:01:20' WHERE `payment_methods`.`id` = 3
- (2.5ms) COMMIT
- User medic2@mail.com update, Complete!
- PaymentMethod Load (0.3ms) SELECT `payment_methods`.* FROM `payment_methods`
- (0.1ms) BEGIN
- SQL (0.2ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:18:30', `updated_at` = '2018-06-15 21:01:22' WHERE `payment_methods`.`id` = 1
- (2.6ms) COMMIT
- (0.1ms) BEGIN
- SQL (0.2ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:18:30', `updated_at` = '2018-06-15 21:01:22' WHERE `payment_methods`.`id` = 2
- (2.6ms) COMMIT
- (0.1ms) BEGIN
- SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:18:30', `updated_at` = '2018-06-15 21:01:22' WHERE `payment_methods`.`id` = 3
- (2.6ms) COMMIT
- User medic4@mail.com update, Complete!
Add Comment
Please, Sign In to add comment