Class: Spree::AffirmCheckout

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spree/affirm_checkout.rb

Instance Method Summary collapse

Instance Method Details

#actionsObject



83
84
85
# File 'app/models/spree/affirm_checkout.rb', line 83

def actions
  %w{capture void credit}
end

#can_capture?(payment) ⇒ Boolean

Indicates whether its possible to capture the payment

Returns:



88
89
90
# File 'app/models/spree/affirm_checkout.rb', line 88

def can_capture?(payment)
  (payment.pending? || payment.checkout?) && !payment.response_code.blank?
end

#can_credit?(payment) ⇒ Boolean

Indicates whether its possible to credit the payment. Note that most gateways require that the payment be settled first which generally happens within 12-24 hours of the transaction.

Returns:



99
100
101
102
103
# File 'app/models/spree/affirm_checkout.rb', line 99

def can_credit?(payment)
  return false unless payment.completed?
  return false unless payment.order.payment_state == 'credit_owed'
  payment.credit_allowed > 0
end

#can_void?(payment) ⇒ Boolean

Indicates whether its possible to void the payment.

Returns:



93
94
95
# File 'app/models/spree/affirm_checkout.rb', line 93

def can_void?(payment)
  !payment.void? && payment.pending? && !payment.response_code.blank?
end

#check_matching_billing_addressObject



55
56
57
58
59
60
61
# File 'app/models/spree/affirm_checkout.rb', line 55

def check_matching_billing_address
  # ensure we have a standardized address format
  details['billing']['address'] = normalize_affirm_address details['billing']['address']

  # validate address
  check_address_match details["billing"], order.bill_address, :billing_address
end

#check_matching_billing_emailObject



71
72
73
74
75
# File 'app/models/spree/affirm_checkout.rb', line 71

def check_matching_billing_email
  if details["billing"]["email"].present? and details["billing"]["email"].casecmp(order.email) != 0
    errors.add :billing_email, "Billing email mismatch"
  end
end

#check_matching_product_keyObject



77
78
79
80
81
# File 'app/models/spree/affirm_checkout.rb', line 77

def check_matching_product_key
  if details["config"]["financial_product_key"] != payment_method.preferred_product_key
    errors.add :financial_product_key, "Product key mismatch"
  end
end

#check_matching_shipping_addressObject



63
64
65
66
67
68
69
# File 'app/models/spree/affirm_checkout.rb', line 63

def check_matching_shipping_address
  # ensure we have a standardized address format
  details['shipping']['address'] = normalize_affirm_address details['shipping']['address']

  # validate address
  check_address_match details["shipping"], order.ship_address, :shipping_address
end

#check_valid_productsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/spree/affirm_checkout.rb', line 28

def check_valid_products
  # ensure the number of line items matches
  if details["items"].size != order.line_items.size
    errors.add :line_items, "Order size mismatch"
  end

  # iterate through the line items of the checkout
  order.line_items.each do |line_item|

    # check that the line item sku exists in the affirm checkout
    if !(_item = details["items"][line_item.variant.sku])
      errors.add :line_items, "Line Item not in checkout details"

    # check quantity & price
    elsif _item["qty"].to_i   != line_item.quantity.to_i
      errors.add :line_items, "Quantity mismatch"

    elsif _item["unit_price"].to_i != (line_item.price*100).to_i
      errors.add :line_items, "Price mismatch"

    end
  end

  # all products match
  true
end

#detailsObject



14
15
16
# File 'app/models/spree/affirm_checkout.rb', line 14

def details
  @details ||= payment_method.provider.get_checkout token
end

#nameObject



10
11
12
# File 'app/models/spree/affirm_checkout.rb', line 10

def name
  "Affirm Checkout"
end

#validate_checkout_matches_orderObject



18
19
20
21
22
23
24
25
26
# File 'app/models/spree/affirm_checkout.rb', line 18

def validate_checkout_matches_order
  return if self.id

  check_valid_products
  check_matching_shipping_address
  check_matching_billing_address
  check_matching_billing_email
  check_matching_product_key
end