solidus_six_saferpay 0.1.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) 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 +82 -16
  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/services/spree/solidus_six_saferpay/order_not_found_handler.rb +28 -0
  30. data/app/services/spree/solidus_six_saferpay/payment_not_found_handler.rb +28 -0
  31. data/app/services/spree/solidus_six_saferpay/payment_processing_success_handler.rb +26 -0
  32. data/app/views/spree/checkout/payment/_saferpay_payment.html.erb +2 -2
  33. data/app/views/spree/solidus_six_saferpay/checkout/{iframe_breakout_redirect.html.erb → iframe_breakout_redirect.erb} +1 -1
  34. data/bin/console +17 -0
  35. data/bin/rails +18 -0
  36. data/bin/rake +7 -0
  37. data/bin/setup +8 -0
  38. data/config/initializers/assets.rb +3 -0
  39. data/config/locales/de.yml +3 -0
  40. data/config/locales/en.yml +3 -0
  41. data/config/locales/fr.yml +3 -0
  42. data/config/routes.rb +8 -8
  43. data/lib/generators/solidus_six_saferpay/install/install_generator.rb +15 -3
  44. data/lib/solidus_six_saferpay.rb +6 -3
  45. data/lib/solidus_six_saferpay/configuration.rb +2 -7
  46. data/lib/solidus_six_saferpay/engine.rb +19 -10
  47. data/lib/solidus_six_saferpay/gateway.rb +11 -11
  48. data/lib/solidus_six_saferpay/payment_page_gateway.rb +8 -9
  49. data/lib/solidus_six_saferpay/transaction_gateway.rb +8 -9
  50. data/lib/solidus_six_saferpay/version.rb +3 -1
  51. data/solidus_six_saferpay.gemspec +42 -0
  52. data/spec/controllers/spree/solidus_six_saferpay/payment_page/checkout_controller_spec.rb +16 -0
  53. data/spec/controllers/spree/solidus_six_saferpay/transaction/checkout_controller_spec.rb +16 -0
  54. data/spec/factories/payment_methods.rb +23 -0
  55. data/spec/factories/payments.rb +11 -0
  56. data/spec/factories/spree/six_saferpay_payments.rb +118 -0
  57. data/spec/models/spree/six_saferpay_payment_spec.rb +203 -0
  58. data/spec/rails_helper.rb +73 -0
  59. data/spec/services/spree/solidus_six_saferpay/assert_payment_page_spec.rb +22 -0
  60. data/spec/services/spree/solidus_six_saferpay/authorize_payment_spec.rb +39 -0
  61. data/spec/services/spree/solidus_six_saferpay/authorize_transaction_spec.rb +24 -0
  62. data/spec/services/spree/solidus_six_saferpay/cancel_authorized_payment_spec.rb +58 -0
  63. data/spec/services/spree/solidus_six_saferpay/initialize_payment_page_spec.rb +81 -0
  64. data/spec/services/spree/solidus_six_saferpay/initialize_payment_spec.rb +40 -0
  65. data/spec/services/spree/solidus_six_saferpay/initialize_transaction_spec.rb +84 -0
  66. data/spec/services/spree/solidus_six_saferpay/inquire_payment_page_payment_spec.rb +20 -0
  67. data/spec/services/spree/solidus_six_saferpay/inquire_payment_spec.rb +39 -0
  68. data/spec/services/spree/solidus_six_saferpay/inquire_transaction_payment_spec.rb +21 -0
  69. data/spec/services/spree/solidus_six_saferpay/order_not_found_handler_spec.rb +30 -0
  70. data/spec/services/spree/solidus_six_saferpay/payment_not_found_handler_spec.rb +30 -0
  71. data/spec/services/spree/solidus_six_saferpay/payment_processing_success_handler_spec.rb +34 -0
  72. data/spec/services/spree/solidus_six_saferpay/payment_validator_spec.rb +100 -0
  73. data/spec/services/spree/solidus_six_saferpay/process_authorized_payment_spec.rb +39 -0
  74. data/spec/services/spree/solidus_six_saferpay/process_payment_page_payment_spec.rb +20 -0
  75. data/spec/services/spree/solidus_six_saferpay/process_transaction_payment_spec.rb +20 -0
  76. data/spec/solidus_six_saferpay/configuration_spec.rb +12 -0
  77. data/spec/solidus_six_saferpay/error_handler_spec.rb +65 -0
  78. data/spec/solidus_six_saferpay/gateway_response_spec.rb +70 -0
  79. data/spec/solidus_six_saferpay/gateway_spec.rb +365 -0
  80. data/spec/solidus_six_saferpay/payment_page_gateway_spec.rb +392 -0
  81. data/spec/solidus_six_saferpay/transaction_gateway_spec.rb +390 -0
  82. data/spec/spec_helper.rb +21 -0
  83. data/spec/support/route_access.rb +6 -0
  84. data/spec/support/shared_examples/authorize_payment.rb +132 -0
  85. data/spec/support/shared_examples/checkout_controller.rb +294 -0
  86. data/spec/support/shared_examples/inquire_payment.rb +118 -0
  87. data/spec/support/shared_examples/process_authorized_payment.rb +202 -0
  88. data/spec/support/uses_payment_page_gateway.rb +29 -0
  89. data/spec/support/uses_transaction_gateway.rb +28 -0
  90. metadata +261 -62
  91. data/app/services/spree/solidus_six_saferpay/inquire_transaction.rb +0 -7
