Class: ActiveMerchant::Billing::StripeGateway
- Inherits:
-
Object
- Object
- ActiveMerchant::Billing::StripeGateway
- Defined in:
- lib/stripe/ext/active_merchant/active_merchant.rb
Defined Under Namespace
Classes: BankAccount
Constant Summary collapse
- BANK_ACCOUNT_HOLDER_TYPE_MAPPING =
{ "personal" => "individual", "business" => "company", }
Instance Method Summary collapse
-
#add_amount(post, money, options, include_currency = false) ⇒ Object
Ignore the call to localized_amount upstream (money is already cents here).
- #bank_account_params(bank_account, options = {}) ⇒ Object
- #commit(method, url, parameters = nil, options = {}) ⇒ Object
- #create_managed_account(account = {}, options = {}) ⇒ Object
- #get_balance(options = {}) ⇒ Object
- #headers(options = {}) ⇒ Object
- #old_headers ⇒ Object
-
#purchase(money, payment, options = {}) ⇒ Object
To create a charge on a card or a token, call.
- #store(payment, options = {}) ⇒ Object
- #user_agent ⇒ Object
Instance Method Details
#add_amount(post, money, options, include_currency = false) ⇒ Object
Ignore the call to localized_amount upstream (money is already cents here)
46 47 48 49 50 |
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 46 def add_amount(post, money, , include_currency = false) currency = [:currency] || currency(money) post[:amount] = money post[:currency] = currency.downcase if include_currency end |
#bank_account_params(bank_account, options = {}) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 158 def bank_account_params(bank_account, = {}) account_holder_type = BANK_ACCOUNT_HOLDER_TYPE_MAPPING[bank_account.type] post = { bank_account: { account_number: bank_account.account_number, country: 'US', currency: 'usd', routing_number: bank_account.routing_number, name: bank_account.bank_name, account_holder_type: account_holder_type, } } end |
#commit(method, url, parameters = nil, options = {}) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 130 def commit(method, url, parameters = nil, = {}) if [:bank_account] response = api_request(:post, url) success = response["error"].nil? Response.new(success, nil, response) else (parameters, ) if parameters response = api_request(method, url, parameters, ) success = !response.key?("error") card = card_from_response(response) avs_code = AVS_CODE_TRANSLATOR["line1: #{card["address_line1_check"]}, zip: #{card["address_zip_check"]}"] cvc_code = CVC_CODE_TRANSLATOR[card["cvc_check"]] Response.new(success, success ? "Transaction approved" : response["error"]["message"], response, :test => response.has_key?("livemode") ? !response["livemode"] : false, :authorization => (success, url, method, response), :avs_result => { :code => avs_code }, :cvv_result => cvc_code, :emv_authorization => (response), :error_code => success ? nil : error_code_from(response) ) end end |
#create_managed_account(account = {}, options = {}) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 16 def create_managed_account(account = {}, = {}) post = account.dup post[:country] ||= 'US' post[:managed] = true commit(:post, 'accounts', post, ) end |
#get_balance(options = {}) ⇒ Object
12 13 14 |
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 12 def get_balance( = {}) commit(:get, 'balance', nil, ) end |
#headers(options = {}) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 36 def headers( = {}) headers = old_headers() stripe_account = .delete(:stripe_account) headers['Stripe-Account'] = stripe_account unless stripe_account.nil? headers end |
#old_headers ⇒ Object
34 |
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 34 alias_method :old_headers, :headers |
#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, ... })
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 59 def purchase(money, payment, = {}) responses = 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 post = create_post_for_auth_or_purchase(money, payment, ) commit(:post, 'charges', post, ) end end.responses.last end |
#store(payment, options = {}) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 72 def store(payment, = {}) params = {} post = {} if payment.is_a?(ApplePayPaymentToken) token_exchange_response = tokenize_apple_pay_token(payment) params = { :card => token_exchange_response.params["token"]["id"] } if token_exchange_response.success? elsif payment.is_a?(BankAccount) post.merge!(bank_account_params(payment, )) else add_creditcard(params, payment, ) end unless payment.is_a?(BankAccount) post[:validate] = [:validate] unless [:validate].nil? post[:description] = [:description] if [:description] post[:email] = [:email] if [:email] end if post[:bank_account] customer_url = "customers" if [:customer] customer_url += "/#{CGI.escape([:customer])}/sources" end responses = MultiResponse.run do |r| # get token and associate it with the customer r.process { commit(:post, "tokens?#{post_data(post)}", nil, { bank_account: true }) } if r.success? r.process { commit(:post, customer_url, { source: r.params["id"] } ) } end end.responses if [:customer] return responses.first else return responses.last end elsif [:account] add_external_account(post, params, payment) commit(:post, "accounts/#{CGI.escape([:account])}/external_accounts", post, ) elsif [:customer] MultiResponse.run(:first) do |r| # The /cards endpoint does not update other customer parameters. r.process { commit(:post, "customers/#{CGI.escape([:customer])}/cards", params, ) } if [: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([:customer], post) } end end else commit(:post, 'customers', post.merge(params), ) end end |
#user_agent ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/stripe/ext/active_merchant/active_merchant.rb', line 24 def user_agent @@ua ||= JSON.dump({ :bindings_version => KB_PLUGIN_VERSION, :lang => 'ruby', :lang_version => "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})", :platform => RUBY_PLATFORM, :publisher => 'killbill' }) end |