Sha256: 8b838163837f9a5c4ab81ba0d72f31aea7e7cc04b4c531d53bda0888543c78ae

Contents?: true

Size: 1.51 KB

Versions: 6

Compression:

Stored size: 1.51 KB

Contents

module StripeWrapper
  class Customer < ApplicationRecord

    belongs_to :user
    has_many :charges

    def to_s
      self.user.to_s
    end

    def self.get_customer(user)
      Customer.find_by_user_id(user.id) rescue nil
    end

    def self.build_stripe_customer(stripe_customer)
      return Customer.new(Customer.white_params(stripe_customer))
    end

    def self.find_or_create_customer(token,user)
      customer = Customer.get_customer(user)
      
      return customer if customer.present?()

      # If the customer is not present, we create it

      stripe_customer = StripeWrapper.create_customer(token,user)

      customer = Customer.build_stripe_customer(stripe_customer)

      customer.customer_id = stripe_customer.id
      customer.user_id = user.id

      customer.save

      return customer
    end

    def process_new_charge(amount,metadata={},currency='clp',description='')
      begin
        stripe_charge = StripeWrapper.create_charge(self.customer_id,nil,amount,currency,metadata,description)
        charge = Charge.build_stripe_charge(stripe_charge)
        charge.customer_id = self.id
        if charge.save
          return charge        
        else
          return nil
        end
      end
    end

    private
      def self.white_params(stripe_customer)
        params = ActionController::Parameters.new(stripe_customer.as_json)
        params.permit(:customer_id, :account_balance, :created, :currency, :default_source, :delinquent, :description, :discount, :email, :livemode)
      end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
stripe_wrapper-0.1.6 app/models/stripe_wrapper/customer.rb
stripe_wrapper-0.1.5 app/models/stripe_wrapper/customer.rb
stripe_wrapper-0.1.4 app/models/stripe_wrapper/customer.rb
stripe_wrapper-0.1.3 app/models/stripe_wrapper/customer.rb
stripe_wrapper-0.1.2 app/models/stripe_wrapper/customer.rb
stripe_wrapper-0.1.1 app/models/stripe_wrapper/customer.rb