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

Constants inherited from Gateway

Gateway::CREDIT_DEPRECATION_MESSAGE, Gateway::CURRENCIES_WITHOUT_FRACTIONS, Gateway::DEBIT_CARDS

Instance Attribute Summary

Attributes inherited from Gateway

#options

Instance Method Summary collapse

Methods inherited from Gateway

#card_brand, card_brand, inherited, supports?, #test?

Methods included from CreditCardFormatting

#format

Constructor Details

#initialize(options = {}) ⇒ StripeGateway

Returns a new instance of StripeGateway.



32
33
34
35
36
37
38
39
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 32

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



86
87
88
89
90
91
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 86

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



41
42
43
44
45
46
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 41

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



61
62
63
64
65
66
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 61

def capture(money, authorization, options = {})
  post = {:amount => amount(money)}
  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, ... })


55
56
57
58
59
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 55

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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 72

def refund(money, identification, options = {})
  post = {:amount => amount(money)}
  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), options) }
  end
end

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



93
94
95
96
97
98
99
100
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 93

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

  post = {:amount => amount(money)}
  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)



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 103

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



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

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



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

def update(customer_id, creditcard, options = {})
  options = options.merge(:customer => customer_id, :set_default => true)
  store(creditcard, options)
end

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



133
134
135
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 133

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

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



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

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