Class: ActiveMerchant::Billing::StripeGateway

Inherits:
Gateway
  • Object
show all
Defined in:
lib/active_merchant/billing/gateways/stripe.rb

Direct Known Subclasses

WebpayGateway

Defined Under Namespace

Classes: StripePaymentToken

Constant Summary collapse

AVS_CODE_TRANSLATOR =
{
  'line1: pass, zip: pass' => 'Y',
  'line1: pass, zip: fail' => 'A',
  'line1: pass, zip: unchecked' => 'B',
  'line1: fail, zip: pass' => 'Z',
  'line1: fail, zip: fail' => 'N',
  'line1: unchecked, zip: pass' => 'P',
  'line1: unchecked, zip: unchecked' => 'I'
}
CVC_CODE_TRANSLATOR =
{
  'pass' => 'M',
  'fail' => 'N',
  'unchecked' => 'P'
}
CURRENCIES_WITHOUT_FRACTIONS =
['BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'VUV', 'XAF', 'XOF', 'XPF']
STANDARD_ERROR_CODE_MAPPING =
{
  'incorrect_number' => STANDARD_ERROR_CODE[:incorrect_number],
  'invalid_number' => STANDARD_ERROR_CODE[:invalid_number],
  'invalid_expiry_month' => STANDARD_ERROR_CODE[:invalid_expiry_date],
  'invalid_expiry_year' => STANDARD_ERROR_CODE[:invalid_expiry_date],
  'invalid_cvc' => STANDARD_ERROR_CODE[:invalid_cvc],
  'expired_card' => STANDARD_ERROR_CODE[:expired_card],
  'incorrect_cvc' => STANDARD_ERROR_CODE[:incorrect_cvc],
  'incorrect_zip' => STANDARD_ERROR_CODE[:incorrect_zip],
  'card_declined' => STANDARD_ERROR_CODE[:card_declined],
  'processing_error' => STANDARD_ERROR_CODE[:processing_error]
}

Constants inherited from Gateway

Gateway::CREDIT_DEPRECATION_MESSAGE, Gateway::DEBIT_CARDS, Gateway::RECURRING_DEPRECATION_MESSAGE, Gateway::STANDARD_ERROR_CODE

Instance Attribute Summary

Attributes inherited from Gateway

#options

Instance Method Summary collapse

Methods inherited from Gateway

card_brand, #card_brand, #generate_unique_id, inherited, non_fractional_currency?, #supported_countries, supported_countries, supported_countries=, supports?, #test?

Methods included from CreditCardFormatting

#expdate, #format

Constructor Details

#initialize(options = {}) ⇒ StripeGateway

Returns a new instance of StripeGateway


48
49
50
51
52
53
54
55
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 48

def initialize(options = {})
  requires!(options, :login)
  @api_key = options[:login]
  @fee_refund_api_key = options[:fee_refund_login]
  @version = options[:version]

  super
end

Instance Method Details

#application_fee_from_response(response) ⇒ Object


125
126
127
128
129
130
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 125

def application_fee_from_response(response)
  return unless response.success?

  application_fees = response.params["data"].select { |fee| fee["object"] == "application_fee" }
  application_fees.first["id"] unless application_fees.empty?
end

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


57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 57

def authorize(money, payment, options = {})
  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 |r|
      post = create_post_for_auth_or_purchase(money, payment, options)
      post[:capture] = "false"
      commit(:post, 'charges', post, options)
    end
  end.responses.last
end

#capture(money, authorization, options = {}) ⇒ Object


91
92
93
94
95
96
97
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 91

def capture(money, authorization, options = {})
  post = {}
  add_amount(post, money, options)
  add_application_fee(post, options)

  commit(:post, "charges/#{CGI.escape(authorization)}/capture", post, options)
end

#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, ... })

78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 78

def purchase(money, payment, options = {})
  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 |r|
      post = create_post_for_auth_or_purchase(money, payment, options)
      commit(:post, 'charges', post, options)
    end
  end.responses.last
end

#refund(money, identification, options = {}) ⇒ Object


103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 103

def refund(money, identification, options = {})
  post = {}
  add_amount(post, money, options)
  post[:refund_application_fee] = true if options[:refund_application_fee]

  MultiResponse.run(:first) do |r|
    r.process { commit(:post, "charges/#{CGI.escape(identification)}/refund", post, options) }

    return r unless options[:refund_fee_amount]

    r.process { fetch_application_fees(identification, options) }
    r.process { refund_application_fee(options[:refund_fee_amount], application_fee_from_response(r.responses.last), options) }
  end
end

#refund_application_fee(money, identification, options = {}) ⇒ Object


132
133
134
135
136
137
138
139
140
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 132

def refund_application_fee(money, identification, options = {})
  return Response.new(false, "Application fee id could not be found") unless identification

  post = {}
  add_amount(post, money, options)
  options.merge!(:key => @fee_refund_api_key)

  commit(:post, "application_fees/#{CGI.escape(identification)}/refund", post, options)
end

#scrub(transcript) ⇒ Object


211
212
213
214
215
216
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 211

def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
    gsub(%r((card\[number\]=)\d+), '\1[FILTERED]').
    gsub(%r((card\[cvc\]=)\d+), '\1[FILTERED]')
end

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

Note: creating a new credit card will not change the customer's existing default credit card (use :set_default => true)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 143

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

  if payment.is_a?(ApplePayPaymentToken)
    token_exchange_response = tokenize_apple_pay_token(payment)
    card_params = { card: token_exchange_response.params["token"]["id"] } if token_exchange_response.success?
  else
    add_creditcard(card_params, payment, options)
  end

  post[:description] = options[:description] if options[:description]
  post[:email] = options[:email] if options[:email]

  if 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", card_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(card_params), options)
  end
end

#supports_scrubbing?Boolean

Returns:

  • (Boolean)

207
208
209
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 207

def supports_scrubbing?
  true
end

#tokenize_apple_pay_token(apple_pay_payment_token, options = {}) ⇒ Object


196
197
198
199
200
201
202
203
204
205
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 196

def tokenize_apple_pay_token(apple_pay_payment_token, options = {})
  token_response = api_request(:post, "tokens?pk_token=#{CGI.escape(apple_pay_payment_token.payment_data.to_json)}")
  success = !token_response.key?("error")

  if success && token_response.key?("id")
    Response.new(success, nil, token: token_response)
  else
    Response.new(success, token_response["error"]["message"])
  end
end

#unstore(customer_id, options = {}, deprecated_options = {}) ⇒ Object


183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 183

def unstore(customer_id, options = {}, deprecated_options = {})
  if options.kind_of?(String)
    ActiveMerchant.deprecated "Passing the card_id as the 2nd parameter is deprecated. Put it in the options hash instead."
    options = deprecated_options.merge(card_id: options)
  end

  if options[:card_id]
    commit(:delete, "customers/#{CGI.escape(customer_id)}/cards/#{CGI.escape(options[:card_id])}", nil, options)
  else
    commit(:delete, "customers/#{CGI.escape(customer_id)}", nil, options)
  end
end

#update(customer_id, card_id, options = {}) ⇒ Object


175
176
177
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 175

def update(customer_id, card_id, options = {})
  commit(:post, "customers/#{CGI.escape(customer_id)}/cards/#{CGI.escape(card_id)}", options, options)
end

#update_customer(customer_id, options = {}) ⇒ Object


179
180
181
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 179

def update_customer(customer_id, options = {})
  commit(:post, "customers/#{CGI.escape(customer_id)}", options, options)
end

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


118
119
120
121
122
123
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 118

def verify(payment, options = {})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(50, payment, options) }
    r.process(:ignore_result) { void(r.authorization, options) }
  end
end

#void(identification, options = {}) ⇒ Object


99
100
101
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 99

def void(identification, options = {})
  commit(:post, "charges/#{CGI.escape(identification)}/refund", {}, options)
end