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
# 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]
  super
end

Instance Method Details

#application_fee_from_response(response) ⇒ Object



83
84
85
86
87
88
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 83

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



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

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



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

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


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

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



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

def refund(money, identification, options = {})
  post = {:amount => amount(money)}

  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



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

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)



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

def store(creditcard, options = {})
  post = {}
  add_creditcard(post, creditcard, options)
  post[:description] = options[:description]
  post[:email] = options[:email]

  if options[:customer]
    MultiResponse.run(:first) do |r|
      r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/cards", post, options) }

      return r unless options[:set_default] and r.success? and !r.params["id"].blank?

      r.process { update_customer(options[:customer], :default_card => r.params["id"]) }
    end
  else
    commit(:post, 'customers', post, options)
  end
end

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



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

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



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

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



124
125
126
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 124

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

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



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

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