Module: TradoStripeModule::ActiveRecord::ClassMethods

Defined in:
lib/trado_stripe_module/active_record.rb

Instance Method Summary collapse

Instance Method Details

#has_order_stripeObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/trado_stripe_module/active_record.rb', line 6

def has_order_stripe
    attr_accessible :stripe_customer_id, :stripe_card_token, :stripe_card_last4, :stripe_card_brand, :stripe_card_expiry_date
    attr_accessor :stripe_card_token

    after_commit :create_stripe_customer,                           on: :create, if: :no_stripe_customer_id?
    after_commit :update_stripe_customer,                           on: :update, if: :email_or_billing_name_changed?

    define_method("stripe_customer") do
        begin
            @customer ||= Stripe::Customer.retrieve(stripe_customer_id)
        rescue Stripe::InvalidRequestError
            create_stripe_customer
        end
    end

    define_method("stripe_cards") do
        stripe_customer.sources.all(object: "card")['data']
    end

    define_method("default_card") do
        stripe_customer.sources.retrieve(stripe_customer.default_source)
    end

    define_method("create_stripe_card") do
        card = stripe_customer.sources.create(source: stripe_card_token)
        self.update(
            stripe_card_last4: card.last4,
            stripe_card_brand: card.brand,
            stripe_card_expiry_date: "#{card.exp_month}/#{card.exp_year}"
        )
    end

    define_method("remove_redundant_stripe_cards") do
        stripe_cards.each do |card|
            stripe_customer.sources.retrieve(card.id).delete()
        end
    end

    define_method("create_stripe_customer") do
        customer = Stripe::Customer.create(
            email: email,
            description: billing_address.full_name
        )
        self.update_column(:stripe_customer_id, customer.id)
    end

    define_method("update_stripe_customer") do
        customer = stripe_customer
        customer.email = email
        customer.description = billing_address.full_name
        customer.save
    end

    define_method("no_stripe_customer_id?") do
        stripe_customer_id.present? ? false : true
    end

    define_method("email_or_billing_name_changed?") do
        email_changed? || billing_address.first_name_changed? || billing_address.last_name_changed?
    end
end

#has_store_setting_stripeObject



72
73
74
75
76
# File 'lib/trado_stripe_module/active_record.rb', line 72

def has_store_setting_stripe
    attr_accessible :stripe_statement_descriptor

    validates :stripe_statement_descriptor,                     length: { maximum: 22, message: :too_long }, format: { with: /\A[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*\z/}
end

#has_transaction_stripeObject



68
69
70
# File 'lib/trado_stripe_module/active_record.rb', line 68

def has_transaction_stripe
    attr_accessible :stripe_charge_id
end