Sha256: 8a3c3a45e03839ea7d2249d04c5085919a4ca14be521a4ea5930534c4fcf3a54

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module Gemgento

  # @author Gemgento LLC
  class SavedCreditCard < ActiveRecord::Base
    belongs_to :user

    has_one :address, as: :addressable, class_name: 'Address', dependent: :destroy, inverse_of: :addressable

    accepts_nested_attributes_for :address

    before_create :magento_create, if: -> { Config[:extensions]['authorize-net-cim-payment-module'] }
    before_destroy :magento_destroy, if: -> { Config[:extensions]['authorize-net-cim-payment-module'] }

    attr_accessor :cc_cid

    private

    def magento_create
      response = Gemgento::API::SOAP::Authnetcim::Payment.create self

      if response.success?
        # mask the credit card number
        self.cc_number = self.cc_number.split(//).last(4).join
        self.cc_number = 'XXXX' + self.cc_number
        self.token = response.body[:result]

        return true
      else
        errors.add(:base, response.body[:faultstring])
        return false
      end
    end

    def magento_destroy
      response = Gemgento::API::SOAP::Authnetcim::Payment.destroy self.user.magento_id, self.token

      if response.success?
        return true
      else
        errors.add(:base, response.body[:faultstring])
        return false
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gemgento-2.8.0 app/models/gemgento/saved_credit_card.rb