Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Commit

Permalink
Merge pull request activemerchant#966 from Shopify/stripe_expand
Browse files Browse the repository at this point in the history
Stripe - Expanding Objects
  • Loading branch information
odorcicd committed Dec 18, 2013
2 parents 2a8a472 + 9cd98cc commit ff2b248
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 32 deletions.
45 changes: 21 additions & 24 deletions lib/active_merchant/billing/gateways/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def authorize(money, creditcard, options = {})
post = create_post_for_auth_or_purchase(money, creditcard, options)
post[:capture] = "false"

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

# To create a charge on a card or a token, call
Expand All @@ -53,31 +53,30 @@ def authorize(money, creditcard, options = {})
def purchase(money, creditcard, options = {})
post = create_post_for_auth_or_purchase(money, creditcard, options)

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

def capture(money, authorization, options = {})
post = {:amount => amount(money)}
add_application_fee(post, options)

commit(:post, "charges/#{CGI.escape(authorization)}/capture", post)
commit(:post, "charges/#{CGI.escape(authorization)}/capture", post, options)
end

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

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

MultiResponse.run(:first) do |r|
r.process { commit(:post, "charges/#{CGI.escape(identification)}/refund", post, commit_options) }
r.process { commit(:post, "charges/#{CGI.escape(identification)}/refund", post, 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) }
r.process { fetch_application_fees(identification, options) }
r.process { refund_application_fee(options[:refund_fee_amount], application_fee_from_response(r), options) }
end
end

Expand All @@ -104,17 +103,16 @@ def store(creditcard, options = {})
post[:description] = options[:description]
post[:email] = options[:email]

commit_options = generate_options(options)
if options[:customer]
MultiResponse.run(:first) do |r|
r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/cards", post, commit_options) }
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, commit_options)
commit(:post, 'customers', post, options)
end
end

Expand All @@ -124,14 +122,14 @@ def update(customer_id, creditcard, options = {})
end

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

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

Expand Down Expand Up @@ -159,6 +157,10 @@ def add_application_fee(post, options)
post[:application_fee] = options[:application_fee] if options[:application_fee]
end

def add_expand_parameters(post, options)
post[:expand] = Array.wrap(options[:expand])
end

def add_customer_data(post, options)
metadata_options = [:description, :ip, :user_agent, :referrer]
post.update(options.slice(*metadata_options))
Expand Down Expand Up @@ -233,21 +235,14 @@ def post_data(params)
h["#{key}[#{k}]"] = v unless v.blank?
end
post_data(h)
elsif value.is_a?(Array)
value.map { |v| "#{key}[]=#{CGI.escape(v.to_s)}" }.join("&")
else
"#{key}=#{CGI.escape(value.to_s)}"
end
end.compact.join("&")
end

def generate_options(raw_options)
options = generate_meta(raw_options)
options.merge!(raw_options.slice(:version, :key))
end

def generate_meta(options)
{:meta => {:ip => options[:ip]}}
end

def headers(options = {})
@@ua ||= JSON.dump({
:bindings_version => ActiveMerchant::VERSION,
Expand All @@ -263,13 +258,15 @@ def headers(options = {})
"Authorization" => "Basic " + Base64.encode64(key.to_s + ":").strip,
"User-Agent" => "Stripe/v1 ActiveMerchantBindings/#{ActiveMerchant::VERSION}",
"X-Stripe-Client-User-Agent" => @@ua,
"X-Stripe-Client-User-Metadata" => options[:meta].to_json
"X-Stripe-Client-User-Metadata" => {:ip => options[:ip]}.to_json
}
headers.merge!("Stripe-Version" => options[:version]) if options[:version]
headers
end

def commit(method, url, parameters=nil, options = {})
add_expand_parameters(parameters, options) if parameters

raw_response = response = nil
success = false
begin
Expand Down
14 changes: 6 additions & 8 deletions lib/active_merchant/billing/gateways/webpay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ def capture(money, authorization, options = {})

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

MultiResponse.run do |r|
r.process { commit(:post, "charges/#{CGI.escape(identification)}/refund", post, commit_options) }
r.process { commit(:post, "charges/#{CGI.escape(identification)}/refund", post, 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) }
r.process { fetch_application_fees(identification, options) }
r.process { refund_application_fee(options[:refund_fee_amount], application_fee_from_response(r), options) }
end
end

Expand All @@ -56,17 +55,16 @@ def store(creditcard, options = {})
post[:description] = options[:description]
post[:email] = options[:email]

commit_options = generate_options(options)
if options[:customer]
MultiResponse.run(:first) do |r|
r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/", post, commit_options) }
r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/", 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, commit_options)
commit(:post, 'customers', post, options)
end
end

Expand All @@ -93,7 +91,7 @@ def headers(options = {})
"Authorization" => "Basic " + Base64.encode64(@api_key.to_s + ":").strip,
"User-Agent" => "Webpay/v1 ActiveMerchantBindings/#{ActiveMerchant::VERSION}",
"X-Webpay-Client-User-Agent" => @@ua,
"X-Webpay-Client-User-Metadata" => options[:meta].to_json
"X-Webpay-Client-User-Metadata" => {:ip => options[:ip]}.to_json
}
end
end
Expand Down
7 changes: 7 additions & 0 deletions test/remote/gateways/remote_stripe_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,11 @@ def test_creditcard_purchase_with_customer
assert_equal "charge", response.params["object"]
assert response.params["paid"]
end

def test_expanding_objects
assert response = @gateway.purchase(@amount, @credit_card, @options.merge(:expand => 'balance_transaction'))
assert_success response
assert response.params['balance_transaction'].is_a?(Hash)
assert_equal 'balance_transaction', response.params['balance_transaction']['object']
end
end
20 changes: 20 additions & 0 deletions test/unit/gateways/stripe_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,26 @@ def generate_options_should_allow_key
assert_equal({:key => '12345'}, generate_options({:key => '12345'}))
end

def test_passing_expand_parameters
@gateway.expects(:ssl_request).with do |method, url, post, headers|
assert post.include?("expand[]=balance_transaction")
end.returns(successful_authorization_response)

@options.merge!(:expand => :balance_transaction)

@gateway.authorize(@amount, @credit_card, @options)
end

def test_passing_expand_parameters_as_array
@gateway.expects(:ssl_request).with do |method, url, post, headers|
assert post.include?("expand[]=balance_transaction&expand[]=customer")
end.returns(successful_authorization_response)

@options.merge!(:expand => [:balance_transaction, :customer])

@gateway.authorize(@amount, @credit_card, @options)
end

private

# Create new customer and set default credit card
Expand Down

0 comments on commit ff2b248

Please sign in to comment.