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
@@ -5,111 +5,15 @@ module Spree
5
5
  RSpec.describe InquireTransactionPayment do
6
6
 
7
7
  let(:payment) { create(:six_saferpay_payment) }
8
-
9
8
  subject { described_class.new(payment) }
10
9
 
11
-
12
10
  describe '#gateway' do
13
11
  it_behaves_like "it uses the transaction gateway"
14
12
  end
15
13
 
16
-
17
14
  describe '#call' do
18
-
19
- before do
20
- allow(subject).to receive(:gateway).and_return(double('gateway', inquire: gateway_response))
21
- end
22
-
23
- context 'when gateway response is not successful' do
24
- let(:gateway_success) { false }
25
- let(:error_behaviour) { "ABORT" }
26
- let(:error_name) { "VALIDATION_FAILED" }
27
- let(:error_message) { "Request validation failed" }
28
- let(:api_response) { nil }
29
- let(:translated_general_error) { "General Error" }
30
- let(:translated_user_message) { "User Message" }
31
-
32
- let(:gateway_response) do
33
- ::SolidusSixSaferpay::GatewayResponse.new(
34
- gateway_success,
35
- "initialize success: #{gateway_success}",
36
- api_response,
37
- error_name: error_name,
38
- )
39
- end
40
-
41
- it 'still indicates success' do
42
- subject.call
43
-
44
- expect(subject).to be_success
45
- end
46
-
47
- it 'adds the error message to the response hash' do
48
- expect { subject.call }.to change { payment.response_hash }.from({}).to({error: error_name})
49
- end
50
-
51
- it 'sets the user message according to the api error code' do
52
- expect(I18n).to receive(:t).with(:general_error, scope: [:solidus_six_saferpay, :errors]).once.and_return(translated_general_error)
53
- expect(I18n).to receive(:t).with(error_name, scope: [:six_saferpay, :error_names]).once.and_return(translated_user_message)
54
-
55
- subject.call
56
-
57
- expect(subject.user_message).to eq("#{translated_general_error}: #{translated_user_message}")
58
- end
59
- end
60
-
61
- context 'when successful' do
62
- let(:transaction_status) { "AUTHORIZED" }
63
- let(:transaction_id) { "723n4MAjMdhjSAhAKEUdA8jtl9jb" }
64
- let(:transaction_date) { "2015-01-30T12:45:22.258+01:00" }
65
- let(:amount_value) { "100" }
66
- let(:amount_currency) { "USD" }
67
- let(:brand_name) { 'PaymentBrand' }
68
- let(:display_text) { "xxxx xxxx xxxx 1234" }
69
- let(:six_transaction_reference) { "0:0:3:723n4MAjMdhjSAhAKEUdA8jtl9jb" }
70
-
71
- let(:payment_means) do
72
- SixSaferpay::ResponsePaymentMeans.new(
73
- brand: SixSaferpay::Brand.new(name: brand_name),
74
- display_text: display_text
75
- )
76
- end
77
-
78
- # https://saferpay.github.io/jsonapi/#Payment_v1_PaymentPage_Assert
79
- let(:api_response) do
80
- SixSaferpay::SixPaymentPage::AssertResponse.new(
81
- response_header: SixSaferpay::ResponseHeader.new(request_id: 'test', spec_version: 'test'),
82
- transaction: SixSaferpay::Transaction.new(
83
- type: "PAYMENT",
84
- status: transaction_status,
85
- id: transaction_id,
86
- date: transaction_date,
87
- amount: SixSaferpay::Amount.new(value: amount_value, currency_code: amount_currency),
88
- six_transaction_reference: six_transaction_reference,
89
- ),
90
- payment_means: payment_means
91
- )
92
- end
93
-
94
- let(:gateway_success) { true }
95
- let(:gateway_response) do
96
- ::SolidusSixSaferpay::GatewayResponse.new(
97
- gateway_success,
98
- "initialize success: #{gateway_success}",
99
- api_response
100
- )
101
- end
102
-
103
- it 'updates the response hash' do
104
- expect { subject.call }.to change { payment.response_hash }.from(payment.response_hash).to(api_response.to_h)
105
- end
106
-
107
- it 'indicates success' do
108
- subject.call
109
-
110
- expect(subject).to be_success
111
- end
112
- end
15
+ let(:api_response_class) { SixSaferpay::SixTransaction::InquireResponse }
16
+ it_behaves_like "inquire_payment"
113
17
  end
114
18
 
115
19
  end
@@ -13,213 +13,8 @@ module Spree
13
13
  end
14
14
 
15
15
  describe '#call' do
