class JeraPayment::Services::Iugu::Subscriptions::UpdateCredits

Public Class Methods

new(resource, credits, action) click to toggle source
# File lib/jera_payment/services/iugu/subscriptions/update_credits.rb, line 6
def initialize(resource, credits, action)
  @resource = resource
  @credits = { quantity: credits }
  @action = action
end

Public Instance Methods

call() click to toggle source
# File lib/jera_payment/services/iugu/subscriptions/update_credits.rb, line 12
def call
  begin
    ApplicationRecord.transaction do
      iugu_subscription_credits = eval("JeraPayment::Api::Iugu::Subscription.#{@action.to_s}_credits(@resource.api_id, @credits,
                                                                                                     @resource&.customer&.sub_account&.api_token)")

      raise(StandardError, iugu_subscription_credits[:errors]) if iugu_subscription_credits[:errors].present?

      set_api_attributes(iugu_subscription_credits)
    end
  rescue Exception => error
    add_error(error.message)
  end

  @resource
end

Private Instance Methods

set_api_attributes(attributes) click to toggle source
# File lib/jera_payment/services/iugu/subscriptions/update_credits.rb, line 30
def set_api_attributes(attributes)
  @resource.update_columns(credits: attributes[:credits])
end