Class: Spree::SolidusSixSaferpay::PaymentValidator

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree/solidus_six_saferpay/payment_validator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(saferpay_payment) ⇒ PaymentValidator

Returns a new instance of PaymentValidator.



10
11
12
13
# File 'app/services/spree/solidus_six_saferpay/payment_validator.rb', line 10

def initialize(saferpay_payment)
  @order = saferpay_payment.order
  @saferpay_payment = saferpay_payment
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



4
5
6
# File 'app/services/spree/solidus_six_saferpay/payment_validator.rb', line 4

def order
  @order
end

#saferpay_paymentObject (readonly)

Returns the value of attribute saferpay_payment.



4
5
6
# File 'app/services/spree/solidus_six_saferpay/payment_validator.rb', line 4

def saferpay_payment
  @saferpay_payment
end

Class Method Details

.call(saferpay_payment) ⇒ Object



6
7
8
# File 'app/services/spree/solidus_six_saferpay/payment_validator.rb', line 6

def self.call(saferpay_payment)
  new(saferpay_payment).call
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
# File 'app/services/spree/solidus_six_saferpay/payment_validator.rb', line 15

def call
  saferpay_transaction = saferpay_payment.transaction

  validate_payment_authorized(saferpay_transaction)
  validate_order_reference(saferpay_transaction)
  validate_order_amount(saferpay_transaction)
end

#validate_order_amount(saferpay_transaction) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/services/spree/solidus_six_saferpay/payment_validator.rb', line 39

def validate_order_amount(saferpay_transaction)
  order_amount = Spree::Money.new(order.total, currency: order.currency)

  saferpay_transaction_currency = saferpay_transaction.amount.currency_code
  if order_amount.currency.iso_code != saferpay_transaction_currency
    error("Currency should be '#{order.currency}', is: '#{saferpay_transaction_currency}'")
  end

  saferpay_transaction_cents = saferpay_transaction.amount.value
  if order_amount.cents.to_s != saferpay_transaction_cents
    error("Order total (cents) should be '#{order_amount.cents}', is: '#{saferpay_transaction_cents}'")
  end

  true
end

#validate_order_reference(saferpay_transaction) ⇒ Object



31
32
33
34
35
36
37
# File 'app/services/spree/solidus_six_saferpay/payment_validator.rb', line 31

def validate_order_reference(saferpay_transaction)
  if order.number != saferpay_transaction.order_id
    error("Order ID should be '#{order.number}', is: '#{saferpay_transaction.order_id}'")
  end

  true
end

#validate_payment_authorized(saferpay_transaction) ⇒ Object



23
24
25
26
27
28
29
# File 'app/services/spree/solidus_six_saferpay/payment_validator.rb', line 23

def validate_payment_authorized(saferpay_transaction)
  if saferpay_transaction.status != "AUTHORIZED"
    error("Status should be 'AUTHORIZED', is: '#{saferpay_transaction.status}'")
  end

  true
end