solidus_six_saferpay 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/solidus_six_saferpay/saferpay_payment.js +4 -1
  3. data/app/controllers/spree/solidus_six_saferpay/checkout_controller.rb +93 -16
  4. data/app/controllers/spree/solidus_six_saferpay/transaction/checkout_controller.rb +1 -1
  5. data/app/models/spree/payment_method/saferpay_payment_method.rb +1 -1
  6. data/app/models/spree/payment_method/saferpay_payment_page.rb +2 -2
  7. data/app/models/spree/payment_method/saferpay_transaction.rb +2 -2
  8. data/app/services/spree/solidus_six_saferpay/cancel_authorized_payment.rb +33 -0
  9. data/app/services/spree/solidus_six_saferpay/initialize_payment.rb +0 -1
  10. data/app/services/spree/solidus_six_saferpay/initialize_transaction.rb +0 -1
  11. data/app/services/spree/solidus_six_saferpay/inquire_payment.rb +26 -1
  12. data/app/views/spree/checkout/payment/_saferpay_payment.html.erb +2 -2
  13. data/app/views/spree/solidus_six_saferpay/checkout/{iframe_breakout_redirect.html.erb → iframe_breakout_redirect.erb} +1 -1
  14. data/config/locales/de.yml +3 -0
  15. data/config/locales/en.yml +3 -0
  16. data/config/locales/fr.yml +3 -0
  17. data/config/routes.rb +2 -2
  18. data/lib/solidus_six_saferpay/configuration.rb +2 -0
  19. data/lib/solidus_six_saferpay/gateway.rb +6 -7
  20. data/lib/solidus_six_saferpay/version.rb +1 -1
  21. data/spec/controllers/spree/solidus_six_saferpay/payment_page/checkout_controller_spec.rb +8 -198
  22. data/spec/controllers/spree/solidus_six_saferpay/transaction/checkout_controller_spec.rb +8 -221
  23. data/spec/services/spree/solidus_six_saferpay/assert_payment_page_spec.rb +2 -128
  24. data/spec/services/spree/solidus_six_saferpay/authorize_transaction_spec.rb +3 -127
  25. data/spec/services/spree/solidus_six_saferpay/cancel_authorized_payment_spec.rb +58 -0
  26. data/spec/services/spree/solidus_six_saferpay/initialize_payment_page_spec.rb +0 -2
  27. data/spec/services/spree/solidus_six_saferpay/initialize_transaction_spec.rb +0 -1
  28. data/spec/services/spree/solidus_six_saferpay/inquire_payment_page_payment_spec.rb +2 -98
  29. data/spec/services/spree/solidus_six_saferpay/inquire_transaction_payment_spec.rb +2 -98
  30. data/spec/services/spree/solidus_six_saferpay/process_payment_page_payment_spec.rb +1 -206
  31. data/spec/services/spree/solidus_six_saferpay/process_transaction_payment_spec.rb +1 -200
  32. data/spec/support/shared_examples/authorize_payment.rb +132 -0
  33. data/spec/support/shared_examples/checkout_controller.rb +342 -0
  34. data/spec/support/shared_examples/inquire_payment.rb +118 -0
  35. data/spec/support/shared_examples/process_authorized_payment.rb +202 -0
  36. metadata +14 -6
  37. data/app/services/spree/solidus_six_saferpay/inquire_transaction.rb +0 -7
  38. data/spec/controllers/spree/solidus_six_saferpay/checkout_controller_spec.rb +0 -41
