Sha256: 8fa9fd102345221f40838a4274857d481c9b5866afb0db7d1ea23f531cd0a506

Contents?: true

Size: 1008 Bytes

Versions: 14

Compression:

Stored size: 1008 Bytes

Contents

module Spree
  class Subscription < ActiveRecord::Base
    module ApiHandler
      extend ActiveSupport::Concern

      included do
        attr_accessor :card_token
        before_create :subscribe
        before_update :unsubscribe, :if => [:unsubscribed_at_changed?, :unsubscribed_at?]
      end

      def subscribe
        provider.subscribe(self)
        self.subscribed_at = Time.current
      end

      def unsubscribe
        provider.unsubscribe(self)
      end

      def save_and_manage_api(*args)
        begin
          new_record? ? save : update_attributes(*args)
        rescue provider.error_class, ActiveRecord::RecordNotFound => e
          logger.error "Error while subscribing: #{e.message}"
          errors.add :base, "There was a problem with your credit card"
          false
        end
      end

      def provider
        plan.try(:recurring).present? ? plan.recurring : (raise ActiveRecord::RecordNotFound.new("Provider not found."))
      end

      private
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
spree_account_recurring-1.1.5 app/models/concerns/spree/subscription/api_handler.rb
spree_account_recurring-1.0.7 app/models/concerns/spree/subscription/api_handler.rb
spree_account_recurring-1.1.4 app/models/concerns/spree/subscription/api_handler.rb
spree_account_recurring-1.0.6 app/models/concerns/spree/subscription/api_handler.rb
spree_account_recurring-1.0.5 app/models/concerns/spree/subscription/api_handler.rb
spree_account_recurring-1.1.3 app/models/concerns/spree/subscription/api_handler.rb
spree_account_recurring-1.1.2 app/models/concerns/spree/subscription/api_handler.rb
spree_account_recurring-1.0.4 app/models/concerns/spree/subscription/api_handler.rb
spree_account_recurring-1.0.3 app/models/concerns/spree/subscription/api_handler.rb
spree_account_recurring-1.0.2 app/models/concerns/spree/subscription/api_handler.rb
spree_account_recurring-1.1.1 app/models/concerns/spree/subscription/api_handler.rb
spree_account_recurring-1.1.0 app/models/concerns/spree/subscription/api_handler.rb
spree_account_recurring-1.0.1 app/models/concerns/spree/subscription/api_handler.rb
spree_account_recurring-1.0.0 app/models/concerns/spree/subscription/api_handler.rb