solidus_six_saferpay 0.1.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +35 -0
  3. data/.gem_release.yml +5 -0
  4. data/.github/stale.yml +17 -0
  5. data/.gitignore +18 -0
  6. data/.rspec +2 -0
  7. data/.rubocop.yml +2 -0
  8. data/.travis.yml +12 -0
  9. data/Gemfile +33 -0
  10. data/LICENSE +26 -0
  11. data/README.md +36 -20
  12. data/Rakefile +4 -54
  13. data/app/assets/config/solidus_six_saferpay_manifest.js +1 -0
  14. data/app/assets/images/solidus_six_saferpay/.keep +0 -0
  15. data/app/assets/javascripts/solidus_six_saferpay/saferpay_payment.js +4 -1
  16. data/app/assets/javascripts/spree/backend/solidus_six_saferpay.js +2 -0
  17. data/app/assets/javascripts/spree/frontend/solidus_six_saferpay.js +2 -0
  18. data/app/assets/stylesheets/spree/backend/solidus_six_saferpay.css +4 -0
  19. data/app/assets/stylesheets/spree/frontend/solidus_six_saferpay.css +4 -0
  20. data/app/controllers/spree/solidus_six_saferpay/checkout_controller.rb +133 -17
  21. data/app/controllers/spree/solidus_six_saferpay/transaction/checkout_controller.rb +1 -1
  22. data/app/models/spree/payment_method/saferpay_payment_method.rb +1 -1
  23. data/app/models/spree/payment_method/saferpay_payment_page.rb +2 -2
  24. data/app/models/spree/payment_method/saferpay_transaction.rb +2 -2
  25. data/app/services/spree/solidus_six_saferpay/cancel_authorized_payment.rb +33 -0
  26. data/app/services/spree/solidus_six_saferpay/initialize_payment.rb +0 -1
  27. data/app/services/spree/solidus_six_saferpay/initialize_transaction.rb +0 -1
  28. data/app/services/spree/solidus_six_saferpay/inquire_payment.rb +27 -1
  29. data/app/views/spree/checkout/payment/_saferpay_payment.html.erb +2 -2
  30. data/app/views/spree/solidus_six_saferpay/checkout/{iframe_breakout_redirect.html.erb → iframe_breakout_redirect.erb} +1 -1
  31. data/bin/console +17 -0
  32. data/bin/rails +18 -0
  33. data/bin/rake +7 -0
  34. data/bin/setup +8 -0
  35. data/config/initializers/assets.rb +3 -0
  36. data/config/locales/de.yml +4 -1
  37. data/config/locales/en.yml +4 -1
  38. data/config/locales/fr.yml +4 -1
  39. data/config/routes.rb +8 -8
  40. data/lib/generators/solidus_six_saferpay/install/install_generator.rb +15 -3
  41. data/lib/solidus_six_saferpay.rb +6 -3
  42. data/lib/solidus_six_saferpay/configuration.rb +6 -7
  43. data/lib/solidus_six_saferpay/engine.rb +19 -10
  44. data/lib/solidus_six_saferpay/gateway.rb +11 -11
  45. data/lib/solidus_six_saferpay/payment_page_gateway.rb +8 -9
  46. data/lib/solidus_six_saferpay/transaction_gateway.rb +8 -9
  47. data/lib/solidus_six_saferpay/version.rb +3 -1
  48. data/solidus_six_saferpay.gemspec +42 -0
  49. data/spec/controllers/spree/solidus_six_saferpay/payment_page/checkout_controller_spec.rb +16 -0
  50. data/spec/controllers/spree/solidus_six_saferpay/transaction/checkout_controller_spec.rb +16 -0
  51. data/spec/factories/payment_methods.rb +23 -0
  52. data/spec/factories/payments.rb +11 -0
  53. data/spec/factories/spree/six_saferpay_payments.rb +118 -0
  54. data/spec/models/spree/six_saferpay_payment_spec.rb +203 -0
  55. data/spec/rails_helper.rb +73 -0
  56. data/spec/services/spree/solidus_six_saferpay/assert_payment_page_spec.rb +22 -0
  57. data/spec/services/spree/solidus_six_saferpay/authorize_payment_spec.rb +39 -0
  58. data/spec/services/spree/solidus_six_saferpay/authorize_transaction_spec.rb +24 -0
  59. data/spec/services/spree/solidus_six_saferpay/cancel_authorized_payment_spec.rb +58 -0
  60. data/spec/services/spree/solidus_six_saferpay/initialize_payment_page_spec.rb +81 -0
  61. data/spec/services/spree/solidus_six_saferpay/initialize_payment_spec.rb +40 -0
  62. data/spec/services/spree/solidus_six_saferpay/initialize_transaction_spec.rb +84 -0
  63. data/spec/services/spree/solidus_six_saferpay/inquire_payment_page_payment_spec.rb +20 -0
  64. data/spec/services/spree/solidus_six_saferpay/inquire_payment_spec.rb +39 -0
  65. data/spec/services/spree/solidus_six_saferpay/inquire_transaction_payment_spec.rb +21 -0
  66. data/spec/services/spree/solidus_six_saferpay/payment_validator_spec.rb +100 -0
  67. data/spec/services/spree/solidus_six_saferpay/process_authorized_payment_spec.rb +39 -0
  68. data/spec/services/spree/solidus_six_saferpay/process_payment_page_payment_spec.rb +20 -0
  69. data/spec/services/spree/solidus_six_saferpay/process_transaction_payment_spec.rb +20 -0
  70. data/spec/solidus_six_saferpay/configuration_spec.rb +15 -0
  71. data/spec/solidus_six_saferpay/error_handler_spec.rb +65 -0
  72. data/spec/solidus_six_saferpay/gateway_response_spec.rb +70 -0
  73. data/spec/solidus_six_saferpay/gateway_spec.rb +365 -0
  74. data/spec/solidus_six_saferpay/payment_page_gateway_spec.rb +392 -0
  75. data/spec/solidus_six_saferpay/transaction_gateway_spec.rb +390 -0
  76. data/spec/spec_helper.rb +21 -0
  77. data/spec/support/route_access.rb +6 -0
  78. data/spec/support/shared_examples/authorize_payment.rb +132 -0
  79. data/spec/support/shared_examples/checkout_controller.rb +342 -0
  80. data/spec/support/shared_examples/inquire_payment.rb +118 -0
  81. data/spec/support/shared_examples/process_authorized_payment.rb +202 -0
  82. data/spec/support/uses_payment_page_gateway.rb +29 -0
  83. data/spec/support/uses_transaction_gateway.rb +28 -0
  84. metadata +250 -101
  85. data/app/services/spree/solidus_six_saferpay/inquire_transaction.rb +0 -7