@@ -0,0 +1,118 @@
1
+ RSpec.shared_examples 'inquire_payment' do
2
+
3
+ before do
4
+ allow(subject).to receive(:gateway).and_return(double('gateway', inquire: gateway_response))
5
+ end
6
+
7
+ context 'when gateway response is not successful' do
8
+ let(:gateway_success) { false }
9
+ let(:error_behaviour) { "ABORT" }
10
+ let(:error_name) { "VALIDATION_FAILED" }
11
+ let(:error_message) { "Request validation failed" }
12
+ let(:api_response) { nil }
13
+ let(:translated_general_error) { "General Error" }
14
+ let(:translated_user_message) { "User Message" }
15
+
16
+ let(:gateway_response) do
17
+ ::SolidusSixSaferpay::GatewayResponse.new(
18
+ gateway_success,
19
+ "initialize success: #{gateway_success}",
20
+ api_response,
21
+ error_name: error_name,
22
+ )
23
+ end
24
+
25
+ it 'still indicates success' do
26
+ subject.call
27
+
28
+ expect(subject).to be_success
29
+ end
30
+
31
+ it 'adds the error message to the response hash' do
32
+ expect { subject.call }.to change { payment.response_hash }.from({}).to({error: error_name})
33
+ end
34
+
35
+ it 'sets the user message according to the api error code' do
36
+ expect(I18n).to receive(:t).with(:general_error, scope: [:solidus_six_saferpay, :errors]).once.and_return(translated_general_error)
37
+ expect(I18n).to receive(:t).with(error_name, scope: [:six_saferpay, :error_names]).once.and_return(translated_user_message)
38
+
39
+ subject.call
40
+
41
+ expect(subject.user_message).to eq("#{translated_general_error}: #{translated_user_message}")
42
+ end
43
+ end
44
+
45
+ context 'when gateway response is successful' do
46
+ let(:transaction_status) { "AUTHORIZED" }
47
+ let(:transaction_id) { "723n4MAjMdhjSAhAKEUdA8jtl9jb" }
48
+ let(:transaction_date) { "2015-01-30T12:45:22.258+01:00" }
49
+ let(:amount_value) { "100" }
50
+ let(:amount_currency) { "USD" }
51
+ let(:brand_name) { 'PaymentBrand' }
52
+ let(:display_text) { "xxxx xxxx xxxx 1234" }
53
+ let(:six_transaction_reference) { "0:0:3:723n4MAjMdhjSAhAKEUdA8jtl9jb" }
54
+
55
+ let(:payment_means) do
56
+ SixSaferpay::ResponsePaymentMeans.new(
57
+ brand: SixSaferpay::Brand.new(name: brand_name),
58
+ display_text: display_text
59
+ )
60
+ end
61
+
62
+ # https://saferpay.github.io/jsonapi/#Payment_v1_PaymentPage_Assert
63
+ # https://saferpay.github.io/jsonapi/#Payment_v1_Transaction_Authorize
64
+ let(:api_response) do
65
+ api_response_class.new(
66
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'test', spec_version: 'test'),
67
+ transaction: SixSaferpay::Transaction.new(
68
+ type: "PAYMENT",
69
+ status: transaction_status,
70
+ id: transaction_id,
71
+ date: transaction_date,
72
+ amount: SixSaferpay::Amount.new(value: amount_value, currency_code: amount_currency),
73
+ six_transaction_reference: six_transaction_reference,
74
+ ),
75
+ payment_means: payment_means
76
+ )
77
+ end
78
+
79
+ let(:gateway_success) { true }
80
+ let(:gateway_response) do
81
+ ::SolidusSixSaferpay::GatewayResponse.new(
82
+ gateway_success,
83
+ "initialize success: #{gateway_success}",
84
+ api_response
85
+ )
86
+ end
87
+
88
+ it 'updates the transaction_id' do
89
+ expect { subject.call }.to change { payment.transaction_id }.from(nil).to(transaction_id)
90
+ end
91
+
92
+ it 'updates the transaction status' do
93
+ expect { subject.call }.to change { payment.transaction_status }.from(nil).to(transaction_status)
94
+ end
95
+
96
+ it 'updates the transaction date' do
97
+ expect { subject.call }.to change { payment.transaction_date }.from(nil).to(DateTime.parse(transaction_date))
98
+ end
99
+
100
+ it 'updates the six_transaction_reference' do
101
+ expect { subject.call }.to change { payment.six_transaction_reference }.from(nil).to(six_transaction_reference)
102
+ end
103
+
104
+ it 'updates the display_text' do
105
+ expect { subject.call }.to change { payment.display_text }.from(nil).to(display_text)
106
+ end
107
+
108
+ it 'updates the response hash' do
109
+ expect { subject.call }.to change { payment.response_hash }.from(payment.response_hash).to(api_response.to_h)
110
+ end
111
+
112
+ it 'indicates success' do
113
+ subject.call
114
+
115
+ expect(subject).to be_success
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,202 @@
1
+ RSpec.shared_examples 'process_authorized_payment' do
2
+ before do
3
+ allow(subject).to receive(:gateway).and_return(double('gateway'))
4
+ end
5
+
6
+ context 'liability_shift check' do
7
+
8
+ before do
9
+ # ensure other methods don't modify outcome
10
+ allow(subject).to receive(:validate_payment!)
11
+ allow(subject).to receive(:cancel_old_solidus_payments)
12
+ allow(payment).to receive(:create_solidus_payment!)
13
+ end
14
+
15
+ context 'when liability shift is required' do
16
+ context 'and liability shift is not granted' do
17
+
18
+ let(:payment) { create(:six_saferpay_payment, :authorized, :without_liability_shift) }
19
+
20
+ it 'cancels the payment' do
21
+ expect(payment.payment_method.preferred_require_liability_shift).to be true
22
+ expect(payment.liability.liability_shift).to be false
23
+
24
+ expect(subject.gateway).to receive(:void).with(payment.transaction_id)
25
+
26
+ subject.call
27
+ end
28
+
29
+ it 'indicates failure' do
30
+ expect(payment.payment_method.preferred_require_liability_shift).to be true
31
+ expect(payment.liability.liability_shift).to be false
32
+
33
+ expect(subject.gateway).to receive(:void).with(payment.transaction_id)
34
+
35
+ subject.call
36
+
37
+ expect(subject).not_to be_success
38
+ end
39
+
40
+ end
41
+
42
+ context 'and liability shift is granted' do
43
+ it "doesn't cancel the payment" do
44
+ expect(payment.payment_method.preferred_require_liability_shift).to be true
45
+ expect(payment.liability.liability_shift).to be true
46
+
47
+ expect(subject.gateway).not_to receive(:void)
48
+
49
+ subject.call
50
+ end
51
+
52
+ it 'passes the liability shift check' do
53
+ expect(payment.payment_method.preferred_require_liability_shift).to be true
54
+ expect(payment.liability.liability_shift).to be true
55
+
56
+ subject.call
57
+
58
+ expect(subject).to be_success
59
+ end
60
+ end
61
+ end
62
+
63
+ context 'when liability shift is not required' do
64
+ let(:payment_method) { create(:saferpay_payment_method, :no_require_liability_shift) }
65
+
66
+ context 'and liability shift is not granted' do
67
+ let(:payment) { create(:six_saferpay_payment, :authorized, :without_liability_shift, payment_method: payment_method) }
68
+
69
+ it "doesn't cancel the payment" do
70
+ expect(payment.payment_method.preferred_require_liability_shift).to be false
71
+ expect(payment.liability.liability_shift).to be false
72
+
73
+ expect(subject.gateway).not_to receive(:void)
74
+
75
+ subject.call
76
+ end
77
+
78
+ it 'passes the liability shift check' do
79
+ expect(payment.payment_method.preferred_require_liability_shift).to be false
80
+ expect(payment.liability.liability_shift).to be false
81
+ subject.call
82
+
83
+ expect(subject).to be_success
84
+ end
85
+ end
86
+
87
+ context 'and liability shift is granted' do
88
+ let(:payment) { create(:six_saferpay_payment, :authorized, payment_method: payment_method) }
89
+ it "doesn't cancel the payment" do
90
+ expect(payment.payment_method.preferred_require_liability_shift).to be false
91
+ expect(payment.liability.liability_shift).to be true
92
+
93
+ expect(subject.gateway).not_to receive(:void)
94
+
95
+ subject.call
96
+ end
97
+
98
+ it 'passes the liability shift check' do
99
+ expect(payment.payment_method.preferred_require_liability_shift).to be false
100
+ expect(payment.liability.liability_shift).to be true
101
+ subject.call
102
+
103
+ expect(subject).to be_success
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ context 'payment validation' do
110
+ before do
111
+ allow(subject).to receive(:gateway).and_return(double('gateway'))
112
+
113
+
114
+ # ensure other methods don't modify outcome
115
+ allow(subject).to receive(:check_liability_shift_requirements!)
116
+ allow(subject).to receive(:cancel_old_solidus_payments)
117
+ allow(payment).to receive(:create_solidus_payment!)
118
+ end
119
+
120
+ it 'validates the payment' do
121
+ expect(Spree::SolidusSixSaferpay::PaymentValidator).to receive(:call).with(payment)
122
+ subject.call
123
+ end
124
+
125
+ context 'when the payment is invalid' do
126
+ it 'cancels the payment' do
127
+ expect(subject.gateway).to receive(:void).with(payment.transaction_id)
128
+
129
+ subject.call
130
+ end
131
+
132
+ it 'indicates failure' do
133
+ allow(subject.gateway).to receive(:void).with(payment.transaction_id)
134
+
135
+ subject.call
136
+
137
+ expect(subject).not_to be_success
138
+ end
139
+ end
140
+
141
+ context 'when the payment is valid' do
142
+ before do
143
+ allow(Spree::SolidusSixSaferpay::PaymentValidator).to receive(:call).with(payment).and_return(true)
144
+ end
145
+
146
+ it "doesn't cancel the payment" do
147
+ expect(subject.gateway).not_to receive(:void)
148
+
149
+ subject.call
150
+ end
151
+
152
+ it 'indicates success' do
153
+ subject.call
154
+
155
+ expect(subject).to be_success
156
+ end
157
+ end
158
+ end
159
+
160
+ context 'when the payment has passed all validations' do
161
+ before do
162
+ allow(subject).to receive(:check_liability_shift_requirements!).and_return(true)
163
+ allow(subject).to receive(:validate_payment!).and_return(true)
164
+ end
165
+
166
+ context 'when previous solidus payments exist for this order' do
167
+ let(:order) { payment.order }
168
+ let!(:previous_payment_invalid) { create(:payment_using_saferpay, order: order) }
169
+ let!(:previous_payment_checkout) { create(:payment_using_saferpay, order: order) }
170
+
171
+ before do
172
+ # This is bad practice because we mock which payments are invalidated here.
173
+ # The reason is that you can't stub methods on AR objects that
174
+ # are loaded from the DB and because #solidus_payments_to_cancel
175
+ # is just AR scopes, I prefer this test over using stuff like
176
+ # #expect_any_instance_of
177
+ allow(subject).to receive(:solidus_payments_to_cancel).and_return([previous_payment_checkout])
178
+ end
179
+
180
+ it 'cancels old solidus payments' do
181
+ expect(previous_payment_invalid).not_to receive(:cancel!)
182
+ expect(previous_payment_checkout).to receive(:cancel!)
183
+
184
+ subject.call
185
+ end
186
+ end
187
+
188
+ it 'creates a new solidus payment' do
189
+ expect(payment).to receive(:create_solidus_payment!)
190
+
191
+ subject.call
192
+ end
193
+
194
+ it 'indicates success' do
195
+ allow(payment).to receive(:create_solidus_payment!)
196
+
197
+ subject.call
198
+
199
+ expect(subject).to be_success
200
+ end
201
+ end
202
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_six_saferpay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Kiener
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-27 00:00:00.000000000 Z
11
+ date: 2020-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solidus_core
@@ -216,12 +216,12 @@ files:
216
216
  - app/services/spree/solidus_six_saferpay/assert_payment_page.rb
