Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #144 from 'stripe/master'

  • Loading branch information...
commit 544a7ad6c5503a65ead17f6de1059d4f2d787d82 2 parents b241b7a + 9c640ab
John Duff jduff authored
23 lib/active_merchant/billing/gateways/stripe.rb
@@ -42,16 +42,33 @@ def purchase(money, creditcard, options = {})
42 42 add_creditcard(post, creditcard, options)
43 43 add_customer(post, options)
44 44 add_customer_data(post, options)
  45 + add_flags(post, options)
45 46
46 47 raise ArgumentError.new("Customer or Credit Card required.") if !post[:card] && !post[:customer]
47 48
48 49 commit('charges', post)
49 50 end
50 51
  52 + def authorize(money, creditcard, options = {})
  53 + purchase(money, creditcard, options.merge(:uncaptured => true))
  54 + end
  55 +
  56 + def capture(money, identification, options = {})
  57 + commit("charges/#{CGI.escape(identification)}/capture", {})
  58 + end
  59 +
51 60 def void(identification, options={})
52 61 commit("charges/#{CGI.escape(identification)}/refund", {})
53 62 end
54 63
  64 + def refund(money, identification, options = {})
  65 + post = {}
  66 +
  67 + post[:amount] = amount(money) if money
  68 +
  69 + commit("charges/#{CGI.escape(identification)}/refund", post)
  70 + end
  71 +
55 72 def store(creditcard, options={})
56 73 post = {}
57 74 add_creditcard(post, creditcard, options)
@@ -106,6 +123,10 @@ def add_customer(post, options)
106 123 post[:customer] = options[:customer] if options[:customer] && !post[:card]
107 124 end
108 125
  126 + def add_flags(post, options)
  127 + post[:uncaptured] = true if options[:uncaptured]
  128 + end
  129 +
109 130 def parse(body)
110 131 JSON.parse(body)
111 132 end
@@ -136,7 +157,7 @@ def headers
136 157 })
137 158
138 159 {
139   - "Authorization" => "Basic " + ActiveSupport::Base64.encode64(@api_key+":").strip,
  160 + "Authorization" => "Basic " + ActiveSupport::Base64.encode64(@api_key.to_s + ":").strip,
140 161 "User-Agent" => "Stripe/v1 ActiveMerchantBindings/#{ActiveMerchant::VERSION}",
141 162 "X-Stripe-Client-User-Agent" => @@ua
142 163 }
46 test/remote/gateways/remote_stripe_test.rb
@@ -30,6 +30,36 @@ def test_unsuccessful_purchase
30 30 assert_equal 'Your card number is invalid', response.message
31 31 end
32 32
  33 + def test_successful_authorize
  34 + assert response = @gateway.authorize(@amount, @credit_card, @options)
  35 + assert_success response
  36 + assert_equal "charge", response.params["object"]
  37 + assert response.params["paid"]
  38 + assert response.params["uncaptured"]
  39 + end
  40 +
  41 + def test_unsuccessful_authorize
  42 + assert response = @gateway.authorize(@amount, @declined_card, @options)
  43 + assert_failure response
  44 + assert_equal 'Your card number is invalid', response.message
  45 + end
  46 +
  47 + def test_successful_capture
  48 + assert response = @gateway.authorize(@amount, @credit_card, @options)
  49 + assert_success response
  50 + assert response.authorization
  51 +
  52 + assert captured = @gateway.capture(nil, response.authorization)
  53 + assert_success captured
  54 + assert !captured.params["uncaptured"]
  55 + end
  56 +
  57 + def test_unsuccessful_capture
  58 + assert captured = @gateway.capture(nil, "active_merchant_fake_charge")
  59 + assert_failure captured
  60 + assert_equal "Invalid charge id: active_merchant_fake_charge", captured.message
  61 + end
  62 +
33 63 def test_successful_void
34 64 assert response = @gateway.purchase(@amount, @credit_card, @options)
35 65 assert_success response
@@ -38,12 +68,26 @@ def test_successful_void
38 68 assert_success void
39 69 end
40 70
41   - def test_unsucessful_void
  71 + def test_unsuccessful_void
42 72 assert void = @gateway.void("active_merchant_fake_charge")
43 73 assert_failure void
44 74 assert_equal "Invalid charge id: active_merchant_fake_charge", void.message
45 75 end
46 76
  77 + def test_successful_refund
  78 + assert response = @gateway.purchase(@amount, @credit_card, @options)
  79 + assert_success response
  80 + assert response.authorization
  81 + assert void = @gateway.refund(@amount - 20, response.authorization)
  82 + assert_success void
  83 + end
  84 +
  85 + def test_unsuccessful_refund
  86 + assert refund = @gateway.refund(@amount, "active_merchant_fake_charge")
  87 + assert_failure refund
  88 + assert_equal "Invalid charge id: active_merchant_fake_charge", refund.message
  89 + end
  90 +
47 91 def test_successful_store
48 92 assert response = @gateway.store(@credit_card, {:description => "Active Merchant Test Customer"})
49 93 assert_success response
143 test/unit/gateways/stripe_test.rb
@@ -8,7 +8,8 @@ def setup
8 8 @gateway = StripeGateway.new(:login => 'login')
9 9
10 10 @credit_card = credit_card()
11   - @amount = 100
  11 + @amount = 400
  12 + @refund_amount = 200
