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



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

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, generate_meta(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)
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, generate_meta(options))
end

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



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

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

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

    return r unless options[:refund_fee_amount]

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

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



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

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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]

  path = if options[:customer]
    "customers/#{CGI.escape(options[:customer])}"
  else
    'customers'
  end

  commit(:post, path, post, generate_meta(options))
end

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



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

def unstore(customer_id, options = {})
  commit(:delete, "customers/#{CGI.escape(customer_id)}", nil, generate_meta(options))
end

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



115
116
117
118
# File 'lib/active_merchant/billing/gateways/stripe.rb', line 115

def update(customer_id, creditcard, options = {})
  options = options.merge(:customer => customer_id)
  store(creditcard, 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", {})
end