217
217
  - app/services/spree/solidus_six_saferpay/authorize_payment.rb
218
218
  - app/services/spree/solidus_six_saferpay/authorize_transaction.rb
219
+ - app/services/spree/solidus_six_saferpay/cancel_authorized_payment.rb
219
220
  - app/services/spree/solidus_six_saferpay/initialize_payment.rb
220
221
  - app/services/spree/solidus_six_saferpay/initialize_payment_page.rb
221
222
  - app/services/spree/solidus_six_saferpay/initialize_transaction.rb
222
223
  - app/services/spree/solidus_six_saferpay/inquire_payment.rb
223
224
  - app/services/spree/solidus_six_saferpay/inquire_payment_page_payment.rb
224
- - app/services/spree/solidus_six_saferpay/inquire_transaction.rb
225
225
  - app/services/spree/solidus_six_saferpay/inquire_transaction_payment.rb
226
226
  - app/services/spree/solidus_six_saferpay/payment_validator.rb
227
227
  - app/services/spree/solidus_six_saferpay/process_authorized_payment.rb
@@ -233,7 +233,7 @@ files:
233
233
  - app/views/spree/api/payments/source_views/_saferpay_payment.json.jbuilder
234
234
  - app/views/spree/checkout/payment/_saferpay_payment.html.erb