16
-
17
- before do
18
- allow(subject).to receive(:gateway).and_return(double('gateway'))
19
- end
20
-
21
- context 'liability_shift check' do
22
-
23
- before do
24
- allow(subject).to receive(:gateway).and_return(double('gateway'))
25
-
26
-
27
- # ensure other methods don't modify outcome
28
- allow(subject).to receive(:validate_payment!)
29
- allow(subject).to receive(:cancel_old_solidus_payments)
30
- allow(payment).to receive(:create_solidus_payment!)
31
- end
32
-
33
- context 'when liability shift is required' do
34
- context 'and liability shift is not granted' do
35
-
36
- let(:payment) { create(:six_saferpay_payment, :authorized, :without_liability_shift) }
37
-
38
- it 'cancels the payment' do
39
- expect(payment.payment_method.preferred_require_liability_shift).to be true
40
- expect(payment.liability.liability_shift).to be false
41
-
42
- expect(subject.gateway).to receive(:void).with(payment.transaction_id)
43
-
44
- subject.call
45
- end
46
-
47
- it 'indicates failure' do
48
- expect(payment.payment_method.preferred_require_liability_shift).to be true
49
- expect(payment.liability.liability_shift).to be false
50
-
51
- expect(subject.gateway).to receive(:void).with(payment.transaction_id)
52
-
53
- subject.call
54
-
55
- expect(subject).not_to be_success
56
- end
57
-
58
- end
59
-
60
- context 'and liability shift is granted' do
61
- it "doesn't cancel the payment" do
62
- expect(payment.payment_method.preferred_require_liability_shift).to be true
63
- expect(payment.liability.liability_shift).to be true
64
-
65
- expect(subject.gateway).not_to receive(:void)
66
-
67
- subject.call
68
- end
69
-
70
- it 'passes the liability shift check' do
71
- expect(payment.payment_method.preferred_require_liability_shift).to be true
72
- expect(payment.liability.liability_shift).to be true
73
-
74
- subject.call
75
-
76
- expect(subject).to be_success
77
- end
78
- end
79
- end
80
-
81
- context 'when liability shift is not required' do
82
- let(:payment_method) { create(:saferpay_payment_method, :no_require_liability_shift) }
83
-
84
- context 'and liability shift is not granted' do
85
- let(:payment) { create(:six_saferpay_payment, :authorized, :without_liability_shift, payment_method: payment_method) }
86
-
87
- it "doesn't cancel the payment" do
88
- expect(payment.payment_method.preferred_require_liability_shift).to be false
89
- expect(payment.liability.liability_shift).to be false
90
-
91
- expect(subject.gateway).not_to receive(:void)
92
-
93
- subject.call
94
- end
95
-
96
- it 'passes the liability shift check' do
97
- expect(payment.payment_method.preferred_require_liability_shift).to be false
98
- expect(payment.liability.liability_shift).to be false
99
- subject.call
100
-
101
- expect(subject).to be_success
102
- end
103
- end
104
-
105
- context 'and liability shift is granted' do
106
- let(:payment) { create(:six_saferpay_payment, :authorized, payment_method: payment_method) }
107
- it "doesn't cancel the payment" do
108
- expect(payment.payment_method.preferred_require_liability_shift).to be false
109
- expect(payment.liability.liability_shift).to be true
110
-
111
- expect(subject.gateway).not_to receive(:void)
112
-
113
- subject.call
114
- end
115
-
116
- it 'passes the liability shift check' do
117
- expect(payment.payment_method.preferred_require_liability_shift).to be false
118
- expect(payment.liability.liability_shift).to be true
119
- subject.call
120
-
121
- expect(subject).to be_success
122
- end
123
- end
124
- end
125
- end
126
-
127
- context 'payment validation' do
128
- before do
129
- allow(subject).to receive(:gateway).and_return(double('gateway'))
130
-
131
-
132
- # ensure other methods don't modify outcome
133
- allow(subject).to receive(:check_liability_shift_requirements!)
134
- allow(subject).to receive(:cancel_old_solidus_payments)
135
- allow(payment).to receive(:create_solidus_payment!)
136
- end
137
-
138
- it 'validates the payment' do
139
- expect(PaymentValidator).to receive(:call).with(payment)
140
- subject.call
141
- end
142
-
143
- context 'when the payment is invalid' do
144
- it 'cancels the payment' do
145
- expect(subject.gateway).to receive(:void).with(payment.transaction_id)
146
-
147
- subject.call
148
- end
149
-
150
- it 'indicates failure' do
151
- allow(subject.gateway).to receive(:void).with(payment.transaction_id)
152
-
153
- subject.call
154
-
155
- expect(subject).not_to be_success
156
- end
157
- end
158
-
159
- context 'when the payment is valid' do
160
- before do
161
- allow(PaymentValidator).to receive(:call).with(payment).and_return(true)
162
- end
163
-
164
- it "doesn't cancel the payment" do
165
- expect(subject.gateway).not_to receive(:void)
166
-
167
- subject.call
168
- end
169
-
170
- it 'indicates success' do
171
- subject.call
172
-
173
- expect(subject).to be_success
174
- end
175
- end
176
- end
177
-
178
- context 'when the payment has passed all validations' do
179
- before do
180
- allow(subject).to receive(:check_liability_shift_requirements!).and_return(true)
181
- allow(subject).to receive(:validate_payment!).and_return(true)
182
- end
183
-
184
- context 'when previous solidus payments exist for this order' do
185
- let(:order) { payment.order }
186
- let!(:previous_payment_invalid) { create(:payment_using_saferpay, order: order) }
187
- let!(:previous_payment_checkout) { create(:payment_using_saferpay, order: order) }
188
-
189
- before do
190
- # This is bad practice because we mock which payments are invalidated here.
191
- # The reason is that you can't stub methods on AR objects that
192
- # are loaded from the DB and because #solidus_payments_to_cancel
193
- # is just AR scopes, I prefer this test over using stuff like
194
- # #expect_any_instance_of
195
- allow(subject).to receive(:solidus_payments_to_cancel).and_return([previous_payment_checkout])
196
- end
197
-
198
- it 'cancels old solidus payments' do
199
- expect(previous_payment_invalid).not_to receive(:cancel!)
200
- expect(previous_payment_checkout).to receive(:cancel!)
201
-
202
- subject.call
203
- end
204
- end
205
-
206
- it 'creates a new solidus payment' do
207
- expect(payment).to receive(:create_solidus_payment!)
208
-
209
- subject.call
210
- end
211
-
212
- it 'indicates success' do
213
- allow(payment).to receive(:create_solidus_payment!)
214
-
215
- subject.call
216
-
217
- expect(subject).to be_success
218
- end
219
- end
220
-
16
+ it_behaves_like 'process_authorized_payment'
221
17
  end