@@ -1,6 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spree/core'
1
4
  module SolidusSixSaferpay
2
- class Engine < ::Rails::Engine
3
- isolate_namespace SolidusSixSaferpay
5
+
6
+ def self.config
7
+ @config ||= Configuration.new
8
+ end
9
+
10
+ def self.configure
11
+ yield config
12
+ end
13
+
14
+ class Engine < Rails::Engine
15
+ include SolidusSupport::EngineExtensions::Decorators
16
+
17
+ isolate_namespace ::Spree
18
+
19
+ engine_name 'solidus_six_saferpay'
4
20
 
5
21
  config.autoload_paths += Dir["#{config.root}/lib/**/"]
6
22
  config.eager_load_paths += Dir["#{config.root}/lib/**/"]
@@ -10,16 +26,9 @@ module SolidusSixSaferpay
10
26
  app.config.spree.payment_methods << Spree::PaymentMethod::SaferpayTransaction
11
27
  end
12
28
 
13
- initializer "solidus_six_payments.assets.precompile" do |app|
14
- app.config.assets.precompile += %w( solidus_six_saferpay/application.css )
15
- app.config.assets.precompile += %w( solidus_six_saferpay/saferpay_payment.js )
16
- app.config.assets.precompile += %w( solidus_six_saferpay/credit_cards/**/*.png )
17
- end
18
-
29
+ # use rspec for tests
19
30
  config.generators do |g|
20
31
  g.test_framework :rspec
21
- g.fixture_replacement :factory_bot
22
- g.factory_bot dir: 'spec/factories'
23
32
  end
24
33
  end
25
34
  end
@@ -5,9 +5,6 @@ module SolidusSixSaferpay
5
5
 
6
6
  def initialize(options = {})
7
7
  SixSaferpay.configure do |config|
8
- config.success_url = options.fetch(:success_url)
9
- config.fail_url = options.fetch(:fail_url)
10
-
11
8
  # Allow config via ENV for static values
12
9
  config.customer_id = options.fetch(:customer_id) { ENV.fetch('SIX_SAFERPAY_CUSTOMER_ID') }
13
10
  config.terminal_id = options.fetch(:terminal_id) { ENV.fetch('SIX_SAFERPAY_TERMINAL_ID') }
@@ -43,7 +40,7 @@ module SolidusSixSaferpay
43
40
  capture(amount, saferpay_payment.transaction_id, options)
44
41
  end
45
42
 
46
- def capture(amount, transaction_id, options={})
43
+ def capture(_amount, transaction_id, options={})
47
44
  transaction_reference = SixSaferpay::TransactionReference.new(transaction_id: transaction_id)
48
45
  payment_capture = SixSaferpay::SixTransaction::Capture.new(transaction_reference: transaction_reference)
49
46
 
@@ -82,15 +79,14 @@ module SolidusSixSaferpay
82
79
  end
83
80
 
84
81
  # aliased to #refund for compatibility with solidus internals
85
- def credit(amount, transaction_id, options = {})
86
- refund(amount, transaction_id, options)
82
+ def credit(amount_cents, transaction_id, options = {})
83
+ refund(amount_cents, transaction_id, options)
87
84
  end