235
235
  - app/views/spree/six_saferpay_payments/_six_saferpay_payment.html.erb
236
- - app/views/spree/solidus_six_saferpay/checkout/iframe_breakout_redirect.html.erb
236
+ - app/views/spree/solidus_six_saferpay/checkout/iframe_breakout_redirect.erb
237
237
  - bin/console
238
238
  - bin/rails
239
239
  - bin/rake
@@ -257,7 +257,6 @@ files:
257
257
  - lib/solidus_six_saferpay/transaction_gateway.rb
258
258
  - lib/solidus_six_saferpay/version.rb
259
259
  - solidus_six_saferpay.gemspec
260
- - spec/controllers/spree/solidus_six_saferpay/checkout_controller_spec.rb
261
260
  - spec/controllers/spree/solidus_six_saferpay/payment_page/checkout_controller_spec.rb
262
261
  - spec/controllers/spree/solidus_six_saferpay/transaction/checkout_controller_spec.rb
263
262
  - spec/dummy/.ruby-version
@@ -325,6 +324,7 @@ files:
325
324
  - spec/services/spree/solidus_six_saferpay/assert_payment_page_spec.rb
326
325
  - spec/services/spree/solidus_six_saferpay/authorize_payment_spec.rb
327
326
  - spec/services/spree/solidus_six_saferpay/authorize_transaction_spec.rb
