Class: ActiveMerchant::Billing::StripeGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/stripe/ext/active_merchant/active_merchant.rb

Defined Under Namespace

Classes: BankAccount

Constant Summary collapse

BANK_ACCOUNT_HOLDER_TYPE_MAPPING =
{
  "personal" => "individual",
  "business" => "company",
}

Instance Method Summary collapse

Instance Method Details

#add_amount(post, money, options, include_currency = false) ⇒ Object

Ignore the call to localized_amount upstream (money is already cents here)



46
47
48
49
50
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 46

def add_amount(post, money, options, include_currency = false)
  currency = options[:currency] || currency(money)
  post[:amount] = money
  post[:currency] = currency.downcase if include_currency
end

#bank_account_params(bank_account, options = {}) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 158

def (, options = {})
   = BANK_ACCOUNT_HOLDER_TYPE_MAPPING[.type]

  post = {
    bank_account: {
      account_number: .,
      country: 'US',
      currency: 'usd',
      routing_number: .routing_number,
      name: .bank_name,
      account_holder_type: ,
    }
  }
end

#commit(method, url, parameters = nil, options = {}) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 130

def commit(method, url, parameters = nil, options = {})
  if options[:bank_account]
    response = api_request(:post, url)
    success = response["error"].nil?

    Response.new(success, nil, response)
  else
    add_expand_parameters(parameters, options) if parameters
    response = api_request(method, url, parameters, options)

    success = !response.key?("error")

    card = card_from_response(response)
    avs_code = AVS_CODE_TRANSLATOR["line1: #{card["address_line1_check"]}, zip: #{card["address_zip_check"]}"]
    cvc_code = CVC_CODE_TRANSLATOR[card["cvc_check"]]
    Response.new(success,
                 success ? "Transaction approved" : response["error"]["message"],
                 response,
                 :test => response.has_key?("livemode") ? !response["livemode"] : false,
                 :authorization => authorization_from(success, url, method, response),
                 :avs_result => { :code => avs_code },
                 :cvv_result => cvc_code,
                 :emv_authorization => emv_authorization_from_response(response),
                 :error_code => success ? nil : error_code_from(response)
                )
  end
end

#create_managed_account(account = {}, options = {}) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 16

def ( = {}, options = {})
  post = .dup
  post[:country] ||= 'US'
  post[:managed] = true

  commit(:post, 'accounts', post, options)
end

#get_balance(options = {}) ⇒ Object



12
13
14
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 12

def get_balance(options = {})
  commit(:get, 'balance', nil, options)
end

#headers(options = {}) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 36

def headers(options = {})
  headers = old_headers(options)

   = options.delete(:stripe_account)
  headers['Stripe-Account'] =  unless .nil?

  headers
end

#old_headersObject



34
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 34

alias_method :old_headers, :headers

#purchase(money, payment, options = {}) ⇒ Object

To create a charge on a card or a token, call

purchase(money, card_hash_or_token, { ... })

To create a charge on a customer, call

purchase(money, nil, { :customer => id, ... })


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 59

def purchase(money, payment, options = {})
  responses = MultiResponse.run do |r|
    if payment.is_a?(ApplePayPaymentToken)
      r.process { tokenize_apple_pay_token(payment) }
      payment = StripePaymentToken.new(r.params["token"]) if r.success?
    end
    r.process do
      post = create_post_for_auth_or_purchase(money, payment, options)
      commit(:post, 'charges', post, options)
    end
  end.responses.last
end

#store(payment, options = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 72

def store(payment, options = {})
  params = {}
  post = {}

  if payment.is_a?(ApplePayPaymentToken)
    token_exchange_response = tokenize_apple_pay_token(payment)
    params = { :card => token_exchange_response.params["token"]["id"] } if token_exchange_response.success?
  elsif payment.is_a?(BankAccount)
    post.merge!((payment, options))
  else
    add_creditcard(params, payment, options)
  end

  unless payment.is_a?(BankAccount)
    post[:validate] = options[:validate] unless options[:validate].nil?
    post[:description] = options[:description] if options[:description]
    post[:email] = options[:email] if options[:email]
  end

  if post[:bank_account]
    customer_url = "customers"
    if options[:customer]
      customer_url += "/#{CGI.escape(options[:customer])}/sources"
    end
    responses = MultiResponse.run do |r|
      # get token and associate it with the customer
      r.process { commit(:post, "tokens?#{post_data(post)}", nil, { bank_account: true }) }

      if r.success?
        r.process { commit(:post, customer_url, { source: r.params["id"] } ) }
      end
    end.responses
    if options[:customer]
      return responses.first
    else
      return responses.last
    end
  elsif options[:account]
    (post, params, payment)
    commit(:post, "accounts/#{CGI.escape(options[:account])}/external_accounts", post, options)
  elsif options[:customer]
    MultiResponse.run(:first) do |r|
      # The /cards endpoint does not update other customer parameters.
      r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/cards", params, options) }

      if options[:set_default] and r.success? and !r.params['id'].blank?
        post[:default_card] = r.params['id']
      end

      if post.count > 0
        r.process { update_customer(options[:customer], post) }
      end
    end
  else
    commit(:post, 'customers', post.merge(params), options)
  end
end

#user_agentObject



24
25
26
27
28
29
30
31
32
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 24

def user_agent
  @@ua ||= JSON.dump({
                         :bindings_version => KB_PLUGIN_VERSION,
                         :lang => 'ruby',
                         :lang_version => "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
                         :platform => RUBY_PLATFORM,
                         :publisher => 'killbill'
                     })
end