Class: Killbill::Stripe::StripePaymentMethod

Inherits:
Plugin::ActiveMerchant::ActiveRecord::PaymentMethod
  • Object
show all
Defined in:
lib/stripe/models/payment_method.rb

Class Method Summary collapse

Class Method Details

.from_response(kb_account_id, kb_payment_method_id, kb_tenant_id, cc_or_token, response, options, extra_params = {:source_type => "cc"}, model = ::Killbill::Stripe::StripePaymentMethod) ⇒ Object



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
# File 'lib/stripe/models/payment_method.rb', line 7

def self.from_response(, kb_payment_method_id, kb_tenant_id, cc_or_token, response, options, extra_params = {:source_type => "cc"}, model = ::Killbill::Stripe::StripePaymentMethod)
  stripe_customer_id = options[:customer] || self.(, kb_tenant_id)
  if response.params["bank_account"]
    extra_params = {} #overwrite extra params because they will be passed with assumption of CC
    payment_response = {
      "token" => response.params["bank_account"]["id"],
      "address_country" => response.params["bank_account"]["country"],
    }
    customer_response = { "id" => stripe_customer_id }
    extra_params[:bank_name] = response.params["bank_account"]["bank_name"]
    extra_params[:bank_routing_number] = response.params["bank_account"]["routing_number"]
    extra_params[:source_type] = "bank_account"
    cc_or_token ||= response.params["bank_account"]["id"]
  elsif !stripe_customer_id.blank? && response.respond_to?(:responses)
    payment_response     = response.responses.first.params
    customer_response = response.responses.last.params
  elsif response.params['sources']
    payment_response     = response.params['sources']['data'][0]
    customer_response = response.params
    if response.params['sources']['data'][0]["object"] == "bank_account"
      extra_params[:bank_name] = response.params["sources"]["data"][0]["bank_name"]
      extra_params[:bank_routing_number] = response.params["sources"]["data"][0]["routing_number"]
      extra_params[:source_type] = "bank_account"
    end
  else
    payment_response = {}
    customer_response = { 'id' => stripe_customer_id }
  end

  super(,
        kb_payment_method_id,
        kb_tenant_id,
        cc_or_token,
        response,
        options,
        {
            :stripe_customer_id => customer_response['id'],
            :token              => payment_response['id'],
            :cc_first_name      => payment_response['name'],
            :cc_last_name       => nil,
            :cc_type            => payment_response['brand'],
            :cc_exp_month       => payment_response['exp_month'],
            :cc_exp_year        => payment_response['exp_year'],
            :cc_last_4          => payment_response['last4'],
            :address1           => payment_response['address_line1'],
            :address2           => payment_response['address_line2'],
            :city               => payment_response['address_city'],
            :state              => payment_response['address_state'],
            :zip                => payment_response['address_zip'],
            :country            => payment_response['address_country']
        }.merge!(extra_params.compact), # Don't override with nil values
        model)
end

.search_where_clause(t, search_key) ⇒ Object



61
62
63
# File 'lib/stripe/models/payment_method.rb', line 61

def self.search_where_clause(t, search_key)
  super.or(t[:stripe_customer_id].eq(search_key))
end

.stripe_customer_id_from_kb_account_id(kb_account_id, tenant_id) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/stripe/models/payment_method.rb', line 65

def self.(, tenant_id)
  pms = (, tenant_id)
  return nil if pms.empty?

  stripe_customer_ids = Set.new
  pms.each { |pm| stripe_customer_ids << pm.stripe_customer_id }
  raise "No Stripe customer id found for account #{}" if stripe_customer_ids.empty?
  raise "Kill Bill account #{} mapping to multiple Stripe customers: #{stripe_customer_ids.to_a}" if stripe_customer_ids.size > 1
  stripe_customer_ids.first
end