327
+ - spec/services/spree/solidus_six_saferpay/cancel_authorized_payment_spec.rb
328
328
  - spec/services/spree/solidus_six_saferpay/initialize_payment_page_spec.rb
329
329
  - spec/services/spree/solidus_six_saferpay/initialize_payment_spec.rb
330
330
  - spec/services/spree/solidus_six_saferpay/initialize_transaction_spec.rb
@@ -343,6 +343,10 @@ files:
343
343
  - spec/solidus_six_saferpay/transaction_gateway_spec.rb
344
344
  - spec/spec_helper.rb
345
345
  - spec/support/route_access.rb
346
+ - spec/support/shared_examples/authorize_payment.rb
347
+ - spec/support/shared_examples/checkout_controller.rb
348
+ - spec/support/shared_examples/inquire_payment.rb
349
+ - spec/support/shared_examples/process_authorized_payment.rb
346
350
  - spec/support/uses_payment_page_gateway.rb
347
351
  - spec/support/uses_transaction_gateway.rb
348
352
  homepage: https://github.com/fadendaten/solidus_six_saferpay
@@ -372,7 +376,6 @@ signing_key:
372
376
  specification_version: 4
373
377
  summary: Saferpay Payment Page and Transaction payment methods for Solidus
374
378
  test_files:
375
- - spec/controllers/spree/solidus_six_saferpay/checkout_controller_spec.rb
376
379
  - spec/controllers/spree/solidus_six_saferpay/payment_page/checkout_controller_spec.rb
377
380
  - spec/controllers/spree/solidus_six_saferpay/transaction/checkout_controller_spec.rb
378
381
  - spec/dummy/.ruby-version
@@ -440,6 +443,7 @@ test_files:
440
443
  - spec/services/spree/solidus_six_saferpay/assert_payment_page_spec.rb
441
444
  - spec/services/spree/solidus_six_saferpay/authorize_payment_spec.rb
442
445
  - spec/services/spree/solidus_six_saferpay/authorize_transaction_spec.rb
446
+ - spec/services/spree/solidus_six_saferpay/cancel_authorized_payment_spec.rb
443
447
  - spec/services/spree/solidus_six_saferpay/initialize_payment_page_spec.rb
444
448
  - spec/services/spree/solidus_six_saferpay/initialize_payment_spec.rb
445
449
  - spec/services/spree/solidus_six_saferpay/initialize_transaction_spec.rb
@@ -458,5 +462,9 @@ test_files:
458
462
  - spec/solidus_six_saferpay/transaction_gateway_spec.rb
459
463
  - spec/spec_helper.rb
460
464
  - spec/support/route_access.rb
465
+ - spec/support/shared_examples/authorize_payment.rb
466
+ - spec/support/shared_examples/checkout_controller.rb
467
+ - spec/support/shared_examples/inquire_payment.rb
468
+ - spec/support/shared_examples/process_authorized_payment.rb
461
469
  - spec/support/uses_payment_page_gateway.rb
462
470
  - spec/support/uses_transaction_gateway.rb
@@ -1,7 +0,0 @@
1
- module Spree
2
- module SolidusSixSaferpay
3
- class InquireTransactionPayment < InquirePayment
4
- include UseTransactionGateway
5
- end
6
- end
7
- end
@@ -1,41 +0,0 @@
1
- require 'rails_helper'
2
-
3
- RSpec.describe Spree::SolidusSixSaferpay::CheckoutController, type: :controller do
4
- routes { Spree::Core::Engine.routes }
5
-
6
- let(:user) { create(:user) }
7
- let(:order) { create(:order) }
8
- let(:payment_method) { create(:saferpay_payment_method_transaction) }
9
- let(:payment) { create(:six_saferpay_payment) }
10
-
11
- let(:subject) { described_class.new }
12
-
13
- before do
14
- allow(controller).to receive_messages try_spree_current_user: user
15
- allow(controller).to receive_messages current_order: order
16
- end
17
-
18
- describe '#initialize_payment' do
19
- it 'is not implemented in this superclass' do
20
- expect { subject.send(:initialize_payment, order, payment_method) }.to raise_error(NotImplementedError)
21
- end
22
- end
23
-
24
- describe '#authorize_payment' do
25
- it 'is not implemented in this superclass' do
26
- expect { subject.send(:authorize_payment, payment) }.to raise_error(NotImplementedError)
27
- end
28
- end
29
-
30
- describe '#process_authorization' do
31
- it 'is not implemented in this superclass' do
32
- expect { subject.send(:process_authorization, payment) }.to raise_error(NotImplementedError)
33
- end
34
- end
35
-
36
- describe '#inquire_payment' do
37
- it 'is not implemented in this superclass' do
38
- expect { subject.send(:inquire_payment, payment) }.to raise_error(NotImplementedError)
39
- end
40
- end
41
- end