spree_gateway 2.2.0 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +9 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +23 -0
  5. data/Gemfile +5 -0
  6. data/Guardfile +9 -0
  7. data/LICENSE.md +26 -0
  8. data/README.md +71 -0
  9. data/Rakefile +15 -0
  10. data/Versionfile +7 -0
  11. data/app/assets/javascripts/spree/frontend/spree_gateway.js +3 -0
  12. data/app/assets/javascripts/store/gateway/stripe.js.coffee +58 -0
  13. data/app/assets/javascripts/store/spree_gateway.js +2 -0
  14. data/app/assets/stylesheets/spree/frontend/spree_gateway.css +3 -0
  15. data/app/controllers/spree/checkout_controller_decorator.rb +51 -0
  16. data/app/controllers/spree/skrill_status_controller.rb +39 -0
  17. data/app/models/spree/billing_integration/skrill/quick_checkout.rb +48 -0
  18. data/app/models/spree/gateway/authorize_net.rb +16 -0
  19. data/app/models/spree/gateway/authorize_net_cim.rb +132 -0
  20. data/app/models/spree/gateway/balanced_gateway.rb +67 -0
  21. data/app/models/spree/gateway/banwire.rb +15 -0
  22. data/app/models/spree/gateway/beanstream.rb +193 -0
  23. data/app/models/spree/gateway/braintree_gateway.rb +147 -0
  24. data/app/models/spree/gateway/card_save.rb +10 -0
  25. data/app/models/spree/gateway/data_cash.rb +10 -0
  26. data/app/models/spree/gateway/eway.rb +20 -0
  27. data/app/models/spree/gateway/fatzebra.rb +15 -0
  28. data/app/models/spree/gateway/linkpoint.rb +28 -0
  29. data/app/models/spree/gateway/maxipago.rb +14 -0
  30. data/app/models/spree/gateway/moneris.rb +10 -0
  31. data/app/models/spree/gateway/pay_junction.rb +16 -0
  32. data/app/models/spree/gateway/pay_pal_gateway.rb +12 -0
  33. data/app/models/spree/gateway/payflow_pro.rb +17 -0
  34. data/app/models/spree/gateway/paymill.rb +12 -0
  35. data/app/models/spree/gateway/pin_gateway.rb +15 -0
  36. data/app/models/spree/gateway/sage_pay.rb +11 -0
  37. data/app/models/spree/gateway/samurai.rb +63 -0
  38. data/app/models/spree/gateway/secure_pay_au.rb +10 -0
  39. data/app/models/spree/gateway/stripe_gateway.rb +103 -0
  40. data/app/models/spree/gateway/usa_epay.rb +9 -0
  41. data/app/models/spree/gateway/worldpay.rb +91 -0
  42. data/app/models/spree/skrill_transaction.rb +19 -0
  43. data/app/views/spree/admin/log_entries/_braintree.html.erb +31 -0
  44. data/app/views/spree/admin/log_entries/_stripe.html.erb +28 -0
  45. data/app/views/spree/admin/payments/source_forms/_quickcheckout.html.erb +8 -0
  46. data/app/views/spree/admin/payments/source_forms/_stripe.html.erb +1 -0
  47. data/app/views/spree/admin/payments/source_views/_quickcheckout.html.erb +39 -0
  48. data/app/views/spree/admin/payments/source_views/_stripe.html.erb +1 -0
  49. data/app/views/spree/checkout/payment/_quickcheckout.html.erb +26 -0
  50. data/app/views/spree/checkout/payment/_stripe.html.erb +21 -0
  51. data/config/initializers/savon.rb +3 -0
  52. data/config/locales/bg.yml +11 -0
  53. data/config/locales/de.yml +11 -0
  54. data/config/locales/en.yml +30 -0
  55. data/config/locales/sv.yml +11 -0
  56. data/config/routes.rb +13 -0
  57. data/db/migrate/20111118164631_create_skrill_transactions.rb +14 -0
  58. data/db/migrate/20121017004102_update_braintree_payment_method_type.rb +9 -0
  59. data/db/migrate/20130213222555_update_stripe_payment_method_type.rb +9 -0
  60. data/db/migrate/20130415222802_update_balanced_payment_method_type.rb +9 -0
  61. data/db/migrate/20131008221012_update_paypal_payment_method_type.rb +9 -0
  62. data/db/migrate/20131112133401_migrate_stripe_preferences.rb +8 -0
  63. data/lib/active_merchant/billing/skrill.rb +18 -0
  64. data/lib/generators/spree_gateway/install/install_generator.rb +28 -0
  65. data/lib/spree_gateway.rb +4 -0
  66. data/lib/spree_gateway/engine.rb +35 -0
  67. data/script/rails +7 -0
  68. data/spec/controllers/spree/checkout_controller_spec.rb +13 -0
  69. data/spec/controllers/spree/skrill_status_controller_spec.rb +8 -0
  70. data/spec/factories/payment_method_factory.rb +6 -0
  71. data/spec/factories/skrill_transaction_factory.rb +10 -0
  72. data/spec/features/stripe_checkout_spec.rb +82 -0
  73. data/spec/lib/active_merchant/billing_skrill_spec.rb +18 -0
  74. data/spec/models/billing_integration/skrill_quick_checkout_spec.rb +11 -0
  75. data/spec/models/gateway/authorize_net_cim_spec.rb +29 -0
  76. data/spec/models/gateway/authorize_net_spec.rb +23 -0
  77. data/spec/models/gateway/balanced_gateway_spec.rb +17 -0
  78. data/spec/models/gateway/banwire_spec.rb +11 -0
  79. data/spec/models/gateway/beanstream_spec.rb +17 -0
  80. data/spec/models/gateway/braintree_gateway_spec.rb +319 -0
  81. data/spec/models/gateway/card_save_spec.rb +11 -0
  82. data/spec/models/gateway/data_cache_spec.rb +11 -0
  83. data/spec/models/gateway/eway_spec.rb +29 -0
  84. data/spec/models/gateway/fatzebra_spec.rb +51 -0
  85. data/spec/models/gateway/linkpoint_spec.rb +62 -0
  86. data/spec/models/gateway/maxipago_spec.rb +17 -0
  87. data/spec/models/gateway/moneris_spec.rb +11 -0
  88. data/spec/models/gateway/pay_junction_spec.rb +23 -0
  89. data/spec/models/gateway/pay_pal_spec.rb +11 -0
  90. data/spec/models/gateway/payflow_pro_spec.rb +23 -0
  91. data/spec/models/gateway/paymill_spec.rb +11 -0
  92. data/spec/models/gateway/pin_gateway_spec.rb +56 -0
  93. data/spec/models/gateway/sage_pay_spec.rb +11 -0
  94. data/spec/models/gateway/samurai_spec.rb +17 -0
  95. data/spec/models/gateway/secure_pay_au_spec.rb +11 -0
  96. data/spec/models/gateway/stripe_gateway_spec.rb +117 -0
  97. data/spec/models/gateway/usa_epay_spec.rb +49 -0
  98. data/spec/models/gateway/worldpay_spec.rb +11 -0
  99. data/spec/models/savon_spec.rb +9 -0
  100. data/spec/models/skrill_transaction_spec.rb +9 -0
  101. data/spec/spec_helper.rb +49 -0
  102. data/spree_gateway.gemspec +40 -0
  103. metadata +140 -5
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::CheckoutController do
4
+ context '#skrill_return' do
5
+ specify do
6
+ end
7
+ end
8
+
9
+ context '#skrill_cancel' do
10
+ specify do
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::SkrillStatusController do
4
+ context '#update' do
5
+ specify do
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ FactoryGirl.define do
2
+ factory :skrill_quick_checkout, class: Spree::BillingIntegration::Skrill::QuickCheckout do
3
+ name 'Skrill - Quick Checkout'
4
+ environment 'test'
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ FactoryGirl.define do
2
+ factory :skrill_transaction, class: Spree::SkrillTransaction do
3
+ email ''
4
+ amount 0.0
5
+ currency 'USD'
6
+ transaction_id nil
7
+ customer_id nil
8
+ payment_type nil
9
+ end
10
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Stripe checkout" do
4
+ let!(:country) { create(:country, :states_required => true) }
5
+ let!(:state) { create(:state, :country => country) }
6
+ let!(:shipping_method) { create(:shipping_method) }
7
+ let!(:stock_location) { create(:stock_location) }
8
+ let!(:mug) { create(:product, :name => "RoR Mug") }
9
+ let!(:stripe_payment_method) do
10
+ Spree::Gateway::StripeGateway.create!(
11
+ :name => "Stripe",
12
+ :preferred_secret_key => "sk_test_VCZnDv3GLU15TRvn8i2EsaAN",
13
+ :preferred_publishable_key => "pk_test_Cuf0PNtiAkkMpTVC2gwYDMIg",
14
+ :environment => "test"
15
+ )
16
+ end
17
+
18
+ let!(:zone) { create(:zone) }
19
+
20
+ before do
21
+ user = create(:user)
22
+
23
+ order = OrderWalkthrough.up_to(:delivery)
24
+ order.stub :confirmation_required? => true
25
+
26
+ order.reload
27
+ order.user = user
28
+ order.update!
29
+
30
+ Spree::CheckoutController.any_instance.stub(:current_order => order)
31
+ Spree::CheckoutController.any_instance.stub(:try_spree_current_user => user)
32
+ Spree::CheckoutController.any_instance.stub(:skip_state_validation? => true)
33
+
34
+ visit spree.checkout_state_path(:payment)
35
+ end
36
+
37
+ # This will pass the CC data to the server and the StripeGateway class handles it
38
+ it "can process a valid payment (without JS)" do
39
+ fill_in "Card Number", :with => "4242 4242 4242 4242"
40
+ fill_in "Card Code", :with => "123"
41
+ fill_in "Expiration", :with => "01 / #{Time.now.year + 1}"
42
+ click_button "Save and Continue"
43
+ page.current_url.should include("/checkout/confirm")
44
+ click_button "Place Order"
45
+ page.should have_content("Your order has been processed successfully")
46
+ end
47
+
48
+ # This will fetch a token from Stripe.com and then pass that to the webserver.
49
+ # The server then processes the payment using that token.
50
+ it "can process a valid payment (with JS)", :js => true do
51
+ fill_in "Card Number", :with => "4242 4242 4242 4242"
52
+ # Otherwise ccType field does not get updated correctly
53
+ page.execute_script("$('.cardNumber').trigger('change')")
54
+ fill_in "Card Code", :with => "123"
55
+ fill_in "Expiration", :with => "01 / #{Time.now.year + 1}"
56
+ click_button "Save and Continue"
57
+ sleep(5) # Wait for Stripe API to return + form to submit
58
+ page.current_url.should include("/checkout/confirm")
59
+ click_button "Place Order"
60
+ page.should have_content("Your order has been processed successfully")
61
+ end
62
+
63
+ it "shows an error with an invalid credit card number", :js => true do
64
+ click_button "Save and Continue"
65
+ page.should have_content("This card number looks invalid")
66
+ end
67
+
68
+ it "shows an error with invalid security fields", :js => true do
69
+ fill_in "Card Number", :with => "4242 4242 4242 4242"
70
+ fill_in "Expiration", :with => "01 / #{Time.now.year + 1}"
71
+ click_button "Save and Continue"
72
+ page.should have_content("Your card's security code is invalid.")
73
+ end
74
+
75
+ it "shows an error with invalid expiry fields", :js => true do
76
+ fill_in "Card Number", :with => "4242 4242 4242 4242"
77
+ fill_in "Expiration", :with => "00 / #{Time.now.year + 1}"
78
+ fill_in "Card Code", :with => "123"
79
+ click_button "Save and Continue"
80
+ page.should have_content("Your card's expiration month is invalid.")
81
+ end
82
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveMerchant::Billing::Skrill do
4
+ let(:url) { 'https://www.moneybookers.com/app/payment.pl' }
5
+
6
+ context '.service_url' do
7
+ it 'return its url' do
8
+ expect(subject.service_url).to eq url
9
+ end
10
+ end
11
+
12
+ context '.payment_url' do
13
+ it 'prepend options to url' do
14
+ options = { 'hi' => 'you' }
15
+ expect(subject.payment_url(options)).to eq "#{url}?hi=you"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::BillingIntegration::Skrill::QuickCheckout, focus: true do
4
+ let(:quick_checkout) { create(:skrill_quick_checkout) }
5
+
6
+ context '.provider_class' do
7
+ it 'is a Billing::Skrill class' do
8
+ expect(quick_checkout.provider_class).to eq ::ActiveMerchant::Billing::Skrill
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Gateway::AuthorizeNetCim do
4
+ let (:gateway) { described_class.new }
5
+
6
+ context '.provider_class' do
7
+ it 'is a AuthorizeNetCim gateway' do
8
+ expect(gateway.provider_class).to eq ::Spree::Gateway::AuthorizeNetCim
9
+ end
10
+ end
11
+
12
+ context '.payment_profiles_supported?' do
13
+ it 'return true' do
14
+ expect(subject.payment_profiles_supported?).to be_true
15
+ end
16
+ end
17
+
18
+ describe 'options' do
19
+ it 'include :test => true when :test_mode is true' do
20
+ gateway.preferred_test_mode = true
21
+ expect(gateway.options[:test]).to be_true
22
+ end
23
+
24
+ it 'does not include :test when :test_mode is false' do
25
+ gateway.preferred_test_mode = false
26
+ expect(gateway.options[:test]).to be_nil
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Gateway::AuthorizeNet do
4
+ let (:gateway) { described_class.create!(name: 'Authorize.net') }
5
+
6
+ context '.provider_class' do
7
+ it 'is a AuthorizeNet gateway' do
8
+ expect(gateway.provider_class).to eq ::ActiveMerchant::Billing::AuthorizeNetGateway
9
+ end
10
+ end
11
+
12
+ describe 'options' do
13
+ it 'include :test => true when :test_mode is true' do
14
+ gateway.preferred_test_mode = true
15
+ expect(gateway.options[:test]).to be_true
16
+ end
17
+
18
+ it 'does not include :test when test_mode is false' do
19
+ gateway.preferred_test_mode = false
20
+ expect(gateway.options[:test]).to be_false
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Gateway::BalancedGateway do
4
+ let(:gateway) { described_class.create!(name: 'Balanced') }
5
+
6
+ context '.provider_class' do
7
+ it 'is a Balanced gateway' do
8
+ expect(gateway.provider_class).to eq ::ActiveMerchant::Billing::BalancedGateway
9
+ end
10
+ end
11
+
12
+ context '.payment_profiles_supported?' do
13
+ it 'return true' do
14
+ expect(gateway.payment_profiles_supported?).to be_true
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Gateway::Banwire do
4
+ let(:gateway) { described_class.create!(name: 'Banwire') }
5
+
6
+ context '.provider_class' do
7
+ it 'is a Banwire gateway' do
8
+ expect(gateway.provider_class).to eq ::ActiveMerchant::Billing::BanwireGateway
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Gateway::Beanstream do
4
+ let(:gateway) { described_class.create!(name: 'Beanstream') }
5
+
6
+ context '.provider_class' do
7
+ it 'is a Beanstream gateway' do
8
+ expect(gateway.provider_class).to eq ::ActiveMerchant::Billing::BeanstreamGateway
9
+ end
10
+ end
11
+
12
+ context '.payment_profiles_supported?' do
13
+ it 'return true' do
14
+ expect(gateway.payment_profiles_supported?).to be_true
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,319 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Gateway::BraintreeGateway do
4
+
5
+ before do
6
+ Spree::Gateway.update_all(active: false)
7
+ @gateway = Spree::Gateway::BraintreeGateway.create!(name: 'Braintree Gateway', environment: 'sandbox', active: true)
8
+ @gateway.set_preference(:environment, 'sandbox')
9
+ @gateway.set_preference(:merchant_id, 'zbn5yzq9t7wmwx42')
10
+ @gateway.set_preference(:public_key, 'ym9djwqpkxbv3xzt')
11
+ @gateway.set_preference(:private_key, '4ghghkyp2yy6yqc8')
12
+ @gateway.save!
13
+
14
+ with_payment_profiles_off do
15
+ country = create(:country, name: 'United States', iso_name: 'UNITED STATES', iso3: 'USA', iso: 'US', numcode: 840)
16
+ state = create(:state, name: 'Maryland', abbr: 'MD', country: country)
17
+ address = create(:address,
18
+ firstname: 'John',
19
+ lastname: 'Doe',
20
+ address1: '1234 My Street',
21
+ address2: 'Apt 1',
22
+ city: 'Washington DC',
23
+ zipcode: '20123',
24
+ phone: '(555)555-5555',
25
+ state: state,
26
+ country: country
27
+ )
28
+
29
+ order = create(:order_with_totals, bill_address: address, ship_address: address)
30
+ order.update!
31
+
32
+ @credit_card = create(:credit_card,
33
+ verification_value: '123',
34
+ number: '5105105105105100',
35
+ month: 9,
36
+ year: Time.now.year + 1,
37
+ first_name: 'John',
38
+ last_name: 'Doe',
39
+ cc_type: 'mastercard')
40
+
41
+ @payment = create(:payment, source: @credit_card, order: order, payment_method: @gateway, amount: 10.00)
42
+ @payment.payment_method.environment = 'test'
43
+ end
44
+ end
45
+
46
+ describe 'merchant_account_id' do
47
+ before do
48
+ @gateway.set_preference(:merchant_account_id, merchant_account_id)
49
+ end
50
+
51
+ context 'with merchant_account_id empty' do
52
+ let(:merchant_account_id) { '' }
53
+
54
+ it 'does not be present in options' do
55
+ expect(@gateway.options.keys.include?(:merchant_account_id)).to be_false
56
+ end
57
+ end
58
+
59
+ context 'with merchant_account_id set on gateway' do
60
+ let(:merchant_account_id) { 'test' }
61
+
62
+ it 'have a perferred_merchant_account_id' do
63
+ expect(@gateway.preferred_merchant_account_id).to eq merchant_account_id
64
+ end
65
+
66
+ it 'have a preferences[:merchant_account_id]' do
67
+ expect(@gateway.preferences.keys.include?(:merchant_account_id)).to be_true
68
+ end
69
+
70
+ it 'is present in options' do
71
+ expect(@gateway.options.keys.include?(:merchant_account_id)).to be_true
72
+ end
73
+ end
74
+ end
75
+
76
+ context '.provider_class' do
77
+ it 'is a BraintreeBlue gateway' do
78
+ expect(@gateway.provider_class).to eq ::ActiveMerchant::Billing::BraintreeBlueGateway
79
+ end
80
+ end
81
+
82
+ context '.payment_profiles_supported?' do
83
+ it 'return true' do
84
+ expect(@gateway.payment_profiles_supported?).to be_true
85
+ end
86
+ end
87
+
88
+ context 'preferences' do
89
+ it 'does not include server + test_mode' do
90
+ expect { @gateway.preferences.fetch(:server) }.to raise_error(StandardError)
91
+ end
92
+ end
93
+
94
+ describe 'authorize' do
95
+ it 'return a success response with an authorization code' do
96
+ result = @gateway.authorize(500, @credit_card)
97
+
98
+ expect(result.success?).to be_true
99
+ expect(result.authorization).to match /\A\w{6}\z/
100
+ expect(Braintree::Transaction::Status::Authorized).to eq Braintree::Transaction.find(result.authorization).status
101
+ end
102
+
103
+ shared_examples 'a valid credit card' do
104
+ it 'work through the spree payment interface' do
105
+ Spree::Config.set auto_capture: false
106
+ expect(@payment.log_entries.size).to eq(0)
107
+
108
+ @payment.process!
109
+ expect(@payment.log_entries.size).to eq(1)
110
+ expect(@payment.response_code).to match /\A\w{6}\z/
111
+ expect(@payment.state).to eq 'pending'
112
+
113
+ transaction = ::Braintree::Transaction.find(@payment.response_code)
114
+ expect(transaction.status).to eq Braintree::Transaction::Status::Authorized
115
+
116
+ card_number = @credit_card.number[0..5] + '******' + @credit_card.number[-4..-1]
117
+ expect(transaction.credit_card_details.masked_number).to eq card_number
118
+ expect(transaction.credit_card_details.expiration_date).to eq "09/#{Time.now.year + 1}"
119
+ expect(transaction.customer_details.first_name).to eq 'John'
120
+ expect(transaction.customer_details.last_name).to eq 'Doe'
121
+ end
122
+ end
123
+
124
+ context 'when the card is a mastercard' do
125
+ before do
126
+ @credit_card.number = '5105105105105100'
127
+ @credit_card.cc_type = 'mastercard'
128
+ @credit_card.save
129
+ end
130
+
131
+ it_behaves_like 'a valid credit card'
132
+ end
133
+
134
+ context 'when the card is a visa' do
135
+ before do
136
+ @credit_card.number = '4111111111111111'
137
+ @credit_card.cc_type = 'visa'
138
+ @credit_card.save
139
+ end
140
+
141
+ it_behaves_like 'a valid credit card'
142
+ end
143
+
144
+ context 'when the card is an amex' do
145
+ before do
146
+ @credit_card.number = '378282246310005'
147
+ @credit_card.verification_value = '1234'
148
+ @credit_card.cc_type = 'amex'
149
+ @credit_card.save
150
+ end
151
+
152
+ it_behaves_like 'a valid credit card'
153
+ end
154
+
155
+ context 'when the card is a JCB' do
156
+ before do
157
+ @credit_card.number = '3530111333300000'
158
+ @credit_card.cc_type = 'jcb'
159
+ @credit_card.save
160
+ end
161
+
162
+ it_behaves_like 'a valid credit card'
163
+ end
164
+
165
+ context 'when the card is a diners club' do
166
+ before do
167
+ @credit_card.number = '36050000000003'
168
+ @credit_card.cc_type = 'dinersclub'
169
+ @credit_card.save
170
+ end
171
+
172
+ it_behaves_like 'a valid credit card'
173
+ end
174
+ end
175
+
176
+ describe 'capture' do
177
+ it 'do capture a previous authorization' do
178
+ @payment.process!
179
+ expect(@payment.log_entries.size).to eq(1)
180
+ expect(@payment.response_code).to match /\A\w{6}\z/
181
+
182
+ transaction = ::Braintree::Transaction.find(@payment.response_code)
183
+ expect(transaction.status).to eq Braintree::Transaction::Status::Authorized
184
+
185
+ capture_result = @gateway.capture(@payment,:ignored_arg_credit_card, :ignored_arg_options)
186
+ expect(capture_result.success?).to be_true
187
+
188
+ transaction = ::Braintree::Transaction.find(@payment.response_code)
189
+ expect(transaction.status).to eq Braintree::Transaction::Status::SubmittedForSettlement
190
+ end
191
+
192
+ it 'raise an error if capture fails using spree interface' do
193
+ Spree::Config.set(auto_capture: false)
194
+ expect(@payment.log_entries.size).to eq(0)
195
+
196
+ @payment.process!
197
+ expect(@payment.log_entries.size).to eq(1)
198
+
199
+ transaction = ::Braintree::Transaction.find(@payment.response_code)
200
+ expect(transaction.status).to eq Braintree::Transaction::Status::Authorized
201
+
202
+ pending 'undefined method reopen for nil:NilClass'
203
+ @payment.payment_source.capture(@payment) # as done in PaymentsController#fire
204
+ transaction = ::Braintree::Transaction.find(@payment.response_code)
205
+ expect(transaction.status).to eq Braintree::Transaction::Status::SubmittedForSettlement
206
+ expect {
207
+ @payment.payment_source.capture(@payment)
208
+ }.to raise_error(Spree::Core::GatewayError, 'Cannot submit for settlement unless status is authorized. (91507)')
209
+ end
210
+ end
211
+
212
+ context 'purchase' do
213
+ it 'return a success response with an authorization code' do
214
+ result = @gateway.purchase(500, @credit_card)
215
+ expect(result.success?).to be_true
216
+ expect(result.authorization).to match /\A\w{6}\z/
217
+ expect(Braintree::Transaction::Status::SubmittedForSettlement).to eq Braintree::Transaction.find(result.authorization).status
218
+ end
219
+
220
+ it 'work through the spree payment interface with payment profiles' do
221
+ purchase_using_spree_interface
222
+ transaction = ::Braintree::Transaction.find(@payment.response_code)
223
+ expect(transaction.credit_card_details.token).not_to be_nil
224
+ end
225
+
226
+ it 'work through the spree payment interface without payment profiles' do
227
+ with_payment_profiles_off do
228
+ purchase_using_spree_interface(false)
229
+ transaction = ::Braintree::Transaction.find(@payment.response_code)
230
+ expect(transaction.credit_card_details.token).to be_nil
231
+ end
232
+ end
233
+ end
234
+
235
+ context 'credit' do
236
+ it 'work through the spree interface' do
237
+ pending 'undefined method credit for #<TestCard:0x007fdba1809ad8>'
238
+ @payment.amount += 100.00
239
+ purchase_using_spree_interface
240
+ credit_using_spree_interface
241
+ end
242
+ end
243
+
244
+ context 'void' do
245
+ it 'work through the spree credit_card / payment interface' do
246
+ expect(@payment.log_entries.size).to eq(0)
247
+ @payment.process!
248
+
249
+ expect(@payment.log_entries.size).to eq(1)
250
+ expect(@payment.response_code).to match /\A\w{6}\z/
251
+
252
+ pending 'expected: submitted_for_settlement got: authorized'
253
+ transaction = Braintree::Transaction.find(@payment.response_code)
254
+ expect(transaction.status).to eq Braintree::Transaction::Status::SubmittedForSettlement
255
+
256
+ @credit_card.void(@payment)
257
+ transaction = Braintree::Transaction.find(transaction.id)
258
+ expect(transaction.status).to eq Braintree::Transaction::Status::Voided
259
+ end
260
+ end
261
+
262
+ context 'update_card_number' do
263
+ it 'passes through gateway_payment_profile_id' do
264
+ credit_card = { 'token' => 'testing', 'last_4' => '1234', 'masked_number' => '5555**5555' }
265
+ @gateway.update_card_number(@payment.source, credit_card)
266
+ expect(@payment.source.gateway_payment_profile_id).to eq 'testing'
267
+ end
268
+ end
269
+
270
+ def credit_using_spree_interface
271
+ expect(@payment.log_entries.size).to eq(1)
272
+ @payment.source.credit(@payment) # as done in PaymentsController#fire
273
+ expect(@payment.log_entries.size).to eq(2)
274
+
275
+ # Let's get the payment record associated with the credit
276
+ @payment = @order.payments.last
277
+ expect(@payment.response_code).to match /\A\w{6}\z/
278
+
279
+ transaction = ::Braintree::Transaction.find(@payment.response_code)
280
+ expect(transaction.type).to eq Braintree::Transaction::Type::Credit
281
+ expect(transaction.status).to eq Braintree::Transaction::Status::SubmittedForSettlement
282
+ expect(transaction.credit_card_details.masked_number).to eq '510510******5100'
283
+ expect(transaction.credit_card_details.expiration_date).to eq "09/#{Time.now.year + 1}"
284
+ expect(transaction.customer_details.first_name).to eq 'John'
285
+ expect(transaction.customer_details.last_name).to eq 'Doe'
286
+ end
287
+
288
+ def purchase_using_spree_interface(profile=true)
289
+ Spree::Config.set(auto_capture: true)
290
+ @payment.send(:create_payment_profile) if profile
291
+ @payment.log_entries.size == 0
292
+ @payment.process! # as done in PaymentsController#create
293
+ @payment.log_entries.size == 1
294
+ expect(@payment.response_code).to match /\A\w{6}\z/
295
+ expect(@payment.state).to eq 'completed'
296
+
297
+ transaction = ::Braintree::Transaction.find(@payment.response_code)
298
+ expect(Braintree::Transaction::Status::SubmittedForSettlement).to eq transaction.status
299
+ expect(transaction.credit_card_details.masked_number).to eq '510510******5100'
300
+ expect(transaction.credit_card_details.expiration_date).to eq "09/#{Time.now.year + 1}"
301
+ expect(transaction.customer_details.first_name).to eq 'John'
302
+ expect(transaction.customer_details.last_name).to eq 'Doe'
303
+ end
304
+
305
+ def with_payment_profiles_off(&block)
306
+ Spree::Gateway::BraintreeGateway.class_eval do
307
+ def payment_profiles_supported?
308
+ false
309
+ end
310
+ end
311
+ yield
312
+ ensure
313
+ Spree::Gateway::BraintreeGateway.class_eval do
314
+ def payment_profiles_supported?
315
+ true
316
+ end
317
+ end
318
+ end
319
+ end