@@ -0,0 +1,39 @@
1
+ require 'rails_helper'
2
+
3
+ module Spree
4
+ module SolidusSixSaferpay
5
+ RSpec.describe InquirePayment do
6
+
7
+ let(:payment) { create(:six_saferpay_payment, :authorized) }
8
+
9
+ let(:service) { described_class.new(payment) }
10
+
11
+ describe '.call' do
12
+ it 'calls an initialized service with given order and payment method' do
13
+ expect(described_class).to receive(:new).with(payment).and_return(service)
14
+ expect(service).to receive(:call)
15
+
16
+ described_class.call(payment)
17
+ end
18
+ end
19
+
20
+ describe '#call' do
21
+ it 'fails because gateway raises an error' do
22
+ expect { service.call }.to raise_error(NotImplementedError)
23
+ end
24
+ end
25
+
26
+ describe '#gateway' do
27
+ it 'raises an error because the gateway must be defined in subclasses' do
28
+ expect { service.gateway }.to raise_error(NotImplementedError)
29
+ end
30
+ end
31
+
32
+ describe '#success?' do
33
+ it 'is initially false' do
34
+ expect(service).not_to be_success
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,21 @@
1
+ require 'rails_helper'
2
+
3
+ module Spree
4
+ module SolidusSixSaferpay
5
+ RSpec.describe InquireTransactionPayment do
6
+
7
+ let(:payment) { create(:six_saferpay_payment) }
8
+ subject { described_class.new(payment) }
9
+
10
+ describe '#gateway' do
11
+ it_behaves_like "it uses the transaction gateway"
12
+ end
13
+
14
+ describe '#call' do
15
+ let(:api_response_class) { SixSaferpay::SixTransaction::InquireResponse }
16
+ it_behaves_like "inquire_payment"
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,100 @@
1
+ require 'rails_helper'
2
+
3
+ module Spree
4
+ module SolidusSixSaferpay
5
+ RSpec.describe PaymentValidator do
6
+
7
+ let(:payment) { create(:six_saferpay_payment, :authorized) }
8
+
9
+ let(:service) { described_class.new(payment) }
10
+
11
+ describe '.call' do
12
+ it 'calls an initialized service with given saferpay payment' do
13
+ expect(described_class).to receive(:new).with(payment).and_return(service)
14
+ expect(service).to receive(:call)
15
+
16
+ described_class.call(payment)
17
+ end
18
+ end
19
+
20
+ describe '#call' do
21
+ it 'performs multiple validations on the payment' do
22
+ expect(service).to receive(:validate_payment_authorized).with(payment.transaction)
23
+ expect(service).to receive(:validate_order_reference).with(payment.transaction)
24
+ expect(service).to receive(:validate_order_amount).with(payment.transaction)
25
+
26
+ service.call
27
+ end
28
+ end
29
+
30
+ describe '#validate_payment_authorized' do
31
+ let(:saferpay_transaction) { instance_double("SixSaferpay::Transaction", status: "AUTHORIZED") }
32
+
33
+ context 'when the saferpay status is AUTHORIZED' do
34
+ it 'passes validation' do
35
+ expect(service.validate_payment_authorized(saferpay_transaction)).to be true
36
+ end
37
+ end
38
+
39
+ context 'when the saferpay status is CAPTURED' do
40
+ let(:saferpay_transaction) { instance_double("SixSaferpay::Transaction", status: "CAPTURED") }
41
+ it 'raises an error' do
42
+ expect{ service.validate_payment_authorized(saferpay_transaction) }.to raise_error(::SolidusSixSaferpay::InvalidSaferpayPayment)
43
+ end
44
+ end
45
+
46
+ context 'when the saferpay status is PENDING' do
47
+ let(:saferpay_transaction) { instance_double("SixSaferpay::Transaction", status: "PENDING") }
48
+ it 'raises an error' do
49
+ expect{ service.validate_payment_authorized(saferpay_transaction) }.to raise_error(::SolidusSixSaferpay::InvalidSaferpayPayment)
50
+ end
51
+ end
52
+ end
53
+
54
+ describe '#validate_order_reference' do
55
+ context 'when the saferpay order reference matches the solidus order' do
56
+ let(:saferpay_transaction) { instance_double("SixSaferpay::Transaction", order_id: payment.order.number) }
57
+
58
+ it 'passes validation' do
59
+ expect(service.validate_order_reference(saferpay_transaction)).to be true
60
+ end
61
+ end
62
+
63
+ context 'when the saferpay order reference does not match the solidus order' do
64
+ let(:saferpay_transaction) { instance_double("SixSaferpay::Transaction", order_id: "OTHER") }
65
+
66
+ it 'raises an error' do
67
+ expect{ service.validate_order_reference(saferpay_transaction) }.to raise_error(::SolidusSixSaferpay::InvalidSaferpayPayment)
68
+ end
69
+ end
70
+ end
71
+
72
+ describe '#validate_order_amount' do
73
+ let(:saferpay_transaction) { instance_double("SixSaferpay::Transaction", amount: saferpay_amount) }
74
+ context 'when the saferpay payment values match the solidus order values' do
75
+ let(:saferpay_amount) { instance_double("SixSaferpay::Amount", value: (payment.order.amount * 100).to_s, currency_code: payment.order.currency) }
76
+
77
+ it 'passes validation' do
78
+ expect(service.validate_order_amount(saferpay_transaction)).to be true
79
+ end
80
+ end
81
+
82
+ context 'when the saferpay payment currency does not match the solidus order currency' do
83
+ let(:saferpay_amount) { instance_double("SixSaferpay::Amount", value: (payment.order.amount * 100).to_s, currency_code: "OTHER") }
84
+
85
+ it 'raises an error' do
86
+ expect{ service.validate_order_amount(saferpay_transaction) }.to raise_error(::SolidusSixSaferpay::InvalidSaferpayPayment)
87
+ end
88
+ end
89
+
90
+ context 'when the saferpay payment value does not match the solidus order value' do
91
+ let(:saferpay_amount) { instance_double("SixSaferpay::Amount", value: ((payment.order.amount + 5) * 100).to_s, currency_code: payment.order.currency) }
92
+
93
+ it 'raises an error' do
94
+ expect{ service.validate_order_amount(saferpay_transaction) }.to raise_error(::SolidusSixSaferpay::InvalidSaferpayPayment)
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,39 @@
1
+ require 'rails_helper'
2
+
3
+ module Spree
4
+ module SolidusSixSaferpay
5
+ RSpec.describe ProcessAuthorizedPayment do
6
+
7
+ let(:payment) { create(:six_saferpay_payment, :authorized) }
8
+
9
+ let(:service) { described_class.new(payment) }
10
+
11
+ describe '.call' do
12
+ it 'calls an initialized service with given order and payment method' do
13
+ expect(described_class).to receive(:new).with(payment).and_return(service)
14
+ expect(service).to receive(:call)
15
+
16
+ described_class.call(payment)
17
+ end
18
+ end
19
+
20
+ describe '#call' do
21
+ it 'fails because gateway raises an error' do
22
+ expect { service.call }.to raise_error(NotImplementedError)
23
+ end
24
+ end
25
+
26
+ describe '#gateway' do
27
+ it 'raises an error because the gateway must be defined in subclasses' do
28
+ expect { service.gateway }.to raise_error(NotImplementedError)
29
+ end
30
+ end
31
+
32
+ describe '#success?' do
33
+ it 'is initially false' do
34
+ expect(service).not_to be_success
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ require 'rails_helper'
2
+
3
+ module Spree
4
+ module SolidusSixSaferpay
5
+ RSpec.describe ProcessPaymentPagePayment do
6
+
7
+ let(:payment) { create(:six_saferpay_payment, :authorized) }
8
+
9
+ subject { described_class.new(payment) }
10
+
11
+ describe '#gateway' do
12
+ it_behaves_like "it uses the payment page gateway"
13
+ end
14
+
15
+ describe '#call' do
16
+ it_behaves_like 'process_authorized_payment'
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'rails_helper'
2
+
3
+ module Spree
4
+ module SolidusSixSaferpay
5
+ RSpec.describe ProcessTransactionPayment do
6
+
7
+ let(:payment) { create(:six_saferpay_payment, :authorized) }
8
+
9
+ subject { described_class.new(payment) }
10
+
11
+ describe '#gateway' do
12
+ it_behaves_like "it uses the transaction gateway"
13
+ end
14
+
15
+ describe '#call' do
16
+ it_behaves_like 'process_authorized_payment'
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusSixSaferpay
4
+ RSpec.describe Configuration do
5
+
6
+ describe '.config' do
7
+ it 'exposes a configurable payment success handler' do
8
+ expect(described_class).to respond_to(:payment_processing_success_handler)
9
+ end
10
+ it 'exposes a configurable list of error handlers' do
11
+ expect(described_class).to respond_to(:error_handlers)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,65 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusSixSaferpay
4
+ RSpec.describe ErrorHandler do
5
+
6
+ let(:error) { StandardError.new }
7
+ let(:error_handlers) { [] }
8
+
9
+ before do
10
+ allow(Configuration).to receive(:error_handlers).and_return(error_handlers)
11
+ end
12
+
13
+ describe '.handle' do
14
+
15
+ it 'defaults to level :error' do
16
+ expect(Rails.logger).to receive(:error).with(error)
17
+ described_class.handle(error)
18
+ end
19
+
20
+ it 'allows for configuring the error level' do
21
+ expect(Rails.logger).to receive(:info).with(error)
22
+ described_class.handle(error, level: :info)
23
+ end
24
+
25
+ context 'when any attached handler can not receive our error messages' do
26
+ let(:error_handler1) { double("Handler1", to_s: 'handler1', call: true) }
27
+ let(:error_handler2) { double("Handler2", to_s: 'handler2') }
28
+ let(:error_handler3) { double("Handler3", to_s: 'handler3', call: true) }
29
+
30
+ let(:error_handlers) { [error_handler1, error_handler2, error_handler3] }
31
+
32
+ before do
33
+ allow(error_handler1).to receive(:respond_to?).with(:call).and_return(true)
34
+ allow(error_handler2).to receive(:respond_to?).with(:call).and_return(false)
35
+ allow(error_handler3).to receive(:respond_to?).with(:call).and_return(true)
36
+ end
37
+
38
+ it 'informs about the misconfiguration via Rails logger' do
39
+ expect(Rails.logger).to receive(:warn).with(/ERROR:.*handler2.*/)
40
+
41
+ described_class.handle(error)
42
+ end
43
+
44
+ it 'skips to the next error handler' do
45
+ expect(error_handler1).to receive(:call).with(error, level: :error)
46
+ expect(error_handler2).not_to receive(:call)
47
+ expect(error_handler3).to receive(:call).with(error, level: :error)
48
+
49
+ described_class.handle(error)
50
+ end
51
+ end
52
+
53
+ context 'when an attached handler can receive our error messages' do
54
+ let(:error_handler) { double("CustomErrorHandler", call: true) }
55
+ let(:error_handlers) { [error_handler] }
56
+
57
+ it 'forwards the error to the error handler' do
58
+ expect(error_handler).to receive(:call).with(error, level: :error)
59
+
60
+ described_class.handle(error)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,70 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusSixSaferpay
4
+ RSpec.describe GatewayResponse do
5
+ let(:success) { true }
6
+ let(:message) { double('message') }
7
+ let(:api_response) { double('API response') }
8
+ let(:options) { {} }
9
+
10
+ subject { described_class.new(success, message, api_response, options) }
11
+
12
+ describe '#initialize' do
13
+ let(:error_name) { double("error_name") }
14
+ let(:authorization) { double("authorization") }
15
+
16
+ describe 'when given option :error_name' do
17
+ let(:options) { { error_name: error_name } }
18
+
19
+ it 'sets the error name' do
20
+ expect(subject.error_name).to eq(error_name)
21
+ end
22
+ end
23
+
24
+ describe 'when given option :authorization' do
25
+ let(:options) { { authorization: authorization } }
26
+
27
+ it 'sets the authorization' do
28
+ expect(subject.authorization).to eq(authorization)
29
+ end
30
+
31
+ end
32
+ end
33
+
34
+ describe '#success?' do
35
+ context 'when initialized as a success' do
36
+ let(:success) { true }
37
+
38
+ it 'is true' do
39
+ expect(subject).to be_success
40
+ end
41
+ end
42
+
43
+ context 'when initialized as failure' do
44
+ let(:success) { false }
45
+
46
+ it 'is false' do
47
+ expect(subject).not_to be_success
48
+ end
49
+ end
50
+ end
51
+
52
+ describe '#to_s' do
53
+ it 'returns the message' do
54
+ expect(subject.to_s).to eq(message)
55
+ end
56
+ end
57
+
58
+ describe '#avs_result' do
59
+ it 'should be an empty hash' do
60
+ expect(subject.avs_result).to eq({})
61
+ end
62
+ end
63
+
64
+ describe '#cvv_result' do
65
+ it 'should be nil' do
66
+ expect(subject.cvv_result).to be_nil
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,365 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusSixSaferpay
4
+ RSpec.describe Gateway do
5
+ # config options
6
+ let(:customer_id) { 'CUSTOMER_ID' }
7
+ let(:terminal_id) { 'TERMINAL_ID' }
8
+ let(:username) { 'USERNAME' }
9
+ let(:password) { 'PASSWORD' }
10
+ let(:base_url) { 'https://test.saferpay-api-host.test' }
11
+ let(:css_url) { '/custom/css/url' }
12
+
13
+ let(:gateway) do
14
+ described_class.new(
15
+ customer_id: customer_id,
16
+ terminal_id: terminal_id,
17
+ username: username,
18
+ password: password,
19
+ base_url: base_url,
20
+ css_url: css_url
21
+ )
22
+ end
23
+
24
+ describe '#new' do
25
+
26
+ it 'configures the API client' do
27
+ gateway = described_class.new(
28
+ customer_id: customer_id,
29
+ terminal_id: terminal_id,
30
+ username: username,
31
+ password: password,
32
+ base_url: base_url,
33
+ css_url: css_url
34
+ )
35
+
36
+ config = SixSaferpay.config
37
+
38
+ expect(config.customer_id).to eq(customer_id)
39
+ expect(config.terminal_id).to eq(terminal_id)
40
+ expect(config.username).to eq(username)
41
+ expect(config.password).to eq(password)
42
+ expect(config.base_url).to eq(base_url)
43
+ expect(config.css_url).to eq(css_url)
44
+ end
45
+
46
+ context 'when global options are not passed' do
47
+
48
+ before do
49
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_CUSTOMER_ID').and_return(customer_id)
50
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_TERMINAL_ID').and_return(terminal_id)
51
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_USERNAME').and_return(username)
52
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_PASSWORD').and_return(password)
53
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_BASE_URL').and_return(base_url)
54
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_CSS_URL').and_return(css_url)
55
+ end
56
+
57
+ it 'falls back to ENV vars' do
58
+ gateway = described_class.new
59
+
60
+ config = SixSaferpay.config
61
+
62
+ expect(config.customer_id).to eq(customer_id)
63
+ expect(config.terminal_id).to eq(terminal_id)
64
+ expect(config.username).to eq(username)
65
+ expect(config.password).to eq(password)
66
+ expect(config.base_url).to eq(base_url)
67
+ expect(config.css_url).to eq(css_url)
68
+ end
69
+ end
70
+ end
71
+
72
+ describe '#initialize_payment' do
73
+
74
+ let(:order) { create(:order) }
75
+ let(:payment_method) { create(:saferpay_payment_method) }
76
+
77
+ it 'fails because it does not know which interface to use' do
78
+ expect { gateway.initialize_payment(order, payment_method) }.to raise_error(NotImplementedError)
79
+ end
80
+ end
81
+
82
+ describe '#authorize' do
83
+
84
+ let(:payment) { create(:six_saferpay_payment) }
85
+ let(:amount) { payment.order.total }
86
+
87
+ it 'fails because authorize must be defined in a subclass' do
88
+ expect { gateway.authorize(amount, payment) }.to raise_error(NotImplementedError)
89
+ end
90
+ end
91
+
92
+ describe '#inquire' do
93
+
94
+ let(:payment) { create(:six_saferpay_payment) }
95
+
96
+ it 'fails because inquire must be defined in a subclass' do
97
+ expect { gateway.inquire(payment) }.to raise_error(NotImplementedError)
98
+ end
99
+ end
100
+
101
+ describe '#purchase' do
102
+ let(:payment) { create(:six_saferpay_payment) }
103
+ let(:amount) { payment.order.total }
104
+
105
+ it 'delegates to capture (with a different signature)' do
106
+ expect(gateway).to receive(:capture).with(amount, payment.transaction_id, {})
107
+
108
+ gateway.purchase(amount, payment)
109
+ end
110
+
111
+ end
112
+
113
+ describe '#capture' do
114
+ let(:amount) { 500 }
115
+ let(:transaction_id) { "TRANSACTION_ID" }
116
+
117
+ let(:api_capture_response) do
118
+ SixSaferpay::SixTransaction::CaptureResponse.new(
119
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
120
+ capture_id: 'CAPTURE_ID',
121
+ status: 'CAPTURED',
122
+ date: '2015-01-30T12:45:22.258+01:00'
123
+ )
124
+ end
125
+
126
+ let(:transaction_reference) { instance_double("SixSaferpay::TransactionReference", transaction_id: transaction_id) }
127
+ let(:saferpay_capture) { instance_double("SixSaferpay::SixTransaction::Capture") }
128
+
129
+ it 'captures the given transaction via the Saferpay API' do
130
+ expect(SixSaferpay::TransactionReference).to receive(:new).with(transaction_id: transaction_id).and_return(transaction_reference)
131
+ expect(SixSaferpay::SixTransaction::Capture).to receive(:new).with(transaction_reference: transaction_reference).and_return(saferpay_capture)
132
+ expect(SixSaferpay::Client).to receive(:post).with(saferpay_capture).and_return(api_capture_response)
133
+
134
+ gateway.capture(amount, transaction_id)
135
+ end
136
+
137
+ context 'when the capture is successful' do
138
+ before do
139
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Capture)).and_return(api_capture_response)
140
+ end
141
+
142
+ it 'returns a success gateway response' do
143
+ expect(GatewayResponse).to receive(:new).with(true, instance_of(String), api_capture_response, { authorization: api_capture_response.capture_id } )
144
+
145
+ gateway.capture(amount, transaction_id)
146
+ end
147
+ end
148
+
149
+ context 'when the API raises an error' do
150
+ let(:six_saferpay_error) do
151
+ SixSaferpay::Error.new(
152
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
153
+ behavior: 'ABORT',
154
+ error_name: 'INVALID_TRANSACTION',
155
+ error_message: 'error_message'
156
+ )
157
+ end
158
+
159
+ before do
160
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Capture)).and_raise(six_saferpay_error)
161
+ end
162
+
163
+ it 'handles the error gracefully' do
164
+ expect(GatewayResponse).to receive(:new).with(false, six_saferpay_error.error_message, nil, error_name: six_saferpay_error.error_name)
165
+
166
+ gateway.capture(amount, transaction_id)
167
+ end
168
+ end
169
+ end
170
+
171
+ describe '#void' do
172
+ let(:transaction_id) { 'TRANSACTION_ID' }
173
+
174
+ let(:transaction_reference) { instance_double("SixSaferpay::TransactionReference") }
175
+ let(:saferpay_cancel) { instance_double("SixSaferpay::SixTransaction::Cancel") }
176
+
177
+ let(:api_cancel_response) do
178
+ SixSaferpay::SixTransaction::CancelResponse.new(
179
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
180
+ transaction_id: transaction_id,
181
+ )
182
+ end
183
+
184
+ it 'cancels the payment' do
185
+ expect(SixSaferpay::TransactionReference).to receive(:new).with(transaction_id: transaction_id).and_return(transaction_reference)
186
+ expect(SixSaferpay::SixTransaction::Cancel).to receive(:new).with(transaction_reference: transaction_reference).and_return(saferpay_cancel)
187
+ expect(SixSaferpay::Client).to receive(:post).with(saferpay_cancel).and_return(api_cancel_response)
188
+
189
+ gateway.void(transaction_id)
190
+ end
191
+
192
+ context 'when the cancellation is successful' do
193
+ before do
194
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Cancel)).and_return(api_cancel_response)
195
+ end
196
+
197
+ it 'returns a success gateway response' do
198
+ expect(GatewayResponse).to receive(:new).with(true, instance_of(String), api_cancel_response, {})
199
+
200
+ gateway.void(transaction_id)
201
+ end
202
+ end
203
+
204
+ context 'when the API raises an error' do
205
+ let(:six_saferpay_error) do
206
+ SixSaferpay::Error.new(
207
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
208
+ behavior: 'ABORT',
209
+ error_name: 'INVALID_TRANSACTION',
210
+ error_message: 'error_message'
211
+ )
212
+ end
213
+
214
+ before do
215
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Cancel)).and_raise(six_saferpay_error)
216
+ end
217
+
218
+ it 'handles the error gracefully' do
219
+ expect(GatewayResponse).to receive(:new).with(false, six_saferpay_error.error_message, nil, error_name: six_saferpay_error.error_name)
220
+
221
+ gateway.void(transaction_id)
222
+ end
223
+ end
224
+ end
225
+
226
+ describe '#try_void' do
227
+ let(:transaction_id) { "TRANSACTION_ID" }
228
+ let(:payment) { create(:payment, response_code: transaction_id) }
229
+
230
+ context 'if payment is in checkout state and has transaction_id' do
231
+ it 'voids the payment' do
232
+ expect(gateway).to receive(:void).with(transaction_id, originator: gateway)
233
+
234
+ gateway.try_void(payment)
235
+ end
236
+ end
237
+ end
238
+
239
+ describe '#credit' do
240
+ let(:amount) { 400 }
241
+ let(:transaction_id) { 'TRANSACTION_ID' }
242
+ let(:options) { {a: 'a', b: 'b'} }
243
+
244
+ it 'is aliased to #refund' do
245
+ expect(gateway).to receive(:refund).with(amount, transaction_id, options)
246
+
247
+ gateway.credit(amount, transaction_id, options)
248
+ end
249
+ end
250
+
251
+ describe '#refund' do
252
+ let(:transaction_amount) { 400 }
253
+ let(:refund_amount) { 300 }
254
+ let(:transaction_id) { 'TRANSACTION_ID' }
255
+ let(:refund_id) { 'REFUND_ID' }
256
+
257
+ let!(:payment) { create(:payment_using_saferpay, response_code: transaction_id, amount: transaction_amount) }
258
+
259
+ let(:saferpay_refund) do
260
+ amount = SixSaferpay::Amount.new(value: (refund_amount * 100), currency_code: payment.order.currency)
261
+ refund = SixSaferpay::Refund.new(amount: amount, order_id: payment.order.number)
262
+ capture_reference = SixSaferpay::CaptureReference.new(capture_id: transaction_id)
263
+
264
+ SixSaferpay::SixTransaction::Refund.new(
265
+ refund: refund,
266
+ capture_reference: capture_reference
267
+ )
268
+ end
269
+
270
+ let(:api_refund_response) do
271
+ transaction = SixSaferpay::Transaction.new(
272
+ type: 'REFUND',
273
+ status: 'AUTHORIZED',
274
+ id: refund_id,
275
+ date: '2015-01-30T12:45:22.258+01:00',
276
+ amount: SixSaferpay::Amount.new(
277
+ value: refund_amount,
278
+ currency_code: payment.order.currency
279
+ ),
280
+ six_transaction_reference: 'SIX_TRANSACTION_REFERENCE'
281
+ )
282
+
283
+ payment_means = SixSaferpay::ResponsePaymentMeans.new(
284
+ brand: SixSaferpay::Brand.new(name: 'BrandName'),
285
+ display_text: 'xxxxxxxxxxxx1234'
286
+ )
287
+
288
+ SixSaferpay::SixTransaction::RefundResponse.new(
289
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
290
+ transaction: transaction,
291
+ payment_means: payment_means
292
+ )
293
+ end
294
+
295
+ it 'refunds and directly captures the payment' do
296
+ expect(SixSaferpay::SixTransaction::Refund).to receive(:new).with(refund: instance_of(SixSaferpay::Refund), capture_reference: instance_of(SixSaferpay::CaptureReference)).and_return(saferpay_refund)
297
+
298
+ expect(SixSaferpay::Client).to receive(:post).with(saferpay_refund).and_return(api_refund_response)
299
+
300
+ expect(gateway).to receive(:capture).with(refund_amount, refund_id, {})
301
+
302
+ gateway.refund(refund_amount, transaction_id)
303
+ end
304
+
305
+ context 'when the refund is successful' do
306
+ let(:api_capture_response) do
307
+ SixSaferpay::SixTransaction::CaptureResponse.new(
308
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
309
+ capture_id: 'CAPTURE_ID',
310
+ status: 'CAPTURED',
311
+ date: '2015-01-30T12:45:22.258+01:00'
312
+ )
313
+ end
314
+
315
+ before do
316
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Refund)).and_return(api_refund_response)
317
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Capture)).and_return(api_capture_response)
318
+ end
319
+
320
+ it 'returns a success gateway response' do
321
+ expect(GatewayResponse).to receive(:new).with(true, instance_of(String), api_capture_response, { authorization: 'CAPTURE_ID' })
322
+
323
+ gateway.refund(refund_amount, transaction_id)
324
+ end
325
+ end
326
+
327
+ context 'when the API raises an error' do
328
+ let(:six_saferpay_error) do
329
+ SixSaferpay::Error.new(
330
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
331
+ behavior: 'ABORT',
332
+ error_name: 'INVALID_TRANSACTION',
333
+ error_message: 'error_message'
334
+ )
335
+ end
336
+
337
+ context 'during refunding' do
338
+ before do
339
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Refund)).and_raise(six_saferpay_error)
340
+ end
341
+
342
+ it 'handles the error gracefully' do
343
+ expect(GatewayResponse).to receive(:new).with(false, six_saferpay_error.error_message, nil, error_name: six_saferpay_error.error_name)
344
+
345
+ gateway.refund(refund_amount, transaction_id)
346
+ end
347
+ end
348
+
349
+ context 'during capture' do
350
+ before do
351
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Refund)).and_return(api_refund_response)
352
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Capture)).and_raise(six_saferpay_error)
353
+ end
354
+
355
+ it 'handles the error gracefully' do
356
+ expect(GatewayResponse).to receive(:new).with(false, six_saferpay_error.error_message, nil, error_name: six_saferpay_error.error_name)
357
+
358
+ gateway.refund(refund_amount, transaction_id)
359
+ end
360
+ end
361
+
362
+ end
363
+ end
364
+ end
365
+ end