Sha256: 649bf286fe78d2d12d60039a1c6b36edea0153698aa01ca855dcf01675e8158a

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

module TresDelta
  class CreditCard
    attr_reader :number, :name, :billing_address, :type, :nickname, :customer, :security_code

    attr_accessor :token, :expiration_month, :expiration_year

    def initialize(params = {})
      @number           = params[:number] || params[:card_account_number]
      @expiration_month = params[:expiration_month]
      @expiration_year  = params[:expiration_year]
      @token            = params[:token]
      @name             = params[:name]
      @billing_address  = Address.new(params[:billing_address] || {})
      @type             = params[:type]
      @nickname         = params[:nickname]
      @customer         = params[:customer] || Customer.new
      @security_code    = params[:security_code]
    end

    def save
      if token.nil?
        Vault.add_stored_credit_card(customer, self).success?
      else
        Vault.update_stored_credit_card(customer, self).success?
      end
    end

    class << self
      def create(customer, params = {})
        CreditCard.new(params).tap do |credit_card|
          add_card = Vault.add_stored_credit_card(customer, credit_card)
          raise InvalidCreditCard unless add_card.success?

          credit_card.token = add_card.token
        end
      end

      def find(customer, token, load_number = false)
        stored_card_details = Vault.get_stored_credit_card(customer, token, load_number)
        raise CreditCardNotFound unless stored_card_details.success?

        CreditCard.new(stored_card_details.credit_card.merge(customer: customer))
      end
    end
  end

  class InvalidCreditCard < Exception; end
  class CreditCardNotFound < Exception; end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tres_delta-0.1.0 lib/tres_delta/credit_card.rb