12 13
13 14 @options = {
14 15 :billing_address => address(),
@@ -28,6 +29,32 @@ def test_successful_purchase
28 29 assert response.test?
29 30 end
30 31
  32 + def test_successful_authorize
  33 + @gateway.expects(:ssl_request).returns(successful_authorize_response)
  34 +
  35 + assert response = @gateway.authorize(@amount, @credit_card, @options)
  36 + assert_instance_of Response, response
  37 + assert_success response
  38 + assert response
  39 +
  40 + # Replace with authorization number from the successful response
  41 + assert_equal 'ch_test_charge', response.authorization
  42 + assert response.test?
  43 + end
  44 +
  45 + def test_successful_capture
  46 + @gateway.expects(:ssl_request).returns(successful_purchase_response)
  47 +
  48 + assert response = @gateway.capture(nil, 'ch_test_charge')
  49 + assert_instance_of Response, response
  50 + assert_success response
  51 + assert response
  52 +
  53 + # Replace with authorization number from the successful response
  54 + assert_equal 'ch_test_charge', response.authorization
  55 + assert response.test?
  56 + end
  57 +
31 58 def test_successful_void
32 59 @gateway.expects(:ssl_request).returns(successful_purchase_response(true))
33 60
@@ -40,6 +67,18 @@ def test_successful_void
40 67 assert response.test?
41 68 end
42 69
  70 + def test_successful_refund
  71 + @gateway.expects(:ssl_request).returns(successful_partially_refunded_response)
  72 +
  73 + assert response = @gateway.refund(@refund_amount, 'ch_test_charge')
  74 + assert_instance_of Response, response
  75 + assert_success response
  76 +
  77 + # Replace with authorization number from the successful response
  78 + assert_equal 'ch_test_charge', response.authorization
  79 + assert response.test?
  80 + end
  81 +
43 82 def test_unsuccessful_request
44 83 @gateway.expects(:ssl_request).returns(failed_purchase_response)
45 84
@@ -56,14 +95,6 @@ def test_invalid_raw_response
56 95 assert_match /^Invalid response received from the Stripe API/, response.message
57 96 end
58 97
59   - def test_ensure_does_not_respond_to_authorize
60   - assert !@gateway.respond_to?(:authorize)
61   - end
62   -
63   - def test_ensure_does_not_respond_to_capture
64   - assert !@gateway.respond_to?(:capture) || @gateway.method(:capture).owner != @gateway.class
65   - end
66   -
67 98 def test_add_customer
68 99 post = {}
69 100 @gateway.send(:add_customer, post, {:customer => "test_customer"})
@@ -92,6 +123,10 @@ def test_add_address
92 123 assert_equal @options[:billing_address][:country], post[:card][:address_country]
93 124 end
94 125
  126 + def test_ensure_does_not_respond_to_credit
  127 + assert !@gateway.respond_to?(:credit)
  128 + end
  129 +
95 130 def test_gateway_without_credentials
96 131 assert_raises ArgumentError do
97 132 StripeGateway.new
@@ -109,27 +144,75 @@ def test_purchase_without_card_or_customer
109 144 # Place raw successful response from gateway here
110 145 def successful_purchase_response(refunded=false)
111 146 <<-RESPONSE
112   - {
113   - "attempted": true,
114   - "refunded": #{refunded},
115   - "paid": true,
116   - "amount": 400,
117   - "card": {
118   - "type": "Visa",
119   - "country": "US",
120   - "exp_month": 9,
121   - "last4": "4242",
122   - "exp_year": #{Time.now.year + 1},
123   - "object": "card",
124   - "id": "cc_test_card"
125   - },
126   - "id": "ch_test_charge",
127   - "livemode": false,
128   - "description": "Test Purchase",
129   - "currency": "usd",
130   - "object": "charge",
131   - "created": 1307309607
132   - }
  147 +{
  148 + "amount": 400,
  149 + "created": 1309131571,
  150 + "currency": "usd",
  151 + "description": "Test Purchase",
  152 + "id": "ch_test_charge",
  153 + "livemode": false,
  154 + "object": "charge",
  155 + "paid": true,
  156 + "refunded": #{refunded},
  157 + "card": {
  158 + "country": "US",
  159 + "exp_month": 9,
  160 + "exp_year": #{Time.now.year + 1},
  161 + "last4": "4242",
  162 + "object": "card",
  163 + "type": "Visa"
  164 + }
  165 +}
  166 + RESPONSE
  167 + end
  168 +
  169 + def successful_partially_refunded_response
  170 + <<-RESPONSE
  171 +{
  172 + "amount": 400,
  173 + "amount_refunded": 200,
  174 + "created": 1309131571,
  175 + "currency": "usd",
  176 + "description": "Test Purchase",
  177 + "id": "ch_test_charge",
  178 + "livemode": false,
  179 + "object": "charge",
  180 + "paid": true,
  181 + "refunded": true,
  182 + "card": {
  183 + "country": "US",
  184 + "exp_month": 9,
  185 + "exp_year": #{Time.now.year + 1},
  186 + "last4": "4242",
  187 + "object": "card",
  188 + "type": "Visa"
  189 + }
  190 +}
  191 + RESPONSE
  192 + end
  193 +
  194 + def successful_authorize_response
  195 + <<-RESPONSE
  196 +{
  197 + "amount": 400,
  198 + "created": 1309131571,
  199 + "currency": "usd",
  200 + "description": "Test Purchase",
  201 + "id": "ch_test_charge",
  202 + "livemode": false,
  203 + "object": "charge",
  204 + "paid": true,
  205 + "refunded": true,
  206 + "uncaptured": true,
  207 + "card": {
  208 + "country": "US",
  209 + "exp_month": 9,
  210 + "exp_year": #{Time.now.year + 1},
  211 + "last4": "4242",
  212 + "object": "card",
  213 + "type": "Visa"
  214 + }
  215 +}
133 216 RESPONSE
134 217 end
135 218

0 comments on commit 544a7ad

Please sign in to comment.
Something went wrong with that request. Please try again.