88
85
 
89
- def refund(amount, transaction_id, options = {})
86
+ def refund(amount_cents, transaction_id, options = {})
90
87
  payment = Spree::Payment.find_by!(response_code: transaction_id)
91
- refund_amount = Spree::Money.new(amount, currency: payment.currency)
92
88
 
93
- saferpay_amount = SixSaferpay::Amount.new(value: refund_amount.cents, currency_code: payment.currency)
89
+ saferpay_amount = SixSaferpay::Amount.new(value: amount_cents, currency_code: payment.currency)
94
90
  saferpay_refund = SixSaferpay::Refund.new(amount: saferpay_amount, order_id: payment.order.number)
95
91
  capture_reference = SixSaferpay::CaptureReference.new(capture_id: payment.transaction_id)
96
92
 
@@ -99,7 +95,7 @@ module SolidusSixSaferpay
99
95
  if refund_response = SixSaferpay::Client.post(payment_refund)
100
96
 
101
97
  # actually capture the refund
102
- capture(amount, refund_response.transaction.id, options)
98
+ capture(amount_cents, refund_response.transaction.id, options)
103
99
  end
104
100
 
105
101
  rescue SixSaferpay::Error => e
@@ -156,7 +152,7 @@ module SolidusSixSaferpay
156
152
  )
157
153
  payer = SixSaferpay::Payer.new(language_code: I18n.locale, billing_address: billing_address, delivery_address: delivery_address)
158
154
 
159
- params = { payment: payment, payer: payer }
155
+ params = { payment: payment, payer: payer, return_urls: return_urls(order) }
160
156
 
161
157
  six_payment_methods = payment_method.enabled_payment_methods
162
158
  params.merge!(payment_methods: six_payment_methods) unless six_payment_methods.blank?
@@ -164,6 +160,10 @@ module SolidusSixSaferpay
164
160
  params
165
161
  end
166
162
 
163
+ def return_urls(order)
164
+ raise NotImplementedError, "Must be implemented in PaymentPageGateway or TransactionGateway"
165
+ end
166
+
167
167
  def response(success, message, api_response, options = {})
168
168
  GatewayResponse.new(success, message, api_response, options)
169
169
  end
@@ -1,15 +1,6 @@
1
1
  module SolidusSixSaferpay
2
2
  class PaymentPageGateway < Gateway
3
3
 
4
- def initialize(options = {})
5
- super(
6
- options.merge(
7
- success_url: url_helpers.solidus_six_saferpay_payment_page_success_url,
8
- fail_url: url_helpers.solidus_six_saferpay_payment_page_fail_url
9
- )
10
- )
11
- end
12
-
13
4
  def inquire(saferpay_payment, options = {})
14
5
  inquire_response = perform_assert_request(saferpay_payment, options)
15
6
 
@@ -47,6 +38,14 @@ module SolidusSixSaferpay
47
38
  SixSaferpay::SixPaymentPage::Initialize.new(interface_initialize_params(order, payment_method))
48
39
  end
49
40
 
41
+ def return_urls(order)
42
+ SixSaferpay::ReturnUrls.new(
43
+ success: url_helpers.solidus_six_saferpay_payment_page_success_url(order.number),
44
+ fd_fail: url_helpers.solidus_six_saferpay_payment_page_fail_url(order.number),
45
+ fd_abort: url_helpers.solidus_six_saferpay_payment_page_fail_url(order.number)
46
+ )
47
+ end
48
+
50
49
  def perform_assert_request(saferpay_payment, options = {})
51
50
  payment_page_assert = SixSaferpay::SixPaymentPage::Assert.new(token: saferpay_payment.token)
52
51
  SixSaferpay::Client.post(payment_page_assert)
@@ -1,15 +1,6 @@
1
1
  module SolidusSixSaferpay
2
2
  class TransactionGateway < Gateway
3
3
 
4
- def initialize(options = {})
5
- super(
6
- options.merge(
7
- success_url: url_helpers.solidus_six_saferpay_transaction_success_url,
8
- fail_url: url_helpers.solidus_six_saferpay_transaction_fail_url
9
- )
10
- )
11
- end
12
-
13
4
  def inquire(saferpay_payment, options = {})