222
-
223
18
  end
224
19
  end
225
20
  end
@@ -13,206 +13,7 @@ module Spree
13
13
  end
14
14
 
15
15
  describe '#call' do
16
- context 'liability_shift check' do
17
-
18
- before do
19
- allow(subject).to receive(:gateway).and_return(double('gateway'))
20
-
21
-
22
- # ensure other methods don't modify outcome
23
- allow(subject).to receive(:validate_payment!)
24
- allow(subject).to receive(:cancel_old_solidus_payments)
25
- allow(payment).to receive(:create_solidus_payment!)
26
- end
27
-
28
- context 'when liability shift is required' do
29
- context 'and liability shift is not granted' do
30
-
31
- let(:payment) { create(:six_saferpay_payment, :authorized, :without_liability_shift) }
32
-
33
- it 'cancels the payment' do
34
- expect(payment.payment_method.preferred_require_liability_shift).to be true
35
- expect(payment.liability.liability_shift).to be false
36
-
37
- expect(subject.gateway).to receive(:void).with(payment.transaction_id)
38
-
39
- subject.call
40
- end
41
-
42
- it 'indicates failure' do
43
- expect(payment.payment_method.preferred_require_liability_shift).to be true
44
- expect(payment.liability.liability_shift).to be false
45
-
46
- expect(subject.gateway).to receive(:void).with(payment.transaction_id)
47
-
48
- subject.call
49
-
50
- expect(subject).not_to be_success
51
- end
52
-
53
- end
54
-
55
- context 'and liability shift is granted' do
56
- it "doesn't cancel the payment" do
57
- expect(payment.payment_method.preferred_require_liability_shift).to be true
58
- expect(payment.liability.liability_shift).to be true
59
-
60
- expect(subject.gateway).not_to receive(:void)
61
-
62
- subject.call
63
- end
64
-
65
- it 'passes the liability shift check' do
66
- expect(payment.payment_method.preferred_require_liability_shift).to be true
67
- expect(payment.liability.liability_shift).to be true
68
-
69
- subject.call
70
-
71
- expect(subject).to be_success
72
- end
73
- end
74
- end
75
-
76
- context 'when liability shift is not required' do
77
- let(:payment_method) { create(:saferpay_payment_method, :no_require_liability_shift) }
78
-
79
- context 'and liability shift is not granted' do
80
- let(:payment) { create(:six_saferpay_payment, :authorized, :without_liability_shift, payment_method: payment_method) }
81
-
82
- it "doesn't cancel the payment" do
83
- expect(payment.payment_method.preferred_require_liability_shift).to be false
84
- expect(payment.liability.liability_shift).to be false
85
-
86
- expect(subject.gateway).not_to receive(:void)
87
-
88
- subject.call
89
- end
90
-
91
- it 'passes the liability shift check' do
92
- expect(payment.payment_method.preferred_require_liability_shift).to be false
93
- expect(payment.liability.liability_shift).to be false
94
- subject.call
95
-
96
- expect(subject).to be_success
97
- end
98
- end
99
-
100
- context 'and liability shift is granted' do
101
- let(:payment) { create(:six_saferpay_payment, :authorized, payment_method: payment_method) }
102
- it "doesn't cancel the payment" do
103
- expect(payment.payment_method.preferred_require_liability_shift).to be false
104
- expect(payment.liability.liability_shift).to be true
105
-
106
- expect(subject.gateway).not_to receive(:void)
107
-
108
- subject.call
109
- end
110
-
111
- it 'passes the liability shift check' do
112
- expect(payment.payment_method.preferred_require_liability_shift).to be false
113
- expect(payment.liability.liability_shift).to be true
114
- subject.call
115
-
116
- expect(subject).to be_success
117
- end
118
- end
119
- end
120
- end
121
-
122
- context 'payment validation' do
123
- before do
124
- allow(subject).to receive(:gateway).and_return(double('gateway'))
125
-
126
-
127
- # ensure other methods don't modify outcome
128
- allow(subject).to receive(:check_liability_shift_requirements!)
129
- allow(subject).to receive(:cancel_old_solidus_payments)
130
- allow(payment).to receive(:create_solidus_payment!)
131
- end
132
-
133
- it 'validates the payment' do
134
- expect(PaymentValidator).to receive(:call).with(payment)
135
- subject.call
136
- end
137
-
138
- context 'when the payment is invalid' do
139
- it 'cancels the payment' do
140
- expect(subject.gateway).to receive(:void).with(payment.transaction_id)
141
-
142
- subject.call
143
- end
144
-
145
- it 'indicates failure' do
146
- allow(subject.gateway).to receive(:void).with(payment.transaction_id)
147
-
148
- subject.call
149
-
150
- expect(subject).not_to be_success
151
- end
152
- end
153
-
154
- context 'when the payment is valid' do
155
- before do
156
- allow(PaymentValidator).to receive(:call).with(payment).and_return(true)
157
- end
158
-
159
- it "doesn't cancel the payment" do
160
- expect(subject.gateway).not_to receive(:void)
161
-
162
- subject.call
163
- end
164
-
165
- it 'indicates success' do
166
- subject.call
167
-
168
- expect(subject).to be_success
169
- end
170
- end
171
- end
172
-
173
- context 'when the payment has passed all validations' do
174
- before do
175
- allow(subject).to receive(:check_liability_shift_requirements!).and_return(true)
176
- allow(subject).to receive(:validate_payment!).and_return(true)
177
- end
178
-
179
- context 'when previous solidus payments exist for this order' do
180
- let(:order) { payment.order }
181
- let!(:previous_payment_invalid) { create(:payment_using_saferpay, order: order) }
182
- let!(:previous_payment_checkout) { create(:payment_using_saferpay, order: order) }
183
-
184
- before do
185
- # This is bad practice because we mock which payments are invalidated here.
186
- # The reason is that you can't stub methods on AR objects that
187
- # are loaded from the DB and because #solidus_payments_to_cancel
188
- # is just AR scopes, I prefer this test over using stuff like
189
- # #expect_any_instance_of
190
- allow(subject).to receive(:solidus_payments_to_cancel).and_return([previous_payment_checkout])
191
- end
192
-
193
- it 'cancels old solidus payments' do
194
- expect(previous_payment_invalid).not_to receive(:cancel!)
195
- expect(previous_payment_checkout).to receive(:cancel!)
196
-
197
- subject.call
198
- end
199
- end
200
-
201
- it 'creates a new solidus payment' do
202
- expect(payment).to receive(:create_solidus_payment!)
203
-
204
- subject.call
205
- end
206
-
207
- it 'indicates success' do
208
- allow(payment).to receive(:create_solidus_payment!)
209
-
210
- subject.call
211
-
212
- expect(subject).to be_success
213
- end
214
- end
215
-
16
+ it_behaves_like 'process_authorized_payment'
216
17
  end
217
18
  end
218
19
  end