Class: ActiveMerchant::Billing::StripeGateway

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

Direct Known Subclasses

WebpayGateway

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']

Constants inherited from Gateway

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

Instance Attribute Summary

Attributes inherited from Gateway

#options

Instance Method Summary collapse

Methods inherited from Gateway

#card_brand, card_brand, #generate_unique_id, inherited, 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.



35
36
37
38
39
40
41
42
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 35

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



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

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, creditcard, options = {}) ⇒ Object



44
45
46
47
48
49
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 44

def authorize(money, creditcard, options = {})
  post = create_post_for_auth_or_purchase(money, creditcard, options)
  post[:capture] = "false"

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

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



64
65
66
67
68
69
70
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 64

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


58
59
60
61
62
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 58

def purchase(money, creditcard, options = {})
  post = create_post_for_auth_or_purchase(money, creditcard, options)

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

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



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

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



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

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

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

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 116

def store(creditcard, options = {})
  post = {}
  card_params = {}
  add_creditcard(card_params, creditcard, options)
  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

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



149
150
151
152
153
154
155
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 149

def unstore(customer_id, card_id = nil, options = {})
  if card_id.nil?
    commit(:delete, "customers/#{CGI.escape(customer_id)}", nil, options)
  else
    commit(:delete, "customers/#{CGI.escape(customer_id)}/cards/#{CGI.escape(card_id)}", nil, options)
  end
end

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



141
142
143
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 141

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



145
146
147
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 145

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

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



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

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

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



72
73
74
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 72

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