14
5
  transaction_inquire = SixSaferpay::SixTransaction::Inquire.new(transaction_reference: saferpay_payment.transaction_id)
15
6
  inquire_response = SixSaferpay::Client.post(transaction_inquire)
@@ -43,5 +34,13 @@ module SolidusSixSaferpay
43
34
  def interface_initialize_object(order, payment_method)
44
35
  SixSaferpay::SixTransaction::Initialize.new(interface_initialize_params(order, payment_method))
45
36
  end
37
+
38
+ def return_urls(order)
39
+ SixSaferpay::ReturnUrls.new(
40
+ success: url_helpers.solidus_six_saferpay_transaction_success_url(order.number),
41
+ fd_fail: url_helpers.solidus_six_saferpay_transaction_fail_url(order.number),
42
+ fd_abort: url_helpers.solidus_six_saferpay_transaction_fail_url(order.number)
43
+ )
44
+ end
46
45
  end
47
46
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SolidusSixSaferpay
2
- VERSION = '0.1.7'
4
+ VERSION = '0.4.0'
3
5
  end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/solidus_six_saferpay/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'solidus_six_saferpay'
7
+ spec.version = SolidusSixSaferpay::VERSION
8
+ spec.authors = ['Simon Kiener']
9
+ spec.email = ['jugglinghobo@gmail.com']
10
+
11
+ spec.summary = 'Saferpay Payment Page and Transaction payment methods for Solidus'
12
+ spec.description = 'Adds Saferpay Payment Page and Transaction payment methods to your Solidus application'
13
+ spec.homepage = 'https://github.com/fadendaten/solidus_six_saferpay'
14
+ spec.license = 'MIT'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/fadendaten/solidus_six_saferpay'
18
+
19
+ spec.required_ruby_version = Gem::Requirement.new('~> 2.4')
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ files = Dir.chdir(__dir__) { `git ls-files -z`.split("\x0") }
24
+
25
+ spec.files = files.grep_v(%r{^(test|spec|features)/})
26
+ spec.test_files = files.grep(%r{^(test|spec|features)/})
27
+ spec.bindir = "exe"
28
+ spec.executables = files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency 'solidus_core', ['>= 2.0.0', '< 3']
32
+ spec.add_dependency 'solidus_support', '~> 0.4.0'
33
+ spec.add_dependency "six_saferpay", "~> 2.2"
34
+ spec.add_dependency "rails-i18n", "~> 5.1"
35
+
36
+ spec.add_development_dependency 'solidus_dev_support'
37
+
38
+ spec.add_development_dependency "shoulda-matchers", "~> 4.1"
39
+ spec.add_development_dependency "rails-controller-testing", "~> 1.0", ">= 1.0.4"
40
+ spec.add_development_dependency "pry", "~> 0.12"
41
+ spec.add_development_dependency "pry-rails", "~> 0.3"
42
+ end
@@ -0,0 +1,16 @@
1
+ require 'rails_helper'
2
+
3
+ module Spree
4
+ module SolidusSixSaferpay
5
+ RSpec.describe PaymentPage::CheckoutController do
6
+
7
+ let(:initialize_payment_service_class) { InitializePaymentPage }
8
+ let(:authorize_payment_service_class) { AssertPaymentPage }
9
+ let(:process_authorization_service_class) { ProcessPaymentPagePayment }
10
+ let(:inquire_payment_service_class) { InquirePaymentPagePayment }
11
+
12
+ it_behaves_like "checkout_controller"
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'rails_helper'
2
+
3
+ module Spree
4
+ module SolidusSixSaferpay
5
+ RSpec.describe Transaction::CheckoutController do
6
+
7
+ let(:initialize_payment_service_class) { InitializeTransaction }
8
+ let(:authorize_payment_service_class) { AuthorizeTransaction }
9
+ let(:process_authorization_service_class) { ProcessTransactionPayment }
10
+ let(:inquire_payment_service_class) { InquireTransactionPayment }
11
+
12
+ it_behaves_like "checkout_controller"
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ FactoryBot.define do
2
+
3
+ factory :saferpay_payment_method, class: Spree::PaymentMethod::SaferpayPaymentMethod do
4
+ name { "saferpay_payment_method" }
5
+ preferred_require_liability_shift { true }
6
+
7
+ trait :no_require_liability_shift do
8
+ preferred_require_liability_shift { false }
9
+ end
10
+
11
+ trait :no_as_iframe do
12
+ preferred_as_iframe { false }
13
+ end
14
+ end
15
+
16
+ factory :saferpay_payment_method_payment_page, class: Spree::PaymentMethod::SaferpayPaymentPage, parent: :saferpay_payment_method do
17
+ name { "saferpay_payment_page" }
18
+ end
19
+
20
+ factory :saferpay_payment_method_transaction, class: Spree::PaymentMethod::SaferpayTransaction, parent: :saferpay_payment_method do
21
+ name { "saferpay_transaction" }
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ FactoryBot.define do
2
+
3
+ factory :payment_using_saferpay, class: Spree::Payment do
4
+ association(:payment_method, factory: :saferpay_payment_method)
5
+ source { create(:six_saferpay_payment, :authorized, order: order, payment_method: payment_method) }
6
+ order
7
+ state { 'checkout' }
8
+ response_code { '12345' }
9
+ end
10
+
11
+ end
@@ -0,0 +1,118 @@
1
+ FactoryBot.define do
2
+ factory :six_saferpay_payment, class: Spree::SixSaferpayPayment do
3
+ order
4
+ association :payment_method, factory: :saferpay_payment_method
5
+ expiration { Time.current + 2.hours }
6
+ sequence(:token, (1..100000).to_a.shuffle.to_enum)
7
+
8
+ trait :authorized do
9
+ sequence(:transaction_id) { |n| "TRANSACTION_ID_#{n}" }
10
+ transaction_status { "AUTHORIZED" }
11
+ transaction_date { DateTime.parse("2019-07-25T13:34:44.677+02:00") }
12
+ sequence(:six_transaction_reference) { |n| "SIX_TRANSACTION_REFERENCE_#{n}" }
13
+ display_text { "xxxx xxxx xxxx 1234" }
14
+
15
+ response_hash do
16
+ {
17
+ response_header: {
18
+ request_id: "request-id",
19
+ spec_version: "1.12"
20
+ },
21
+ transaction: {
22
+ type: "PAYMENT",
23
+ status: "AUTHORIZED",
24
+ id: "0QKl2GAnEK0OvA90vClhAESYEGYb",
25
+ date: "2019-07-25T13:34:44.677+02:00",
26
+ amount: {
27
+ value: "20000",
28
+ currency_code: "CHF"
29
+ },
30
+ order_id: "ORDER_ID",
31
+ acquirer_name: "ACQUIRER_NAME",
32
+ acquirer_reference: "ACQUIRER_REFERENCE",
33
+ six_transaction_reference: "SIX_TRANASACTION_REFERENCE",
34
+ approval_code: "APPROVAL CODE"
35
+ },
36
+ payment_means: {
37
+ brand: {
38
+ payment_method: "MASTERCARD",
39
+ name: "MasterCard"
40
+ },
41
+ display_text: "xxxx xxxx xxxx 1234",
42
+ card: {
43
+ masked_number: "xxxxxxxxxxxx1234",
44
+ exp_year: 2019,
45
+ exp_month: 7,
46
+ holder_name: "John Doe",
47
+ country_code: "US"
48
+ }
49
+ },
50
+ payer: {
51
+ ip_address: "IP ADDRESS",
52
+ ip_location: "CH",
53
+ delivery_address: {
54
+ first_name: "Simon",
55
+ last_name: "Knorrli",
56
+ street: "STREET",
57
+ zip: "ZIP",
58
+ city: "CITY",
59
+ country_code: "CH"
60
+ },
61
+ billing_address: {
62
+ first_name: "Simon",
63
+ last_name: "Knorrli",
64
+ street: "STREET",
65
+ zip: "ZIP",
66
+ city: "CITY",
67
+ country_code: "CH"
68
+ }
69
+ },
70
+ liability: {
71
+ liability_shift: true,
72
+ liable_entity: "ThreeDs",
73
+ three_ds: {
74
+ authenticated: true,
75
+ liability_shift: true,
76
+ xid: "3DS-XID"
77
+ }
78
+ }
79
+ }
80
+ end
81
+
82
+ end
83
+
84
+ trait :without_liability_shift do
85
+ authorized
86
+
87
+ after(:build) do |payment|
88
+ payment.response_hash.merge!(
89
+ liability: {
90
+ liability_shift: false,
91
+ liable_entity: "ThreeDs",
92
+ three_ds: {
93
+ authenticated: false,
94
+ liability_shift: false,
95
+ xid: "3DS-XID"
96
+ }
97
+ }
98
+ )
99
+ end
100
+ end
101
+
102
+ trait :dcc do
103
+ authorized
104
+
105
+ after(:build) do |payment|
106
+ payment.response_hash.merge!(
107
+ dcc: {
108
+ payer_amount: {
109
+ value: "18972",
110
+ currency_code: "USD"
111
+ }
112
+ }
113
+ )
114
+ end
115
+ end
116
+
117
+ end
118
+ end
@@ -0,0 +1,203 @@
1
+ require 'rails_helper'
2
+
3
+ module Spree
4
+ RSpec.describe SixSaferpayPayment, type: :model do
5
+ let(:payment) { FactoryBot.create(:six_saferpay_payment) }
6
+ describe 'associations' do
7
+ it { is_expected.to belong_to :order }
8
+ it { is_expected.to belong_to :payment_method }
9
+ end
10
+
11
+ describe 'validations' do
12
+ it { is_expected.to validate_presence_of :token }
13
+ it { is_expected.to validate_presence_of :expiration }
14
+ end
15
+
16
+ describe "#create_solidus_payment!" do
17
+ it 'creates a Solidus::Payment with the correct information' do
18
+ expect(Spree::Payment.count).to eq(0)
19
+ solidus_payment = payment.create_solidus_payment!
20
+ expect(Spree::Payment.count).to eq(1)
21
+ expect(solidus_payment.order).to eq(payment.order)
22
+ expect(solidus_payment.payment_method).to eq(payment.payment_method)
23
+ expect(solidus_payment.response_code).to eq(payment.transaction_id)
24
+ expect(solidus_payment.amount).to eq(payment.order.total)
25
+ expect(solidus_payment.source).to eq(payment)
26
+ end
27
+ end
28
+
29
+ describe '#address' do
30
+ it "returns the order's billing address" do
31
+ expect(payment.address).to eq(payment.order.bill_address)
32
+ end
33
+ end
34
+
35
+ describe '#payment_means' do
36
+ context 'when the payment is still unprocessed' do
37
+ it 'returns nil' do
38
+ expect(payment.payment_means).to be_nil
39
+ end
40
+ end
41
+
42
+ context 'when the payment is authorized' do
43
+ let(:payment) { FactoryBot.create(:six_saferpay_payment, :authorized) }
44
+
45
+ it 'returns a SixSaferpay::ResponsePaymentMeans' do
46
+ expect(payment.payment_means).to be_a(SixSaferpay::ResponsePaymentMeans)
47
+ end
48
+
49
+ it 'sets the API response attributes correctly' do
50
+ expect(payment.payment_means.brand.payment_method).to eq("MASTERCARD")
51
+ expect(payment.payment_means.brand.name).to eq("MasterCard")
52
+ expect(payment.payment_means.display_text).to eq("xxxx xxxx xxxx 1234")
53
+ expect(payment.payment_means.card.masked_number).to eq("xxxxxxxxxxxx1234")
54
+ expect(payment.payment_means.card.exp_year).to eq(2019)
55
+ expect(payment.payment_means.card.exp_month).to eq(7)
56
+ expect(payment.payment_means.card.holder_name).to eq("John Doe")
57
+ expect(payment.payment_means.card.country_code).to eq("US")
58
+ end
59
+ end
60
+ end
61
+
62
+ describe '#transaction' do
63
+ context 'when the payment is still unprocessed' do
64
+ it 'returns nil' do
65
+ expect(payment.transaction).to be_nil
66
+ end
67
+ end
68
+
69
+ context 'when the payment is authorized' do
70
+ let(:payment) { FactoryBot.create(:six_saferpay_payment, :authorized) }
71
+
72
+ it 'returns a SixSaferpay::Transaction' do
73
+ expect(payment.transaction).to be_a(SixSaferpay::Transaction)
74
+ end
75
+
76
+ it 'sets the API response attributes correctly' do
77
+ expect(payment.transaction.type).to eq("PAYMENT")
78
+ expect(payment.transaction.status).to eq("AUTHORIZED")
79
+ expect(payment.transaction.amount.value).to eq('20000')
80
+ expect(payment.transaction.amount.currency_code).to eq("CHF")
81
+ end
82
+ end
83
+ end
84
+
85
+ describe '#liability' do
86
+ context 'when the payment is still unprocessed' do
87
+ it 'returns nil' do
88
+ expect(payment.liability).to be_nil
89
+ end
90
+ end
91
+
92
+ context 'when the payment is authorized' do
93
+ let(:payment) { FactoryBot.create(:six_saferpay_payment, :authorized) }
94
+ it 'returns a SixSaferpay::Liability' do
95
+ expect(payment.liability).to be_a(SixSaferpay::Liability)
96
+ end
97
+
98
+ it 'sets the API response attributes correctly' do
99
+ expect(payment.liability.liability_shift).to be true
100
+ expect(payment.liability.liable_entity).to eq("ThreeDs")
101
+ end
102
+ end
103
+ end
104
+
105
+ describe '#card' do
106
+ context 'when the payment is still unprocessed' do
107
+ it 'returns nil' do
108
+ expect(payment.card).to be_nil
109
+ end
110
+ end
111
+
112
+ context 'when the payment is authorized' do
113
+ let(:payment) { FactoryBot.create(:six_saferpay_payment, :authorized) }
114
+
115
+ it 'returns a SixSaferpay::ResponseCard' do
116
+ expect(payment.card).to be_a(SixSaferpay::ResponseCard)
117
+ end
118
+ end
119
+ end
120
+
121
+ describe '#name' do
122
+ context 'when the payment is still unprocessed' do
123
+ it 'returns nil' do
124
+ expect(payment.name).to be_nil
125
+ end
126
+ end
127
+
128
+ context 'when the payment is authorized' do
129
+ let(:payment) { FactoryBot.create(:six_saferpay_payment, :authorized) }
130
+
131
+ it 'returns the card holder name' do
132
+ expect(payment.name).to eq("John Doe")
133
+ end
134
+ end
135
+ end
136
+
137
+ describe '#brand_name' do
138
+ context 'when the payment is still unprocessed' do
139
+ it 'returns nil' do
140
+ expect(payment.brand_name).to be_nil
141
+ end
142
+ end
143
+
144
+ context 'when the payment is authorized' do
145
+ let(:payment) { FactoryBot.create(:six_saferpay_payment, :authorized) }
146
+
147
+ it 'returns the brand name' do
148
+ expect(payment.brand_name).to eq("MasterCard")
149
+ end
150
+ end
151
+ end
152
+
153
+ describe '#month' do
154
+ context 'when the payment is still unprocessed' do
155
+ it 'returns nil' do
156
+ expect(payment.month).to be_nil
157
+ end
158
+ end
159
+
160
+ context 'when the payment is authorized' do
161
+ let(:payment) { FactoryBot.create(:six_saferpay_payment, :authorized) }
162
+
163
+ it 'returns the card expiration month' do
164
+ expect(payment.month).to eq(7)
165
+ end
166
+ end
167
+ end
168
+
169
+ describe '#year' do
170
+ context 'when the payment is still unprocessed' do
171
+ it 'returns nil' do
172
+ expect(payment.year).to be_nil
173
+ end
174
+ end
175
+
176
+ context 'when the payment is authorized' do
177
+ let(:payment) { FactoryBot.create(:six_saferpay_payment, :authorized) }
178
+
179
+ it 'returns the card expiration year' do
180
+ expect(payment.year).to eq(2019)
181
+ end
182
+ end
183
+ end
184
+
185
+ describe '#icon_name' do
186
+ context 'when the payment is still unprocessed' do
187
+ it 'returns nil' do
188
+ expect(payment.icon_name).to be_nil
189
+ end
190
+ end
191
+
192
+ context 'when the payment is authorized' do
193
+ let(:payment) { FactoryBot.create(:six_saferpay_payment, :authorized) }
194
+
195
+ it 'returns a downcased brand name' do
196
+ expect(payment.icon_name).to eq("mastercard")
197
+ end
198
+ end
199
+ end
200
+ end
201
+
202
+ end
203
+